From f1dd6ec2dfc374101231fb95232b18b22d0aa346 Mon Sep 17 00:00:00 2001 From: Gannon McGibbon Date: Thu, 27 Jun 2024 21:12:35 -0400 Subject: [PATCH] Add file open support for route lookup To support mapping controller actions to routes in Shopify/ruby-lsp-rails#241. --- vscode/src/common.ts | 1 + vscode/src/openFile.ts | 38 ++++++++++++++++++++++++++++++++++++++ vscode/src/rubyLsp.ts | 12 ++++++++++++ vscode/src/telemetry.ts | 2 +- 4 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 vscode/src/openFile.ts diff --git a/vscode/src/common.ts b/vscode/src/common.ts index 041e9954a..8af0332b8 100644 --- a/vscode/src/common.ts +++ b/vscode/src/common.ts @@ -21,6 +21,7 @@ export enum Command { DisplayAddons = "rubyLsp.displayAddons", RunTask = "rubyLsp.runTask", BundleInstall = "rubyLsp.bundleInstall", + OpenFile = "rubyLsp.openFile", } export interface RubyInterface { diff --git a/vscode/src/openFile.ts b/vscode/src/openFile.ts new file mode 100644 index 000000000..bf097146a --- /dev/null +++ b/vscode/src/openFile.ts @@ -0,0 +1,38 @@ +import * as vscode from "vscode"; + +import { Telemetry } from "./telemetry"; +import { Workspace } from "./workspace"; + +interface SourceLocation { + file: string; + line: number; + character?: number; + endLine?: number; + endCharacter?: number; +} + +export async function openFile( + telemetry: Telemetry, + workspace: Workspace | undefined, + sourceLocation: SourceLocation, +) { + const { file, ...location } = sourceLocation; + const { + line, + character = 0, + endLine = line, + endCharacter = character, + } = location; + const selection = new vscode.Range(line, character, endLine, endCharacter); + const uri = vscode.Uri.parse(`file://${file}`); + const doc = await vscode.workspace.openTextDocument(uri); + + await vscode.window.showTextDocument(doc, { selection }); + + if (workspace?.lspClient?.serverVersion) { + await telemetry.sendCodeLensEvent( + "open_file", + workspace.lspClient.serverVersion, + ); + } +} diff --git a/vscode/src/rubyLsp.ts b/vscode/src/rubyLsp.ts index e2d28736d..27df2a83e 100644 --- a/vscode/src/rubyLsp.ts +++ b/vscode/src/rubyLsp.ts @@ -8,6 +8,7 @@ import { Command, STATUS_EMITTER } from "./common"; import { ManagerIdentifier, ManagerConfiguration } from "./ruby"; import { StatusItems } from "./status"; import { TestController } from "./testController"; +import { openFile } from "./openFile"; import { Debugger } from "./debugger"; import { DependenciesTree } from "./dependenciesTree"; @@ -413,6 +414,17 @@ export class RubyLsp { terminal.sendText("bundle install"); }, ), + vscode.commands.registerCommand( + Command.OpenFile, + (rubySourceLocation: [string, string]) => { + const [file, line] = rubySourceLocation; + const workspace = this.currentActiveWorkspace(); + return openFile(this.telemetry, workspace, { + file, + line: parseInt(line, 10) - 1, + }); + }, + ), ); } diff --git a/vscode/src/telemetry.ts b/vscode/src/telemetry.ts index ef08f0fce..4c3af7236 100644 --- a/vscode/src/telemetry.ts +++ b/vscode/src/telemetry.ts @@ -20,7 +20,7 @@ export interface ConfigurationEvent { } export interface CodeLensEvent { - type: "test" | "debug" | "test_in_terminal"; + type: "test" | "debug" | "test_in_terminal" | "open_file"; lspVersion: string; }