Skip to content

Commit

Permalink
Merge pull request #4 from sransara/support-relative-paths
Browse files Browse the repository at this point in the history
Support relative paths and some other usability improvements
  • Loading branch information
sransara authored Jul 26, 2024
2 parents 5ac8a35 + d727b12 commit 98b6d0c
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 15 deletions.
2 changes: 1 addition & 1 deletion external/speedscope
34 changes: 31 additions & 3 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,37 @@ export class Commands {
return;
}
uri = uris[0];
}
if (typeof uri === "string") {
uri = vscode.Uri.file(uri);
} else if (typeof uri === "string" || uri instanceof String) {
if (uri.startsWith('.')) {
// Resolve relative path using workspace folders
if (!vscode.workspace.workspaceFolders) {
vscode.window.showErrorMessage(`No workspace found to resolve relative path: ${uri}`);
return;
}
// Try to find the file in any of the workspace folders
try {
uri = await Promise.any(
vscode.workspace.workspaceFolders.map(async (folder) => {
const maybeUri = vscode.Uri.joinPath(folder.uri, uri as string);
await vscode.workspace.fs.stat(maybeUri);
return maybeUri;
}),
);
}
catch (e) {
vscode.window.showErrorMessage(`File not found in any workspace: ${uri}`);
return;
}
}
else {
try {
uri = vscode.Uri.file(uri as string);
await vscode.workspace.fs.stat(uri);
} catch (e) {
vscode.window.showErrorMessage(`File not found: ${uri}`);
return;
}
}
}
await vscode.commands.executeCommand(
"vscode.openWith",
Expand Down
29 changes: 19 additions & 10 deletions src/editor-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,16 +95,25 @@ export class SpeedscopeEditorProvider
if (e.clientEvent === "ready") {
this.logger.info(`Speedscope view for ${document.uri} is ready`);
this.logger.info(`Trying to load document: ${document.uri}`);
const docbytes = await vscode.workspace.fs.readFile(document.uri);
let filename = document.uri.path;
if (filename.includes("/")) {
filename = filename.slice(filename.lastIndexOf("/") + 1);
}
webviewPanel.webview.postMessage({
serverCommand: "openFile",
filename,
docbytes: new Uint8Array(docbytes),
});
await vscode.window.withProgress(
{
location: vscode.ProgressLocation.Notification,
title: "Loading profile...",
cancellable: false,
},
async () => {
const docbytes = await vscode.workspace.fs.readFile(document.uri);
let filename = document.uri.path;
if (filename.includes("/")) {
filename = filename.slice(filename.lastIndexOf("/") + 1);
}
webviewPanel.webview.postMessage({
serverCommand: "openFile",
filename,
docbytes: new Uint8Array(docbytes),
});
},
);
} else if (e.clientCommand === "console") {
const prefix = `speedscope view: console.${e.method}:`;
switch (e.method) {
Expand Down
15 changes: 14 additions & 1 deletion src/test/suite/extension.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ suite("Extension Test Suite", () => {

// openSpeedscope command should accept string argument as well as Uri
// this is necessary to support adding keybindings for example
test("Test openSpeedscope command with string argument", async () => {
test("Test openSpeedscope command with absolute path string argument", async () => {
const filePath = path.join(workspaceUri.path, "simple.prof");
await vscode.commands.executeCommand(
`${extensionName}.openSpeedscope`,
Expand All @@ -65,4 +65,17 @@ suite("Extension Test Suite", () => {
const extensionApi: PublicApi =
vscode.extensions.getExtension(extensionId)!.exports;
});

// Test if the extension can open a file with a relative path
test("Test openSpeedscope command with relative path string argument", async () => {
const filePath = "./simple.prof";
await vscode.commands.executeCommand(
`${extensionName}.openSpeedscope`,
filePath,
);

// Testing if the extension activated
const extensionApi: PublicApi =
vscode.extensions.getExtension(extensionId)!.exports;
});
});

0 comments on commit 98b6d0c

Please sign in to comment.