From b3b7b9da5f3552bd61d6bcd7be14ebff7c9863ce Mon Sep 17 00:00:00 2001 From: Toshii <94262432+0xToshii@users.noreply.github.com> Date: Mon, 21 Apr 2025 16:41:34 -0700 Subject: [PATCH] ENG-516 Slash commands (#3044) * scroll * menu * changeset * nit --- .changeset/clean-birds-count.md | 5 ++ .../src/components/chat/ChatTextArea.tsx | 49 ++++++++++++++++--- webview-ui/src/utils/slash-commands.ts | 22 +++++++++ 3 files changed, 69 insertions(+), 7 deletions(-) create mode 100644 .changeset/clean-birds-count.md diff --git a/.changeset/clean-birds-count.md b/.changeset/clean-birds-count.md new file mode 100644 index 00000000..4eba3dbf --- /dev/null +++ b/.changeset/clean-birds-count.md @@ -0,0 +1,5 @@ +--- +"claude-dev": minor +--- + +menu fix for slash commands diff --git a/webview-ui/src/components/chat/ChatTextArea.tsx b/webview-ui/src/components/chat/ChatTextArea.tsx index 19a068de..686ad020 100644 --- a/webview-ui/src/components/chat/ChatTextArea.tsx +++ b/webview-ui/src/components/chat/ChatTextArea.tsx @@ -17,9 +17,11 @@ import { } from "@/utils/context-mentions" import { SlashCommand, + slashCommandDeleteRegex, shouldShowSlashCommandsMenu, getMatchingSlashCommands, insertSlashCommand, + removeSlashCommand, validateSlashCommand, } from "@/utils/slash-commands" import { useMetaKeyDetection, useShortcut } from "@/utils/hooks" @@ -253,6 +255,7 @@ const ChatTextArea = forwardRef( const [selectedMenuIndex, setSelectedMenuIndex] = useState(-1) const [selectedType, setSelectedType] = useState(null) const [justDeletedSpaceAfterMention, setJustDeletedSpaceAfterMention] = useState(false) + const [justDeletedSpaceAfterSlashCommand, setJustDeletedSpaceAfterSlashCommand] = useState(false) const [intendedCursorPosition, setIntendedCursorPosition] = useState(null) const contextMenuContainerRef = useRef(null) const [showModelSelector, setShowModelSelector] = useState(false) @@ -525,13 +528,14 @@ const ChatTextArea = forwardRef( charBeforeCursor === " " || charBeforeCursor === "\n" || charBeforeCursor === "\r\n" const charAfterIsWhitespace = charAfterCursor === " " || charAfterCursor === "\n" || charAfterCursor === "\r\n" - // checks if char before cursor is whitespace after a mention + + // Check if we're right after a space that follows a mention or slash command if ( charBeforeIsWhitespace && - inputValue.slice(0, cursorPosition - 1).match(new RegExp(mentionRegex.source + "$")) // "$" is added to ensure the match occurs at the end of the string + inputValue.slice(0, cursorPosition - 1).match(new RegExp(mentionRegex.source + "$")) ) { + // File mention handling const newCursorPosition = cursorPosition - 1 - // if mention is followed by another word, then instead of deleting the space separating them we just move the cursor to the end of the mention if (!charAfterIsWhitespace) { event.preventDefault() textAreaRef.current?.setSelectionRange(newCursorPosition, newCursorPosition) @@ -539,17 +543,44 @@ const ChatTextArea = forwardRef( } setCursorPosition(newCursorPosition) setJustDeletedSpaceAfterMention(true) - } else if (justDeletedSpaceAfterMention) { + setJustDeletedSpaceAfterSlashCommand(false) + } else if (charBeforeIsWhitespace && inputValue.slice(0, cursorPosition - 1).match(slashCommandDeleteRegex)) { + // New slash command handling + const newCursorPosition = cursorPosition - 1 + if (!charAfterIsWhitespace) { + event.preventDefault() + textAreaRef.current?.setSelectionRange(newCursorPosition, newCursorPosition) + setCursorPosition(newCursorPosition) + } + setCursorPosition(newCursorPosition) + setJustDeletedSpaceAfterSlashCommand(true) + setJustDeletedSpaceAfterMention(false) + } + // Handle the second backspace press for mentions or slash commands + else if (justDeletedSpaceAfterMention) { const { newText, newPosition } = removeMention(inputValue, cursorPosition) if (newText !== inputValue) { event.preventDefault() setInputValue(newText) - setIntendedCursorPosition(newPosition) // Store the new cursor position in state + setIntendedCursorPosition(newPosition) } setJustDeletedSpaceAfterMention(false) setShowContextMenu(false) - } else { + } else if (justDeletedSpaceAfterSlashCommand) { + // New slash command deletion + const { newText, newPosition } = removeSlashCommand(inputValue, cursorPosition) + if (newText !== inputValue) { + event.preventDefault() + setInputValue(newText) + setIntendedCursorPosition(newPosition) + } + setJustDeletedSpaceAfterSlashCommand(false) + setShowSlashCommandsMenu(false) + } + // Default case - reset flags if none of the above apply + else { setJustDeletedSpaceAfterMention(false) + setJustDeletedSpaceAfterSlashCommand(false) } } }, @@ -566,6 +597,10 @@ const ChatTextArea = forwardRef( justDeletedSpaceAfterMention, queryItems, fileSearchResults, + showSlashCommandsMenu, + selectedSlashCommandsIndex, + slashCommandsQuery, + handleSlashCommandsSelect, ], ) @@ -792,7 +827,7 @@ const ChatTextArea = forwardRef( if (isValidCommand) { const fullCommand = processedText.substring(slashIndex, endIndex) // includes slash - const highlighted = `${fullCommand}` + const highlighted = `${fullCommand}` processedText = processedText.substring(0, slashIndex) + highlighted + processedText.substring(endIndex) } } diff --git a/webview-ui/src/utils/slash-commands.ts b/webview-ui/src/utils/slash-commands.ts index 0fe878f2..dd9ca7a5 100644 --- a/webview-ui/src/utils/slash-commands.ts +++ b/webview-ui/src/utils/slash-commands.ts @@ -13,6 +13,28 @@ export const SUPPORTED_SLASH_COMMANDS: SlashCommand[] = [ // Regex for detecting slash commands in text export const slashCommandRegex = /\/([a-zA-Z0-9_-]+)(\s|$)/ export const slashCommandRegexGlobal = new RegExp(slashCommandRegex.source, "g") +export const slashCommandDeleteRegex = /^\s*\/([a-zA-Z0-9_-]+)$/ + +/** + * Removes a slash command at the cursor position + */ +export function removeSlashCommand(text: string, position: number): { newText: string; newPosition: number } { + const beforeCursor = text.slice(0, position) + const afterCursor = text.slice(position) + + // Check if we're at the end of a slash command + const matchEnd = beforeCursor.match(slashCommandDeleteRegex) + + if (matchEnd) { + // If we're at the end of a slash command, remove it + const newText = text.slice(0, position - matchEnd[0].length) + afterCursor.replace(" ", "") // removes the first space after the command + const newPosition = position - matchEnd[0].length + return { newText, newPosition } + } + + // If we're not at the end of a slash command, just return the original text and position + return { newText: text, newPosition: position } +} /** * Determines whether the slash command menu should be displayed based on text input