xonecas.io
62159
Sy

symb / commit dbbea61

fix: edit tool opens with cursor at edit point

xonecas2026-02-14 dbbea61a3a97d5f6e389c39da914bdbde875006b parent 90adcd8

2 files changed

internal/tui/tui.go+3−0
@@ -227,6 +227,9 @@ type Model struct {
227227 // Editor state
228228 editorFilePath string // absolute path of the file currently shown in the editor
229229
230+ // Pending tool calls: maps tool call ID → arguments for line extraction
231+ pendingToolCalls map[string]provider.ToolCall
232+
230233 // Mouse state
231234 resizingPane bool
232235
internal/tui/update.go+42−1
@@ -293,6 +293,10 @@ func (m *Model) applyAssistantMsg(msg llmAssistantMsg) {
293293 }
294294 }
295295 for _, tc := range msg.toolCalls {
296+ if m.pendingToolCalls == nil {
297+ m.pendingToolCalls = make(map[string]provider.ToolCall)
298+ }
299+ m.pendingToolCalls[tc.ID] = tc
296300 entry := m.styles.ToolArrow.Render("→") + " " + m.styles.ToolCall.Render(formatToolCall(tc))
297301 wasBottom := m.appendText(entry)
298302 if wasBottom {
@@ -320,10 +324,13 @@ func (m *Model) applyToolResultMsg(msg llmToolResultMsg) {
320324 filePath = sm[1]
321325 }
322326
323- // Extract start line from "(lines N-M)" for cursor positioning.
327+ // Extract target line for cursor positioning.
328+ // Read results: from "(lines N-M)". Edit results: from the tool call arguments.
324329 var startLine int
325330 if sm := toolResultLineRe.FindStringSubmatch(msg.content); sm != nil {
326331 startLine, _ = strconv.Atoi(sm[1])
332+ } else if tc, ok := m.pendingToolCalls[msg.toolCallID]; ok && tc.Name == "Edit" {
333+ startLine = toolCallEditLine(tc.Arguments)
327334 }
328335
329336 lines := strings.Split(msg.content, "\n")
@@ -354,6 +361,40 @@ func (m *Model) applyToolResultMsg(msg llmToolResultMsg) {
354361 }
355362 }
356363
364+// toolCallEditLine extracts the target line from an Edit tool call's arguments.
365+// Returns the start line of the edit operation (replace/insert/delete), or 0.
366+func toolCallEditLine(args json.RawMessage) int {
367+ var parsed struct {
368+ Replace *struct {
369+ Start struct {
370+ Line int `json:"line"`
371+ } `json:"start"`
372+ } `json:"replace"`
373+ Insert *struct {
374+ After struct {
375+ Line int `json:"line"`
376+ } `json:"after"`
377+ } `json:"insert"`
378+ Delete *struct {
379+ Start struct {
380+ Line int `json:"line"`
381+ } `json:"start"`
382+ } `json:"delete"`
383+ }
384+ if json.Unmarshal(args, &parsed) != nil {
385+ return 0
386+ }
387+ switch {
388+ case parsed.Replace != nil:
389+ return parsed.Replace.Start.Line
390+ case parsed.Insert != nil:
391+ return parsed.Insert.After.Line
392+ case parsed.Delete != nil:
393+ return parsed.Delete.Start.Line
394+ }
395+ return 0
396+}
397+
357398 // formatToolCall renders a tool call as Name(key="val", key2="val2").
358399 // Long values are truncated. Falls back to Name(...) on parse errors.
359400 func formatToolCall(tc provider.ToolCall) string {