Repository navigation
fix(webview): preserve existing text when selecting slash command at start #11728
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 8 commits
43b524f
e5c1bdd
66f42c1
74cb1ad
9cc9f73
de3db79
669fcc2
828f038
64f16b2
5906cbb
bf2810a
b5d436f
a9ff187
b578ca0
b1aa2bc
c1b7438
c26bb84
4664540
366a1c9
d1cc665
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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"] }), | ||
|
|
@@ -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, | ||
|
|
@@ -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, | ||
|
|
@@ -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( | ||
| { | ||
| 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", () => { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() | ||
| }) | ||
| }) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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[] = [ | ||
| { | ||
|
|
@@ -250,6 +251,7 @@ export function useSlashCommand( | |
|
|
||
| const close = () => { | ||
| setQuery(null) | ||
| setSlashEnd(null) | ||
| } | ||
|
|
||
| const onInput = (val: string, cursor: number) => { | ||
|
|
@@ -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) | ||
|
|
@@ -268,6 +271,7 @@ export function useSlashCommand( | |
| request() | ||
| setQuery(value) | ||
| setIndex(0) | ||
| setSlashEnd(cursor) | ||
| } | ||
|
|
||
| const select = ( | ||
|
|
@@ -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) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 Reply with
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| setQuery(`${cmd.name} `) | ||
|
|
||
There was a problem hiding this comment.
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.