Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,12 @@ or absolute path:
# yaml-language-server: $schema=/absolute/path/to/schema
```

or IntelliJ compatible format:

```yaml
# $schema: <urlOrPathToTheSchema>
```

### Schema priority

The following is the priority of schema association in highest to lowest priority:
Expand Down
6 changes: 3 additions & 3 deletions src/languageservice/services/modelineUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,21 @@ export function getSchemaFromModeline(doc: SingleYAMLDocument | JSONDocument): s
return isModeline(lineComment);
});
if (yamlLanguageServerModeline != undefined) {
const schemaMatchs = yamlLanguageServerModeline.match(/\$schema=\S+/g);
const schemaMatchs = yamlLanguageServerModeline.match(/\$schema(?:=|:\s*)(\S+)/);
if (schemaMatchs !== null && schemaMatchs.length >= 1) {
if (schemaMatchs.length >= 2) {
console.log(
'Several $schema attributes have been found on the yaml-language-server modeline. The first one will be picked.'
);
}
return schemaMatchs[0].substring('$schema='.length);
return schemaMatchs[1];
}
}
}
return undefined;
}

export function isModeline(lineText: string): boolean {
const matchModeline = lineText.match(/^#\s+yaml-language-server\s*:/g);
const matchModeline = lineText.match(/^#\s+(?:yaml-language-server\s*:|\$schema:)/g);
return matchModeline !== null && matchModeline.length === 1;
}
31 changes: 17 additions & 14 deletions src/languageservice/services/yamlCompletion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,21 +336,24 @@ export class YamlCompletion {
}

if (isModeline(lineContent) || isInComment(doc.tokens, offset)) {
const schemaIndex = lineContent.indexOf('$schema=');
if (schemaIndex !== -1 && schemaIndex + '$schema='.length <= position.character) {
this.schemaService.getAllSchemas().forEach((schema) => {
const schemaIdCompletion: CompletionItem = {
kind: CompletionItemKind.Constant,
label: schema.name ?? schema.uri,
detail: schema.description,
insertText: schema.uri,
insertTextFormat: InsertTextFormat.PlainText,
insertTextMode: InsertTextMode.asIs,
};
result.items.push(schemaIdCompletion);
});
const schemaMatch = lineContent.match(/\$schema[=:]/);
if (schemaMatch) {
const schemaIndex = schemaMatch.index;
if (schemaIndex + schemaMatch[0].length <= position.character) {
this.schemaService.getAllSchemas().forEach((schema) => {
const schemaIdCompletion: CompletionItem = {
kind: CompletionItemKind.Constant,
label: schema.name ?? schema.uri,
detail: schema.description,
insertText: schema.uri,
insertTextFormat: InsertTextFormat.PlainText,
insertTextMode: InsertTextMode.asIs,
};
result.items.push(schemaIdCompletion);
});
}
return result;
}
return result;
}

if (!schema || schema.errors.length) {
Expand Down
10 changes: 10 additions & 0 deletions test/autoCompletion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2022,6 +2022,16 @@ describe('Auto Completion Tests', () => {
.then(done, done);
});

it('Provide completion from schema declared in file with $schema: format', (done) => {
const content = `# $schema: ${uri}\n- `;
const completion = parseSetup(content, content.length);
completion
.then(function (result) {
assert.equal(result.items.length, 3);
})
.then(done, done);
});

it('Provide completion from schema declared in file with several attributes', (done) => {
const content = `# yaml-language-server: $schema=${uri} anothermodeline=value\n- `;
const completion = parseSetup(content, content.length);
Expand Down