From 3f341d9cf7410b3be1440307262e5e122cd9fcc5 Mon Sep 17 00:00:00 2001 From: canvrno <46584286+canvrno@users.noreply.github.com> Date: Thu, 4 Sep 2025 15:01:13 -0700 Subject: [PATCH] Fix: Slash command / mention features remove following word (#5986) * Fixed issue where slash command feature would remove first word after the command name * Removed slashCommandsQueryRef * Add fix for mentions as well * Updated mentions test --------- Co-authored-by: Kevin Bond --- .changeset/late-wasps-agree.md | 5 +++ .../src/components/chat/ChatTextArea.tsx | 33 +++++++++++-------- .../utils/__tests__/context-mentions.test.ts | 4 +-- webview-ui/src/utils/context-mentions.ts | 14 ++++++-- webview-ui/src/utils/slash-commands.ts | 12 ++++--- 5 files changed, 45 insertions(+), 23 deletions(-) create mode 100644 .changeset/late-wasps-agree.md diff --git a/.changeset/late-wasps-agree.md b/.changeset/late-wasps-agree.md new file mode 100644 index 00000000..1f6b0d13 --- /dev/null +++ b/.changeset/late-wasps-agree.md @@ -0,0 +1,5 @@ +--- +"claude-dev": patch +--- + +Fixed minor bug where entering slash commands would result in the following word being removed from the chat text area diff --git a/webview-ui/src/components/chat/ChatTextArea.tsx b/webview-ui/src/components/chat/ChatTextArea.tsx index a7799f70..a37f2ba6 100644 --- a/webview-ui/src/components/chat/ChatTextArea.tsx +++ b/webview-ui/src/components/chat/ChatTextArea.tsx @@ -420,7 +420,7 @@ const ChatTextArea = forwardRef( setSearchLoading(true) // Map ContextMenuOptionType to FileSearchType enum - let searchType + let searchType: FileSearchType | undefined if (type === ContextMenuOptionType.File) { searchType = FileSearchType.FILE } else if (type === ContextMenuOptionType.Folder) { @@ -450,6 +450,9 @@ const ChatTextArea = forwardRef( setShowContextMenu(false) setSelectedType(null) + const queryLength = searchQuery.length + setSearchQuery("") + if (textAreaRef.current) { let insertValue = value || "" if (type === ContextMenuOptionType.URL) { @@ -464,7 +467,12 @@ const ChatTextArea = forwardRef( insertValue = value || "" } - const { newValue, mentionIndex } = insertMention(textAreaRef.current.value, cursorPosition, insertValue) + const { newValue, mentionIndex } = insertMention( + textAreaRef.current.value, + cursorPosition, + insertValue, + queryLength, + ) setInputValue(newValue) const newCursorPosition = newValue.indexOf(" ", mentionIndex + insertValue.length) + 1 @@ -481,15 +489,17 @@ const ChatTextArea = forwardRef( }, 0) } }, - [setInputValue, cursorPosition], + [setInputValue, cursorPosition, searchQuery], ) const handleSlashCommandsSelect = useCallback( (command: SlashCommand) => { setShowSlashCommandsMenu(false) + const queryLength = slashCommandsQuery.length + setSlashCommandsQuery("") if (textAreaRef.current) { - const { newValue, commandIndex } = insertSlashCommand(textAreaRef.current.value, command.name) + const { newValue, commandIndex } = insertSlashCommand(textAreaRef.current.value, command.name, queryLength) const newCursorPosition = newValue.indexOf(" ", commandIndex + 1 + command.name.length) + 1 setInputValue(newValue) @@ -504,7 +514,7 @@ const ChatTextArea = forwardRef( }, 0) } }, - [setInputValue], + [setInputValue, slashCommandsQuery], ) const handleKeyDown = useCallback( @@ -512,6 +522,7 @@ const ChatTextArea = forwardRef( if (showSlashCommandsMenu) { if (event.key === "Escape") { setShowSlashCommandsMenu(false) + setSlashCommandsQuery("") return } @@ -554,6 +565,7 @@ const ChatTextArea = forwardRef( // event.preventDefault() setSelectedType(null) setSelectedMenuIndex(DEFAULT_CONTEXT_MENU_OPTION) + setSearchQuery("") return } @@ -1126,15 +1138,8 @@ const ChatTextArea = forwardRef( // Get model display name const modelDisplayName = useMemo(() => { const { selectedProvider, selectedModelId } = normalizeApiConfiguration(apiConfiguration, mode) - const { - vsCodeLmModelSelector, - togetherModelId, - fireworksModelId, - lmStudioModelId, - ollamaModelId, - liteLlmModelId, - requestyModelId, - } = getModeSpecificFields(apiConfiguration, mode) + const { vsCodeLmModelSelector, togetherModelId, lmStudioModelId, ollamaModelId, liteLlmModelId, requestyModelId } = + getModeSpecificFields(apiConfiguration, mode) const unknownModel = "unknown" if (!apiConfiguration) { return unknownModel diff --git a/webview-ui/src/utils/__tests__/context-mentions.test.ts b/webview-ui/src/utils/__tests__/context-mentions.test.ts index f410bc34..12c5efcf 100644 --- a/webview-ui/src/utils/__tests__/context-mentions.test.ts +++ b/webview-ui/src/utils/__tests__/context-mentions.test.ts @@ -41,9 +41,9 @@ describe("context-mentions", () => { const position = 10 const value = "/path with spaces/file.txt" - const result = insertMention(text, position, value) + const result = insertMention(text, position, value, 3) - expect(result.newValue).toBe('Check @"/path with spaces/file.txt" and more') + expect(result.newValue).toBe('Check @"/path with spaces/file.txt" and more') expect(result.mentionIndex).toBe(6) }) diff --git a/webview-ui/src/utils/context-mentions.ts b/webview-ui/src/utils/context-mentions.ts index 2d3b800e..6a3686c1 100644 --- a/webview-ui/src/utils/context-mentions.ts +++ b/webview-ui/src/utils/context-mentions.ts @@ -8,7 +8,12 @@ export interface SearchResult { label?: string } -export function insertMention(text: string, position: number, value: string): { newValue: string; mentionIndex: number } { +export function insertMention( + text: string, + position: number, + value: string, + partialQueryLength: number = 0, +): { newValue: string; mentionIndex: number } { const beforeCursor = text.slice(0, position) const afterCursor = text.slice(position) @@ -26,8 +31,11 @@ export function insertMention(text: string, position: number, value: string): { if (lastAtIndex !== -1) { // If there's an '@' symbol, replace everything after it with the new mention - const beforeMention = text.slice(0, lastAtIndex) - newValue = beforeMention + "@" + formattedValue + " " + afterCursor.replace(/^[^\s]*/, "") + const beforeAt = text.substring(0, lastAtIndex + 1) + const afterPartialQuery = text.substring(lastAtIndex + 1 + partialQueryLength) + + // replace the partial query with the full mention + newValue = beforeAt + formattedValue + (afterPartialQuery.startsWith(" ") ? afterPartialQuery : " " + afterPartialQuery) mentionIndex = lastAtIndex } else { // If there's no '@' symbol, insert the mention at the cursor position diff --git a/webview-ui/src/utils/slash-commands.ts b/webview-ui/src/utils/slash-commands.ts index d0b78e5c..ab6b36c4 100644 --- a/webview-ui/src/utils/slash-commands.ts +++ b/webview-ui/src/utils/slash-commands.ts @@ -157,15 +157,19 @@ export function getMatchingSlashCommands( /** * Insert a slash command at position or replace partial command */ -export function insertSlashCommand(text: string, commandName: string): { newValue: string; commandIndex: number } { +export function insertSlashCommand( + text: string, + commandName: string, + partialCommandLength: number, +): { newValue: string; commandIndex: number } { const slashIndex = text.indexOf("/") - // where the command ends, at the end of entire text or first space - const commandEndIndex = text.indexOf(" ", slashIndex) + const beforeSlash = text.substring(0, slashIndex + 1) + const afterPartialCommand = text.substring(slashIndex + 1 + partialCommandLength) // replace the partial command with the full command const newValue = - text.substring(0, slashIndex + 1) + commandName + (commandEndIndex > -1 ? text.substring(commandEndIndex) : " ") // add extra space at the end if only slash command + beforeSlash + commandName + (afterPartialCommand.startsWith(" ") ? afterPartialCommand : " " + afterPartialCommand) return { newValue, commandIndex: slashIndex } }