summaryrefslogtreecommitdiffhomepage
path: root/internal/llm/tools/lsp_references.go
blob: 6e0090f4eaff99a8a6c59f8c9a4f11ffe238ff1b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package tools

import (
	"context"
	"encoding/json"
	"fmt"
	"strings"

	"github.com/sst/opencode/internal/lsp"
	"github.com/sst/opencode/internal/lsp/protocol"
)

type ReferencesParams struct {
	FilePath           string `json:"file_path"`
	Line               int    `json:"line"`
	Column             int    `json:"column"`
	IncludeDeclaration bool   `json:"include_declaration"`
}

type referencesTool struct {
	lspClients map[string]*lsp.Client
}

const (
	ReferencesToolName    = "references"
	referencesDescription = `Find all references to a symbol at a specific position in a file.
WHEN TO USE THIS TOOL:
- Use when you need to find all places where a symbol is used
- Helpful for understanding code usage and dependencies
- Great for refactoring and impact analysis

HOW TO USE:
- Provide the path to the file containing the symbol
- Specify the line number (1-based) where the symbol appears
- Specify the column number (1-based) where the symbol appears
- Optionally set include_declaration to include the declaration in results
- Results show all locations where the symbol is referenced

FEATURES:
- Finds references across files in the project
- Works with variables, functions, classes, interfaces, etc.
- Returns file paths, lines, and columns of all references

LIMITATIONS:
- Requires a functioning LSP server for the file type
- May not find all references depending on LSP capabilities
- Results depend on the accuracy of the LSP server

TIPS:
- Use in conjunction with Definition tool to understand symbol origins
- Combine with View tool to examine the references
`
)

func NewReferencesTool(lspClients map[string]*lsp.Client) BaseTool {
	return &referencesTool{
		lspClients,
	}
}

func (b *referencesTool) Info() ToolInfo {
	return ToolInfo{
		Name:        ReferencesToolName,
		Description: referencesDescription,
		Parameters: map[string]any{
			"file_path": map[string]any{
				"type":        "string",
				"description": "The path to the file containing the symbol",
			},
			"line": map[string]any{
				"type":        "integer",
				"description": "The line number (1-based) where the symbol appears",
			},
			"column": map[string]any{
				"type":        "integer",
				"description": "The column number (1-based) where the symbol appears",
			},
			"include_declaration": map[string]any{
				"type":        "boolean",
				"description": "Whether to include the declaration in the results",
			},
		},
		Required: []string{"file_path", "line", "column"},
	}
}

func (b *referencesTool) Run(ctx context.Context, call ToolCall) (ToolResponse, error) {
	var params ReferencesParams
	if err := json.Unmarshal([]byte(call.Input), &params); err != nil {
		return NewTextErrorResponse(fmt.Sprintf("error parsing parameters: %s", err)), nil
	}

	lsps := b.lspClients

	if len(lsps) == 0 {
		return NewTextResponse("\nLSP clients are still initializing. References lookup will be available once they're ready.\n"), nil
	}

	// Ensure file is open in LSP
	notifyLspOpenFile(ctx, params.FilePath, lsps)

	// Convert 1-based line/column to 0-based for LSP protocol
	line := max(0, params.Line-1)
	column := max(0, params.Column-1)

	output := getReferences(ctx, params.FilePath, line, column, params.IncludeDeclaration, lsps)

	return NewTextResponse(output), nil
}

func getReferences(ctx context.Context, filePath string, line, column int, includeDeclaration bool, lsps map[string]*lsp.Client) string {
	var results []string

	for lspName, client := range lsps {
		// Create references params
		uri := fmt.Sprintf("file://%s", filePath)
		referencesParams := protocol.ReferenceParams{
			TextDocumentPositionParams: protocol.TextDocumentPositionParams{
				TextDocument: protocol.TextDocumentIdentifier{
					URI: protocol.DocumentUri(uri),
				},
				Position: protocol.Position{
					Line:      uint32(line),
					Character: uint32(column),
				},
			},
			Context: protocol.ReferenceContext{
				IncludeDeclaration: includeDeclaration,
			},
		}

		// Get references
		references, err := client.References(ctx, referencesParams)
		if err != nil {
			results = append(results, fmt.Sprintf("Error from %s: %s", lspName, err))
			continue
		}

		if len(references) == 0 {
			results = append(results, fmt.Sprintf("No references found by %s", lspName))
			continue
		}

		// Format the locations
		results = append(results, fmt.Sprintf("References found by %s:", lspName))
		for _, loc := range references {
			path := strings.TrimPrefix(string(loc.URI), "file://")
			// Convert 0-based line/column to 1-based for display
			refLine := loc.Range.Start.Line + 1
			refColumn := loc.Range.Start.Character + 1
			results = append(results, fmt.Sprintf("  %s:%d:%d", path, refLine, refColumn))
		}
	}

	if len(results) == 0 {
		return "No references found for the symbol at the specified position."
	}

	return strings.Join(results, "\n")
}