From 8786fcaf34ddfa36c8f55fbbbd5ce095c5448471 Mon Sep 17 00:00:00 2001 From: LeoAnders Date: Mon, 13 Oct 2025 16:59:01 -0300 Subject: [PATCH] Revert "Fix unified definition navigation and Ctrl+Click local resolution (#32)" This reverts commit dd066d51c69cdb092a394285c4e7aabd482b0c7c. --- package.json | 5 + src/ccs/commands/followDefinitionLink.ts | 32 ++++ src/ccs/commands/goToDefinitionLocalFirst.ts | 26 +++ .../navigation/followDefinitionLink.ts | 23 --- .../navigation/goToDefinitionLocalFirst.ts | 19 --- .../commands/navigation/navigateDefinition.ts | 148 ------------------ src/ccs/index.ts | 5 +- 7 files changed, 65 insertions(+), 193 deletions(-) create mode 100644 src/ccs/commands/followDefinitionLink.ts create mode 100644 src/ccs/commands/goToDefinitionLocalFirst.ts delete mode 100644 src/ccs/commands/navigation/followDefinitionLink.ts delete mode 100644 src/ccs/commands/navigation/goToDefinitionLocalFirst.ts delete mode 100644 src/ccs/commands/navigation/navigateDefinition.ts diff --git a/package.json b/package.json index e4bd1208..2c2f2b77 100644 --- a/package.json +++ b/package.json @@ -538,6 +538,11 @@ } ], "editor/title": [ + { + "command": "vscode-objectscript.ccs.goToDefinition", + "group": "navigation@0", + "when": "editorTextFocus && editorLangId =~ /^objectscript/" + }, { "command": "-editor.action.revealDefinition", "group": "navigation@0", diff --git a/src/ccs/commands/followDefinitionLink.ts b/src/ccs/commands/followDefinitionLink.ts new file mode 100644 index 00000000..b5f81ad3 --- /dev/null +++ b/src/ccs/commands/followDefinitionLink.ts @@ -0,0 +1,32 @@ +import * as vscode from "vscode"; + +import { lookupCcsDefinition } from "../features/definitionLookup/lookup"; + +export async function followDefinitionLink(documentUri: string, line: number, character: number): Promise { + if (!documentUri || typeof line !== "number" || typeof character !== "number") { + return; + } + + const uri = vscode.Uri.parse(documentUri); + const document = + vscode.workspace.textDocuments.find((doc) => doc.uri.toString() === documentUri) ?? + (await vscode.workspace.openTextDocument(uri)); + + const position = new vscode.Position(line, character); + const tokenSource = new vscode.CancellationTokenSource(); + + try { + const location = await lookupCcsDefinition(document, position, tokenSource.token); + if (location) { + await vscode.window.showTextDocument(location.uri, { selection: location.range }); + return; + } + + await vscode.window.showTextDocument(document, { + selection: new vscode.Range(position, position), + }); + await vscode.commands.executeCommand("editor.action.revealDefinition"); + } finally { + tokenSource.dispose(); + } +} diff --git a/src/ccs/commands/goToDefinitionLocalFirst.ts b/src/ccs/commands/goToDefinitionLocalFirst.ts new file mode 100644 index 00000000..31d48b38 --- /dev/null +++ b/src/ccs/commands/goToDefinitionLocalFirst.ts @@ -0,0 +1,26 @@ +import * as vscode from "vscode"; + +import { lookupCcsDefinition } from "../features/definitionLookup/lookup"; + +export async function goToDefinitionLocalFirst(): Promise { + const editor = vscode.window.activeTextEditor; + if (!editor) { + return; + } + + const { document, selection } = editor; + const position = selection.active; + const tokenSource = new vscode.CancellationTokenSource(); + + try { + const location = await lookupCcsDefinition(document, position, tokenSource.token); + if (location) { + await vscode.window.showTextDocument(location.uri, { selection: location.range }); + return; + } + } finally { + tokenSource.dispose(); + } + + await vscode.commands.executeCommand("editor.action.revealDefinition"); +} diff --git a/src/ccs/commands/navigation/followDefinitionLink.ts b/src/ccs/commands/navigation/followDefinitionLink.ts deleted file mode 100644 index dd7feac8..00000000 --- a/src/ccs/commands/navigation/followDefinitionLink.ts +++ /dev/null @@ -1,23 +0,0 @@ -import * as vscode from "vscode"; - -import { navigateToDefinition } from "./navigateDefinition"; - -export async function followDefinitionLink(documentUri: string, line: number, character: number): Promise { - if (!documentUri || typeof line !== "number" || typeof character !== "number") { - return; - } - - const uri = vscode.Uri.parse(documentUri); - const document = - vscode.workspace.textDocuments.find((doc) => doc.uri.toString() === documentUri) ?? - (await vscode.workspace.openTextDocument(uri)); - - const position = new vscode.Position(line, character); - const editor = vscode.window.visibleTextEditors.find((item) => item.document === document); - - await navigateToDefinition(document, position, { - preview: false, - preserveFocus: false, - viewColumn: editor?.viewColumn, - }); -} diff --git a/src/ccs/commands/navigation/goToDefinitionLocalFirst.ts b/src/ccs/commands/navigation/goToDefinitionLocalFirst.ts deleted file mode 100644 index ff68b04d..00000000 --- a/src/ccs/commands/navigation/goToDefinitionLocalFirst.ts +++ /dev/null @@ -1,19 +0,0 @@ -import * as vscode from "vscode"; - -import { navigateToDefinition } from "./navigateDefinition"; - -export async function goToDefinitionLocalFirst(): Promise { - const editor = vscode.window.activeTextEditor; - if (!editor) { - return; - } - - const { document, selection } = editor; - const position = selection.active; - - await navigateToDefinition(document, position, { - preview: false, - preserveFocus: false, - viewColumn: editor.viewColumn, - }); -} diff --git a/src/ccs/commands/navigation/navigateDefinition.ts b/src/ccs/commands/navigation/navigateDefinition.ts deleted file mode 100644 index 63180980..00000000 --- a/src/ccs/commands/navigation/navigateDefinition.ts +++ /dev/null @@ -1,148 +0,0 @@ -import * as vscode from "vscode"; - -import { currentFile, type CurrentTextFile } from "../../../utils"; -import { extractDefinitionQuery, type QueryMatch } from "../../features/definitionLookup/extractQuery"; -import { lookupCcsDefinition } from "../../features/definitionLookup/lookup"; - -export interface NavigateOpts { - preview?: boolean; - preserveFocus?: boolean; - viewColumn?: vscode.ViewColumn; -} - -export async function navigateToDefinition( - document: vscode.TextDocument, - position: vscode.Position, - opts: NavigateOpts = {} -): Promise { - const tokenSource = new vscode.CancellationTokenSource(); - try { - const ccsLocation = await lookupCcsDefinition(document, position, tokenSource.token); - if (tokenSource.token.isCancellationRequested) { - return false; - } - if (ccsLocation) { - await showLocation(ccsLocation, opts); - return true; - } - - if (tokenSource.token.isCancellationRequested) { - return false; - } - - const match = extractDefinitionQuery(document, position); - if (match) { - const current = currentFile(document); - if (current && isLocalDefinition(match, current)) { - return await useNativeDefinitionProvider(document, position); - } - } - - return await useNativeDefinitionProvider(document, position); - } finally { - tokenSource.dispose(); - } -} - -type DefinitionResults = vscode.Location | vscode.Location[] | vscode.LocationLink[]; - -/* - * Ask VS Code's definition providers first (non-UI), then run the standard reveal command. - * This preserves native behaviors (peek/preview) and avoids unnecessary reopens. - */ -async function useNativeDefinitionProvider(document: vscode.TextDocument, position: vscode.Position): Promise { - let definitions: DefinitionResults | undefined; - try { - definitions = await vscode.commands.executeCommand( - "vscode.executeDefinitionProvider", - document.uri, - position - ); - } catch (error) { - return false; - } - - if (!hasDefinitions(definitions)) { - return false; - } - - // Center the source position if it's outside the viewport, without smooth animation - const editor = getEditorForDocument(document); - if (editor) { - const selection = new vscode.Selection(position, position); - editor.selection = selection; - editor.revealRange(selection, vscode.TextEditorRevealType.InCenterIfOutsideViewport); - } - - await vscode.commands.executeCommand("editor.action.revealDefinition"); - return true; -} - -// Minimal “has result” check for any of the definition shapes -function hasDefinitions(definitions: DefinitionResults | undefined): boolean { - if (!definitions) { - return false; - } - if (Array.isArray(definitions)) { - return definitions.length > 0; - } - return true; -} - -// Open the target and place the caret exactly at the returned range (no extra reveal) -async function showLocation(location: vscode.Location, opts: NavigateOpts): Promise { - const showOptions: vscode.TextDocumentShowOptions = { selection: location.range }; - if (opts.preview !== undefined) { - showOptions.preview = opts.preview; - } - if (opts.preserveFocus !== undefined) { - showOptions.preserveFocus = opts.preserveFocus; - } - if (opts.viewColumn !== undefined) { - showOptions.viewColumn = opts.viewColumn; - } - await vscode.window.showTextDocument(location.uri, showOptions); -} - -// Try to reuse an already visible editor for the document (avoids reopen flicker) -function getEditorForDocument(document: vscode.TextDocument): vscode.TextEditor | undefined { - return ( - vscode.window.visibleTextEditors.find((editor) => editor.document === document) ?? - (vscode.window.activeTextEditor?.document === document ? vscode.window.activeTextEditor : undefined) - ); -} - -// Compare current file name (without extension) to routine name (case-insensitive) -function isCurrentRoutine(current: CurrentTextFile, routineName: string): boolean { - const match = current.name.match(/^(.*)\.(mac|int|inc)$/i); - if (!match) { - return false; - } - return match[1].toLowerCase() === routineName.toLowerCase(); -} - -// Compare current .cls name (without .cls) to class name (case-insensitive) -function isCurrentClass(current: CurrentTextFile, className: string): boolean { - if (!current.name.toLowerCase().endsWith(".cls")) { - return false; - } - const currentClassName = current.name.slice(0, -4); - return currentClassName.toLowerCase() === className.toLowerCase(); -} - -// Local if symbol belongs to the same routine or same class as the current file -function isLocalDefinition(match: QueryMatch, current: CurrentTextFile): boolean { - if (!match.symbolName) { - return false; - } - - if (match.kind === "labelRoutine") { - return isCurrentRoutine(current, match.symbolName); - } - - if (match.kind === "class") { - return isCurrentClass(current, match.symbolName); - } - - return false; -} diff --git a/src/ccs/index.ts b/src/ccs/index.ts index 7c82b9c5..9d0e709b 100644 --- a/src/ccs/index.ts +++ b/src/ccs/index.ts @@ -13,9 +13,8 @@ export { type QueryMatch, type QueryKind, } from "./features/definitionLookup/extractQuery"; -export { goToDefinitionLocalFirst } from "./commands/navigation/goToDefinitionLocalFirst"; -export { followDefinitionLink } from "./commands/navigation/followDefinitionLink"; -export { navigateToDefinition, type NavigateOpts } from "./commands/navigation/navigateDefinition"; +export { goToDefinitionLocalFirst } from "./commands/goToDefinitionLocalFirst"; +export { followDefinitionLink } from "./commands/followDefinitionLink"; export { PrioritizedDefinitionProvider } from "./providers/PrioritizedDefinitionProvider"; export { DefinitionDocumentLinkProvider,