Skip to content

Commit

Permalink
FindSymbolDeclarationInWorkspace jumps to correct position for struct…
Browse files Browse the repository at this point in the history
… symbols.
  • Loading branch information
pherrymason committed Jan 12, 2024
1 parent dc36715 commit 796e8f6
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 11 deletions.
5 changes: 3 additions & 2 deletions server/lsp/indexables/struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ type Struct struct {
BaseIndexable
}

func NewStruct(name string, members []StructMember, docId string) Struct {
func NewStruct(name string, members []StructMember, docId string, idRange Range) Struct {
return Struct{
name: name,
members: members,
BaseIndexable: BaseIndexable{
documentURI: docId,
identifierRange: idRange,
documentURI: docId,
},
}
}
Expand Down
6 changes: 4 additions & 2 deletions server/lsp/language_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,12 @@ func createEnumerator(name string, pRange [4]uint) indexables.Enumerator {
return enumerator
}

func createStruct(docId string, name string, members []indexables.StructMember) indexables.Indexable {
func createStruct(docId string, name string, members []indexables.StructMember, idRange indexables.Range) indexables.Indexable {
return indexables.NewStruct(
name,
members,
docId,
idRange,
)
}

Expand Down Expand Up @@ -167,7 +168,8 @@ func TestLanguage_FindSymbolDeclarationInWorkspace_symbol_same_scope(t *testing.
createStruct("x", "MyStructure", []indexables.StructMember{
indexables.NewStructMember("enabled", "bool"),
indexables.NewStructMember("key", "char"),
}),
},
indexables.NewRange(0, 7, 0, 18)),
},
}

Expand Down
5 changes: 3 additions & 2 deletions server/lsp/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,8 @@ func (p *Parser) nodeToVariable(doc *Document, node *sitter.Node, sourceCode []b
}

func (p *Parser) nodeToStruct(doc *Document, node *sitter.Node, sourceCode []byte) idx.Struct {
name := node.Child(1).Content(sourceCode)
nameNode := node.Child(1)
name := nameNode.Content(sourceCode)
// TODO parse attributes
bodyNode := node.Child(2)

Expand All @@ -166,7 +167,7 @@ func (p *Parser) nodeToStruct(doc *Document, node *sitter.Node, sourceCode []byt
}
}

_struct := idx.NewStruct(name, fields, doc.URI)
_struct := idx.NewStruct(name, fields, doc.URI, idx.NewRangeFromSitterPositions(nameNode.StartPoint(), nameNode.EndPoint()))

return _struct
}
Expand Down
11 changes: 6 additions & 5 deletions server/lsp/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func TestFindIdentifiers_should_assign_different_scopes_to_same_name_identifiers
}, identifiers)
}*/

func TestFindSymbols_finds_function_root_and_global_variables_declarations(t *testing.T) {
func TestExtractSymbols_finds_function_root_and_global_variables_declarations(t *testing.T) {
source := `int value = 1;`
doc := NewDocumentFromString("x", source)
parser := createParser()
Expand All @@ -109,7 +109,7 @@ func TestFindSymbols_finds_function_root_and_global_variables_declarations(t *te
assert.Equal(t, expectedRoot, symbols)
}

func TestFindSymbols_finds_function_root_and_global_enum_declarations(t *testing.T) {
func TestExtractSymbols_finds_function_root_and_global_enum_declarations(t *testing.T) {
source := `enum Colors { RED, BLUE, GREEN };`
doc := NewDocumentFromString("x", source)
parser := createParser()
Expand Down Expand Up @@ -138,7 +138,7 @@ func TestFindSymbols_finds_function_root_and_global_enum_declarations(t *testing
assert.Equal(t, &enum, symbols.Enums["Colors"])
}

func TestFindSymbols_finds_function_root_and_global_enum_with_base_type_declarations(t *testing.T) {
func TestExtractSymbols_finds_function_root_and_global_enum_with_base_type_declarations(t *testing.T) {
source := `enum Colors:int { RED, BLUE, GREEN };`
doc := NewDocumentFromString("x", source)
parser := createParser()
Expand Down Expand Up @@ -167,7 +167,7 @@ func TestFindSymbols_finds_function_root_and_global_enum_with_base_type_declarat
assert.Equal(t, &enum, symbols.Enums["Colors"])
}

func TestFindSymbols_finds_function_root_and_global_struct_declarations(t *testing.T) {
func TestExtractSymbols_finds_function_root_and_global_struct_declarations(t *testing.T) {
source := `struct MyStructure {
bool enabled;
char key;
Expand All @@ -184,12 +184,13 @@ func TestFindSymbols_finds_function_root_and_global_struct_declarations(t *testi
idx.NewStructMember("key", "char"),
},
"x",
idx.NewRange(0, 7, 0, 18),
)

assert.Equal(t, expectedStruct, symbols.Structs["MyStructure"])
}

func TestFindSymbols_finds_function_declaration_identifiers(t *testing.T) {
func TestExtractSymbols_finds_function_declaration_identifiers(t *testing.T) {
source := `fn void test() {
return 1;
}
Expand Down

0 comments on commit 796e8f6

Please sign in to comment.