-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommandAdapters.js
More file actions
94 lines (82 loc) · 2.14 KB
/
Copy pathcommandAdapters.js
File metadata and controls
94 lines (82 loc) · 2.14 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
function normalizeShowReferencesArguments(vscode, args) {
if (!Array.isArray(args)) {
return args;
}
const [uri, position, locations, ...rest] = args;
return [
toUri(vscode, uri),
toPosition(vscode, position),
toLocations(vscode, locations),
...rest
];
}
function toUri(vscode, value) {
if (value === undefined || value === null) {
return undefined;
}
if (typeof value === 'string') {
return vscode.Uri.parse(value);
}
if (typeof value.with === 'function' && typeof value.scheme === 'string') {
return value;
}
if (typeof value === 'object' && typeof value.scheme === 'string') {
if (typeof vscode.Uri.from === 'function') {
return vscode.Uri.from(value);
}
if (typeof value.toString === 'function') {
return vscode.Uri.parse(value.toString());
}
}
return undefined;
}
function toPosition(vscode, value) {
if (!value) {
return undefined;
}
if (value instanceof vscode.Position) {
return value;
}
const line = toNumber(value.line);
const character = toNumber(value.character);
if (line === undefined || character === undefined) {
return undefined;
}
return new vscode.Position(line, character);
}
function toLocations(vscode, value) {
if (!Array.isArray(value)) {
return [];
}
return value.map((location) => toLocation(vscode, location)).filter(Boolean);
}
function toLocation(vscode, value) {
if (!value) {
return undefined;
}
if (value instanceof vscode.Location) {
return value;
}
const uri = toUri(vscode, value.uri);
const start = toPosition(vscode, value.range && value.range.start);
const end = toPosition(vscode, value.range && value.range.end);
if (!uri || !start || !end) {
return undefined;
}
return new vscode.Location(uri, new vscode.Range(start, end));
}
function toNumber(value) {
if (typeof value === 'number' && Number.isFinite(value)) {
return value;
}
if (typeof value === 'string' && value.trim() !== '') {
const number = Number(value);
if (Number.isFinite(number)) {
return number;
}
}
return undefined;
}
module.exports = {
normalizeShowReferencesArguments
};