Skip to content

Commit

Permalink
Fix bug in language.findSymbolDeclarationInDocPositionScope not findi…
Browse files Browse the repository at this point in the history
…ng elements in current scope.
  • Loading branch information
pherrymason committed Jan 20, 2024
1 parent 377e80e commit 7a6ba20
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 3 deletions.
7 changes: 4 additions & 3 deletions server/lsp/language/language.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ func (l *Language) findClosestSymbolDeclaration(word string, docId protocol.Docu
// -----------

// Not found yet, let's try search the symbol defined as global in other files
// Note: Iterating a map is not guaranteed to be done always in the same order.
for _, scope := range l.functionTreeByDocument {
found, foundDepth := findDeepFirst(word, position, &scope, 0, AnyPosition)

Expand All @@ -115,8 +116,8 @@ func (l *Language) findClosestSymbolDeclaration(word string, docId protocol.Docu

// Search for symbol in docId
func (l *Language) findSymbolDeclarationInDocPositionScope(identifier string, docId protocol.DocumentUri, position protocol.Position) (indexables.Indexable, error) {
scopedTree, ok := l.functionTreeByDocument[docId]
if !ok {
scopedTree, found := l.functionTreeByDocument[docId]
if !found {
return nil, errors.New("Document is not indexed")
}

Expand All @@ -127,7 +128,7 @@ func (l *Language) findSymbolDeclarationInDocPositionScope(identifier string, do

func findDeepFirst(identifier string, position protocol.Position, function *indexables.Function, depth uint, mode FindMode) (indexables.Indexable, uint) {
if mode == InPosition &&
!function.GetDeclarationRange().HasPosition(position) {
!function.GetDocumentRange().HasPosition(position) {
return nil, depth
}

Expand Down
65 changes: 65 additions & 0 deletions server/lsp/language/language_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,70 @@ func createParser() p.Parser {
return p.NewParser(logger)
}

func TestLanguage_findSymbolDeclarationInDocPositionScope_cursor_on_declaration_resolves_to_same_declaration(t *testing.T) {
language := NewLanguage()

// Doc A
docA := "a"
moduleA := "modA"
fileA := idx.NewFunctionBuilder("main", "void", moduleA, docA).
WithDocumentRange(0, 0, 0, 20).
Build()
fileA.AddVariable(idx.NewVariableBuilder("out", "Out", moduleA, docA).
WithIdentifierRange(0, 0, 0, 10).
Build(),
)
language.functionTreeByDocument[docA] = fileA

resolvedSymbol, err := language.findSymbolDeclarationInDocPositionScope("out", docA, protocol.Position{0, 5})

assert.Equal(t, nil, err)
assert.Equal(t,
idx.NewVariableBuilder("out", "Out", moduleA, docA).
WithIdentifierRange(0, 0, 0, 10).
Build(),
resolvedSymbol,
)
}

func TestLanguage_findClosestSymbolDeclaration_cursor_on_declaration_resolves_to_same_declaration(t *testing.T) {
language := NewLanguage()

// Doc A
docA := "a"
moduleA := "modA"
fileA := idx.NewFunctionBuilder("main", "void", moduleA, docA).
Build()
fileA.AddVariable(idx.NewVariableBuilder("out", "Out", moduleA, docA).
WithIdentifierRange(0, 0, 0, 10).
Build(),
)
language.functionTreeByDocument[docA] = fileA

// Doc B
docB := "b"
moduleB := "modB"
fileB := idx.NewFunctionBuilder("main", "void", moduleB, docB).
Build()
fileB.AddVariable(idx.NewVariableBuilder("out", "int", moduleB, docB).
WithIdentifierRange(0, 0, 0, 10).
Build(),
)
language.functionTreeByDocument[docB] = fileB
// Add more docs to the map to increase possibility of iterating in random ways
language.functionTreeByDocument["3"] = idx.NewFunctionBuilder("aaa", "void", "aaa", "aaa").Build()
language.functionTreeByDocument["4"] = idx.NewFunctionBuilder("bbb", "void", "bbb", "bbb").Build()

resolvedSymbol := language.findClosestSymbolDeclaration("out", docA, protocol.Position{0, 5})

assert.Equal(t,
idx.NewVariableBuilder("out", "Out", moduleA, docA).
WithIdentifierRange(0, 0, 0, 10).
Build(),
resolvedSymbol,
)
}

func TestLanguage_FindHoverInformation(t *testing.T) {
language := NewLanguage()
parser := createParser()
Expand Down Expand Up @@ -94,6 +158,7 @@ func newDeclarationParams(docId string, line protocol.UInteger, char protocol.UI
WorkDoneProgressParams: protocol.WorkDoneProgressParams{},
}
}

func TestLanguage_FindSymbolDeclarationInWorkspace_symbol_same_scope(t *testing.T) {
module := "mod"
cases := []struct {
Expand Down

0 comments on commit 7a6ba20

Please sign in to comment.