Repository navigation
Expand file tree
/
Copy pathreference.ts
More file actions
75 lines (64 loc) · 2.36 KB
/
Copy pathreference.ts
File metadata and controls
75 lines (64 loc) · 2.36 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
/**
* @file ReferenceProvider
* @author netcon
*/
import * as vscode from 'vscode';
import router from '@/router';
import { showSourcegraphSymbolMessage } from '@/messages';
import adapterManager from '@/adapters/manager';
export class GitHub1sReferenceProvider implements vscode.ReferenceProvider, vscode.Disposable {
private static instance: GitHub1sReferenceProvider | null = null;
private readonly disposable: vscode.Disposable;
private constructor() {}
public static getInstance(): GitHub1sReferenceProvider {
if (GitHub1sReferenceProvider.instance) {
return GitHub1sReferenceProvider.instance;
}
return (GitHub1sReferenceProvider.instance = new GitHub1sReferenceProvider());
}
dispose() {
this.disposable?.dispose();
}
async provideReferences(
document: vscode.TextDocument,
position: vscode.Position,
_context: vscode.ReferenceContext,
_token: vscode.CancellationToken
): Promise<vscode.Location[]> {
const symbolRange = document.getWordRangeAtPosition(position);
const symbol = symbolRange ? document.getText(symbolRange) : '';
if (!symbol) {
return [];
}
const authority = document.uri.authority || (await router.getAuthority());
const [repo, ref] = authority.split('+').filter(Boolean);
const { scheme, path } = document.uri;
const { line, character } = position;
const dataSource = await adapterManager.getCurrentAdapter().resolveDataSource();
const symbolReferences = await dataSource.provideSymbolReferences(repo, ref, path, line, character, symbol);
if (symbolReferences.length) {
showSourcegraphSymbolMessage(repo, ref, path, line, character);
}
return symbolReferences.map(({ scope, path, range }) => {
const isSameRepo = !scope || (scope.scheme === scheme && scope.repo === repo);
// if the reference target and the searched symbol is in the same
// repository, just replace the `document.uri.path` with targetPath
// (so that the target file will open with expanding the file explorer)
const uri = isSameRepo
? document.uri.with({ path: `/${path}` })
: vscode.Uri.parse('').with({
scheme: scope!.scheme,
authority: `${scope!.repo}+${scope!.ref}`,
path: `/${path}`,
});
const { start, end } = range;
return {
uri,
range: new vscode.Range(
new vscode.Position(start.line, start.character),
new vscode.Position(end.line, end.character)
),
};
});
}
}