Skip to content
Open
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
1 change: 1 addition & 0 deletions apps/server/src/keybindings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ it.layer(NodeServices.layer)("keybindings", (it) => {
assert.equal(defaultsByCommand.get("modelPicker.toggle"), "mod+shift+m");
assert.equal(defaultsByCommand.get("sidebar.toggle"), "mod+b");
assert.equal(defaultsByCommand.get("rightPanel.toggle"), "mod+alt+b");
assert.equal(defaultsByCommand.get("project.add"), "alt+a");
assert.equal(defaultsByCommand.get("terminal.splitVertical"), "mod+shift+d");
assert.equal(defaultsByCommand.get("modelPicker.jump.1"), "mod+1");
assert.equal(defaultsByCommand.get("modelPicker.jump.9"), "mod+9");
Expand Down
27 changes: 27 additions & 0 deletions apps/web/src/components/CommandPalette.logic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,36 @@ import {
buildThreadActionItems,
enumerateCommandPaletteItems,
filterCommandPaletteGroups,
shouldHandleCommandPaletteShortcut,
type CommandPaletteGroup,
} from "./CommandPalette.logic";

describe("shouldHandleCommandPaletteShortcut", () => {
it("does not capture the add-project shortcut from an editable target", () => {
expect(
shouldHandleCommandPaletteShortcut({
command: "project.add",
editableTarget: true,
}),
).toBe(false);
});

it("still handles add-project outside editors and the palette toggle everywhere", () => {
expect(
shouldHandleCommandPaletteShortcut({
command: "project.add",
editableTarget: false,
}),
).toBe(true);
expect(
shouldHandleCommandPaletteShortcut({
command: "commandPalette.toggle",
editableTarget: true,
}),
).toBe(true);
});
});

describe("enumerateCommandPaletteItems", () => {
it("assigns positional jump shortcuts to the first nine displayed items", () => {
const items = Array.from({ length: 10 }, (_, index) => ({
Expand Down
10 changes: 10 additions & 0 deletions apps/web/src/components/CommandPalette.logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ export const RECENT_THREAD_LIMIT = 12;
export const ITEM_ICON_CLASS = "size-4 text-muted-foreground/80";
export const ADDON_ICON_CLASS = "size-4";

export function shouldHandleCommandPaletteShortcut(input: {
command: KeybindingCommand | null;
editableTarget: boolean;
}): boolean {
if (input.command === "commandPalette.toggle") {
return true;
}
return input.command === "project.add" && !input.editableTarget;
}

export interface CommandPaletteItem {
readonly kind: "action" | "submenu";
readonly value: string;
Expand Down
85 changes: 53 additions & 32 deletions apps/web/src/components/CommandPalette.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ import {
getCommandPaletteMode,
ITEM_ICON_CLASS,
RECENT_THREAD_LIMIT,
shouldHandleCommandPaletteShortcut,
} from "./CommandPalette.logic";
import { orderItemsByPreferredIds, sortLogicalProjectsForSidebar } from "./Sidebar.logic";
import { resolveEnvironmentOptionLabel } from "./BranchToolbar.logic";
Expand Down Expand Up @@ -408,16 +409,31 @@ export function CommandPalette({ children }: { children: ReactNode }) {
terminalOpen,
},
});
if (command !== "commandPalette.toggle") {
const target =
event.target instanceof Element
? event.target.closest(
'input, textarea, select, [contenteditable]:not([contenteditable="false"])',
)
: null;
if (
!shouldHandleCommandPaletteShortcut({
command,
editableTarget: target !== null,
})
) {
return;
}
event.preventDefault();
event.stopPropagation();
toggleOpen();
if (command === "project.add") {
openAddProject();
} else {
toggleOpen();
}
Comment thread
cursor[bot] marked this conversation as resolved.
};
window.addEventListener("keydown", onKeyDown);
return () => window.removeEventListener("keydown", onKeyDown);
}, [keybindings, terminalOpen, toggleOpen]);
}, [keybindings, openAddProject, terminalOpen, toggleOpen]);

useEffect(
() =>
Expand Down Expand Up @@ -1146,6 +1162,35 @@ function OpenCommandPaletteDialog(props: {
]);

const actionItems: Array<CommandPaletteActionItem | CommandPaletteSubmenuItem> = [];
const addProjectAction: CommandPaletteActionItem = {
kind: "action",
value: "action:add-project",
searchTerms: [
"add project",
"folder",
"directory",
"browse",
"clone",
"remote",
"repository",
"repo",
"git",
"github",
"gitlab",
"bitbucket",
"azure",
"devops",
"url",
"environment",
],
title: "Add project",
icon: <FolderPlusIcon className={ITEM_ICON_CLASS} />,
shortcutCommand: "project.add",
keepOpen: true,
run: async () => {
openAddProjectFlow();
},
};

if (projects.length > 0) {
const activeProjectTitle =
Expand Down Expand Up @@ -1182,38 +1227,14 @@ function OpenCommandPaletteDialog(props: {
title: "New thread in...",
icon: <SquarePenIcon className={ITEM_ICON_CLASS} />,
addonIcon: <SquarePenIcon className={ADDON_ICON_CLASS} />,
groups: [{ value: "projects", label: "Projects", items: projectThreadItems }],
Comment thread
cursor[bot] marked this conversation as resolved.
groups: [
{ value: "projects", label: "Projects", items: projectThreadItems },
{ value: "actions", label: "Actions", items: [addProjectAction] },
],
});
}

actionItems.push({
kind: "action",
value: "action:add-project",
searchTerms: [
"add project",
"folder",
"directory",
"browse",
"clone",
"remote",
"repository",
"repo",
"git",
"github",
"gitlab",
"bitbucket",
"azure",
"devops",
"url",
"environment",
],
title: "Add project",
icon: <FolderPlusIcon className={ITEM_ICON_CLASS} />,
keepOpen: true,
run: async () => {
openAddProjectFlow();
},
});
actionItems.push(addProjectAction);

if (wslAddProjectEnvironmentOption) {
actionItems.push({
Expand Down
30 changes: 30 additions & 0 deletions apps/web/src/keybindings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,18 @@ const DEFAULT_BINDINGS = compile([
},
{ shortcut: modShortcut("o", { shiftKey: true }), command: "chat.new" },
{ shortcut: modShortcut("n", { shiftKey: true }), command: "chat.newLocal" },
{
shortcut: {
key: "a",
metaKey: false,
ctrlKey: false,
shiftKey: false,
altKey: true,
modKey: false,
},
command: "project.add",
whenAst: whenNot(whenIdentifier("terminalFocus")),
},
{ shortcut: modShortcut("o"), command: "editor.openFavorite" },
{ shortcut: modShortcut("[", { shiftKey: true }), command: "thread.previous" },
{ shortcut: modShortcut("]", { shiftKey: true }), command: "thread.next" },
Expand Down Expand Up @@ -327,6 +339,7 @@ describe("shortcutLabelForCommand", () => {
shortcutLabelForCommand(DEFAULT_BINDINGS, "commandPalette.toggle", "MacIntel"),
"⌘K",
);
assert.strictEqual(shortcutLabelForCommand(DEFAULT_BINDINGS, "project.add", "MacIntel"), "⌥A");
assert.strictEqual(
shortcutLabelForCommand(DEFAULT_BINDINGS, "modelPicker.toggle", "Linux"),
"Ctrl+Shift+M",
Expand Down Expand Up @@ -514,6 +527,23 @@ describe("chat/editor shortcuts", () => {
);
});

it("matches project.add shortcut outside terminal focus", () => {
assert.strictEqual(
resolveShortcutCommand(event({ key: "å", code: "KeyA", altKey: true }), DEFAULT_BINDINGS, {
platform: "MacIntel",
context: { terminalFocus: false },
}),
"project.add",
);
assert.notStrictEqual(
resolveShortcutCommand(event({ key: "å", code: "KeyA", altKey: true }), DEFAULT_BINDINGS, {
platform: "MacIntel",
context: { terminalFocus: true },
}),
"project.add",
);
});

it("matches diff.toggle shortcut outside terminal focus", () => {
assert.isTrue(
isDiffToggleShortcut(event({ key: "d", metaKey: true }), DEFAULT_BINDINGS, {
Expand Down
6 changes: 6 additions & 0 deletions packages/contracts/src/keybindings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ it.effect("parses keybinding rules", () =>
});
assert.strictEqual(parsedLocal.command, "chat.newLocal");

const parsedAddProject = yield* decode(KeybindingRule, {
key: "alt+a",
command: "project.add",
});
assert.strictEqual(parsedAddProject.command, "project.add");

const parsedModelPickerToggle = yield* decode(KeybindingRule, {
key: "mod+shift+m",
command: "modelPicker.toggle",
Expand Down
1 change: 1 addition & 0 deletions packages/contracts/src/keybindings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ const STATIC_KEYBINDING_COMMANDS = [
"commandPalette.toggle",
"chat.new",
"chat.newLocal",
"project.add",
"editor.openFavorite",
...MODEL_PICKER_KEYBINDING_COMMANDS,
...THREAD_KEYBINDING_COMMANDS,
Expand Down
1 change: 1 addition & 0 deletions packages/shared/src/keybindings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export const DEFAULT_KEYBINDINGS: ReadonlyArray<KeybindingRule> = [
{ key: "mod+n", command: "chat.new", when: "!terminalFocus" },
{ key: "mod+shift+o", command: "chat.new", when: "!terminalFocus" },
{ key: "mod+shift+n", command: "chat.newLocal", when: "!terminalFocus" },
{ key: "alt+a", command: "project.add", when: "!terminalFocus" },
Comment thread
colonelpanic8 marked this conversation as resolved.
Comment thread
colonelpanic8 marked this conversation as resolved.
{ key: "mod+shift+m", command: "modelPicker.toggle", when: "!terminalFocus" },
{ key: "mod+o", command: "editor.openFavorite" },
{ key: "mod+shift+[", command: "thread.previous" },
Expand Down
Loading