diff --git a/CHANGELOG.md b/CHANGELOG.md index d77e6ba..248034e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,9 @@ All notable changes to the Simics Target Debug extension will be documented here ### 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)). + - The Start button in TCF Launch Configurations is no longer disabled while any Simics Target Debug session runs, only for configurations that connect to an agent (host and port) that already has a session. 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 e4d0374..8e59977 100644 --- a/src/launchSettings/editorProvider.ts +++ b/src/launchSettings/editorProvider.ts @@ -400,11 +400,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); } } @@ -465,6 +468,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();