|
| 1 | +import * as vscode from "vscode"; |
| 2 | + |
| 3 | +import { currentFile, type CurrentTextFile } from "../../../utils"; |
| 4 | +import { extractDefinitionQuery, type QueryMatch } from "../../features/definitionLookup/extractQuery"; |
| 5 | +import { lookupCcsDefinition } from "../../features/definitionLookup/lookup"; |
| 6 | + |
| 7 | +export interface NavigateOpts { |
| 8 | + preview?: boolean; |
| 9 | + preserveFocus?: boolean; |
| 10 | + viewColumn?: vscode.ViewColumn; |
| 11 | +} |
| 12 | + |
| 13 | +export async function navigateToDefinition( |
| 14 | + document: vscode.TextDocument, |
| 15 | + position: vscode.Position, |
| 16 | + opts: NavigateOpts = {} |
| 17 | +): Promise<boolean> { |
| 18 | + const tokenSource = new vscode.CancellationTokenSource(); |
| 19 | + try { |
| 20 | + const ccsLocation = await lookupCcsDefinition(document, position, tokenSource.token); |
| 21 | + if (tokenSource.token.isCancellationRequested) { |
| 22 | + return false; |
| 23 | + } |
| 24 | + if (ccsLocation) { |
| 25 | + await showLocation(ccsLocation, opts); |
| 26 | + return true; |
| 27 | + } |
| 28 | + |
| 29 | + if (tokenSource.token.isCancellationRequested) { |
| 30 | + return false; |
| 31 | + } |
| 32 | + |
| 33 | + const match = extractDefinitionQuery(document, position); |
| 34 | + if (match) { |
| 35 | + const current = currentFile(document); |
| 36 | + if (current && isLocalDefinition(match, current)) { |
| 37 | + return await useNativeDefinitionProvider(document, position); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + return await useNativeDefinitionProvider(document, position); |
| 42 | + } finally { |
| 43 | + tokenSource.dispose(); |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +type DefinitionResults = vscode.Location | vscode.Location[] | vscode.LocationLink[]; |
| 48 | + |
| 49 | +/* |
| 50 | + * Ask VS Code's definition providers first (non-UI), then run the standard reveal command. |
| 51 | + * This preserves native behaviors (peek/preview) and avoids unnecessary reopens. |
| 52 | + */ |
| 53 | +async function useNativeDefinitionProvider(document: vscode.TextDocument, position: vscode.Position): Promise<boolean> { |
| 54 | + let definitions: DefinitionResults | undefined; |
| 55 | + try { |
| 56 | + definitions = await vscode.commands.executeCommand<DefinitionResults>( |
| 57 | + "vscode.executeDefinitionProvider", |
| 58 | + document.uri, |
| 59 | + position |
| 60 | + ); |
| 61 | + } catch (error) { |
| 62 | + return false; |
| 63 | + } |
| 64 | + |
| 65 | + if (!hasDefinitions(definitions)) { |
| 66 | + return false; |
| 67 | + } |
| 68 | + |
| 69 | + // Center the source position if it's outside the viewport, without smooth animation |
| 70 | + const editor = getEditorForDocument(document); |
| 71 | + if (editor) { |
| 72 | + const selection = new vscode.Selection(position, position); |
| 73 | + editor.selection = selection; |
| 74 | + editor.revealRange(selection, vscode.TextEditorRevealType.InCenterIfOutsideViewport); |
| 75 | + } |
| 76 | + |
| 77 | + await vscode.commands.executeCommand("editor.action.revealDefinition"); |
| 78 | + return true; |
| 79 | +} |
| 80 | + |
| 81 | +// Minimal “has result” check for any of the definition shapes |
| 82 | +function hasDefinitions(definitions: DefinitionResults | undefined): boolean { |
| 83 | + if (!definitions) { |
| 84 | + return false; |
| 85 | + } |
| 86 | + if (Array.isArray(definitions)) { |
| 87 | + return definitions.length > 0; |
| 88 | + } |
| 89 | + return true; |
| 90 | +} |
| 91 | + |
| 92 | +// Open the target and place the caret exactly at the returned range (no extra reveal) |
| 93 | +async function showLocation(location: vscode.Location, opts: NavigateOpts): Promise<void> { |
| 94 | + const showOptions: vscode.TextDocumentShowOptions = { selection: location.range }; |
| 95 | + if (opts.preview !== undefined) { |
| 96 | + showOptions.preview = opts.preview; |
| 97 | + } |
| 98 | + if (opts.preserveFocus !== undefined) { |
| 99 | + showOptions.preserveFocus = opts.preserveFocus; |
| 100 | + } |
| 101 | + if (opts.viewColumn !== undefined) { |
| 102 | + showOptions.viewColumn = opts.viewColumn; |
| 103 | + } |
| 104 | + await vscode.window.showTextDocument(location.uri, showOptions); |
| 105 | +} |
| 106 | + |
| 107 | +// Try to reuse an already visible editor for the document (avoids reopen flicker) |
| 108 | +function getEditorForDocument(document: vscode.TextDocument): vscode.TextEditor | undefined { |
| 109 | + return ( |
| 110 | + vscode.window.visibleTextEditors.find((editor) => editor.document === document) ?? |
| 111 | + (vscode.window.activeTextEditor?.document === document ? vscode.window.activeTextEditor : undefined) |
| 112 | + ); |
| 113 | +} |
| 114 | + |
| 115 | +// Compare current file name (without extension) to routine name (case-insensitive) |
| 116 | +function isCurrentRoutine(current: CurrentTextFile, routineName: string): boolean { |
| 117 | + const match = current.name.match(/^(.*)\.(mac|int|inc)$/i); |
| 118 | + if (!match) { |
| 119 | + return false; |
| 120 | + } |
| 121 | + return match[1].toLowerCase() === routineName.toLowerCase(); |
| 122 | +} |
| 123 | + |
| 124 | +// Compare current .cls name (without .cls) to class name (case-insensitive) |
| 125 | +function isCurrentClass(current: CurrentTextFile, className: string): boolean { |
| 126 | + if (!current.name.toLowerCase().endsWith(".cls")) { |
| 127 | + return false; |
| 128 | + } |
| 129 | + const currentClassName = current.name.slice(0, -4); |
| 130 | + return currentClassName.toLowerCase() === className.toLowerCase(); |
| 131 | +} |
| 132 | + |
| 133 | +// Local if symbol belongs to the same routine or same class as the current file |
| 134 | +function isLocalDefinition(match: QueryMatch, current: CurrentTextFile): boolean { |
| 135 | + if (!match.symbolName) { |
| 136 | + return false; |
| 137 | + } |
| 138 | + |
| 139 | + if (match.kind === "labelRoutine") { |
| 140 | + return isCurrentRoutine(current, match.symbolName); |
| 141 | + } |
| 142 | + |
| 143 | + if (match.kind === "class") { |
| 144 | + return isCurrentClass(current, match.symbolName); |
| 145 | + } |
| 146 | + |
| 147 | + return false; |
| 148 | +} |
0 commit comments