Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
75 changes: 71 additions & 4 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,47 @@ async function addSymbolFile(symbolFiles: SymbolFilesProvider): Promise<void> {
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<string | undefined> {
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<string | undefined> {
Expand All @@ -311,17 +352,38 @@ function promptSymbolFilePath(workspaceFolder?: string, initialPath = ""): Promi

async function update(value: string): Promise<void> {
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",
Expand All @@ -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());
Expand Down
15 changes: 13 additions & 2 deletions src/launchSettings/editorProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<vscode.Uri | undefined> {
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);
}
}
Expand Down Expand Up @@ -465,6 +468,14 @@ async function exists(file: string): Promise<boolean> {
}
}

async function isFolder(file: string): Promise<boolean> {
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();
Expand Down