| 9 | import adapterManager from '@/adapters/manager'; |
| 10 | |
| 11 | export class GitHub1sDefinitionProvider implements vscode.DefinitionProvider, vscode.Disposable { |
| 12 | private static instance: GitHub1sDefinitionProvider | null = null; |
| 13 | private readonly disposable: vscode.Disposable; |
| 14 | |
| 15 | private constructor() {} |
| 16 | |
| 17 | public static getInstance(): GitHub1sDefinitionProvider { |
| 18 | if (GitHub1sDefinitionProvider.instance) { |
| 19 | return GitHub1sDefinitionProvider.instance; |
| 20 | } |
| 21 | return (GitHub1sDefinitionProvider.instance = new GitHub1sDefinitionProvider()); |
| 22 | } |
| 23 | |
| 24 | dispose() { |
| 25 | this.disposable?.dispose(); |
| 26 | } |
| 27 | |
| 28 | async provideDefinition( |
| 29 | document: vscode.TextDocument, |
| 30 | position: vscode.Position, |
| 31 | _token: vscode.CancellationToken, |
| 32 | ): Promise<vscode.Definition | vscode.LocationLink[]> { |
| 33 | const symbolRange = document.getWordRangeAtPosition(position); |
| 34 | const symbol = symbolRange ? document.getText(symbolRange) : ''; |
| 35 | |
| 36 | if (!symbol) { |
| 37 | return []; |
| 38 | } |
| 39 | |
| 40 | const authority = document.uri.authority || (await router.getAuthority()); |
| 41 | const [repo, ref] = authority.split('+').filter(Boolean); |
| 42 | const { scheme, path } = document.uri; |
| 43 | const { line, character } = position; |
| 44 | |
| 45 | const dataSource = await adapterManager.getCurrentAdapter().resolveDataSource(); |
| 46 | const symbolDefinitions = await dataSource.provideSymbolDefinitions(repo, ref, path, line, character, symbol); |
| 47 | |
| 48 | if (symbolDefinitions.length) { |
| 49 | showSourcegraphSymbolMessage(repo, ref, path, line, character); |
| 50 | } |
| 51 | |
| 52 | return symbolDefinitions.map(({ scope, path, range }) => { |
| 53 | const isSameRepo = !scope || (scope.scheme === scheme && scope.repo === repo); |
| 54 | // if the definition target and the searched symbol is in the same |
| 55 | // repository, just replace the `document.uri.path` with targetPath |
| 56 | // (so that the target file will open with expanding the file explorer) |
| 57 | const uri = isSameRepo |
| 58 | ? document.uri.with({ path: `/${path}` }) |
| 59 | : vscode.Uri.parse('').with({ |
| 60 | scheme: scope!.scheme, |
| 61 | authority: `${scope!.repo}+${scope!.ref}`, |
| 62 | path: `/${path}`, |
| 63 | }); |
| 64 | const { start, end } = range; |
| 65 | return { |
| 66 | uri, |
| 67 | range: new vscode.Range( |
| 68 | new vscode.Position(start.line, start.character), |
nothing calls this directly
no outgoing calls
no test coverage detected