From cf39edeee85c39fa50b3657ccc726a4abd4396d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Pupier?= Date: Wed, 15 Jul 2020 17:36:39 +0200 Subject: [PATCH] WIP Support schema associations in yaml files #204 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Aurélien Pupier --- .../services/yamlCompletion.ts | 2 +- .../services/yamlSchemaService.ts | 19 ++++++- test/autoCompletion.withschemainfile.test.ts | 54 +++++++++++++++++++ 3 files changed, 72 insertions(+), 3 deletions(-) create mode 100644 test/autoCompletion.withschemainfile.test.ts diff --git a/src/languageservice/services/yamlCompletion.ts b/src/languageservice/services/yamlCompletion.ts index 061c1b4d8..a5f5a6042 100644 --- a/src/languageservice/services/yamlCompletion.ts +++ b/src/languageservice/services/yamlCompletion.ts @@ -150,7 +150,7 @@ export class YAMLCompletion extends JSONCompletion { } currentDoc.currentDocIndex = currentDocIndex; - return this.schemaService.getSchemaForResource(document.uri, currentDoc).then(schema => { + return this.schemaService.getSchemaForResource(document, currentDoc).then(schema => { if (!schema) { return Promise.resolve(result); diff --git a/src/languageservice/services/yamlSchemaService.ts b/src/languageservice/services/yamlSchemaService.ts index 1d1e0c682..550a08dba 100644 --- a/src/languageservice/services/yamlSchemaService.ts +++ b/src/languageservice/services/yamlSchemaService.ts @@ -14,6 +14,7 @@ import { URI } from 'vscode-uri'; import * as nls from 'vscode-nls'; import { convertSimple2RegExpPattern } from '../utils/strings'; +import { TextDocument } from 'vscode-languageserver'; const localize = nls.loadMessageBundle(); export declare type CustomSchemaProvider = (uri: string) => Thenable; @@ -214,11 +215,25 @@ export class YAMLSchemaService extends JSONSchemaService { } //tslint:enable - public getSchemaForResource (resource: string, doc = undefined): Thenable { + public getSchemaForResource (textDocument: TextDocument, doc = undefined): Thenable { + const resource: string = textDocument.uri; const resolveSchema = () => { - + const seen: { [schemaId: string]: boolean } = Object.create(null); const schemas: string[] = []; + + const wholeText = textDocument.getText(); + const yamlModelineIndex = wholeText.indexOf('# yaml-language-server:'); + if (yamlModelineIndex != -1) { + const modelineContent = wholeText.substring(yamlModelineIndex, wholeText.indexOf('\n', yamlModelineIndex + '# yaml-language-server:'.length)); + const indexOfJsonSchemaParameter = modelineContent.indexOf('$schema='); + if(modelineContent.indexOf('$schema=') != -1){ + const schema = modelineContent.substring(indexOfJsonSchemaParameter + '$schema='.length); + schemas.push(schema); + seen[schema] = true; + } + } + for (const entry of this.filePatternAssociations) { if (entry.matchesPattern(resource)) { for (const schemaId of entry.getURIs()) { diff --git a/test/autoCompletion.withschemainfile.test.ts b/test/autoCompletion.withschemainfile.test.ts new file mode 100644 index 000000000..f4860beec --- /dev/null +++ b/test/autoCompletion.withschemainfile.test.ts @@ -0,0 +1,54 @@ +/*--------------------------------------------------------------------------------------------- + * Copyright (c) Red Hat. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for license information. + *--------------------------------------------------------------------------------------------*/ +import { TextDocument } from 'vscode-languageserver'; +import { getLanguageService } from '../src/languageservice/yamlLanguageService'; +import { toFsPath, schemaRequestService, workspaceContext } from './utils/testHelper'; +import assert = require('assert'); +import path = require('path'); + +const languageService = getLanguageService(schemaRequestService, workspaceContext, [], null); + +const languageSettings = { + schemas: [], + completion: true +}; + +const uri = toFsPath(path.join(__dirname, './fixtures/testArrayMaxProperties.json')); +languageService.configure(languageSettings); + +suite('Auto Completion Tests with schema defined in file', () => { + + describe('yamlCompletion with schema defined in file', function () { + + describe('doComplete', function () { + + function setup(content: string) { + return TextDocument.create('file://~/Desktop/vscode-k8s/test.yaml', 'yaml', 0, content); + } + + function parseSetup(content: string, position) { + const testTextDocument = setup(content); + return languageService.doComplete(testTextDocument, testTextDocument.positionAt(position), false); + } + + it('Provide completion from schema decalred in file', done => { + const content = `# yaml-language-server: $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 overriding schemastore ', done => { + + }); + + it('Provide completion from schema declared in file overriding yaml.schemas ', done => { + + }); + + }); + }); +});