Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[typescript] Better paths matching for move to existing file quickpick #181231

Merged
merged 6 commits into from
Aug 9, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -175,47 +175,80 @@ class MoveToFileRefactorCommand implements Command {
if (response.type !== 'response' || !response.body) {
return;
}
const body = response.body;

const selectExistingFileItem: vscode.QuickPickItem = {
label: vscode.l10n.t("Select existing file..."),
};
const selectNewFileItem: vscode.QuickPickItem = {
label: vscode.l10n.t("Enter new file path..."),
};

type DestinationItem = vscode.QuickPickItem & { readonly file: string };
type DestinationItem = vscode.QuickPickItem & { readonly file?: string };
const selectExistingFileItem: vscode.QuickPickItem = { label: vscode.l10n.t("Select existing file...") };
const selectNewFileItem: vscode.QuickPickItem = { label: vscode.l10n.t("Enter new file path...") };

const workspaceFolder = vscode.workspace.getWorkspaceFolder(document.uri);
const destinationItems = response.body.files.map((file): DestinationItem => {
const uri = this.client.toResource(file);
const parentDir = Utils.dirname(uri);

let description;
if (workspaceFolder) {
if (uri.scheme === Schemes.file) {
description = path.relative(workspaceFolder.uri.fsPath, parentDir.fsPath);
const quickPick = vscode.window.createQuickPick<DestinationItem>();
quickPick.ignoreFocusOut = true;

// true so we don't skip computing in the first call
let quickPickInRelativeMode = true;
const updateItems = () => {
const relativeQuery = ['./', '../'].find(str => quickPick.value.startsWith(str));
if (quickPickInRelativeMode === false && !!relativeQuery === false) {
return;
}
quickPickInRelativeMode = !!relativeQuery;
const destinationItems = body.files.map((file): DestinationItem | undefined => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be optimized to not compute anything if the user has not typed ./ or ../ to start the path

const uri = this.client.toResource(file);
const parentDir = Utils.dirname(uri);
const filename = Utils.basename(uri);

let description: string | undefined;
if (workspaceFolder) {
if (uri.scheme === Schemes.file) {
description = path.relative(workspaceFolder.uri.fsPath, parentDir.fsPath);
} else {
description = path.posix.relative(workspaceFolder.uri.path, parentDir.path);
}
if (relativeQuery) {
const convertRelativePath = (str: string) => {
return !str.startsWith('../') ? `./${str}` : str;
};

const relativePath = convertRelativePath(path.relative(path.dirname(document.uri.fsPath), uri.fsPath));
zardoy marked this conversation as resolved.
Show resolved Hide resolved
if (!relativePath.startsWith(relativeQuery)) {
return;
}
description = relativePath;
}
} else {
description = path.posix.relative(workspaceFolder.uri.path, parentDir.path);
description = parentDir.fsPath;
}
} else {
description = parentDir.fsPath;
}

return {
file,
label: Utils.basename(uri),
description,
};
});

const picked = await vscode.window.showQuickPick([
selectExistingFileItem,
selectNewFileItem,
{ label: vscode.l10n.t("Destination Files"), kind: vscode.QuickPickItemKind.Separator },
...destinationItems
], {
title: vscode.l10n.t("Move to File"),
placeHolder: vscode.l10n.t("Select move destination"),
return {
file,
label: Utils.basename(uri),
description: relativeQuery ? description : path.join(description, filename),
};
});
quickPick.items = [
selectExistingFileItem,
selectNewFileItem,
{ label: vscode.l10n.t("Destination Files"), kind: vscode.QuickPickItemKind.Separator },
...coalesce(destinationItems)
];
};
quickPick.title = vscode.l10n.t("Move to File");
quickPick.placeholder = vscode.l10n.t("Enter file path");
quickPick.matchOnDescription = true;
quickPick.onDidChangeValue(updateItems);
updateItems();

const picked = await new Promise<DestinationItem | undefined>(resolve => {
quickPick.onDidAccept(() => {
resolve(quickPick.selectedItems[0]);
quickPick.dispose();
});
quickPick.onDidHide(() => {
resolve(undefined);
quickPick.dispose();
});
quickPick.show();
});
if (!picked) {
return;
Expand All @@ -236,7 +269,7 @@ class MoveToFileRefactorCommand implements Command {
});
return picked ? this.client.toTsFilePath(picked) : undefined;
} else {
return (picked as DestinationItem).file;
return picked.file;
}
}
}
Expand Down