From c7b512da4e7ddaf074df4993dae2af0cdea3f74d Mon Sep 17 00:00:00 2001 From: Magnus Andersson Date: Mon, 9 Nov 2026 12:09:35 +0100 Subject: [PATCH] Improve the file picker for symbol files Signed-off-by: Magnus Andersson --- CHANGELOG.md | 5 ++ src/extension.ts | 75 ++++++++++++++++++++++++++-- src/launchSettings/editorProvider.ts | 15 +++++- 3 files changed, 89 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 13cce66..108f71c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,11 @@ All notable changes to the Simics Target Debug extension will be documented here ## [Unreleased] +### Fixed + +- The file pickers for symbol files now offer the standard file dialog, a `..` entry, and pick files in the + current folder ([#29](https://github.com/intel/simics-vs-code-tcf-debug/issues/29)). + ## [0.5.0] - 2026-11-02 Initial release. diff --git a/src/extension.ts b/src/extension.ts index ab5d0ce..109b6d0 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -290,6 +290,47 @@ async function addSymbolFile(symbolFiles: SymbolFilesProvider): Promise { interface SymbolPathChoice extends vscode.QuickPickItem { path: string; directory?: boolean; + browse?: "file" | "folder"; +} + +const BROWSE_CHOICES: SymbolPathChoice[] = [ + { label: "$(file) Browse for a File...", path: "", browse: "file", alwaysShow: true }, + { label: "$(folder-opened) Browse for a Folder...", path: "", browse: "folder", alwaysShow: true }, +]; + +async function browseSymbolPath( + pick: "file" | "folder", + typed: string, + workspaceFolder?: string, +): Promise { + const uris = await vscode.window.showOpenDialog({ + canSelectFiles: pick === "file", + canSelectFolders: pick === "folder", + canSelectMany: false, + defaultUri: startFolder(typed, workspaceFolder), + openLabel: "Select", + }); + return uris?.[0]?.fsPath; +} + +// A file as defaultUri makes VS Code's simple file dialog behave like a save dialog. +function startFolder(typed: string, workspaceFolder?: string): vscode.Uri | undefined { + const value = typed.trim(); + const resolved = !value + ? undefined + : path.isAbsolute(value) + ? value + : workspaceFolder && path.join(workspaceFolder, value); + for (const candidate of resolved ? [resolved, path.dirname(resolved)] : []) { + try { + if (fs.statSync(candidate).isDirectory()) { + return vscode.Uri.file(candidate); + } + } catch { + // Try the next candidate. + } + } + return workspaceFolder ? vscode.Uri.file(workspaceFolder) : undefined; } function promptSymbolFilePath(workspaceFolder?: string, initialPath = ""): Promise { @@ -311,17 +352,38 @@ function promptSymbolFilePath(workspaceFolder?: string, initialPath = ""): Promi async function update(value: string): Promise { const typed = value.trim(); - const manual: SymbolPathChoice[] = typed - ? [{ label: typed, description: "Use this path", path: typed }] - : []; + const manual: SymbolPathChoice[] = [ + ...(typed ? [{ label: typed, description: "Use this path", path: typed }] : []), + ...BROWSE_CHOICES, + ]; picker.items = manual; picker.busy = true; const suggestions = await suggestSymbolPaths(value, workspaceFolder); if (finished || picker.value !== value) { return; } + const listed = value === "" || value.endsWith(path.sep) ? value : undefined; + const listedFolder = + listed === undefined + ? undefined + : path.isAbsolute(listed) + ? listed + : workspaceFolder && path.resolve(workspaceFolder, listed); + const parent: SymbolPathChoice[] = + listed !== undefined && listedFolder && path.dirname(listedFolder) !== path.resolve(listedFolder) + ? [ + { + label: "..", + description: "Parent Folder", + path: path.join(listed, ".."), + directory: true, + alwaysShow: true, + }, + ] + : []; picker.items = [ ...manual, + ...parent, ...suggestions.map((suggestion) => ({ label: suggestion.path + (suggestion.directory ? path.sep : ""), description: suggestion.directory ? "Folder" : "File", @@ -337,7 +399,12 @@ function promptSymbolFilePath(workspaceFolder?: string, initialPath = ""): Promi }); picker.onDidAccept(() => { const selected = picker.activeItems[0]; - if (selected?.directory) { + if (selected?.browse) { + const typed = picker.value; + finished = true; + picker.dispose(); + resolve(browseSymbolPath(selected.browse, typed, workspaceFolder)); + } else if (selected?.directory) { picker.value = selected.path + path.sep; } else if (selected?.path || picker.value.trim()) { finish(selected?.path ?? picker.value.trim()); diff --git a/src/launchSettings/editorProvider.ts b/src/launchSettings/editorProvider.ts index 2802b9a..6c29b80 100644 --- a/src/launchSettings/editorProvider.ts +++ b/src/launchSettings/editorProvider.ts @@ -394,11 +394,14 @@ async function pickPath( return relative === "" ? "${workspaceFolder}" : `\${workspaceFolder}/${relative.split(path.sep).join("/")}`; } -/** The current path if it exists, else its directory if that exists, else the workspace folder. */ +/** + * The current path if it is a folder, else its folder if that exists, else the workspace folder. + * A file would make VS Code's simple file dialog behave like a save dialog. + */ async function startUri(current: string, folder: string | undefined): Promise { const resolved = current.trim() ? hostPath(current, folder) : undefined; for (const candidate of resolved === undefined ? [] : [resolved, path.dirname(resolved)]) { - if (await exists(candidate)) { + if (await isFolder(candidate)) { return vscode.Uri.file(candidate); } } @@ -459,6 +462,14 @@ async function exists(file: string): Promise { } } +async function isFolder(file: string): Promise { + try { + return ((await vscode.workspace.fs.stat(vscode.Uri.file(file))).type & vscode.FileType.Directory) !== 0; + } catch { + return false; + } +} + /** The document text, if it still has the configuration the form showed. */ function checkedText(document: vscode.TextDocument, index: number, name: string): string { const text = document.getText();