lsp_references.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. package tools
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "strings"
  7. "github.com/sst/opencode/internal/lsp"
  8. "github.com/sst/opencode/internal/lsp/protocol"
  9. )
  10. type ReferencesParams struct {
  11. FilePath string `json:"file_path"`
  12. Line int `json:"line"`
  13. Column int `json:"column"`
  14. IncludeDeclaration bool `json:"include_declaration"`
  15. }
  16. type referencesTool struct {
  17. lspClients map[string]*lsp.Client
  18. }
  19. const (
  20. ReferencesToolName = "references"
  21. referencesDescription = `Find all references to a symbol at a specific position in a file.
  22. WHEN TO USE THIS TOOL:
  23. - Use when you need to find all places where a symbol is used
  24. - Helpful for understanding code usage and dependencies
  25. - Great for refactoring and impact analysis
  26. HOW TO USE:
  27. - Provide the path to the file containing the symbol
  28. - Specify the line number (1-based) where the symbol appears
  29. - Specify the column number (1-based) where the symbol appears
  30. - Optionally set include_declaration to include the declaration in results
  31. - Results show all locations where the symbol is referenced
  32. FEATURES:
  33. - Finds references across files in the project
  34. - Works with variables, functions, classes, interfaces, etc.
  35. - Returns file paths, lines, and columns of all references
  36. LIMITATIONS:
  37. - Requires a functioning LSP server for the file type
  38. - May not find all references depending on LSP capabilities
  39. - Results depend on the accuracy of the LSP server
  40. TIPS:
  41. - Use in conjunction with Definition tool to understand symbol origins
  42. - Combine with View tool to examine the references
  43. `
  44. )
  45. func NewReferencesTool(lspClients map[string]*lsp.Client) BaseTool {
  46. return &referencesTool{
  47. lspClients,
  48. }
  49. }
  50. func (b *referencesTool) Info() ToolInfo {
  51. return ToolInfo{
  52. Name: ReferencesToolName,
  53. Description: referencesDescription,
  54. Parameters: map[string]any{
  55. "file_path": map[string]any{
  56. "type": "string",
  57. "description": "The path to the file containing the symbol",
  58. },
  59. "line": map[string]any{
  60. "type": "integer",
  61. "description": "The line number (1-based) where the symbol appears",
  62. },
  63. "column": map[string]any{
  64. "type": "integer",
  65. "description": "The column number (1-based) where the symbol appears",
  66. },
  67. "include_declaration": map[string]any{
  68. "type": "boolean",
  69. "description": "Whether to include the declaration in the results",
  70. },
  71. },
  72. Required: []string{"file_path", "line", "column"},
  73. }
  74. }
  75. func (b *referencesTool) Run(ctx context.Context, call ToolCall) (ToolResponse, error) {
  76. var params ReferencesParams
  77. if err := json.Unmarshal([]byte(call.Input), &params); err != nil {
  78. return NewTextErrorResponse(fmt.Sprintf("error parsing parameters: %s", err)), nil
  79. }
  80. lsps := b.lspClients
  81. if len(lsps) == 0 {
  82. return NewTextResponse("\nLSP clients are still initializing. References lookup will be available once they're ready.\n"), nil
  83. }
  84. // Ensure file is open in LSP
  85. notifyLspOpenFile(ctx, params.FilePath, lsps)
  86. // Convert 1-based line/column to 0-based for LSP protocol
  87. line := max(0, params.Line-1)
  88. column := max(0, params.Column-1)
  89. output := getReferences(ctx, params.FilePath, line, column, params.IncludeDeclaration, lsps)
  90. return NewTextResponse(output), nil
  91. }
  92. func getReferences(ctx context.Context, filePath string, line, column int, includeDeclaration bool, lsps map[string]*lsp.Client) string {
  93. var results []string
  94. for lspName, client := range lsps {
  95. // Create references params
  96. uri := fmt.Sprintf("file://%s", filePath)
  97. referencesParams := protocol.ReferenceParams{
  98. TextDocumentPositionParams: protocol.TextDocumentPositionParams{
  99. TextDocument: protocol.TextDocumentIdentifier{
  100. URI: protocol.DocumentUri(uri),
  101. },
  102. Position: protocol.Position{
  103. Line: uint32(line),
  104. Character: uint32(column),
  105. },
  106. },
  107. Context: protocol.ReferenceContext{
  108. IncludeDeclaration: includeDeclaration,
  109. },
  110. }
  111. // Get references
  112. references, err := client.References(ctx, referencesParams)
  113. if err != nil {
  114. results = append(results, fmt.Sprintf("Error from %s: %s", lspName, err))
  115. continue
  116. }
  117. if len(references) == 0 {
  118. results = append(results, fmt.Sprintf("No references found by %s", lspName))
  119. continue
  120. }
  121. // Format the locations
  122. results = append(results, fmt.Sprintf("References found by %s:", lspName))
  123. for _, loc := range references {
  124. path := strings.TrimPrefix(string(loc.URI), "file://")
  125. // Convert 0-based line/column to 1-based for display
  126. refLine := loc.Range.Start.Line + 1
  127. refColumn := loc.Range.Start.Character + 1
  128. results = append(results, fmt.Sprintf(" %s:%d:%d", path, refLine, refColumn))
  129. }
  130. }
  131. if len(results) == 0 {
  132. return "No references found for the symbol at the specified position."
  133. }
  134. return strings.Join(results, "\n")
  135. }