Skip to content

Commit

Permalink
fix(cmdline): handle wildmenu_hide event (#1541)
Browse files Browse the repository at this point in the history
fix(cmdline): use Ctrl+n/p to select candidates when candidates exist
  • Loading branch information
xiyaowong committed Oct 17, 2023
1 parent 7681ce5 commit 1b68475
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 8 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -1221,12 +1221,12 @@
{
"command": "workbench.action.quickOpenSelectNext",
"key": "ctrl+n",
"when": "inQuickOpen && neovim.mode != cmdline"
"when": "inQuickOpen && (neovim.mode != cmdline || neovim.wildMenuVisible)"
},
{
"command": "workbench.action.quickOpenSelectPrevious",
"key": "ctrl+p",
"when": "inQuickOpen && neovim.mode != cmdline"
"when": "inQuickOpen && (neovim.mode != cmdline || neovim.wildMenuVisible)"
},
{
"key": "ctrl+n",
Expand Down
4 changes: 2 additions & 2 deletions scripts/keybindings/5 widgets.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -187,12 +187,12 @@ const keybinds = [
{
command: "workbench.action.quickOpenSelectNext",
key: "ctrl+n",
when: "inQuickOpen && neovim.mode != cmdline",
when: "inQuickOpen && (neovim.mode != cmdline || neovim.wildMenuVisible)",
},
{
command: "workbench.action.quickOpenSelectPrevious",
key: "ctrl+p",
when: "inQuickOpen && neovim.mode != cmdline",
when: "inQuickOpen && (neovim.mode != cmdline || neovim.wildMenuVisible)",
},
{
key: "ctrl+n",
Expand Down
10 changes: 10 additions & 0 deletions src/command_line.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ export class CommandLineController implements Disposable {

private updatedFromNvim = false; // whether to replace nvim cmdline with new content

private wildMenuVisible = false; // indicates if the wildmenu is visible

public constructor(
private client: NeovimClient,
private callbacks: CommandLineCallbacks,
Expand Down Expand Up @@ -94,6 +96,14 @@ export class CommandLineController implements Disposable {
this.completionItems = items.map((i) => ({ label: i, alwaysShow: true }));
if (this.completionAllowed) {
this.input.items = this.completionItems;
// When deleting the input text to empty, the wildmenu displays all the candidate commands.
// However, the wildmenu is not actually useful in this situation, so it is forced to be invisible.
// This allows Ctrl+n and Ctrl+p to input normally(navigating history) instead of selecting candidates in quickOpen.
const wildMenuVisible = this.input.value.length > 0 && this.completionItems.length > 0;
if (this.wildMenuVisible !== wildMenuVisible) {
this.wildMenuVisible = wildMenuVisible;
commands.executeCommand("setContext", "neovim.wildMenuVisible", this.wildMenuVisible);
}
}
}

Expand Down
9 changes: 5 additions & 4 deletions src/command_line_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,11 @@ export class CommandLineManager implements Disposable {
break;
}
case "wildmenu_show": {
const [items] = args[0];
if (this.commandLine) {
this.commandLine.setCompletionItems(items);
}
this.commandLine?.setCompletionItems(args[0][0]);
break;
}
case "wildmenu_hide": {
this.commandLine?.setCompletionItems([]);
break;
}
case "cmdline_hide": {
Expand Down
1 change: 1 addition & 0 deletions src/eventBus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ type RedrawEventArgs = (
// ["mouse_off"]
| IRedrawEventArg<"mouse_off">
| IRedrawEventArg<"wildmenu_show", [string[]]>
| IRedrawEventArg<"wildmenu_hide">
)[];
// #endregion

Expand Down

0 comments on commit 1b68475

Please sign in to comment.