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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(z): include cwd folders in suggestions for zoxides z command #2300

Merged
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
49 changes: 44 additions & 5 deletions src/z.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,12 @@ const zoxideCompletionSpec: Fig.Spec = {
name: "directory",
filterStrategy: "fuzzy",
suggestCurrentToken: true,
isVariadic: true,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Opted to remove this isVariadic: true since whilst the argument is strictly speaking variadic, the autocomplete for it is going to be wrong 99% of the time and so figured it's better to not give suggestions than give wrong suggestions.

generators: {
custom: async (tokens, executeShellCommand) => {
custom: async (
tokens,
executeShellCommand,
{ currentWorkingDirectory }
) => {
let args;
if (tokens.length < 2 || tokens[1] === "") {
args = ["query", "--list", "--score"];
Expand All @@ -146,16 +149,52 @@ const zoxideCompletionSpec: Fig.Spec = {
args,
});

return stdout.split("\n").map((line) => {
const zoxideFolders = stdout.split("\n").map((line) => {
const trimmedLine = line.trim();
const spaceIndex = trimmedLine.indexOf(" ");
const score = Number(trimmedLine.slice(0, spaceIndex));
const path = trimmedLine.slice(spaceIndex + 1);
const fullPath = trimmedLine.slice(spaceIndex + 1);

const pathSplit = fullPath.split("/");
const parentFullPath = pathSplit
.slice(0, pathSplit.length - 1)
.join("/");
const folderName = pathSplit.at(-1);

const folderIsInCwd = currentWorkingDirectory === parentFullPath;
return {
name: path,
name: folderIsInCwd ? folderName : fullPath,
description: `Score: ${score}`,
icon: "馃捑",
path: fullPath,
priority: folderIsInCwd ? 9000 : score, // assign highest priority when in cwd
};
});

const cwdFolders = (
await getCurrentDirectoryFolders(
currentWorkingDirectory,
executeShellCommand
)
).map(({ name, path }) => ({
name,
description: "Score: 0",
icon: "馃搧",
path,
priority: 8999, // display just below z's own suggestions for cwd
}));

// Prefer zoxide suggestions over cwd suggestions
const uniqueFolders = [...zoxideFolders, ...cwdFolders].reduce(
(acc, folder) => {
if (!acc.some(({ path }) => path === folder.path)) {
acc.push(folder);
}
return acc;
},
[]
);
return uniqueFolders;
},
trigger: {
on: "change",
Expand Down