Skip to content

Commit 9986cce

Browse files
committed
Add support for $schema: shorthand format in modeline
Fixes #950 Signed-off-by: Nicolas Karolak <nicolas@karolak.fr>
1 parent 703a4b8 commit 9986cce

File tree

4 files changed

+36
-17
lines changed

4 files changed

+36
-17
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,12 @@ or absolute path:
287287
# yaml-language-server: $schema=/absolute/path/to/schema
288288
```
289289

290+
or IntelliJ compatible format:
291+
292+
```yaml
293+
# $schema: <urlOrPathToTheSchema>
294+
```
295+
290296
### Schema priority
291297

292298
The following is the priority of schema association in highest to lowest priority:

src/languageservice/services/modelineUtil.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,21 @@ export function getSchemaFromModeline(doc: SingleYAMLDocument | JSONDocument): s
1717
return isModeline(lineComment);
1818
});
1919
if (yamlLanguageServerModeline != undefined) {
20-
const schemaMatchs = yamlLanguageServerModeline.match(/\$schema=\S+/g);
20+
const schemaMatchs = yamlLanguageServerModeline.match(/\$schema[=:]\s*(\S+)/);
2121
if (schemaMatchs !== null && schemaMatchs.length >= 1) {
2222
if (schemaMatchs.length >= 2) {
2323
console.log(
2424
'Several $schema attributes have been found on the yaml-language-server modeline. The first one will be picked.'
2525
);
2626
}
27-
return schemaMatchs[0].substring('$schema='.length);
27+
return schemaMatchs[1];
2828
}
2929
}
3030
}
3131
return undefined;
3232
}
3333

3434
export function isModeline(lineText: string): boolean {
35-
const matchModeline = lineText.match(/^#\s+yaml-language-server\s*:/g);
35+
const matchModeline = lineText.match(/^#\s+(?:yaml-language-server\s*:|\$schema[=:])/g);
3636
return matchModeline !== null && matchModeline.length === 1;
3737
}

src/languageservice/services/yamlCompletion.ts

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -336,21 +336,24 @@ export class YamlCompletion {
336336
}
337337

338338
if (isModeline(lineContent) || isInComment(doc.tokens, offset)) {
339-
const schemaIndex = lineContent.indexOf('$schema=');
340-
if (schemaIndex !== -1 && schemaIndex + '$schema='.length <= position.character) {
341-
this.schemaService.getAllSchemas().forEach((schema) => {
342-
const schemaIdCompletion: CompletionItem = {
343-
kind: CompletionItemKind.Constant,
344-
label: schema.name ?? schema.uri,
345-
detail: schema.description,
346-
insertText: schema.uri,
347-
insertTextFormat: InsertTextFormat.PlainText,
348-
insertTextMode: InsertTextMode.asIs,
349-
};
350-
result.items.push(schemaIdCompletion);
351-
});
339+
const schemaMatch = lineContent.match(/\$schema[=:]/);
340+
if (schemaMatch) {
341+
const schemaIndex = schemaMatch.index;
342+
if (schemaIndex + schemaMatch[0].length <= position.character) {
343+
this.schemaService.getAllSchemas().forEach((schema) => {
344+
const schemaIdCompletion: CompletionItem = {
345+
kind: CompletionItemKind.Constant,
346+
label: schema.name ?? schema.uri,
347+
detail: schema.description,
348+
insertText: schema.uri,
349+
insertTextFormat: InsertTextFormat.PlainText,
350+
insertTextMode: InsertTextMode.asIs,
351+
};
352+
result.items.push(schemaIdCompletion);
353+
});
354+
}
355+
return result;
352356
}
353-
return result;
354357
}
355358

356359
if (!schema || schema.errors.length) {

test/autoCompletion.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2022,6 +2022,16 @@ describe('Auto Completion Tests', () => {
20222022
.then(done, done);
20232023
});
20242024

2025+
it('Provide completion from schema declared in file with $schema: format', (done) => {
2026+
const content = `# $schema: ${uri}\n- `;
2027+
const completion = parseSetup(content, content.length);
2028+
completion
2029+
.then(function (result) {
2030+
assert.equal(result.items.length, 3);
2031+
})
2032+
.then(done, done);
2033+
});
2034+
20252035
it('Provide completion from schema declared in file with several attributes', (done) => {
20262036
const content = `# yaml-language-server: $schema=${uri} anothermodeline=value\n- `;
20272037
const completion = parseSetup(content, content.length);

0 commit comments

Comments
 (0)