Update `yaml` and minor enhancements (#88)

This commit is contained in:
Brandon Waterloo [MSFT] 2022-01-27 11:54:22 -05:00 коммит произвёл GitHub
Родитель aa0376a7ba
Коммит 66f03d67da
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
4 изменённых файлов: 11 добавлений и 33 удалений

14
package-lock.json сгенерированный
Просмотреть файл

@ -11,7 +11,7 @@
"dependencies": {
"vscode-languageserver": "^7.0.0",
"vscode-languageserver-textdocument": "^1.0.3",
"yaml": "^2.0.0-9"
"yaml": "^2.0.0-10"
},
"devDependencies": {
"@types/chai": "^4.3.0",
@ -2156,9 +2156,9 @@
"dev": true
},
"node_modules/yaml": {
"version": "2.0.0-9",
"resolved": "https://registry.npmjs.org/yaml/-/yaml-2.0.0-9.tgz",
"integrity": "sha512-Bf2KowHjyVkIIiGMt7+fbhmlvKOaE8DWuD07bnL4+FQ9sPmEl/5IzGpBpoxPqOaHuyasBjJhyXDcISpJWfhCGw==",
"version": "2.0.0-10",
"resolved": "https://registry.npmjs.org/yaml/-/yaml-2.0.0-10.tgz",
"integrity": "sha512-FHV8s5ODFFQXX/enJEU2EkanNl1UDBUz8oa4k5Qo/sR+Iq7VmhCDkRMb0/mjJCNeAWQ31W8WV6PYStDE4d9EIw==",
"engines": {
"node": ">= 12"
}
@ -3738,9 +3738,9 @@
"dev": true
},
"yaml": {
"version": "2.0.0-9",
"resolved": "https://registry.npmjs.org/yaml/-/yaml-2.0.0-9.tgz",
"integrity": "sha512-Bf2KowHjyVkIIiGMt7+fbhmlvKOaE8DWuD07bnL4+FQ9sPmEl/5IzGpBpoxPqOaHuyasBjJhyXDcISpJWfhCGw=="
"version": "2.0.0-10",
"resolved": "https://registry.npmjs.org/yaml/-/yaml-2.0.0-10.tgz",
"integrity": "sha512-FHV8s5ODFFQXX/enJEU2EkanNl1UDBUz8oa4k5Qo/sR+Iq7VmhCDkRMb0/mjJCNeAWQ31W8WV6PYStDE4d9EIw=="
},
"yargs": {
"version": "16.2.0",

Просмотреть файл

@ -42,6 +42,6 @@
"dependencies": {
"vscode-languageserver": "^7.0.0",
"vscode-languageserver-textdocument": "^1.0.3",
"yaml": "^2.0.0-9"
"yaml": "^2.0.0-10"
}
}

Просмотреть файл

@ -5,24 +5,16 @@
import { ErrorCodes, Position, Range, ResponseError, TextDocumentIdentifier, TextDocumentsConfiguration } from 'vscode-languageserver';
import { TextDocument } from 'vscode-languageserver-textdocument';
import { CST, Document as YamlDocument, Parser, Composer, isDocument } from 'yaml';
import { Document as YamlDocument, isDocument, parseDocument } from 'yaml';
import { CRLF, DocumentSettings, DocumentSettingsParams, DocumentSettingsRequest, LF } from '../client/DocumentSettings';
import { ExtendedPositionParams, PositionInfo } from './ExtendedParams';
import { getCurrentContext } from './utils/ActionContext';
import { Lazy } from './utils/Lazy';
const EmptyDocumentCST: CST.Document = {
type: 'document',
offset: 0,
start: [],
};
// The stated behavior of character number in the `Position` class is to roll back to line length if it exceeds the line length. So, this will work for any line <1m characters. That should cover most of them.
const MaximumLineLength = 1000 * 1000;
export class ComposeDocument {
public readonly fullCst = new Lazy(() => this.buildFullCst());
public readonly documentCst = new Lazy(() => this.buildDocumentCst());
public readonly yamlDocument = new Lazy(() => this.buildYamlDocument());
private documentSettings: DocumentSettings | undefined;
@ -45,8 +37,6 @@ export class ComposeDocument {
private update(doc: TextDocument): ComposeDocument {
this.#textDocument = doc;
this.yamlDocument.clear();
this.documentCst.clear();
this.fullCst.clear();
return this;
}
@ -157,20 +147,8 @@ export class ComposeDocument {
update: (document, changes, version) => document.update(TextDocument.update(document.textDocument, changes, version)),
};
private buildFullCst(): CST.Token[] {
return Array.from(new Parser().parse(this.textDocument.getText()));
}
private buildDocumentCst(): CST.Document {
// The CST can consist of more than just the document
// Get the first `type === 'document'` item out of the list; this is the actual document
// If there isn't one, return `EmptyDocumentCST`
return this.fullCst.value.find(t => t.type === 'document') as CST.Document || EmptyDocumentCST;
}
private buildYamlDocument(): YamlDocument {
const composedTokens = new Composer({ merge: true }).compose(this.fullCst.value, true);
const [yamlDocument] = composedTokens;
const yamlDocument = parseDocument(this.textDocument.getText(), { merge: true, prettyErrors: true });
if (!isDocument(yamlDocument)) {
throw new ResponseError(ErrorCodes.ParseError, 'Malformed YAML document');

Просмотреть файл

@ -68,7 +68,7 @@ services:
},
{
range: Range.create(8, 0, 8, 0),
contentCanary: 'sequence to end with ]',
contentCanary: 'and end with a ]',
},
];