Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
Signed-off-by: Yevhen Vydolob <[email protected]>
  • Loading branch information
evidolob committed Mar 19, 2021
1 parent 230e8f8 commit a2567cd
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
15 changes: 11 additions & 4 deletions src/languageservice/parser/yaml-documents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,15 @@ export class YamlDocuments {
// a mapping of URIs to cached documents
private cache = new Map<string, YamlCachedDocument>();

getYamlDocument(document: TextDocument, customTags: string[] = []): YAMLDocument {
this.ensureCache(document, customTags);
/**
* Get cached YAMLDocument
* @param document TextDocument to parse
* @param customTags YAML custom tags
* @param addRootObject if true and document is empty add empty object {} to force schema usage
* @returns the YAMLDocument
*/
getYamlDocument(document: TextDocument, customTags: string[] = [], addRootObject = false): YAMLDocument {
this.ensureCache(document, customTags, addRootObject);
return this.cache.get(document.uri).document;
}

Expand All @@ -26,7 +33,7 @@ export class YamlDocuments {
this.cache.clear();
}

private ensureCache(document: TextDocument, customTags: string[]): void {
private ensureCache(document: TextDocument, customTags: string[], addRootObject: boolean): void {
const key = document.uri;
if (!this.cache.has(key)) {
this.cache.set(key, { version: -1, document: new YAMLDocument([]) });
Expand All @@ -35,7 +42,7 @@ export class YamlDocuments {
if (this.cache.get(key).version !== document.version) {
let text = document.getText();
// if text is contains only whitespace wrap all text in object to force schema selection
if (!/\S/.test(text)) {
if (addRootObject && !/\S/.test(text)) {
text = `{${text}}`;
}
const doc = parseYAML(text, customTags);
Expand Down
2 changes: 1 addition & 1 deletion src/languageservice/services/yamlValidation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export class YAMLValidation {
return Promise.resolve([]);
}

const yamlDocument: YAMLDocument = yamlDocumentsCache.getYamlDocument(textDocument, this.customTags);
const yamlDocument: YAMLDocument = yamlDocumentsCache.getYamlDocument(textDocument, this.customTags, true);
const validationResult = [];

let index = 0;
Expand Down

0 comments on commit a2567cd

Please sign in to comment.