forked from sveltejs/language-tools
/
findFileReferences.ts
84 lines (75 loc) · 2.79 KB
/
findFileReferences.ts
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
import {
commands,
ExtensionContext,
ProgressLocation,
Uri,
window,
workspace,
Position,
Location,
Range
} from 'vscode';
import { LanguageClient } from 'vscode-languageclient/node';
import { Location as LSLocation } from 'vscode-languageclient';
/**
* adopted from https://github.com/microsoft/vscode/blob/5f3e9c120a4407de3e55465588ce788618526eb0/extensions/typescript-language-features/src/languageFeatures/fileReferences.ts
*/
export async function addFindFileReferencesListener(
getLS: () => LanguageClient,
context: ExtensionContext
) {
const disposable = commands.registerCommand('svelte.typescript.findAllFileReferences', handler);
context.subscriptions.push(disposable);
async function handler(resource?: Uri) {
if (!resource) {
resource = window.activeTextEditor?.document.uri;
}
if (!resource || resource.scheme !== 'file') {
return;
}
const document = await workspace.openTextDocument(resource);
await window.withProgress(
{
location: ProgressLocation.Window,
title: 'Finding file references'
},
async (_, token) => {
const lsLocations = await getLS().sendRequest<LSLocation[] | null>(
'$/getFileReferences',
document.uri.toString(),
token
);
if (!lsLocations) {
return;
}
const config = workspace.getConfiguration('references');
const existingSetting = config.inspect<string>('preferredLocation');
await config.update('preferredLocation', 'view');
try {
await commands.executeCommand(
'editor.action.showReferences',
resource,
new Position(0, 0),
lsLocations.map(
(ref) =>
new Location(
Uri.parse(ref.uri),
new Range(
ref.range.start.line,
ref.range.start.character,
ref.range.end.line,
ref.range.end.character
)
)
)
);
} finally {
await config.update(
'preferredLocation',
existingSetting?.workspaceFolderValue ?? existingSetting?.workspaceValue
);
}
}
);
}
}