|
| 1 | +import vscode from 'vscode'; |
| 2 | +import { SOURCE_DEFINITION_COMMAND } from '@tsrx/typescript-plugin/src/plugin-source-definition.js'; |
| 3 | +import { TYPESCRIPT_7_TRACKING_URL } from '@tsrx/typescript-plugin/src/typescript-version.js'; |
| 4 | +import { typescript_7_on } from './typescript-7.js'; |
| 5 | + |
| 6 | +export const GO_TO_SOURCE_DEFINITION_COMMAND = 'tsrx.goToSourceDefinition'; |
| 7 | + |
| 8 | +/** |
| 9 | + * Whether the TypeScript 7 extension's Go to Source Definition runs in `.tsrx` files. |
| 10 | + * `tsc --lsp` answers its request (`custom/textDocument/sourceDefinition`) for them |
| 11 | + * through the content mapper, but the command only runs in TypeScript and JavaScript |
| 12 | + * files, and no other extension can send the request (microsoft/TypeScript#64576). |
| 13 | + * Set this to true once a fixed extension ships (tsrx-org/tsrx#992). |
| 14 | + */ |
| 15 | +const TYPESCRIPT_7_SOURCE_DEFINITION = false; |
| 16 | + |
| 17 | +const NO_SOURCE_DEFINITIONS = 'No source definitions found.'; |
| 18 | + |
| 19 | +/** |
| 20 | + * **Go to Source Definition** in `.tsrx` files: like Go to Definition, but it goes |
| 21 | + * past a `.d.ts` file to the JavaScript behind it. VS Code's own command and the |
| 22 | + * TypeScript 7 extension's only run in TypeScript and JavaScript files. |
| 23 | + * |
| 24 | + * - VS Code's own TypeScript (5.9 or 6): `typescript.tsserverRequest` sends |
| 25 | + * `@tsrx/typescript-plugin`'s `SOURCE_DEFINITION_COMMAND`, which runs tsserver's |
| 26 | + * `findSourceDefinition`. That command forwards only requests whose name starts |
| 27 | + * with `_`, besides a few built-in ones. |
| 28 | + * - TypeScript 7: Go to Definition, until `TYPESCRIPT_7_SOURCE_DEFINITION`. The first |
| 29 | + * time in a session, a message says why. |
| 30 | + * @returns {import('vscode').Disposable} |
| 31 | + */ |
| 32 | +export function register_source_definition_command() { |
| 33 | + let explained = false; |
| 34 | + return vscode.commands.registerCommand(GO_TO_SOURCE_DEFINITION_COMMAND, async () => { |
| 35 | + const editor = vscode.window.activeTextEditor; |
| 36 | + if (!editor) return; |
| 37 | + const { document } = editor; |
| 38 | + const position = editor.selection.active; |
| 39 | + |
| 40 | + /** @type {import('vscode').Location[] | undefined} */ |
| 41 | + let locations; |
| 42 | + let not_found = NO_SOURCE_DEFINITIONS; |
| 43 | + if (typescript_7_on()) { |
| 44 | + if (TYPESCRIPT_7_SOURCE_DEFINITION) { |
| 45 | + return vscode.commands.executeCommand('typescript.native-preview.goToSourceDefinition'); |
| 46 | + } |
| 47 | + if (!explained) { |
| 48 | + explained = true; |
| 49 | + void vscode.window |
| 50 | + .showInformationMessage( |
| 51 | + 'Go to Source Definition does not work in .tsrx files with TypeScript 7 yet. TSRX opened the definition instead.', |
| 52 | + 'Learn More', |
| 53 | + ) |
| 54 | + .then((choice) => { |
| 55 | + if (choice) void vscode.env.openExternal(vscode.Uri.parse(TYPESCRIPT_7_TRACKING_URL)); |
| 56 | + }); |
| 57 | + } |
| 58 | + locations = await definitions(document.uri, position); |
| 59 | + not_found = 'No definition found.'; |
| 60 | + } else { |
| 61 | + locations = await vscode.window.withProgress( |
| 62 | + { location: vscode.ProgressLocation.Window, title: 'Finding source definitions' }, |
| 63 | + () => source_definitions(document.uri, position), |
| 64 | + ); |
| 65 | + if (!locations) return; |
| 66 | + } |
| 67 | + // The editor and position are explicit, so this works without the editor focus. |
| 68 | + await vscode.commands.executeCommand( |
| 69 | + 'editor.action.goToLocations', |
| 70 | + document.uri, |
| 71 | + position, |
| 72 | + locations, |
| 73 | + 'goto', |
| 74 | + not_found, |
| 75 | + ); |
| 76 | + }); |
| 77 | +} |
| 78 | + |
| 79 | +/** |
| 80 | + * tsserver's source definitions, through `@tsrx/typescript-plugin`'s request. |
| 81 | + * @param {import('vscode').Uri} uri |
| 82 | + * @param {import('vscode').Position} position |
| 83 | + * @returns {Promise<import('vscode').Location[] | undefined>} `undefined` when the request |
| 84 | + * failed (after saying so) or was cancelled. |
| 85 | + */ |
| 86 | +async function source_definitions(uri, position) { |
| 87 | + /** @type {{ type?: string, body?: Array<{ file: string, start: TsserverPosition, end: TsserverPosition }> } | undefined} */ |
| 88 | + let response; |
| 89 | + try { |
| 90 | + response = await vscode.commands.executeCommand( |
| 91 | + 'typescript.tsserverRequest', |
| 92 | + SOURCE_DEFINITION_COMMAND, |
| 93 | + { file: uri, line: position.line + 1, offset: position.character + 1 }, |
| 94 | + ); |
| 95 | + } catch (error) { |
| 96 | + const message = error instanceof Error ? error.message : String(error); |
| 97 | + vscode.window.showErrorMessage(`Go to Source Definition failed: ${message}`); |
| 98 | + return undefined; |
| 99 | + } |
| 100 | + if (response?.type === 'cancelled') return undefined; |
| 101 | + return (response?.body ?? []).map( |
| 102 | + (span) => |
| 103 | + new vscode.Location( |
| 104 | + vscode.Uri.file(span.file), |
| 105 | + new vscode.Range( |
| 106 | + span.start.line - 1, |
| 107 | + span.start.offset - 1, |
| 108 | + span.end.line - 1, |
| 109 | + span.end.offset - 1, |
| 110 | + ), |
| 111 | + ), |
| 112 | + ); |
| 113 | +} |
| 114 | + |
| 115 | +/** |
| 116 | + * Go to Definition's locations, from whichever TypeScript serves the file. |
| 117 | + * @param {import('vscode').Uri} uri |
| 118 | + * @param {import('vscode').Position} position |
| 119 | + * @returns {Promise<import('vscode').Location[]>} |
| 120 | + */ |
| 121 | +async function definitions(uri, position) { |
| 122 | + /** @type {Array<import('vscode').Location | import('vscode').LocationLink> | undefined} */ |
| 123 | + const found = await vscode.commands.executeCommand( |
| 124 | + 'vscode.executeDefinitionProvider', |
| 125 | + uri, |
| 126 | + position, |
| 127 | + ); |
| 128 | + return (found ?? []).map((definition) => |
| 129 | + 'targetUri' in definition |
| 130 | + ? new vscode.Location( |
| 131 | + definition.targetUri, |
| 132 | + definition.targetSelectionRange ?? definition.targetRange, |
| 133 | + ) |
| 134 | + : definition, |
| 135 | + ); |
| 136 | +} |
| 137 | + |
| 138 | +/** @typedef {{ line: number, offset: number }} TsserverPosition One-based, as tsserver counts. */ |
0 commit comments