Skip to content
This repository has been archived by the owner on May 30, 2024. It is now read-only.

Commit

Permalink
Add code lens command for mapping actions to routes
Browse files Browse the repository at this point in the history
  • Loading branch information
gmcgibbon committed Feb 3, 2024
1 parent ca4f02a commit d783acf
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 1 deletion.
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@
"command": "rubyLsp.showSyntaxTree",
"title": "Show syntax tree",
"category": "Ruby LSP"
},
{
"command": "rubyLsp.openFile",
"title": "Opens a local file at a given position",
"category": "Ruby LSP"
}
],
"configuration": {
Expand Down
1 change: 1 addition & 0 deletions src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export enum Command {
DebugTest = "rubyLsp.debugTest",
OpenLink = "rubyLsp.openLink",
ShowSyntaxTree = "rubyLsp.showSyntaxTree",
OpenFile = "rubyLsp.openFile",
}

export interface RubyInterface {
Expand Down
37 changes: 37 additions & 0 deletions src/fileController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import * as vscode from "vscode";

import { Telemetry } from "./telemetry";
import { Workspace } from "./workspace";

export class FileController {
private readonly telemetry: Telemetry;
private readonly currentWorkspace: () => Workspace | undefined;

constructor(
_context: vscode.ExtensionContext,
telemetry: Telemetry,
currentWorkspace: () => Workspace | undefined,
) {
this.telemetry = telemetry;
this.currentWorkspace = currentWorkspace;
}

async openFile(sourceLocation: [string, string]) {
const workspace = this.currentWorkspace();
const file = sourceLocation[0];
const line = parseInt(sourceLocation[1], 10) - 1;

const uri = vscode.Uri.parse(`file://${file}`);
const doc = await vscode.workspace.openTextDocument(uri);
await vscode.window.showTextDocument(doc, {
selection: new vscode.Range(line, 0, line, 0),
});

if (workspace?.lspClient?.serverVersion) {
await this.telemetry.sendCodeLensEvent(
"open_file",
workspace.lspClient.serverVersion,
);
}
}
}
11 changes: 11 additions & 0 deletions src/rubyLsp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { Command, STATUS_EMITTER, pathExists } from "./common";
import { VersionManager } from "./ruby";
import { StatusItems } from "./status";
import { TestController } from "./testController";
import { FileController } from "./fileController";
import { Debugger } from "./debugger";

// The RubyLsp class represents an instance of the entire extension. This should only be instantiated once at the
Expand All @@ -21,6 +22,7 @@ export class RubyLsp {
private readonly context: vscode.ExtensionContext;
private readonly statusItems: StatusItems;
private readonly testController: TestController;
private readonly fileController: FileController;
private readonly debug: Debugger;

constructor(context: vscode.ExtensionContext) {
Expand All @@ -31,6 +33,11 @@ export class RubyLsp {
this.telemetry,
this.currentActiveWorkspace.bind(this),
);
this.fileController = new FileController(
context,
this.telemetry,
this.currentActiveWorkspace.bind(this),
);
this.debug = new Debugger(context, this.workspaceResolver.bind(this));
this.registerCommands(context);

Expand Down Expand Up @@ -328,6 +335,10 @@ export class RubyLsp {
Command.DebugTest,
this.testController.debugTest.bind(this.testController),
),
vscode.commands.registerCommand(
Command.OpenFile,
this.fileController.openFile.bind(this.fileController),
),
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/telemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export interface ConfigurationEvent {
}

export interface CodeLensEvent {
type: "test" | "debug" | "test_in_terminal" | "link";
type: "test" | "debug" | "test_in_terminal" | "link" | "open_file";
lspVersion: string;
}

Expand Down

0 comments on commit d783acf

Please sign in to comment.