( buffer: TextBuffer, dirs: readonly string[], cwd: string, slashCommands: readonly SlashCommand[], commandContext: CommandContext, reverseSearchActive: boolean = false, config?: Config, )
| 41 | } |
| 42 | |
| 43 | export function useCommandCompletion( |
| 44 | buffer: TextBuffer, |
| 45 | dirs: readonly string[], |
| 46 | cwd: string, |
| 47 | slashCommands: readonly SlashCommand[], |
| 48 | commandContext: CommandContext, |
| 49 | reverseSearchActive: boolean = false, |
| 50 | config?: Config, |
| 51 | ): UseCommandCompletionReturn { |
| 52 | const { |
| 53 | suggestions, |
| 54 | activeSuggestionIndex, |
| 55 | visibleStartIndex, |
| 56 | showSuggestions, |
| 57 | isLoadingSuggestions, |
| 58 | isPerfectMatch, |
| 59 | |
| 60 | setSuggestions, |
| 61 | setShowSuggestions, |
| 62 | setActiveSuggestionIndex, |
| 63 | setIsLoadingSuggestions, |
| 64 | setIsPerfectMatch, |
| 65 | setVisibleStartIndex, |
| 66 | |
| 67 | resetCompletionState, |
| 68 | navigateUp, |
| 69 | navigateDown, |
| 70 | } = useCompletion(); |
| 71 | |
| 72 | const cursorRow = buffer.cursor[0]; |
| 73 | const cursorCol = buffer.cursor[1]; |
| 74 | |
| 75 | const { completionMode, query, completionStart, completionEnd } = |
| 76 | useMemo(() => { |
| 77 | const currentLine = buffer.lines[cursorRow] || ''; |
| 78 | if (cursorRow === 0 && isSlashCommand(currentLine.trim())) { |
| 79 | return { |
| 80 | completionMode: CompletionMode.SLASH, |
| 81 | query: currentLine, |
| 82 | completionStart: 0, |
| 83 | completionEnd: currentLine.length, |
| 84 | }; |
| 85 | } |
| 86 | |
| 87 | const codePoints = toCodePoints(currentLine); |
| 88 | for (let i = cursorCol - 1; i >= 0; i--) { |
| 89 | const char = codePoints[i]; |
| 90 | |
| 91 | if (char === ' ') { |
| 92 | let backslashCount = 0; |
| 93 | for (let j = i - 1; j >= 0 && codePoints[j] === '\\'; j--) { |
| 94 | backslashCount++; |
| 95 | } |
| 96 | if (backslashCount % 2 === 0) { |
| 97 | return { |
| 98 | completionMode: CompletionMode.IDLE, |
| 99 | query: null, |
| 100 | completionStart: -1, |
no test coverage detected