| 23 | `; |
| 24 | |
| 25 | export class GitHub1sHoverProvider implements vscode.HoverProvider, vscode.Disposable { |
| 26 | private static instance: GitHub1sHoverProvider | null = null; |
| 27 | private readonly disposable: vscode.Disposable; |
| 28 | |
| 29 | private constructor() {} |
| 30 | |
| 31 | public static getInstance(): GitHub1sHoverProvider { |
| 32 | if (GitHub1sHoverProvider.instance) { |
| 33 | return GitHub1sHoverProvider.instance; |
| 34 | } |
| 35 | return (GitHub1sHoverProvider.instance = new GitHub1sHoverProvider()); |
| 36 | } |
| 37 | |
| 38 | dispose() { |
| 39 | this.disposable?.dispose(); |
| 40 | } |
| 41 | |
| 42 | async getSearchBasedHover( |
| 43 | document: vscode.TextDocument, |
| 44 | position: vscode.Position, |
| 45 | symbol: string, |
| 46 | ): Promise<string | null> { |
| 47 | const { line, character } = position; |
| 48 | const authority = document.uri.authority || (await router.getAuthority()); |
| 49 | const [repo, ref] = authority.split('+').filter(Boolean); |
| 50 | const dataSource = await adapterManager.getCurrentAdapter().resolveDataSource(); |
| 51 | |
| 52 | const requestParams = [repo, ref, document.uri.path, line, character, symbol] as const; |
| 53 | const definitions = await dataSource.provideSymbolDefinitions(...requestParams); |
| 54 | |
| 55 | if (!definitions.length) { |
| 56 | return null; |
| 57 | } |
| 58 | |
| 59 | // use the information of first definition as hover context |
| 60 | const target = definitions[0]; |
| 61 | const isSameRepo = !target.scope || (target.scope.scheme === document.uri.scheme && target.scope.repo === repo); |
| 62 | // if the definition target and the searched symbol is in the same |
| 63 | // repository, just replace the `document.uri.path` with targetPath |
| 64 | const targetFileUri = isSameRepo |
| 65 | ? document.uri.with({ path: `/${target.path}` }) |
| 66 | : vscode.Uri.parse('').with({ |
| 67 | scheme: target.scope?.scheme, |
| 68 | authority: `${target.scope?.repo}+${target.scope?.ref}`, |
| 69 | path: `/${target.path}`, |
| 70 | }); |
| 71 | // open corresponding file with target |
| 72 | const textDocument = await vscode.workspace.openTextDocument(targetFileUri); |
| 73 | // get the content in `[range.start.line - 2, range.end.line + 2]` lines |
| 74 | const startPosition = new vscode.Position(Math.max(0, target.range.start.line - 2), 0); |
| 75 | |
| 76 | const endPosition = textDocument.lineAt(Math.min(textDocument.lineCount - 1, target.range.end.line + 2)).range.end; |
| 77 | const codeText = textDocument.getText(new vscode.Range(startPosition, endPosition)); |
| 78 | |
| 79 | return `\`\`\`${textDocument.languageId}\n${codeText}\n\`\`\``; |
| 80 | } |
| 81 | |
| 82 | async provideHover( |
nothing calls this directly
no outgoing calls
no test coverage detected