Skip to content

Commit 23eb6da

Browse files
change return type of findDefinition to Promise
1 parent e4bfa87 commit 23eb6da

File tree

3 files changed

+45
-45
lines changed

3 files changed

+45
-45
lines changed

src/jsonLanguageService.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export interface LanguageService {
4848
format(document: TextDocument, range: Range, options: FormattingOptions): TextEdit[];
4949
getFoldingRanges(document: TextDocument, context?: FoldingRangesContext): FoldingRange[];
5050
getSelectionRanges(document: TextDocument, positions: Position[], doc: JSONDocument): SelectionRange[];
51-
findDefinition(document: TextDocument, position: Position, doc: JSONDocument): DefinitionLink[];
51+
findDefinition(document: TextDocument, position: Position, doc: JSONDocument): Thenable<DefinitionLink[]>;
5252
}
5353

5454

src/services/jsonDefinition.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,30 @@
44
*--------------------------------------------------------------------------------------------*/
55

66
import { JSONSchemaRef, JSONSchema } from '../jsonSchema';
7-
import { DefinitionLink, Position, TextDocument, ASTNode, PropertyASTNode, Range } from '../jsonLanguageTypes';
7+
import { DefinitionLink, Position, TextDocument, ASTNode, PropertyASTNode, Range, Thenable } from '../jsonLanguageTypes';
88
import { JSONDocument } from '../parser/jsonParser';
99

