Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
43b524f
fix(webview): preserve existing text when selecting slash command at …
rakshith1928 Jun 26, 2026
e5c1bdd
remove unwanted check not related to pr
rakshith1928 Jun 26, 2026
66f42c1
fix(webview): update test mocks for new select() cursor usage and add…
rakshith1928 Jul 7, 2026
74cb1ad
chore: add changeset for slash command trailing text fix
rakshith1928 Jul 7, 2026
9cc9f73
fix(webview): add slashEnd signal to fix cursor-corruption bug when s…
rakshith1928 Jul 7, 2026
de3db79
chore(test): fix redundant double type assertion in use-slash-command…
rakshith1928 Jul 7, 2026
669fcc2
chore(test): fix memory test state and add memory command trailing te…
rakshith1928 Jul 28, 2026
828f038
Merge branch 'main' into fix/slash-command-preserve-inputs
rakshith1928 Jul 29, 2026
64f16b2
address the pr review comments
rakshith1928 Aug 13, 2026
5906cbb
Merge remote-tracking branch 'upstream/main' into fix/slash-command-p…
rakshith1928 Aug 13, 2026
bf2810a
fix(test): use full text length as onInput cursor in review slash test
rakshith1928 Aug 13, 2026
b5d436f
Merge branch 'fix/slash-command-preserve-inputs' of https://github.co…
rakshith1928 Aug 13, 2026
a9ff187
fix: preserve trailing text on no-argument /memory commands
rakshith1928 Aug 14, 2026
b578ca0
refactor: drop review commit and review pr slash suggestions
rakshith1928 Aug 14, 2026
b1aa2bc
fix(webview): place caret at start when preserving trailing text for …
rakshith1928 Aug 14, 2026
c1b7438
fix(kilo-memory): preserve trailing text for /memory show like other …
rakshith1928 Aug 14, 2026
c26bb84
fix(kilo-vscode): update mirrored /memory show fixture for rest field
rakshith1928 Aug 14, 2026
4664540
fix(kilo-vscode): format prompt-input-utils.ts to satisfy prettier
rakshith1928 Aug 14, 2026
366a1c9
fix(kilo-vscode): place caret at start for preserved /memory text
rakshith1928 Aug 14, 2026
d1cc665
Merge branch 'main' into fix/slash-command-preserve-inputs
rakshith1928 Aug 14, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/slash-command-preserve-text.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"kilo-code": patch
---

