Skip to content

Commit eaed24a

Browse files
authored
Merge pull request isaacphi#11 from virtuald/workspace-symbols
definition: match workspace symbols too
2 parents 96b30b6 + 1b8881c commit eaed24a

File tree

9 files changed

+43
-20
lines changed

9 files changed

+43
-20
lines changed

integrationtests/snapshots/clangd/definition/class.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
Symbol: TestClass
44
/TEST_OUTPUT/workspace/clangd/src/consumer.cpp
5+
Kind: Class
56
Range: L7:C1 - L15:C2
67

78
7|class TestClass {

integrationtests/snapshots/clangd/definition/constant.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
Symbol: TEST_CONSTANT
44
/TEST_OUTPUT/workspace/clangd/src/helper.cpp
5+
Kind: Variable
56
Range: L4:C1 - L4:C29
67

78
4|const int TEST_CONSTANT = 42;

integrationtests/snapshots/clangd/definition/foobar.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
Symbol: foo_bar
44
/TEST_OUTPUT/workspace/src/main.cpp
5+
Kind: Function
56
Range: L5:C1 - L8:C2
67

78
5|void foo_bar() {

integrationtests/snapshots/clangd/definition/helperFunction.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
Symbol: helperFunction
44
/TEST_OUTPUT/workspace/clangd/src/helper.cpp
5+
Kind: Function
56
Range: L7:C1 - L7:C71
67

78
7|void helperFunction() { std::cout << "Helper function" << std::endl; }

integrationtests/snapshots/clangd/definition/method.snap

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
Symbol: method
44
/TEST_OUTPUT/workspace/clangd/src/consumer.cpp
5+
Kind: Method
6+
Container Name: TestClass
57
Range: L7:C1 - L15:C2
68

79
7|class TestClass {

integrationtests/snapshots/clangd/definition/struct.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
Symbol: TestStruct
44
/TEST_OUTPUT/workspace/clangd/src/types.cpp
5+
Kind: Class
56
Range: L6:C1 - L8:C2
67

78
6|struct TestStruct {

integrationtests/snapshots/clangd/definition/type.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
Symbol: TestType
44
/TEST_OUTPUT/workspace/clangd/src/types.cpp
5+
Kind: Class
56
Range: L10:C1 - L10:C21
67

78
10|using TestType = int;

integrationtests/snapshots/clangd/definition/variable.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
Symbol: TEST_VARIABLE
44
/TEST_OUTPUT/workspace/clangd/src/helper.cpp
5+
Kind: Variable
56
Range: L5:C1 - L5:C24
67

78
5|int TEST_VARIABLE = 100; // A test variable used for integration testing purposes.

internal/tools/definition.go

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,33 +21,47 @@ func ReadDefinition(ctx context.Context, client *lsp.Client, symbolName string)
2121
container := ""
2222

2323
// Skip symbols that we are not looking for. workspace/symbol may return
24-
// a large number of fuzzy matches.
25-
switch v := symbol.(type) {
26-
case *protocol.SymbolInformation:
27-
// SymbolInformation results have richer data.
28-
kind = fmt.Sprintf("Kind: %s\n", protocol.TableKindMap[v.Kind])
29-
if v.ContainerName != "" {
30-
container = fmt.Sprintf("Container Name: %s\n", v.ContainerName)
24+
// a large number of fuzzy matches. This handles BaseSymbolInformation
25+
doesSymbolMatch := func(vKind protocol.SymbolKind, vContainerName string) bool {
26+
thisName := symbol.GetName()
27+
28+
kind = fmt.Sprintf("Kind: %s\n", protocol.TableKindMap[vKind])
29+
if vContainerName != "" {
30+
container = fmt.Sprintf("Container Name: %s\n", vContainerName)
31+
}
32+
33+
if thisName == symbolName {
34+
return true
3135
}
3236

3337
// Handle different matching strategies based on the search term
3438
if strings.Contains(symbolName, ".") {
35-
// For qualified names like "Type.Method", require exact match
36-
if symbol.GetName() != symbolName {
37-
continue
39+
// For qualified names like "Type.Method", don't do fuzzy match
40+
41+
} else if vKind == protocol.Method {
42+
// For methods, only match if the method name matches exactly Type.symbolName or Type::symbolName or symbolName
43+
if strings.HasSuffix(thisName, "::"+symbolName) || strings.HasSuffix(symbolName, "::"+thisName) {
44+
return true
3845
}
39-
} else {
40-
// For unqualified names like "Method"
41-
if v.Kind == protocol.Method {
42-
// For methods, only match if the method name matches exactly Type.symbolName or Type::symbolName or symbolName
43-
if !strings.HasSuffix(symbol.GetName(), "::"+symbolName) && !strings.HasSuffix(symbol.GetName(), "."+symbolName) && symbol.GetName() != symbolName {
44-
continue
45-
}
46-
} else if symbol.GetName() != symbolName {
47-
// For non-methods, exact match only
48-
continue
46+
47+
if strings.HasSuffix(thisName, "."+symbolName) || strings.HasSuffix(symbolName, "."+thisName) {
48+
return true
4949
}
5050
}
51+
52+
return false
53+
}
54+
55+
switch v := symbol.(type) {
56+
case *protocol.SymbolInformation:
57+
if !doesSymbolMatch(v.Kind, v.ContainerName) {
58+
continue
59+
}
60+
61+
case *protocol.WorkspaceSymbol:
62+
if !doesSymbolMatch(v.Kind, v.ContainerName) {
63+
continue
64+
}
5165
default:
5266
if symbol.GetName() != symbolName {
5367
continue

0 commit comments

Comments
 (0)