Skip to content

Commit

Permalink
Implement find declaration of def.
Browse files Browse the repository at this point in the history
  • Loading branch information
pherrymason committed Jan 14, 2024
1 parent ac55944 commit aff63ee
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 18 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Using tree-sitter grammar rules from https://github.com/zweimach/tree-sitter-c3
- [x] Struct members
- [ ] Struct methods
- [ ] imports
- [~] defines
- [ ] macros: **Needs to update grammar.js**
- [ ] Hover information
- [x] Variable declarations
Expand Down
38 changes: 20 additions & 18 deletions server/lsp/language.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,41 +128,43 @@ func findDeepFirst(identifier string, position protocol.Position, function *inde
return function, depth
}

variable, foundVariableInThisScope := function.Variables[identifier]
enum, foundEnumInThisScope := function.Enums[identifier]
var enumerator indexables.Enumerator
foundEnumeratorInThisScope := false

for _, scopedEnums := range function.Enums {
if scopedEnums.HasEnumerator(identifier) {
enumerator = scopedEnums.GetEnumerator(identifier)
foundEnumeratorInThisScope = true
}
}

_struct, foundStructInThisScope := function.Structs[identifier]

for _, child := range function.ChildrenFunctions {
if result, resultDepth := findDeepFirst(identifier, position, child, depth+1, mode); result != nil {
return result, resultDepth
}
}

variable, foundVariableInThisScope := function.Variables[identifier]
if foundVariableInThisScope {
return variable, depth
}

if foundEnumeratorInThisScope {
return enumerator, depth
}

enum, foundEnumInThisScope := function.Enums[identifier]
if foundEnumInThisScope {
return enum, depth
}

var enumerator indexables.Enumerator
foundEnumeratorInThisScope := false
for _, scopedEnums := range function.Enums {
if scopedEnums.HasEnumerator(identifier) {
enumerator = scopedEnums.GetEnumerator(identifier)
foundEnumeratorInThisScope = true
}
}
if foundEnumeratorInThisScope {
return enumerator, depth
}

_struct, foundStructInThisScope := function.Structs[identifier]
if foundStructInThisScope {
return _struct, depth
}

def, foundDefInScope := function.Defs[identifier]
if foundDefInScope {
return def, depth
}

return nil, depth
}
11 changes: 11 additions & 0 deletions server/lsp/language_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,17 @@ func TestLanguage_FindSymbolDeclarationInWorkspace_symbol_same_scope(t *testing.
},
idx.NewRange(0, 7, 0, 18)),
},
{
"def",
"def Kilo = int;Kilo value = 3;",
"Kilo",
0, 17,
idx.NewDefBuilder("Kilo", "x").
WithResolvesTo("int").
WithIdentifierRange(0, 4, 0, 8).
WithDocumentRange(0, 0, 0, 15).
Build(),
},
}

for _, tt := range cases {
Expand Down

0 comments on commit aff63ee

Please sign in to comment.