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 <kevin@Kevins-MacBook-Pro.local>
This commit is contained in:
canvrnoandKevin Bond authored and GitHub committed 2025-09-04 12:01:13 -10:00
1 parent 0adb1046e4
commit 3f341d9cf7
5 files changed
+45 -23

No files matched your search

+5
View File
@@ -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
+19 -14
View File
@@ -420,7 +420,7 @@ const ChatTextArea = forwardRef<HTMLTextAreaElement, ChatTextAreaProps>(
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<HTMLTextAreaElement, ChatTextAreaProps>(
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<HTMLTextAreaElement, ChatTextAreaProps>(
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<HTMLTextAreaElement, ChatTextAreaProps>(
}, 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<HTMLTextAreaElement, ChatTextAreaProps>(
}, 0)
}
},
[setInputValue],
[setInputValue, slashCommandsQuery],
)
const handleKeyDown = useCallback(
@@ -512,6 +522,7 @@ const ChatTextArea = forwardRef<HTMLTextAreaElement, ChatTextAreaProps>(
if (showSlashCommandsMenu) {
if (event.key === "Escape") {
setShowSlashCommandsMenu(false)
setSlashCommandsQuery("")
return
}
@@ -554,6 +565,7 @@ const ChatTextArea = forwardRef<HTMLTextAreaElement, ChatTextAreaProps>(
// event.preventDefault()
setSelectedType(null)
setSelectedMenuIndex(DEFAULT_CONTEXT_MENU_OPTION)
setSearchQuery("")
return
}
@@ -1126,15 +1138,8 @@ const ChatTextArea = forwardRef<HTMLTextAreaElement, ChatTextAreaProps>(
// 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
@@ -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)
})
+11 -3
View File
@@ -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
+8 -4
View File
@@ -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 }
}