Skip to content

Commit

Permalink
Merge pull request redhat-developer#350 from redhat-developer/fix-jso…
Browse files Browse the repository at this point in the history
…n-ls-compat

Update yaml-language-server to be compatible with vscode-json-languag…
  • Loading branch information
JPinkney authored Nov 12, 2020
2 parents 5c483ee + 6c5b1b3 commit 35e3358
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 109 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"js-yaml": "^3.13.1",
"jsonc-parser": "^2.2.1",
"request-light": "^0.2.4",
"vscode-json-languageservice": "^3.6.0",
"vscode-json-languageservice": "^3.10.0",
"vscode-languageserver": "^5.2.1",
"vscode-languageserver-types": "^3.15.1",
"vscode-nls": "^4.1.2",
Expand Down
31 changes: 0 additions & 31 deletions src/languageservice/services/yamlCompletion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ const localize = nls.loadMessageBundle();

export class YAMLCompletion extends JSONCompletion {
private schemaService: YAMLSchemaService;
private contributions: JSONWorkerContribution[];
private promise: PromiseConstructor;
private customTags: Array<string>;
private completion: boolean;
Expand All @@ -51,7 +50,6 @@ export class YAMLCompletion extends JSONCompletion {
) {
super(schemaService, contributions, promiseConstructor);
this.schemaService = schemaService;
this.contributions = contributions;
this.promise = promiseConstructor || Promise;
this.customTags = [];
this.completion = true;
Expand All @@ -65,18 +63,6 @@ export class YAMLCompletion extends JSONCompletion {
this.configuredIndentation = languageSettings.indentation;
}

public doResolve(item: CompletionItem): Thenable<CompletionItem> {
for (let i = this.contributions.length - 1; i >= 0; i--) {
if (this.contributions[i].resolveCompletion) {
const resolver = this.contributions[i].resolveCompletion(item);
if (resolver) {
return resolver;
}
}
}
return this.promise.resolve(item);
}

public doComplete(document: TextDocument, position: Position, isKubernetes = false): Thenable<CompletionList> {
const result: CompletionList = {
items: [],
Expand Down Expand Up @@ -224,20 +210,6 @@ export class YAMLCompletion extends JSONCompletion {
this.getPropertyCompletions(newSchema, currentDoc, node, addValue, separatorAfter, collector, document);
}

const location = Parser.getNodePath(node);
this.contributions.forEach((contribution) => {
const collectPromise = contribution.collectPropertyCompletions(
document.uri,
location,
currentWord,
addValue,
false,
collector
);
if (collectPromise) {
collectionPromises.push(collectPromise);
}
});
if (!schema && currentWord.length > 0 && document.getText().charAt(offset - currentWord.length - 1) !== '"') {
collector.add({
kind: CompletionItemKind.Property,
Expand All @@ -254,9 +226,6 @@ export class YAMLCompletion extends JSONCompletion {
if (newSchema) {
this.getValueCompletions(newSchema, currentDoc, node, offset, document, collector, types);
}
if (this.contributions.length > 0) {
super.getContributedValueCompletions(currentDoc, node, offset, document, collector, collectionPromises);
}

return this.promise.all(collectionPromises).then(() => {
return result;
Expand Down
17 changes: 0 additions & 17 deletions src/languageservice/services/yamlDefinition.ts

This file was deleted.

19 changes: 19 additions & 0 deletions src/languageservice/services/yamlLinks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Red Hat, Inc. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { TextDocument } from 'vscode-languageserver-types';
import { parse as parseYAML } from '../parser/yamlParser07';
import { findLinks as JSONFindLinks } from 'vscode-json-languageservice/lib/umd/services/jsonLinks';
import { DocumentLink } from 'vscode-languageserver';

export function findLinks(document: TextDocument): Thenable<DocumentLink[]> {
const doc = parseYAML(document.getText());
// Find links across all YAML Documents then report them back once finished
const linkPromises = [];
for (const yamlDoc of doc.documents) {
linkPromises.push(JSONFindLinks(document, yamlDoc));
}
// Wait for all the promises to return and then flatten them into one DocumentLink array
return Promise.all(linkPromises).then((yamlLinkArray) => [].concat(...yamlLinkArray));
}
15 changes: 7 additions & 8 deletions src/languageservice/yamlLanguageService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ import {
Hover,
SymbolInformation,
DocumentSymbol,
CompletionItem,
TextEdit,
DefinitionLink,
DocumentLink,
} from 'vscode-languageserver-types';
import { JSONSchema } from './jsonSchema';
import { YAMLDocumentSymbols } from './services/documentSymbols';
Expand All @@ -24,8 +23,8 @@ import { YAMLHover } from './services/yamlHover';
import { YAMLValidation } from './services/yamlValidation';
import { YAMLFormatter } from './services/yamlFormatter';
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import { getLanguageService as getJSONLanguageService, JSONWorkerContribution } from 'vscode-json-languageservice';
import { findDefinition } from './services/yamlDefinition';
import { JSONWorkerContribution, JSONDocument, DefinitionLink } from 'vscode-json-languageservice';
import { findLinks } from './services/yamlLinks';

export interface LanguageSettings {
validate?: boolean; //Setting for whether we want to validate the schema
Expand Down Expand Up @@ -135,8 +134,8 @@ export interface LanguageService {
doHover(document: TextDocument, position: Position): Thenable<Hover | null>;
findDocumentSymbols(document: TextDocument): SymbolInformation[];
findDocumentSymbols2(document: TextDocument): DocumentSymbol[];
doResolve(completionItem): Thenable<CompletionItem>;
findDefinition(document: TextDocument, position: Position): Thenable<DefinitionLink[]>;
findDefinition(document: TextDocument, position: Position, doc: JSONDocument): Thenable<DefinitionLink[]>;
findLinks(document: TextDocument): Thenable<DocumentLink[]>;
resetSchema(uri: string): boolean;
doFormat(document: TextDocument, options: CustomFormatterOptions): TextEdit[];
addSchema(schemaID: string, schema: JSONSchema): void;
Expand Down Expand Up @@ -177,9 +176,9 @@ export function getLanguageService(
registerCustomSchemaProvider: (schemaProvider: CustomSchemaProvider) => {
schemaService.registerCustomSchemaProvider(schemaProvider);
},
findDefinition,
findDefinition: () => Promise.resolve([]),
findLinks,
doComplete: completer.doComplete.bind(completer),
doResolve: completer.doResolve.bind(completer),
doValidation: yamlValidation.doValidation.bind(yamlValidation),
doHover: hover.doHover.bind(hover),
findDocumentSymbols: yamlDocumentSymbols.findDocumentSymbols.bind(yamlDocumentSymbols),
Expand Down
31 changes: 4 additions & 27 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ let workspaceRoot: URI = null;
let workspaceFolders: WorkspaceFolder[] = [];
let clientDynamicRegisterSupport = false;
let hierarchicalDocumentSymbolSupport = false;
let clientDefinitionLinkSupport = false;
let hasWorkspaceFolderCapability = false;

/****************************
Expand Down Expand Up @@ -406,21 +405,16 @@ connection.onInitialize(
capabilities.textDocument.rangeFormatting &&
capabilities.textDocument.rangeFormatting.dynamicRegistration
);
clientDefinitionLinkSupport = !!(
capabilities.textDocument &&
capabilities.textDocument.definition &&
capabilities.textDocument.definition.linkSupport
);
hasWorkspaceFolderCapability = capabilities.workspace && !!capabilities.workspace.workspaceFolders;
return {
capabilities: {
textDocumentSync: documents.syncKind,
completionProvider: { resolveProvider: true },
completionProvider: { resolveProvider: false },
hoverProvider: true,
documentSymbolProvider: true,
documentFormattingProvider: false,
documentRangeFormattingProvider: false,
definitionProvider: true,
documentLinkProvider: {},
workspace: {
workspaceFolders: {
changeNotifications: true,
Expand Down Expand Up @@ -596,14 +590,6 @@ connection.onCompletion((textDocumentPosition) => {
return customLanguageService.doComplete(textDocument, textDocumentPosition.position, isKubernetes(textDocument));
});

/**
* Like onCompletion, but called only for currently selected completion item
* Provides additional information about the item, not just the keyword
*/
connection.onCompletionResolve((completionItem) => {
return customLanguageService.doResolve(completionItem);
});

/**
* Called when the user hovers with their mouse over a keyword
* Returns an informational tooltip
Expand Down Expand Up @@ -655,22 +641,13 @@ connection.onDocumentFormatting((formatParams) => {
return customLanguageService.doFormat(document, customFormatterSettings);
});

connection.onDefinition((params) => {
connection.onDocumentLinks((params) => {
const document = documents.get(params.textDocument.uri);
if (!document) {
return Promise.resolve([]);
}

const definitionLinksPromise = customLanguageService.findDefinition(document, params.position);
if (clientDefinitionLinkSupport) {
return definitionLinksPromise;
} else {
return definitionLinksPromise.then((definitionLinks) => {
return definitionLinks.map((definitionLink) => {
return { uri: definitionLink.targetUri, range: definitionLink.targetRange };
});
});
}
return customLanguageService.findLinks(document);
});

connection.onRequest(SchemaModificationNotification.type, (modifications: SchemaAdditions | SchemaDeletions) => {
Expand Down
25 changes: 8 additions & 17 deletions test/findDefintion.test.ts → test/findLinks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,44 +5,35 @@
import { setupTextDocument, configureLanguageService } from './utils/testHelper';
import assert = require('assert');
import { ServiceSetup } from './utils/serviceSetup';
import { LocationLink } from '../src';
import { DocumentLink } from 'vscode-languageserver';

const languageService = configureLanguageService(new ServiceSetup().languageSettings);

suite('FindDefintion Tests', () => {
describe('Jump to defintion', function () {
function findDefinitions(content: string, position: number): Thenable<LocationLink[]> {
function findLinks(content: string): Thenable<DocumentLink[]> {
const testTextDocument = setupTextDocument(content);
return languageService.findDefinition(testTextDocument, testTextDocument.positionAt(position));
return languageService.findLinks(testTextDocument);
}

it('Find source defintion', (done) => {
const content =
"definitions:\n link:\n type: string\ntype: object\nproperties:\n uri:\n $ref: '#/definitions/link'\n";
const definitions = findDefinitions(content, content.lastIndexOf('/li'));
const definitions = findLinks(content);
definitions
.then(function (results) {
assert.equal(results.length, 1);
assert.deepEqual(results[0].originSelectionRange, {
assert.deepEqual(results[0].range, {
start: {
line: 6,
character: 10,
character: 11,
},
end: {
line: 6,
character: 30,
},
});
assert.deepEqual(results[0].targetRange, {
start: {
line: 2,
character: 4,
},
end: {
line: 2,
character: 16,
character: 29,
},
});
assert.deepEqual(results[0].target, 'file://~/Desktop/vscode-k8s/test.yaml#3,5');
})
.then(done, done);
});
Expand Down
36 changes: 28 additions & 8 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1285,6 +1285,11 @@ jsonc-parser@^2.2.1:
resolved "https://registry.yarnpkg.com/jsonc-parser/-/jsonc-parser-2.2.1.tgz#db73cd59d78cce28723199466b2a03d1be1df2bc"
integrity sha512-o6/yDBYccGvTz1+QFevz6l6OBZ2+fMVu2JZ9CIhzsYRX4mjaK5IyX9eldUdCmga16zlgQxyrj5pt9kzuj2C02w==

jsonc-parser@^2.3.1:
version "2.3.1"
resolved "https://registry.yarnpkg.com/jsonc-parser/-/jsonc-parser-2.3.1.tgz#59549150b133f2efacca48fe9ce1ec0659af2342"
integrity sha512-H8jvkz1O50L3dMZCsLqiuB2tA7muqbSg1AtGEkN0leAqGjsUzDJir3Zwr02BhqdcITPg3ei3mZ+HjMocAknhhg==

jsprim@^1.2.2:
version "1.4.1"
resolved "https://registry.yarnpkg.com/jsprim/-/jsprim-1.4.1.tgz#313e66bc1e5cc06e438bc1b7499c2e5c56acb6a2"
Expand Down Expand Up @@ -2219,16 +2224,16 @@ [email protected]:
core-util-is "1.0.2"
extsprintf "^1.2.0"

vscode-json-languageservice@^3.6.0:
version "3.6.0"
resolved "https://registry.yarnpkg.com/vscode-json-languageservice/-/vscode-json-languageservice-3.6.0.tgz#133a1e2c3a3dffe38564a1ba948516805c3c1869"
integrity sha512-dXzFywypUZ9T0tjr4fREZiknXDz6vAGx1zsxbQY1+9DOpjMfbz0VLP873KmcbuvL4K3nseKTxc4TKHu8kLXRMw==
vscode-json-languageservice@^3.10.0:
version "3.10.0"
resolved "https://registry.yarnpkg.com/vscode-json-languageservice/-/vscode-json-languageservice-3.10.0.tgz#19eed884fd0f234f8ed2fa0a96e772f293ccc5c4"
integrity sha512-8IvuRSQnjznu+obqy6Dy4S4H68Ke7a3Kb+A0FcdctyAMAWEnrORpCpMOMqEYiPLm/OTYLVWJ7ql3qToDTozu4w==
dependencies:
jsonc-parser "^2.2.1"
jsonc-parser "^2.3.1"
vscode-languageserver-textdocument "^1.0.1"
vscode-languageserver-types "^3.15.1"
vscode-nls "^4.1.2"
vscode-uri "^2.1.1"
vscode-languageserver-types "3.16.0-next.2"
vscode-nls "^5.0.0"
vscode-uri "^2.1.2"

vscode-jsonrpc@^4.0.0:
version "4.0.0"
Expand All @@ -2253,6 +2258,11 @@ [email protected]:
resolved "https://registry.yarnpkg.com/vscode-languageserver-types/-/vscode-languageserver-types-3.14.0.tgz#d3b5952246d30e5241592b6dde8280e03942e743"
integrity sha512-lTmS6AlAlMHOvPQemVwo3CezxBp0sNB95KNPkqp3Nxd5VFEnuG1ByM0zlRWos0zjO3ZWtkvhal0COgiV1xIA4A==

[email protected]:
version "3.16.0-next.2"
resolved "https://registry.yarnpkg.com/vscode-languageserver-types/-/vscode-languageserver-types-3.16.0-next.2.tgz#940bd15c992295a65eae8ab6b8568a1e8daa3083"
integrity sha512-QjXB7CKIfFzKbiCJC4OWC8xUncLsxo19FzGVp/ADFvvi87PlmBSCAtZI5xwGjF5qE0xkLf0jjKUn3DzmpDP52Q==

vscode-languageserver-types@^3.15.1:
version "3.15.1"
resolved "https://registry.yarnpkg.com/vscode-languageserver-types/-/vscode-languageserver-types-3.15.1.tgz#17be71d78d2f6236d414f0001ce1ef4d23e6b6de"
Expand All @@ -2271,6 +2281,11 @@ vscode-nls@^4.1.1, vscode-nls@^4.1.2:
resolved "https://registry.yarnpkg.com/vscode-nls/-/vscode-nls-4.1.2.tgz#ca8bf8bb82a0987b32801f9fddfdd2fb9fd3c167"
integrity sha512-7bOHxPsfyuCqmP+hZXscLhiHwe7CSuFE4hyhbs22xPIhQ4jv99FcR4eBzfYYVLP356HNFpdvz63FFb/xw6T4Iw==

vscode-nls@^5.0.0:
version "5.0.0"
resolved "https://registry.yarnpkg.com/vscode-nls/-/vscode-nls-5.0.0.tgz#99f0da0bd9ea7cda44e565a74c54b1f2bc257840"
integrity sha512-u0Lw+IYlgbEJFF6/qAqG2d1jQmJl0eyAGJHoAJqr2HT4M2BNuQYSEiSE75f52pXHSJm8AlTjnLLbBFPrdz2hpA==

vscode-uri@^1.0.6:
version "1.0.8"
resolved "https://registry.yarnpkg.com/vscode-uri/-/vscode-uri-1.0.8.tgz#9769aaececae4026fb6e22359cb38946580ded59"
Expand All @@ -2281,6 +2296,11 @@ vscode-uri@^2.1.1:
resolved "https://registry.yarnpkg.com/vscode-uri/-/vscode-uri-2.1.1.tgz#5aa1803391b6ebdd17d047f51365cf62c38f6e90"
integrity sha512-eY9jmGoEnVf8VE8xr5znSah7Qt1P/xsCdErz+g8HYZtJ7bZqKH5E3d+6oVNm1AC/c6IHUDokbmVXKOi4qPAC9A==

vscode-uri@^2.1.2:
version "2.1.2"
resolved "https://registry.yarnpkg.com/vscode-uri/-/vscode-uri-2.1.2.tgz#c8d40de93eb57af31f3c715dd650e2ca2c096f1c"
integrity sha512-8TEXQxlldWAuIODdukIb+TR5s+9Ds40eSJrw+1iDDA9IFORPjMELarNQE3myz5XIkWWpdprmJjm1/SxMlWOC8A==

which-module@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/which-module/-/which-module-2.0.0.tgz#d9ef07dce77b9902b8a3a8fa4b31c3e3f7e6e87a"
Expand Down

0 comments on commit 35e3358

Please sign in to comment.