Skip to content

Get up and running with Kimi, GLM, MiniMax, DeepSeek, gpt-oss, Qwen, Gemma and other models.

agent: accept file mentions on Enter (#17384)

Parth Sareen76929b0a8ad8
diff --git a/cmd/tui/chat/chat.go b/cmd/tui/chat/chat.go
index 936c6b2c5..56df5bccd 100644
--- a/cmd/tui/chat/chat.go
+++ b/cmd/tui/chat/chat.go
@@ -599,6 +599,9 @@ func (m chatModel) updateKey(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
if m.applySlashCompletion() {
return m, nil
}
+ if m.applyMentionCompletion() {
+ return m, nil
+ }
return m.handleSubmit()
case tea.KeyCtrlJ:
m.insertInputNewline()
diff --git a/cmd/tui/chat/input.go b/cmd/tui/chat/input.go
index 4a40ac91d..8fda04f89 100644
--- a/cmd/tui/chat/input.go
+++ b/cmd/tui/chat/input.go
@@ -1419,8 +1419,7 @@ func slashCommandInvocation(input string) (string, string, bool) {
}
func (m chatModel) mentionCompletions() []chatCompletion {
- input := string(m.input)
- _, query, ok := activeMentionToken(input)
+ _, query, ok := activeMentionToken(m.input, m.normalizedInputCursor())
if !ok {
return nil
}
@@ -1484,13 +1483,13 @@ func (m chatModel) mentionCompletions() []chatCompletion {
return completions
}
-func activeMentionToken(input string) (int, string, bool) {
- runes := []rune(input)
- start := len(runes)
- for start > 0 && !unicode.IsSpace(runes[start-1]) {
+func activeMentionToken(input []rune, cursor int) (int, string, bool) {
+ cursor = clamp(cursor, 0, len(input))
+ start := cursor
+ for start > 0 && !unicode.IsSpace(input[start-1]) {
start--
}
- token := string(runes[start:])
+ token := string(input[start:cursor])
if !strings.HasPrefix(token, "@") {
return 0, "", false
}
@@ -1548,6 +1547,7 @@ func (m *chatModel) applyCompletion() bool {
}
m.resetPromptHistoryCursor()
selected := completions[clamp(m.complete, 0, len(completions)-1)]
+ cursor := m.normalizedInputCursor()
input := string(m.input)
if strings.HasPrefix(strings.TrimSpace(input), "/") {
m.input = []rune(selected.value)
@@ -1557,22 +1557,35 @@ func (m *chatModel) applyCompletion() bool {
return true
}
- start, _, ok := activeMentionToken(input)
+ start, _, ok := activeMentionToken(m.input, cursor)
if !ok {
return false
}
- suffix := ""
- if !selected.directory {
- suffix = " "
+ completed := []rune("@" + selected.value)
+ if !selected.directory && (cursor == len(m.input) || !unicode.IsSpace(m.input[cursor])) {
+ completed = append(completed, ' ')
+ }
+ next := make([]rune, 0, len(m.input)-cursor+start+len(completed))
+ next = append(next, m.input[:start]...)
+ next = append(next, completed...)
+ next = append(next, m.input[cursor:]...)
+ m.input = next
+ m.inputCursor = start + len(completed)
+ if !selected.directory && m.inputCursor < len(m.input) && unicode.IsSpace(m.input[m.inputCursor]) {
+ m.inputCursor++
}
- next := string([]rune(input)[:start]) + "@" + selected.value + suffix
- m.input = []rune(next)
- m.inputCursor = len(m.input)
m.inputCursorSet = true
m.complete = 0
return true
}
+func (m *chatModel) applyMentionCompletion() bool {
+ if strings.HasPrefix(strings.TrimSpace(string(m.input)), "/") {
+ return false
+ }
+ return m.applyCompletion()
+}
+
func completionIsSelectable(completions []chatCompletion) bool {
return len(completions) > 0 && completions[0].value != ""
}
diff --git a/cmd/tui/chat/input_test.go b/cmd/tui/chat/input_test.go
index 8676181a7..c5a3cf57b 100644
--- a/cmd/tui/chat/input_test.go
+++ b/cmd/tui/chat/input_test.go
@@ -1277,3 +1277,44 @@ func TestChatFileMentionSuggestionsFilterAndComplete(t *testing.T) {
t.Fatalf("completed input = %q", got)
}
}
+
+func TestChatEnterCompletesHighlightedFileMentionWithoutSubmitting(t *testing.T) {
+ dir := t.TempDir()
+ if err := os.WriteFile(filepath.Join(dir, "alpha.md"), []byte("hi"), 0o644); err != nil {
+ t.Fatal(err)
+ }
+ if err := os.WriteFile(filepath.Join(dir, "target.md"), []byte("hi"), 0o644); err != nil {
+ t.Fatal(err)
+ }
+
+ m := chatModel{
+ workingDir: dir,
+ input: []rune("review @ after this"),
+ inputCursor: len([]rune("review @")),
+ inputCursorSet: true,
+ }
+
+ updated, _ := m.Update(tea.KeyMsg{Type: tea.KeyDown})
+ m = updated.(chatModel)
+ if got, want := m.complete, 1; got != want {
+ t.Fatalf("selected completion = %d, want %d", got, want)
+ }
+
+ updated, cmd := m.Update(tea.KeyMsg{Type: tea.KeyEnter})
+ if cmd != nil {
+ t.Fatal("selecting a file mention should not submit the prompt")
+ }
+ m = updated.(chatModel)
+ if got, want := string(m.input), "review @target.md after this"; got != want {
+ t.Fatalf("input = %q, want %q", got, want)
+ }
+ if got, want := m.inputCursor, len([]rune("review @target.md ")); got != want || !m.inputCursorSet {
+ t.Fatalf("cursor = %d (set=%v), want %d after the inserted mention", got, m.inputCursorSet, want)
+ }
+ if len(m.entries) != 0 || len(m.messages) != 0 {
+ t.Fatalf("selecting a file mention submitted the prompt: entries=%#v messages=%#v", m.entries, m.messages)
+ }
+ if completions := m.mentionCompletions(); completions != nil {
+ t.Fatalf("mention selector remained visible after selection: %#v", completions)
+ }
+}