diff --git a/kotlin-vscode/src/directoryNavigator.ts b/kotlin-vscode/src/directoryNavigator.ts new file mode 100644 index 0000000..c9683fb --- /dev/null +++ b/kotlin-vscode/src/directoryNavigator.ts @@ -0,0 +1,42 @@ +import * as vscode from 'vscode'; +import * as fs from 'fs'; + +export function registerDirectoryNavigator(context: vscode.ExtensionContext) { + const disposable = vscode.commands.registerTextEditorCommand( + 'editor.action.revealDefinition', + async (textEditor: vscode.TextEditor) => { + if (textEditor.document.languageId !== 'kotlin') { + return vscode.commands.executeCommand('vscode.executeDefinitionProvider', + textEditor.document.uri, textEditor.selection.active); + } + + const definitions = await vscode.commands.executeCommand( + 'vscode.executeDefinitionProvider', + textEditor.document.uri, + textEditor.selection.active + ); + + if (!definitions?.length) return; + + for (const def of definitions) { + try { + const isDir = fs.statSync(def.uri.fsPath).isDirectory(); + if (isDir) { + await vscode.commands.executeCommand('workbench.view.explorer'); + await vscode.commands.executeCommand('revealInExplorer', def.uri); + return; + } + } catch {} + } + + for (const def of definitions) { + const document = await vscode.workspace.openTextDocument(def.uri); + await vscode.window.showTextDocument(document, { + selection: def.range + }); + } + } + ); + + context.subscriptions.push(disposable); +} diff --git a/kotlin-vscode/src/extension.ts b/kotlin-vscode/src/extension.ts index cda2150..d6dd839 100644 --- a/kotlin-vscode/src/extension.ts +++ b/kotlin-vscode/src/extension.ts @@ -3,6 +3,7 @@ import {commands, type ExtensionContext, Uri, window, workspace} from "vscode" import {registerDecompiler} from "./decompiler" import { initLspClient, startLspClient } from './lspClient'; import { registerStatusBarItem } from './statusBar'; +import { registerDirectoryNavigator } from './directoryNavigator'; export const extensionId = 'kotlin' @@ -35,7 +36,8 @@ export async function activate(context: ExtensionContext) { _context = context registerDecompiler(context) registerExportWorkspaceToJsonCommand(context) + registerDirectoryNavigator(context) registerStatusBarItem() initLspClient() await startLspClient() -} \ No newline at end of file +}