-
Notifications
You must be signed in to change notification settings - Fork 129
Expand file tree
/
Copy pathjsonDefinition.ts
More file actions
96 lines (84 loc) · 2.91 KB
/
jsonDefinition.ts
File metadata and controls
96 lines (84 loc) · 2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { JSONSchemaRef, JSONSchema } from '../jsonSchema';
import { DefinitionLink, Position, TextDocument, ASTNode, PropertyASTNode, Range } from '../jsonLanguageTypes';
import { JSONDocument } from '../parser/jsonParser';
export function findDefinition(document: TextDocument, position: Position, doc: JSONDocument): DefinitionLink[] {
const offset = document.offsetAt(position);
let node = doc.getNodeFromOffset(offset, true);
if (!node || !isRef(node)) {
return [];
}
let propertyNode: PropertyASTNode = node.parent as PropertyASTNode;
let valueNode = propertyNode.valueNode as ASTNode;
let path = valueNode.value as string;
let targetNode = findTargetNode(doc, path);
if (!targetNode) {
return [];
}
let definition: DefinitionLink = {
targetUri: document.uri,
originSelectionRange: createRange(document, valueNode),
targetRange: createRange(document, targetNode),
targetSelectionRange: createRange(document, targetNode)
};
return [definition];
}
function createRange(document: TextDocument, node: ASTNode): Range {
return Range.create(document.positionAt(node.offset), document.positionAt(node.offset + node.length));
}
function isRef(node: ASTNode): boolean {
return node.type === 'string' &&
node.parent &&
node.parent.type === 'property' &&
node.parent.valueNode === node &&
node.parent.keyNode.value === "$ref" ||
false;
}
function findTargetNode(doc: JSONDocument, path: string): ASTNode | null {
let tokens = parseJSONPointer(path);
if (!tokens) {
return null;
}
return findNode(tokens, doc.root);
}
function findNode(pointer: string[], node: ASTNode | null | undefined): ASTNode | null {
if (!node) {
return null;
}
if (pointer.length === 0) {
return node;
}
const token: string = pointer.shift() as string;
if (node && node.type === 'object') {
let propertyNode: PropertyASTNode | undefined = node.properties.find((propertyNode) => propertyNode.keyNode.value === token);
if (!propertyNode) {
return null;
}
return findNode(pointer, propertyNode.valueNode);
} else if (node && node.type === 'array') {
if (token.match(/^(0|[1-9][0-9]*)$/)) {
const index = Number.parseInt(token);
let arrayItem = node.items[index];
if (!arrayItem) {
return null;
}
return findNode(pointer, arrayItem);
}
}
return null;
}
function parseJSONPointer(path: string): string[] | null {
if (path === "#") {
return [];
}
if (path[0] !== '#' || path[1] !== '/') {
return null;
}
return path.substring(2).split(/\//).map(unescape);
}
function unescape(str: string): string {
return str.replace(/~1/g, '/').replace(/~0/g, '~');
}