Preserve existing text when selecting a slash command at the start of the chat input. Previously, typing `/new` or another slash command with text before the cursor would wipe the existing content entirely.
145 changes: 141 additions & 4 deletions packages/kilo-vscode/tests/unit/use-slash-command.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ function setup(sandbox: () => void, options: { enabled?: () => boolean; exclude?
describe("useSlashCommand sandbox action", () => {
it("opens project memory actions from the top-level command", () => {
const ctx = setup(() => {})
const state = { text: "/memory" }
const state = { text: "/mem" }
const textarea = {
value: state.text,
setSelectionRange: () => {},
focus: () => {},
} as unknown as HTMLTextAreaElement

ctx.slash.onInput("/mem", 4)
ctx.slash.onInput(state.text, state.text.length)

expect(ctx.slash.results()).toContainEqual(
expect.objectContaining({ name: "memory", description: "Manage project memory", hints: ["mem"] }),
Expand Down Expand Up @@ -100,7 +100,11 @@ describe("useSlashCommand sandbox action", () => {
it("runs the sandbox toggle as a client command", () => {
const state = { toggles: 0, text: "/sandbox", prevented: 0 }
const ctx = setup(() => state.toggles++)
const textarea = { value: state.text } as HTMLTextAreaElement
const textarea = {
value: state.text,
selectionStart: state.text.length,
setSelectionRange: () => {},
} as unknown as HTMLTextAreaElement
const event = {
key: "Enter",
isComposing: false,
Expand All @@ -122,7 +126,11 @@ describe("useSlashCommand sandbox action", () => {
it("keeps the command text when the sandbox control is disabled", () => {
const state = { toggles: 0, text: "/sandbox" }
const ctx = setup(() => state.toggles++, { enabled: () => false })
const textarea = { value: state.text } as HTMLTextAreaElement
const textarea = {
value: state.text,
selectionStart: state.text.length,
setSelectionRange: () => {},
} as unknown as HTMLTextAreaElement
const event = {
key: "Enter",
isComposing: false,
Expand Down Expand Up @@ -158,3 +166,132 @@ describe("useSlashCommand sandbox action", () => {
ctx.dispose()
})
})

describe("select", () => {
it("preserves trailing text for action commands", () => {
let actionCalls = 0
let currentText = "existing text"
const ctx = setup(() => {})

const textarea = {
value: "/newexisting text",
selectionStart: 4,
setSelectionRange: () => {},
} as unknown as HTMLTextAreaElement
const setText = (text: string) => {
currentText = text
}

ctx.slash.select(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

select() is called without onInput(), so this only tests the selectionStart fallback. Please also cover the slashEnd path.

{
name: "new",
description: "Start a new session",
hints: [],
action: () => {
actionCalls++
},
},
textarea,
setText,
)

expect(textarea.value).toBe("existing text")
expect(currentText).toBe("existing text")
expect(actionCalls).toBe(1)
ctx.dispose()
})

it("preserves trailing text for server commands and sets cursor", () => {
const ctx = setup(() => {})
let currentText = ""
let selectionStart = 0

const textarea = {
value: "/docmdexisting text",
selectionStart: 6,
setSelectionRange: (start: number, end: number) => {
selectionStart = start
},
focus: () => {},
} as unknown as HTMLTextAreaElement
const setText = (text: string) => {
currentText = text
}

ctx.slash.select({ name: "docmd", description: "Run doc command", hints: [] }, textarea, setText)

expect(textarea.value).toBe("/docmd existing text")
expect(currentText).toBe("/docmd existing text")
expect(selectionStart).toBe("/docmd ".length)
ctx.dispose()
})

it("preserves trailing text even when cursor moves after typing slash command", () => {
let actionCalls = 0
let currentText = "existing text"
const ctx = setup(() => {})

// Type slash command: cursor at 4, slashEnd stored as 4
ctx.slash.onInput("/newexisting text", 4)

// Simulate user moving cursor (e.g. ArrowLeft twice)
const textarea = {
value: "/newexisting text",
selectionStart: 2,
setSelectionRange: () => {},
} as unknown as HTMLTextAreaElement
const setText = (text: string) => {
currentText = text
}

ctx.slash.select(
{
name: "new",
description: "Start a new session",
hints: [],
action: () => {
actionCalls++
},
},
textarea,
setText,
)

// Should preserve trailing text from original slashEnd (4), not stale selectionStart (2)
expect(textarea.value).toBe("existing text")
expect(currentText).toBe("existing text")
expect(actionCalls).toBe(1)
ctx.dispose()
})

it("preserves trailing text for memory commands when cursor moves", () => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test only selects memory remember from /memory rem. Add the two-step path: /mem → memory → memory remember.

let currentText = ""
const ctx = setup(() => {})

// Type /memory rem: matches memory pattern, slashEnd set to end of text
const typed = "/memory rem"
ctx.slash.onInput(typed, typed.length)

// Simulate user moving cursor (no onInput fires for arrow keys)
const textarea = {
value: typed,
selectionStart: 3,
setSelectionRange: () => {},
focus: () => {},
} as unknown as HTMLTextAreaElement
const setText = (text: string) => {
currentText = text
}

// Find the memory remember command (nested, no action)
const remembered = ctx.slash.results().find((c) => c.name === "memory remember")
expect(remembered).toBeDefined()

ctx.slash.select(remembered!, textarea, setText)

// Trailing text from slashEnd (end of typed text) — empty — should be preserved correctly
expect(currentText).toBe("/memory remember ")
expect(textarea.value).toBe("/memory remember ")
ctx.dispose()
})
})
23 changes: 16 additions & 7 deletions packages/kilo-vscode/webview-ui/src/hooks/useSlashCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export function useSlashCommand(
const [query, setQuery] = createSignal<string | null>(null)
const [index, setIndex] = createSignal(0)
const [requested, setRequested] = createSignal(false)
const [slashEnd, setSlashEnd] = createSignal<number | null>(null)

const all: SlashCommandEntry[] = [
{
Expand Down Expand Up @@ -250,6 +251,7 @@ export function useSlashCommand(

const close = () => {
setQuery(null)
setSlashEnd(null)
}

const onInput = (val: string, cursor: number) => {
Expand All @@ -259,6 +261,7 @@ export function useSlashCommand(
request()
setQuery(match[1])
setIndex(0)
setSlashEnd(cursor)
return
}
const memory = before.match(/^\/(?:memory|mem)\s+([^\n]*)$/i)
Expand All @@ -268,6 +271,7 @@ export function useSlashCommand(
request()
setQuery(value)
setIndex(0)
setSlashEnd(cursor)
}

const select = (
Expand All @@ -276,20 +280,25 @@ export function useSlashCommand(
setText: (text: string) => void,
onSelect?: () => void,
) => {
const cursor = slashEnd() ?? textarea.selectionStart ?? 0
// trailingText holds text after the slash command.
// slashEnd is the cursor position from onInput when the slash pattern was matched.
const trailingText = textarea.value.substring(cursor)

if (cmd.action) {
if (cmd.enabled && !cmd.enabled()) return
textarea.value = ""
setText("")
textarea.value = trailingText
setText(trailingText)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SUGGESTION: Cursor position is left implicit after preserving trailing text for action commands

The server/nested command branch below explicitly calls textarea.setSelectionRange(...) after rewriting the value, but this action branch only assigns textarea.value/setText. After the assignment the caret ends up wherever the browser clamps the old position (or jumps to the end), which is unpredictable when the user had moved the cursor before selecting. Consider setting the caret explicitly here too (e.g. textarea.setSelectionRange(0, 0) so the cursor sits before the preserved text, mirroring the server-command behavior of placing it right after the command). Totally fine to leave as-is if the current behavior felt right in manual testing.


Reply with @kilocode-bot fix it to have Kilo Code address this issue.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. I was actually looking into whether the cursor issue could be addressed at the controlled SolidJS input level rather than adding cursor handling in each command branch. But for this action branch, I agree it makes sense to set the caret explicitly so the behavior is deterministic.

close()
onSelect?.()
cmd.action()
return
}
const text = `/${cmd.name} `
textarea.value = text
setText(text)
const pos = text.length
textarea.setSelectionRange(pos, pos)
const commandText = `/${cmd.name} `
const updatedText = commandText + trailingText
textarea.value = updatedText
setText(updatedText)
textarea.setSelectionRange(commandText.length, commandText.length)
textarea.focus()
if (cmd.nested) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This approach will fail with nested subcommands, nested branch is not updating slash end. This will break commands like /memory remember . Can you address that?

setQuery(`${cmd.name} `)
Expand Down
Loading