Skip to content

Commit 431533b

Browse files
Support virtual filesystems Fixes #154 (#155)
* Support virtual filesystems Fixes #154 * fix bug in parameter splitting * refactor
1 parent ea5f5aa commit 431533b

File tree

1 file changed

+45
-5
lines changed

1 file changed

+45
-5
lines changed

packages/vscode-js-profile-core/src/open-location.ts

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,20 @@ import { DownloadFileProvider } from './download-file-provider';
1111
import { ISourceLocation } from './location-mapping';
1212
import { properRelative } from './path';
1313

14-
const exists = async (file: string) => {
14+
const enum LinkType {
15+
Command,
16+
URI,
17+
}
18+
19+
type CommandLink = { type: LinkType.Command; command: string; args: unknown[] };
20+
type UriLink = { type: LinkType.URI; uri: vscode.Uri; isFile: boolean };
21+
type Link = CommandLink | UriLink;
22+
23+
const exists = async (uristr: string) => {
1524
try {
16-
await vscode.workspace.fs.stat(vscode.Uri.file(file));
25+
const uri = parseLink(uristr);
26+
if (uri.type === LinkType.Command) return true;
27+
await vscode.workspace.fs.stat(uri.uri);
1728
return true;
1829
} catch {
1930
return false;
@@ -59,19 +70,28 @@ const showPosition = async (
5970
await vscode.window.showTextDocument(doc, { viewColumn, selection: new vscode.Range(pos, pos) });
6071
};
6172

73+
const runCommand = (link: CommandLink) =>
74+
vscode.commands.executeCommand(link.command, ...link.args);
75+
6276
const showPositionInFile = async (
6377
rootPath: string | undefined,
6478
location: ISourceLocation,
6579
viewColumn?: vscode.ViewColumn,
66-
) => {
80+
): Promise<boolean> => {
6781
const diskPaths = getCandidateDiskPaths(rootPath, location.source);
6882
const foundPaths = await Promise.all(diskPaths.map(exists));
6983
const existingIndex = foundPaths.findIndex(ok => ok);
7084
if (existingIndex === -1) {
7185
return false;
7286
}
7387

74-
const doc = await vscode.workspace.openTextDocument(vscode.Uri.file(diskPaths[existingIndex]));
88+
const resolvedLink = parseLink(diskPaths[existingIndex]);
89+
if (resolvedLink.type === LinkType.Command) {
90+
await runCommand(resolvedLink); // delegate finding the position to the command provider
91+
return true;
92+
}
93+
94+
const doc = await vscode.workspace.openTextDocument(resolvedLink.uri);
7595
await showPosition(doc, location.lineNumber, location.columnNumber, viewColumn);
7696
return true;
7797
};
@@ -102,6 +122,23 @@ const showPositionInUrl = async (
102122
await showPosition(document, lineNumber + 1, columnNumber + 1, viewColumn);
103123
return true;
104124
};
125+
/**
126+
* Parses a link into a link object
127+
* @param url
128+
* @returns
129+
*/
130+
const parseLink = (link: string | undefined): Link => {
131+
const matchCommand = link?.match(/^command:([\w\.]+)(?:\?(.*))?/);
132+
if (matchCommand) {
133+
const [command, rawArgs] = matchCommand.slice(1);
134+
const parsed = rawArgs ? JSON.parse(decodeURIComponent(rawArgs)) : [];
135+
const args = Array.isArray(parsed) ? parsed : [parsed];
136+
return { type: LinkType.Command, command, args };
137+
}
138+
if (link?.match(/\w\w+:/))
139+
return { type: LinkType.URI, uri: vscode.Uri.parse(link || ''), isFile: false };
140+
return { type: LinkType.URI, uri: vscode.Uri.file(link || ''), isFile: true };
141+
};
105142

106143
/**
107144
* Gets possible locations for the source on the local disk.
@@ -111,8 +148,11 @@ export const getCandidateDiskPaths = (rootPath: string | undefined, source: Dap.
111148
return [];
112149
}
113150

151+
const uri = parseLink(source.path);
152+
114153
const locations = [source.path];
115-
if (!rootPath) {
154+
if (!rootPath || uri.type === LinkType.Command || !uri.isFile) {
155+
// no resolution for commands and virtual filesystems
116156
return locations;
117157
}
118158

0 commit comments

Comments
 (0)