Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for textDocument/definition #257

Merged
merged 2 commits into from
May 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"js-yaml": "^3.13.1",
"jsonc-parser": "^2.2.1",
"request-light": "^0.2.4",
"vscode-json-languageservice": "^3.5.2",
"vscode-json-languageservice": "^3.6.0",
"vscode-languageserver": "^5.2.1",
"vscode-languageserver-types": "^3.15.1",
"vscode-nls": "^4.1.2",
Expand Down
18 changes: 18 additions & 0 deletions src/languageservice/services/yamlDefinition.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { JSONSchemaRef, JSONSchema } from '../jsonSchema';
import { TextDocument, Position, DefinitionLink } from 'vscode-languageserver-types';
import { parse as parseYAML } from '../parser/yamlParser07';
import { matchOffsetToDocument } from '../utils/arrUtils';
import { findDefinition as JSONFindDefinition } from 'vscode-json-languageservice/lib/umd/services/jsonDefinition';

export function findDefinition(document: TextDocument, position: Position): Thenable<DefinitionLink[]> {
const doc = parseYAML(document.getText());
const offset = document.offsetAt(position);
const currentDoc = matchOffsetToDocument(offset, doc);
if (currentDoc === null) {
return Promise.resolve([]);
}

const currentDocIndex = doc.documents.indexOf(currentDoc);
currentDoc.currentDocIndex = currentDocIndex;
return JSONFindDefinition(document, position, currentDoc);
}
5 changes: 4 additions & 1 deletion src/languageservice/yamlLanguageService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
*--------------------------------------------------------------------------------------------*/

import { YAMLSchemaService, CustomSchemaProvider, SchemaAdditions, SchemaDeletions } from './services/yamlSchemaService';
import { TextDocument, Position, CompletionList, Diagnostic, Hover, SymbolInformation, DocumentSymbol, CompletionItem, TextEdit } from 'vscode-languageserver-types';
import { TextDocument, Position, CompletionList, Diagnostic, Hover, SymbolInformation, DocumentSymbol, CompletionItem, TextEdit, DefinitionLink } from 'vscode-languageserver-types';
import { JSONSchema } from './jsonSchema';
import { YAMLDocumentSymbols } from './services/documentSymbols';
import { YAMLCompletion } from './services/yamlCompletion';
import { YAMLHover } from './services/yamlHover';
import { YAMLValidation } from './services/yamlValidation';
import { YAMLFormatter } from './services/yamlFormatter';
import { getLanguageService as getJSONLanguageService, JSONWorkerContribution } from 'vscode-json-languageservice';
import { findDefinition } from './services/yamlDefinition';

export interface LanguageSettings {
validate?: boolean; //Setting for whether we want to validate the schema
Expand Down Expand Up @@ -116,6 +117,7 @@ export interface LanguageService {
findDocumentSymbols(document: TextDocument): SymbolInformation[];
findDocumentSymbols2(document: TextDocument): DocumentSymbol[];
doResolve(completionItem): Thenable<CompletionItem>;
findDefinition(document: TextDocument, position: Position): Thenable<DefinitionLink[]>;
resetSchema(uri: string): boolean;
doFormat(document: TextDocument, options: CustomFormatterOptions): TextEdit[];
addSchema(schemaID: string, schema: JSONSchema): void;
Expand Down Expand Up @@ -154,6 +156,7 @@ export function getLanguageService(schemaRequestService: SchemaRequestService,
registerCustomSchemaProvider: (schemaProvider: CustomSchemaProvider) => {
schemaService.registerCustomSchemaProvider(schemaProvider);
},
findDefinition,
doComplete: completer.doComplete.bind(completer),
doResolve: completer.doResolve.bind(completer),
doValidation: yamlValidation.doValidation.bind(yamlValidation),
Expand Down
23 changes: 22 additions & 1 deletion src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ let workspaceRoot: URI = null;
let workspaceFolders: WorkspaceFolder[] = [];
let clientDynamicRegisterSupport = false;
let hierarchicalDocumentSymbolSupport = false;
let clientDefinitionLinkSupport = false;

/****************************
* Reusable helper functions
Expand Down Expand Up @@ -353,6 +354,11 @@ connection.onInitialize((params: InitializeParams): InitializeResult => {
capabilities.textDocument.rangeFormatting &&
capabilities.textDocument.rangeFormatting.dynamicRegistration
);
clientDefinitionLinkSupport = !!(
capabilities.textDocument &&
capabilities.textDocument.definition &&
capabilities.textDocument.definition.linkSupport
);

return {
capabilities: {
Expand All @@ -361,7 +367,8 @@ connection.onInitialize((params: InitializeParams): InitializeResult => {
hoverProvider: true,
documentSymbolProvider: true,
documentFormattingProvider: false,
documentRangeFormattingProvider: false
documentRangeFormattingProvider: false,
definitionProvider: true
}
};
});
Expand Down Expand Up @@ -564,6 +571,20 @@ connection.onDocumentFormatting(formatParams => {
return customLanguageService.doFormat(document, customFormatterSettings);
});

connection.onDefinition(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 => definitionLinks.map(definitionLink => ({uri: definitionLink.targetUri, range: definitionLink.targetRange})));
}
});

connection.onRequest(SchemaModificationNotification.type, (modifications: SchemaAdditions | SchemaDeletions) => {
if (modifications.action === MODIFICATION_ACTIONS.add) {
customLanguageService.modifySchemaContent(modifications);
Expand Down
48 changes: 48 additions & 0 deletions test/findDefintion.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Red Hat. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { getLanguageService } from '../src/languageservice/yamlLanguageService';
import { schemaRequestService, setupTextDocument, workspaceContext } from './utils/testHelper';
import assert = require('assert');

const languageService = getLanguageService(schemaRequestService, workspaceContext, [], null);

suite('FindDefintion Tests', () => {

describe('Jump to defintion', function () {

function findDefinitions(content: string, position: number) {
const testTextDocument = setupTextDocument(content);
return languageService.findDefinition(testTextDocument, testTextDocument.positionAt(position));
}

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'));
definitions.then(function (results) {
assert.equal(results.length, 1);
assert.deepEqual(results[0].originSelectionRange, {
start: {
line: 6,
character: 10
},
end: {
line: 6,
character: 30
}
});
assert.deepEqual(results[0].targetRange, {
start: {
line: 2,
character: 4
},
end: {
line: 2,
character: 16
}
});
}).then(done, done);
});
});
});
10 changes: 5 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1490,15 +1490,15 @@ [email protected]:
core-util-is "1.0.2"
extsprintf "^1.2.0"

vscode-json-languageservice@^3.5.2:
version "3.5.2"
resolved "https://registry.yarnpkg.com/vscode-json-languageservice/-/vscode-json-languageservice-3.5.2.tgz#4b898140a8e581359c10660845a4cae15dcbb4f9"
integrity sha512-9cUvBq00O08lpWVVOx6tQ1yLxCHss79nsUdEAVYGomRyMbnPBmc0AkYPcXI9WK1EM6HBo0R9Zo3NjFhcICpy4A==
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==
dependencies:
jsonc-parser "^2.2.1"
vscode-languageserver-textdocument "^1.0.1"
vscode-languageserver-types "^3.15.1"
vscode-nls "^4.1.1"
vscode-nls "^4.1.2"
vscode-uri "^2.1.1"

vscode-jsonrpc@^4.0.0:
Expand Down