@@ -8,6 +8,7 @@ import (
88 "time"
99
1010 tea "charm.land/bubbletea/v2"
11+ "charm.land/lipgloss/v2"
1112 "github.com/charmbracelet/x/ansi"
1213 "github.com/xonecas/symb/internal/highlight"
1314 )
@@ -156,9 +157,15 @@ func (m *Model) handleConvMotion(x, y, totalLines int) {
156157 func (m *Model) handleConvRelease(x, y, totalLines int) tea.Cmd {
157158 m.convDragging = false
158159 if m.convSel != nil && m.convSel.empty() {
159- clickedLine := m.convPosFromScreen(x, y, totalLines).line
160+ cp := m.convPosFromScreen(x, y, totalLines)
160161 m.convSel = nil
161- return m.handleConvClick(clickedLine)
162+ // Ignore clicks on empty space past the last wrapped line.
163+ startLine := m.visibleStartLine()
164+ screenLine := startLine + (y - m.layout.conv.Min.Y)
165+ if screenLine >= totalLines {
166+ return nil
167+ }
168+ return m.handleConvClick(cp.line, cp.col)
162169 }
163170 return nil
164171 }
@@ -216,8 +223,10 @@ func (m Model) translateMouse(msg tea.MouseMsg, offX, offY int) tea.Msg {
216223 return msg
217224 }
218225
219-// isClickableLine returns true if the wrapped line at lineIdx is clickable
220-// (tool result entry or contains a file path reference).
226+// isClickableLine returns true if the wrapped line at lineIdx is clickable.
227+// Tool result entries (first line only, which has the [view] button) and undo
228+// entries are always clickable. Plain text lines are clickable only if they
229+// contain a file path reference.
221230 func (m *Model) isClickableLine(lineIdx int) bool {
222231 lines := m.wrappedConvLines() // ensures convLineSource is also fresh
223232 src := m.convLineSource
@@ -228,35 +237,31 @@ func (m *Model) isClickableLine(lineIdx int) bool {
228237 if entryIdx < 0 || entryIdx >= len(m.convEntries) {
229238 return false
230239 }
231- if m.convEntries[entryIdx].kind == entryToolResult {
232- // Entries with a file path (Read/Edit/Create) are always clickable.
233- if m.convEntries[entryIdx].filePath != "" {
234- return true
235- }
236- // Others (Grep/Shell) only if the line itself contains a file path.
237- if lineIdx < len(lines) {
238- return filePathRe.MatchString(ansi.Strip(lines[lineIdx]))
240+ entry := m.convEntries[entryIdx]
241+ switch entry.kind {
242+ case entryToolResult:
243+ // Only the first wrapped line (containing [view]) is clickable.
244+ if lineIdx > 0 && src[lineIdx-1] == entryIdx {
245+ return false
239246 }
240- return false
241- }
242- if m.convEntries[entryIdx].kind == entryToolDiag {
243- return false
244- }
245- if m.convEntries[entryIdx].kind == entryUndo {
246247 return true
247- }
248- if lineIdx >= len(lines) {
248+ case entryUndo:
249+ return true
250+ case entryToolDiag, entryToolCall, entrySeparator:
249251 return false
252+ default:
253+ // Plain text: clickable if it contains a file path.
254+ if lineIdx >= len(lines) {
255+ return false
256+ }
257+ return filePathRe.MatchString(ansi.Strip(lines[lineIdx]))
250258 }
251- plain := ansi.Strip(lines[lineIdx])
252- return filePathRe.MatchString(plain)
253259 }
254260
255261 // handleConvClick resolves a click on a wrapped conversation line.
256-// If the line belongs to a tool result entry, the full content is opened in
257-// the editor. Otherwise, if the line contains a file path reference
258-// (path/to/file.go:123), that file is opened in the editor.
259-func (m *Model) handleConvClick(wrappedLine int) tea.Cmd {
262+// Tool result [view] buttons open the relevant content in the editor.
263+// Undo buttons trigger an undo. Plain text file paths open the file.
264+func (m *Model) handleConvClick(wrappedLine, col int) tea.Cmd {
260265 m.wrappedConvLines() // ensure convLineSource is fresh
261266 src := m.convLineSource
262267 if wrappedLine < 0 || wrappedLine >= len(src) {
@@ -267,50 +272,120 @@ func (m *Model) handleConvClick(wrappedLine int) tea.Cmd {
267272 return nil
268273 }
269274 entry := m.convEntries[entryIdx]
270- if entry.kind == entryToolDiag {
275+
276+ switch entry.kind {
277+ case entryUndo:
278+ if m.isClickOnCenteredLabel(entry.display, col) {
279+ return func() tea.Msg { return undoMsg{} }
280+ }
281+ return nil
282+
283+ case entryToolResult:
284+ // Only trigger on the [view] label at the end of the line.
285+ if m.isClickOnViewLabel(entry.display, col) {
286+ return m.handleToolResultView(entry)
287+ }
271288 return nil
289+
290+ case entryToolDiag, entryToolCall, entrySeparator:
291+ return nil
292+
293+ default:
294+ // Plain text: try to open a file path reference.
295+ lines := m.wrappedConvLines()
296+ if wrappedLine >= len(lines) {
297+ return nil
298+ }
299+ return m.tryOpenFilePath(ansi.Strip(lines[wrappedLine]))
272300 }
301+}
273302
274- // Undo control: emit undoMsg
275- if entry.kind == entryUndo {
276- return func() tea.Msg { return undoMsg{} }
303+// isClickOnCenteredLabel checks whether a column falls within a centered
304+// label's visible text area in the conversation pane.
305+func (m *Model) isClickOnCenteredLabel(display string, col int) bool {
306+ rw := m.convWidth()
307+ lw := lipgloss.Width(display)
308+ pad := (rw - lw) / 2
309+ if pad < 0 {
310+ pad = 0
277311 }
312+ return col >= pad && col < pad+lw
313+}
278314
279- // Tool result: open the source file or fall back to raw content
280- if entry.kind == entryToolResult {
315+// isClickOnViewLabel checks whether a column falls on the "view" label
316+// at the end of a tool result line.
317+func (m *Model) isClickOnViewLabel(display string, col int) bool {
318+ const viewLabel = "view"
319+ lw := lipgloss.Width(display)
320+ viewStart := lw - len(viewLabel)
321+ if viewStart < 0 {
322+ viewStart = 0
323+ }
324+ return col >= viewStart && col < lw
325+}
326+
327+// handleToolResultView opens the appropriate content in the editor for a
328+// tool result [view] click, based on the tool type.
329+func (m *Model) handleToolResultView(entry convEntry) tea.Cmd {
330+ switch entry.toolName {
331+ case "Read":
332+ // Open file with cursor in the middle of the read range.
281333 if entry.filePath != "" {
282- absPath, _ := filepath.Abs(entry.filePath)
283- if content, err := os.ReadFile(entry.filePath); err == nil {
284- m.editor.SetValue(string(content))
285- m.editor.Language = highlight.DetectLanguage(entry.filePath)
286- m.editor.SetGutterMarkers(GitFileMarkers(m.ctx, entry.filePath))
287- m.editor.DiagnosticLines = nil
288- m.editorFilePath = absPath
289- m.lspErrors = 0
290- m.lspWarnings = 0
291- if entry.line > 0 {
292- m.editor.GotoLine(entry.line)
293- }
294- m.setFocus(focusEditor)
295- return nil
296- }
334+ return m.openFileAtCenter(entry.filePath, entry.line, entry.full)
297335 }
298- // No file path on entry (e.g. Grep/Shell): try to extract a
299- // file:line reference from the clicked line itself.
300- lines := m.wrappedConvLines()
301- if wrappedLine < len(lines) {
302- plain := ansi.Strip(lines[wrappedLine])
303- return m.tryOpenFilePath(plain)
336+ case "Edit":
337+ // Open file with cursor at the end of the changes.
338+ if entry.filePath != "" {
339+ return m.openFile(entry.filePath, entry.line)
304340 }
341+ case "Shell":
342+ // Show the command output in the editor as plain text.
343+ m.showRawContent(entry.full, "text")
344+ return nil
345+ case "WebFetch", "WebSearch":
346+ // Show search/fetch results as markdown.
347+ m.showRawContent(entry.full, "markdown")
348+ return nil
349+ case "SubAgent":
350+ // Show the subagent's response as markdown.
351+ m.showRawContent(entry.full, "markdown")
352+ return nil
353+ default:
354+ // Fallback: try file path, then show raw content.
355+ if entry.filePath != "" {
356+ return m.openFile(entry.filePath, entry.line)
357+ }
358+ m.showRawContent(entry.full, "text")
359+ return nil
305360 }
361+ // Fallback for tools with no file path.
362+ m.showRawContent(entry.full, "text")
363+ return nil
364+}
306365
307- // Try to extract a file path from the clicked line's plain text
308- lines := m.wrappedConvLines()
309- if wrappedLine >= len(lines) {
310- return nil
366+// openFileAtCenter opens a file and positions the cursor in the middle of the
367+// read range (for Read tool results).
368+func (m *Model) openFileAtCenter(path string, startLine int, content string) tea.Cmd {
369+ if sm := toolResultRangeRe.FindStringSubmatch(content); sm != nil {
370+ start, _ := strconv.Atoi(sm[1])
371+ end, _ := strconv.Atoi(sm[2])
372+ if start > 0 && end >= start {
373+ return m.openFile(path, (start+end)/2)
374+ }
311375 }
312- plain := ansi.Strip(lines[wrappedLine])
313- return m.tryOpenFilePath(plain)
376+ return m.openFile(path, startLine)
377+}
378+
379+// showRawContent loads raw text content into the editor for viewing.
380+func (m *Model) showRawContent(content, language string) {
381+ m.editor.SetValue(content)
382+ m.editor.Language = language
383+ m.editor.SetGutterMarkers(nil)
384+ m.editor.DiagnosticLines = nil
385+ m.editorFilePath = ""
386+ m.lspErrors = 0
387+ m.lspWarnings = 0
388+ m.setFocus(focusEditor)
314389 }
315390
316391 // tryOpenFilePath looks for a file:line reference in text and opens it in the editor.