10-
export function findDefinition(document: TextDocument, position: Position, doc: JSONDocument): DefinitionLink[] {
10+
export function findDefinition(document: TextDocument, position: Position, doc: JSONDocument): Thenable<DefinitionLink[]> {
1111
const offset = document.offsetAt(position);
1212
let node = doc.getNodeFromOffset(offset, true);
1313
if (!node || !isRef(node)) {
14-
return [];
14+
return Promise.resolve([]);
1515
}
1616

1717
let propertyNode: PropertyASTNode = node.parent as PropertyASTNode;
1818
let valueNode = propertyNode.valueNode as ASTNode;
1919
let path = valueNode.value as string;
2020
let targetNode = findTargetNode(doc, path);
2121
if (!targetNode) {
22-
return [];
22+
return Promise.resolve([]);
2323
}
2424
let definition: DefinitionLink = {
2525
targetUri: document.uri,
2626
originSelectionRange: createRange(document, valueNode),
2727
targetRange: createRange(document, targetNode),
2828
targetSelectionRange: createRange(document, targetNode)
2929
};
30-
return [definition];
30+
return Promise.resolve([definition]);
3131
}
3232

3333
function createRange(document: TextDocument, node: ASTNode): Range {

src/test/definition.test.ts

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -10,57 +10,57 @@ import { repeat } from '../utils/strings';
1010
import { DefinitionLink } from 'vscode-languageserver-types';
1111

1212
suite('JSON Find Definitions', () => {
13-
const testFindDefinitionFor = function (value: string, expected: {offset: number, length: number} | null): void {
13+
const testFindDefinitionFor = function (value: string, expected: {offset: number, length: number} | null): PromiseLike<void> {
1414
const offset = value.indexOf('|');
1515
value = value.substr(0, offset) + value.substr(offset + 1);
1616

1717
const ls = getLanguageService({ clientCapabilities: ClientCapabilities.LATEST });
1818
const document = TextDocument.create('test://test/test.json', 'json', 0, value);
1919
const position = Position.create(0, offset);
2020
const jsonDoc = ls.parseJSONDocument(document);
21-
const list = ls.findDefinition(document, position, jsonDoc);
22-
if (expected) {
23-
assert.notDeepEqual(list, []);
24-
const startOffset = list[0].targetRange.start.character;
25-
assert.equal(startOffset, expected.offset);
26-
assert.equal(list[0].targetRange.end.character - startOffset, expected.length);
27-
} else {
28-
assert.deepEqual(list, []);
29-
}
30-
21+
return ls.findDefinition(document, position, jsonDoc).then(list => {
22+
if (expected) {
23+
assert.notDeepEqual(list, []);
24+
const startOffset = list[0].targetRange.start.character;
25+
assert.equal(startOffset, expected.offset);
26+
assert.equal(list[0].targetRange.end.character - startOffset, expected.length);
27+
} else {
28+
assert.deepEqual(list, []);
29+
}
30+
});
3131
};
3232

33-
test('FindDefinition invalid ref', function () {
34-
testFindDefinitionFor('{|}', null);
35-
testFindDefinitionFor('{"name": |"John"}', null);
36-
testFindDefinitionFor('{"|name": "John"}', null);
37-
testFindDefinitionFor('{"name": "|John"}', null);
38-
testFindDefinitionFor('{"name": "John", "$ref": "#/|john/name"}', null);
39-
testFindDefinitionFor('{"name": "John", "$ref|": "#/name"}', null);
40-
testFindDefinitionFor('{"name": "John", "$ref": "#/|"}', null);
33+
test('FindDefinition invalid ref', async function () {
34+
await testFindDefinitionFor('{|}', null);
35+
await testFindDefinitionFor('{"name": |"John"}', null);
36+
await testFindDefinitionFor('{"|name": "John"}', null);
37+
await testFindDefinitionFor('{"name": "|John"}', null);
38+
await testFindDefinitionFor('{"name": "John", "$ref": "#/|john/name"}', null);
39+
await testFindDefinitionFor('{"name": "John", "$ref|": "#/name"}', null);
40+
await testFindDefinitionFor('{"name": "John", "$ref": "#/|"}', null);
4141
});
4242

43-
test('FindDefinition valid ref', function () {
44-
testFindDefinitionFor('{"name": "John", "$ref": "#/n|ame"}', {offset: 9, length: 6});
45-
testFindDefinitionFor('{"name": "John", "$ref": "|#/name"}', {offset: 9, length: 6});
46-
testFindDefinitionFor('{"name": "John", "$ref": |"#/name"}', {offset: 9, length: 6});
47-
testFindDefinitionFor('{"name": "John", "$ref": "#/name"|}', {offset: 9, length: 6});
48-
testFindDefinitionFor('{"name": "John", "$ref": "#/name|"}', {offset: 9, length: 6});
49-
testFindDefinitionFor('{"name": "John", "$ref": "#|"}', {offset: 0, length: 29});
43+
test('FindDefinition valid ref', async function () {
44+
await testFindDefinitionFor('{"name": "John", "$ref": "#/n|ame"}', {offset: 9, length: 6});
45+
await testFindDefinitionFor('{"name": "John", "$ref": "|#/name"}', {offset: 9, length: 6});
46+
await testFindDefinitionFor('{"name": "John", "$ref": |"#/name"}', {offset: 9, length: 6});
47+
await testFindDefinitionFor('{"name": "John", "$ref": "#/name"|}', {offset: 9, length: 6});
48+
await testFindDefinitionFor('{"name": "John", "$ref": "#/name|"}', {offset: 9, length: 6});
49+
await testFindDefinitionFor('{"name": "John", "$ref": "#|"}', {offset: 0, length: 29});
5050

5151
const doc = (ref: string) => `{"foo": ["bar", "baz"],"": 0,"a/b": 1,"c%d": 2,"e^f": 3,"i\\\\j": 5,"k\\"l": 6," ": 7,"m~n": 8, "$ref": "|${ref}"}`;
52-
testFindDefinitionFor(doc('#'), {offset: 0, length: 105});
53-
testFindDefinitionFor(doc('#/foo'), {offset: 8, length: 14});
54-
testFindDefinitionFor(doc('#/foo/0'), {offset: 9, length: 5});
55-
testFindDefinitionFor(doc('#/foo/1'), {offset: 16, length: 5});
56-
testFindDefinitionFor(doc('#/foo/01'), null);
57-
testFindDefinitionFor(doc('#/'), {offset: 27, length: 1});
58-
testFindDefinitionFor(doc('#/a~1b'), {offset: 36, length: 1});
59-
testFindDefinitionFor(doc('#/c%d'), {offset: 45, length: 1});
60-
testFindDefinitionFor(doc('#/e^f'), {offset: 54, length: 1});
61-
testFindDefinitionFor(doc('#/i\\\\j'), {offset: 64, length: 1});
62-
testFindDefinitionFor(doc('#/k\\"l'), {offset: 74, length: 1});
63-
testFindDefinitionFor(doc('#/ '), {offset: 81, length: 1});
64-
testFindDefinitionFor(doc('#/m~0n'), {offset: 90, length: 1});
52+
await testFindDefinitionFor(doc('#'), {offset: 0, length: 105});
53+
await testFindDefinitionFor(doc('#/foo'), {offset: 8, length: 14});
54+
await testFindDefinitionFor(doc('#/foo/0'), {offset: 9, length: 5});
55+
await testFindDefinitionFor(doc('#/foo/1'), {offset: 16, length: 5});
56+
await testFindDefinitionFor(doc('#/foo/01'), null);
57+
await testFindDefinitionFor(doc('#/'), {offset: 27, length: 1});
58+
await testFindDefinitionFor(doc('#/a~1b'), {offset: 36, length: 1});
59+
await testFindDefinitionFor(doc('#/c%d'), {offset: 45, length: 1});
60+
await testFindDefinitionFor(doc('#/e^f'), {offset: 54, length: 1});
61+
await testFindDefinitionFor(doc('#/i\\\\j'), {offset: 64, length: 1});
62+
await testFindDefinitionFor(doc('#/k\\"l'), {offset: 74, length: 1});
63+
await testFindDefinitionFor(doc('#/ '), {offset: 81, length: 1});
64+
await testFindDefinitionFor(doc('#/m~0n'), {offset: 90, length: 1});
6565
});
6666
});

0 commit comments

Comments
 (0)