Skip to content

Commit de4e8fd

Browse files
apupierevidolob
authored andcommitted
Provide completion for inlined modeline schema #559
it helps to discover this functionality on yaml files which doesn't have a schema Signed-off-by: Aurélien Pupier <apupier@redhat.com>
1 parent bcb28b7 commit de4e8fd

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

src/languageservice/services/yamlCompletion.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,15 @@ export class YamlCompletion {
175175
try {
176176
const schema = await this.schemaService.getSchemaForResource(document.uri, currentDoc);
177177
if (!schema || schema.errors.length) {
178+
if (position.line === 0 && position.character === 0 && !textBuffer.getLineContent(0).includes('# yaml-language-server')) {
179+
const inlineSchemaCompletion = {
180+
kind: CompletionItemKind.Text,
181+
label: 'Inline schema',
182+
insertText: '# yaml-language-server: $schema=',
183+
insertTextFormat: InsertTextFormat.PlainText,
184+
};
185+
result.items.push(inlineSchemaCompletion);
186+
}
178187
return result;
179188
}
180189

test/autoCompletion.test.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1588,6 +1588,52 @@ describe('Auto Completion Tests', () => {
15881588
});
15891589
assert.strictEqual(result.items.length, 3, `Expecting 3 items in completion but found ${result.items.length}`);
15901590
});
1591+
1592+
const inlineSchemaLabel = 'Inline schema';
1593+
1594+
it('should provide modeline completion on first character with no schema associated and no modeline yet', async () => {
1595+
const testTextDocument = setupSchemaIDTextDocument('', path.join(__dirname, 'test.yaml'));
1596+
yamlSettings.documents = new TextDocumentTestManager();
1597+
(yamlSettings.documents as TextDocumentTestManager).set(testTextDocument);
1598+
const result = await languageHandler.completionHandler({
1599+
position: testTextDocument.positionAt(0),
1600+
textDocument: testTextDocument,
1601+
});
1602+
assert.strictEqual(result.items.length, 1, `Expecting 1 item in completion but found ${result.items.length}`);
1603+
assert.strictEqual(result.items[0].label, inlineSchemaLabel);
1604+
});
1605+
1606+
it('should not provide modeline completion on first character when schema is associated', async () => {
1607+
const specificSchemaId = path.join(__dirname, 'test.yaml');
1608+
const testTextDocument = setupSchemaIDTextDocument('', specificSchemaId);
1609+
languageService.addSchema(specificSchemaId, {
1610+
type: 'object',
1611+
properties: {
1612+
name: {
1613+
type: 'string',
1614+
},
1615+
},
1616+
});
1617+
yamlSettings.documents = new TextDocumentTestManager();
1618+
(yamlSettings.documents as TextDocumentTestManager).set(testTextDocument);
1619+
const result = await languageHandler.completionHandler({
1620+
position: testTextDocument.positionAt(0),
1621+
textDocument: testTextDocument,
1622+
});
1623+
assert.strictEqual(result.items.length, 1, `Expecting 1 item in completion but found ${result.items.length}`);
1624+
assert.notStrictEqual(result.items[0].label, inlineSchemaLabel);
1625+
});
1626+
1627+
it('should not provide modeline completion on first character when modeline already present', async () => {
1628+
const testTextDocument = setupSchemaIDTextDocument('# yaml-language-server', path.join(__dirname, 'test.yaml'));
1629+
yamlSettings.documents = new TextDocumentTestManager();
1630+
(yamlSettings.documents as TextDocumentTestManager).set(testTextDocument);
1631+
const result = await languageHandler.completionHandler({
1632+
position: testTextDocument.positionAt(0),
1633+
textDocument: testTextDocument,
1634+
});
1635+
assert.strictEqual(result.items.length, 0, `Expecting 0 item in completion but found ${result.items.length}`);
1636+
});
15911637
});
15921638

15931639
describe('Configuration based indentation', () => {

0 commit comments

Comments
 (0)