refactor(ui): add references tool (#1940)

Kujtim Hoxha created

Change summary

internal/ui/chat/references.go | 63 ++++++++++++++++++++++++++++++++++++
internal/ui/chat/tools.go      |  2 +
2 files changed, 65 insertions(+)

Detailed changes

internal/ui/chat/references.go 🔗

@@ -0,0 +1,63 @@
+package chat
+
+import (
+	"encoding/json"
+
+	"github.com/charmbracelet/crush/internal/agent/tools"
+	"github.com/charmbracelet/crush/internal/fsext"
+	"github.com/charmbracelet/crush/internal/message"
+	"github.com/charmbracelet/crush/internal/ui/styles"
+)
+
+// ReferencesToolMessageItem is a message item that represents a references tool call.
+type ReferencesToolMessageItem struct {
+	*baseToolMessageItem
+}
+
+var _ ToolMessageItem = (*ReferencesToolMessageItem)(nil)
+
+// NewReferencesToolMessageItem creates a new [ReferencesToolMessageItem].
+func NewReferencesToolMessageItem(
+	sty *styles.Styles,
+	toolCall message.ToolCall,
+	result *message.ToolResult,
+	canceled bool,
+) ToolMessageItem {
+	return newBaseToolMessageItem(sty, toolCall, result, &ReferencesToolRenderContext{}, canceled)
+}
+
+// ReferencesToolRenderContext renders references tool messages.
+type ReferencesToolRenderContext struct{}
+
+// RenderTool implements the [ToolRenderer] interface.
+func (r *ReferencesToolRenderContext) RenderTool(sty *styles.Styles, width int, opts *ToolRenderOpts) string {
+	cappedWidth := cappedMessageWidth(width)
+	if opts.IsPending() {
+		return pendingTool(sty, "Find References", opts.Anim)
+	}
+
+	var params tools.ReferencesParams
+	_ = json.Unmarshal([]byte(opts.ToolCall.Input), &params)
+
+	toolParams := []string{params.Symbol}
+	if params.Path != "" {
+		toolParams = append(toolParams, "path", fsext.PrettyPath(params.Path))
+	}
+
+	header := toolHeader(sty, opts.Status, "Find References", cappedWidth, opts.Compact, toolParams...)
+	if opts.Compact {
+		return header
+	}
+
+	if earlyState, ok := toolEarlyStateContent(sty, opts, cappedWidth); ok {
+		return joinToolParts(header, earlyState)
+	}
+
+	if opts.HasEmptyResult() {
+		return header
+	}
+
+	bodyWidth := cappedWidth - toolBodyLeftPaddingTotal
+	body := sty.Tool.Body.Render(toolOutputPlainContent(sty, opts.Result.Content, bodyWidth, opts.ExpandedContent))
+	return joinToolParts(header, body)
+}

internal/ui/chat/tools.go 🔗

@@ -242,6 +242,8 @@ func NewToolMessageItem(
 		item = NewWebSearchToolMessageItem(sty, toolCall, result, canceled)
 	case tools.TodosToolName:
 		item = NewTodosToolMessageItem(sty, toolCall, result, canceled)
+	case tools.ReferencesToolName:
+		item = NewReferencesToolMessageItem(sty, toolCall, result, canceled)
 	default:
 		if strings.HasPrefix(toolCall.Name, "mcp_") {
 			item = NewMCPToolMessageItem(sty, toolCall, result, canceled)