Skip to content

Commit

Permalink
Add basic web view for profile results
Browse files Browse the repository at this point in the history
  • Loading branch information
vinistock committed Oct 1, 2024
1 parent 42b0053 commit c34029b
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 8 deletions.
14 changes: 14 additions & 0 deletions vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@
"group": "9_cutcopypaste"
}
],
"editor/title": [
{
"command": "rubyLsp.profileFile",
"when": "editorTextFocus && resourceLangId == ruby",
"group": "navigation",
"icon": "$(record)"
}
],
"view/title": [
{
"command": "rubyLsp.fileOperation",
Expand Down Expand Up @@ -155,6 +163,12 @@
"command": "rubyLsp.collectRubyLspInfo",
"title": "Collect Ruby LSP Information for Issue Reporting",
"category": "Ruby LSP"
},
{
"command": "rubyLsp.profileFile",
"title": "Profile file",
"category": "Ruby LSP",
"icon": "$(record)"
}
],
"configuration": {
Expand Down
1 change: 1 addition & 0 deletions vscode/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export enum Command {
NewMinitestFile = "rubyLsp.newMinitestFile",
CollectRubyLspInfo = "rubyLsp.collectRubyLspInfo",
StartServerInDebugMode = "rubyLsp.startServerInDebugMode",
ProfileFile = "rubyLsp.profileFile",
}

export interface RubyInterface {
Expand Down
54 changes: 46 additions & 8 deletions vscode/src/profileTaskProvider.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,47 @@
import path from "path";

import * as vscode from "vscode";

import { Workspace } from "./workspace";

class ProfileView {
private readonly panel: vscode.WebviewPanel;

constructor(fileName: string, profile: any) {
this.panel = vscode.window.createWebviewPanel(
ProfileTaskProvider.TaskType,
`Profile results ${fileName}`,
vscode.ViewColumn.One,
{},
);

this.generateHtml(fileName, profile);
this.panel.onDidDispose(() => {
this.panel.dispose();
});
}

reveal() {
this.panel.reveal();
}

private generateHtml(file: string, profile: any) {
this.panel.webview.html = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Profile results ${file}</title>
</head>
<body>
<h1>Results</h1>
${profile.toString()}
</body>
</html>`;
}
}

class ProfileTaskTerminal implements vscode.Pseudoterminal {
readonly writeEmitter = new vscode.EventEmitter<string>();
onDidWrite: vscode.Event<string> = this.writeEmitter.event;
Expand Down Expand Up @@ -40,23 +80,21 @@ class ProfileTaskTerminal implements vscode.Pseudoterminal {
try {
const profile = await vscode.workspace.fs.readFile(profileUri);
this.writeEmitter.fire(
"Successfully profiled. Generating visualization...",
"Successfully profiled. Generating visualization...\r\n",
);

const profileView = new ProfileView(path.basename(currentFile), profile);
this.closeEmitter.fire(0);
profileView.reveal();
} catch (error) {
this.writeEmitter.fire(
`An error occurred while profiling (press any key to close):\r\n ${stderr}\r\n`,
);
this.closeEmitter.fire(1);
}

this.closeEmitter.fire(0);
}

close(): void {}

// Close the task pseudo terminal if the user presses any keys
handleInput(_data: string): void {
this.closeEmitter.fire(0);
}
}

export class ProfileTaskProvider implements vscode.TaskProvider {
Expand Down
14 changes: 14 additions & 0 deletions vscode/src/rubyLsp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,20 @@ export class RubyLsp {
await workspace?.start(true);
},
),
vscode.commands.registerCommand(Command.ProfileFile, async () => {
const tasks = await vscode.tasks.fetchTasks({
type: ProfileTaskProvider.TaskType,
});

if (tasks.length === 0) {
await vscode.window.showErrorMessage(
"No profile tasks found. Has it been declared in the .vscode/tasks.json file?",
);
return;
}

await vscode.tasks.executeTask(tasks[0]);
}),
);
}

Expand Down

0 comments on commit c34029b

Please sign in to comment.