From 8af5eb0f3c64bc54edc144228a36a8f02b2de762 Mon Sep 17 00:00:00 2001 From: Dustin Spicuzza Date: Fri, 6 Jun 2025 00:46:53 -0400 Subject: [PATCH] Add a hack to remove 'struct ' prefix from symbols - clangd doesn't resolve these properly and this saves some context if we have to ask the LLM to do it? --- internal/tools/definition.go | 11 ++--------- internal/tools/references.go | 11 ++--------- internal/tools/utilities.go | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 18 deletions(-) diff --git a/internal/tools/definition.go b/internal/tools/definition.go index 0ddd3fe..0af14a7 100644 --- a/internal/tools/definition.go +++ b/internal/tools/definition.go @@ -10,16 +10,9 @@ import ( ) func ReadDefinition(ctx context.Context, client *lsp.Client, symbolName string) (string, error) { - symbolResult, err := client.Symbol(ctx, protocol.WorkspaceSymbolParams{ - Query: symbolName, - }) + symbolName, results, err := QuerySymbol(ctx, client, symbolName) if err != nil { - return "", fmt.Errorf("failed to fetch symbol: %v", err) - } - - results, err := symbolResult.Results() - if err != nil { - return "", fmt.Errorf("failed to parse results: %v", err) + return "", err } var definitions []string diff --git a/internal/tools/references.go b/internal/tools/references.go index cb424e5..6fe3f18 100644 --- a/internal/tools/references.go +++ b/internal/tools/references.go @@ -22,16 +22,9 @@ func FindReferences(ctx context.Context, client *lsp.Client, symbolName string) } // First get the symbol location like ReadDefinition does - symbolResult, err := client.Symbol(ctx, protocol.WorkspaceSymbolParams{ - Query: symbolName, - }) + symbolName, results, err := QuerySymbol(ctx, client, symbolName) if err != nil { - return "", fmt.Errorf("failed to fetch symbol: %v", err) - } - - results, err := symbolResult.Results() - if err != nil { - return "", fmt.Errorf("failed to parse results: %v", err) + return "", err } var allReferences []string diff --git a/internal/tools/utilities.go b/internal/tools/utilities.go index e5beb28..841d360 100644 --- a/internal/tools/utilities.go +++ b/internal/tools/utilities.go @@ -1,12 +1,14 @@ package tools import ( + "context" "fmt" "os" "sort" "strconv" "strings" + "github.com/isaacphi/mcp-language-server/internal/lsp" "github.com/isaacphi/mcp-language-server/internal/protocol" ) @@ -167,3 +169,33 @@ func FormatLinesWithRanges(lines []string, ranges []LineRange) string { return result.String() } + +func doQuerySymbol(ctx context.Context, client *lsp.Client, symbolName string) ([]protocol.WorkspaceSymbolResult, error) { + + symbolResult, err := client.Symbol(ctx, protocol.WorkspaceSymbolParams{ + Query: symbolName, + }) + if err != nil { + return nil, fmt.Errorf("failed to fetch symbol: %v", err) + } + + results, err := symbolResult.Results() + if err != nil { + return nil, fmt.Errorf("failed to parse results: %v", err) + } + return results, nil +} + +func QuerySymbol(ctx context.Context, client *lsp.Client, symbolName string) (string, []protocol.WorkspaceSymbolResult, error) { + results, err := doQuerySymbol(ctx, client, symbolName) + + // clangd doesn't resolve "struct foo", only "foo" + if len(results) == 0 && strings.HasPrefix(symbolName, "struct ") { + results, err = doQuerySymbol(ctx, client, symbolName[7:]) + if len(results) > 0 { + symbolName = symbolName[7:] + } + } + + return symbolName, results, err +}