|
| 1 | +import { useCallback, useEffect, useRef, useState } from "react"; |
| 2 | +import { |
| 3 | + getVimCommandSuggestions, |
| 4 | + parseAndExecuteVimCommand, |
| 5 | + type VimCommand, |
| 6 | +} from "@/stores/vim-commands"; |
| 7 | +import { useVimStore } from "@/stores/vim-store"; |
| 8 | +import Command, { |
| 9 | + CommandEmpty, |
| 10 | + CommandHeader, |
| 11 | + CommandInput, |
| 12 | + CommandItem, |
| 13 | + CommandList, |
| 14 | +} from "../../ui/command"; |
| 15 | + |
| 16 | +const VimCommandBar = () => { |
| 17 | + const isCommandMode = useVimStore.use.isCommandMode(); |
| 18 | + const commandInput = useVimStore.use.commandInput(); |
| 19 | + const { exitCommandMode, updateCommandInput, executeCommand } = useVimStore.use.actions(); |
| 20 | + |
| 21 | + const [selectedIndex, setSelectedIndex] = useState(0); |
| 22 | + const [suggestions, setSuggestions] = useState<VimCommand[]>([]); |
| 23 | + const inputRef = useRef<HTMLInputElement>(null); |
| 24 | + const scrollContainerRef = useRef<HTMLDivElement>(null); |
| 25 | + |
| 26 | + // Focus input when vim command mode becomes active |
| 27 | + useEffect(() => { |
| 28 | + if (isCommandMode && inputRef.current) { |
| 29 | + inputRef.current.focus(); |
| 30 | + } |
| 31 | + }, [isCommandMode]); |
| 32 | + |
| 33 | + // Update suggestions when command input changes |
| 34 | + useEffect(() => { |
| 35 | + if (commandInput) { |
| 36 | + setSuggestions(getVimCommandSuggestions(commandInput)); |
| 37 | + } else { |
| 38 | + setSuggestions(getVimCommandSuggestions("")); |
| 39 | + } |
| 40 | + setSelectedIndex(0); |
| 41 | + }, [commandInput]); |
| 42 | + |
| 43 | + // Handle keyboard navigation |
| 44 | + useEffect(() => { |
| 45 | + if (!isCommandMode) return; |
| 46 | + |
| 47 | + const handleKeyDown = (e: KeyboardEvent) => { |
| 48 | + if (e.key === "Escape") { |
| 49 | + e.preventDefault(); |
| 50 | + exitCommandMode(); |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + if (e.key === "Enter") { |
| 55 | + e.preventDefault(); |
| 56 | + |
| 57 | + // If there are suggestions and one is selected, use that |
| 58 | + if (suggestions.length > 0 && selectedIndex < suggestions.length) { |
| 59 | + const selectedCommand = suggestions[selectedIndex]; |
| 60 | + const commandToExecute = selectedCommand.name; |
| 61 | + executeCommand(commandToExecute); |
| 62 | + parseAndExecuteVimCommand(commandToExecute); |
| 63 | + } else if (commandInput.trim()) { |
| 64 | + // Execute the typed command |
| 65 | + executeCommand(commandInput); |
| 66 | + parseAndExecuteVimCommand(commandInput); |
| 67 | + } |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + if (e.key === "ArrowDown" && suggestions.length > 0) { |
| 72 | + e.preventDefault(); |
| 73 | + setSelectedIndex((prev) => (prev + 1) % suggestions.length); |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + if (e.key === "ArrowUp" && suggestions.length > 0) { |
| 78 | + e.preventDefault(); |
| 79 | + setSelectedIndex((prev) => (prev - 1 + suggestions.length) % suggestions.length); |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + if (e.key === "Tab" && suggestions.length > 0) { |
| 84 | + e.preventDefault(); |
| 85 | + const selectedCommand = suggestions[selectedIndex]; |
| 86 | + if (selectedCommand) { |
| 87 | + updateCommandInput(selectedCommand.name); |
| 88 | + } |
| 89 | + return; |
| 90 | + } |
| 91 | + }; |
| 92 | + |
| 93 | + document.addEventListener("keydown", handleKeyDown); |
| 94 | + |
| 95 | + return () => { |
| 96 | + document.removeEventListener("keydown", handleKeyDown); |
| 97 | + }; |
| 98 | + }, [ |
| 99 | + isCommandMode, |
| 100 | + commandInput, |
| 101 | + suggestions, |
| 102 | + selectedIndex, |
| 103 | + exitCommandMode, |
| 104 | + updateCommandInput, |
| 105 | + executeCommand, |
| 106 | + ]); |
| 107 | + |
| 108 | + // Auto-scroll selected item into view |
| 109 | + useEffect(() => { |
| 110 | + if (!isCommandMode || !scrollContainerRef.current) return; |
| 111 | + |
| 112 | + const selectedElement = scrollContainerRef.current.querySelector( |
| 113 | + `[data-item-index="${selectedIndex}"]`, |
| 114 | + ) as HTMLElement; |
| 115 | + |
| 116 | + if (selectedElement) { |
| 117 | + selectedElement.scrollIntoView({ |
| 118 | + behavior: "smooth", |
| 119 | + block: "nearest", |
| 120 | + }); |
| 121 | + } |
| 122 | + }, [selectedIndex, isCommandMode, suggestions]); |
| 123 | + |
| 124 | + const handleInputChange = useCallback( |
| 125 | + (value: string) => { |
| 126 | + updateCommandInput(value); |
| 127 | + }, |
| 128 | + [updateCommandInput], |
| 129 | + ); |
| 130 | + |
| 131 | + const handleItemSelect = useCallback( |
| 132 | + (command: VimCommand) => { |
| 133 | + executeCommand(command.name); |
| 134 | + parseAndExecuteVimCommand(command.name); |
| 135 | + }, |
| 136 | + [executeCommand], |
| 137 | + ); |
| 138 | + |
| 139 | + if (!isCommandMode) { |
| 140 | + return null; |
| 141 | + } |
| 142 | + |
| 143 | + return ( |
| 144 | + <Command isVisible={isCommandMode} className="max-h-80"> |
| 145 | + <CommandHeader onClose={exitCommandMode}> |
| 146 | + <span className="font-mono text-accent text-sm">:</span> |
| 147 | + <CommandInput |
| 148 | + ref={inputRef} |
| 149 | + value={commandInput} |
| 150 | + onChange={handleInputChange} |
| 151 | + placeholder="Enter vim command..." |
| 152 | + className="font-mono" |
| 153 | + /> |
| 154 | + </CommandHeader> |
| 155 | + |
| 156 | + <CommandList ref={scrollContainerRef}> |
| 157 | + {suggestions.length === 0 ? ( |
| 158 | + <CommandEmpty> |
| 159 | + <div className="font-mono"> |
| 160 | + {commandInput ? "No matching commands" : "Type a command"} |
| 161 | + </div> |
| 162 | + </CommandEmpty> |
| 163 | + ) : ( |
| 164 | + <div className="p-0"> |
| 165 | + {suggestions.map((command, index) => { |
| 166 | + const isSelected = index === selectedIndex; |
| 167 | + const displayName = command.aliases?.length |
| 168 | + ? `${command.name} (${command.aliases.join(", ")})` |
| 169 | + : command.name; |
| 170 | + |
| 171 | + return ( |
| 172 | + <CommandItem |
| 173 | + key={command.name} |
| 174 | + data-item-index={index} |
| 175 | + onClick={() => handleItemSelect(command)} |
| 176 | + isSelected={isSelected} |
| 177 | + className="font-mono" |
| 178 | + > |
| 179 | + <div className="min-w-0 flex-1"> |
| 180 | + <div className="truncate text-xs"> |
| 181 | + <span className="text-accent">:</span> |
| 182 | + <span className="text-text">{displayName}</span> |
| 183 | + </div> |
| 184 | + <div className="mt-0.5 truncate text-[10px] text-text-lighter opacity-60"> |
| 185 | + {command.description} |
| 186 | + </div> |
| 187 | + </div> |
| 188 | + </CommandItem> |
| 189 | + ); |
| 190 | + })} |
| 191 | + </div> |
| 192 | + )} |
| 193 | + </CommandList> |
| 194 | + </Command> |
| 195 | + ); |
| 196 | +}; |
| 197 | + |
| 198 | +export default VimCommandBar; |
0 commit comments