Skip to content

Commit

Permalink
Provide completion for inlined modeline schema redhat-developer#559
Browse files Browse the repository at this point in the history
it helps to discover this functionality on yaml files which doesn't have
a schema

Signed-off-by: Aurélien Pupier <[email protected]>
  • Loading branch information
apupier authored and evidolob committed Oct 25, 2021
1 parent bcb28b7 commit de4e8fd
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/languageservice/services/yamlCompletion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,15 @@ export class YamlCompletion {
try {
const schema = await this.schemaService.getSchemaForResource(document.uri, currentDoc);
if (!schema || schema.errors.length) {
if (position.line === 0 && position.character === 0 && !textBuffer.getLineContent(0).includes('# yaml-language-server')) {
const inlineSchemaCompletion = {
kind: CompletionItemKind.Text,
label: 'Inline schema',
insertText: '# yaml-language-server: $schema=',
insertTextFormat: InsertTextFormat.PlainText,
};
result.items.push(inlineSchemaCompletion);
}
return result;
}

Expand Down
46 changes: 46 additions & 0 deletions test/autoCompletion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1588,6 +1588,52 @@ describe('Auto Completion Tests', () => {
});
assert.strictEqual(result.items.length, 3, `Expecting 3 items in completion but found ${result.items.length}`);
});

const inlineSchemaLabel = 'Inline schema';

it('should provide modeline completion on first character with no schema associated and no modeline yet', async () => {
const testTextDocument = setupSchemaIDTextDocument('', path.join(__dirname, 'test.yaml'));
yamlSettings.documents = new TextDocumentTestManager();
(yamlSettings.documents as TextDocumentTestManager).set(testTextDocument);
const result = await languageHandler.completionHandler({
position: testTextDocument.positionAt(0),
textDocument: testTextDocument,
});
assert.strictEqual(result.items.length, 1, `Expecting 1 item in completion but found ${result.items.length}`);
assert.strictEqual(result.items[0].label, inlineSchemaLabel);
});

it('should not provide modeline completion on first character when schema is associated', async () => {
const specificSchemaId = path.join(__dirname, 'test.yaml');
const testTextDocument = setupSchemaIDTextDocument('', specificSchemaId);
languageService.addSchema(specificSchemaId, {
type: 'object',
properties: {
name: {
type: 'string',
},
},
});
yamlSettings.documents = new TextDocumentTestManager();
(yamlSettings.documents as TextDocumentTestManager).set(testTextDocument);
const result = await languageHandler.completionHandler({
position: testTextDocument.positionAt(0),
textDocument: testTextDocument,
});
assert.strictEqual(result.items.length, 1, `Expecting 1 item in completion but found ${result.items.length}`);
assert.notStrictEqual(result.items[0].label, inlineSchemaLabel);
});

it('should not provide modeline completion on first character when modeline already present', async () => {
const testTextDocument = setupSchemaIDTextDocument('# yaml-language-server', path.join(__dirname, 'test.yaml'));
yamlSettings.documents = new TextDocumentTestManager();
(yamlSettings.documents as TextDocumentTestManager).set(testTextDocument);
const result = await languageHandler.completionHandler({
position: testTextDocument.positionAt(0),
textDocument: testTextDocument,
});
assert.strictEqual(result.items.length, 0, `Expecting 0 item in completion but found ${result.items.length}`);
});
});

describe('Configuration based indentation', () => {
Expand Down

0 comments on commit de4e8fd

Please sign in to comment.