Skip to content

Commit

Permalink
Index function return types.
Browse files Browse the repository at this point in the history
  • Loading branch information
pherrymason committed Jan 13, 2024
1 parent 3f59475 commit 2d356e7
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 48 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ Using tree-sitter grammar rules from https://github.com/zweimach/tree-sitter-c3
- [ ] Index Symbols
- [x] Variables & type
- [x] Functions
- [ ] Function arguments
- [ ] Function return type
- [ ] Function arguments
- [x] Function return type
- [x] Enums + Enumerators
- [x] Structs
- [ ] Struct members
Expand Down
46 changes: 25 additions & 21 deletions server/lsp/indexables/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,37 +10,37 @@ const (
)

type Function struct {
_type FunctionType
Name string
ReturnType string
DocumentURI string
identifierRange Range
documentRange Range
Kind protocol.CompletionItemKind
fType FunctionType
name string
returnType string

Variables map[string]Variable
Enums map[string]*Enum
Structs map[string]Struct
ChildrenFunctions map[string]*Function

BaseIndexable
}

func NewAnonymousScopeFunction(name string, docId string, docRange Range, kind protocol.CompletionItemKind) Function {
return newFunctionType(Anonymous, name, docId, Range{}, docRange, kind)
return newFunctionType(Anonymous, name, "", docId, Range{}, docRange, kind)
}

func NewFunction(name string, docId string, identifierRangePosition Range, docRange Range, kind protocol.CompletionItemKind) Function {
return newFunctionType(UserDefined, name, docId, identifierRangePosition, docRange, kind)
func NewFunction(name string, returnType string, docId string, idRange Range, docRange Range, kind protocol.CompletionItemKind) Function {
return newFunctionType(UserDefined, name, returnType, docId, idRange, docRange, kind)
}

func newFunctionType(fType FunctionType, name string, docId string, identifierRangePosition Range, docRange Range, kind protocol.CompletionItemKind) Function {
func newFunctionType(fType FunctionType, name string, returnType string, docId string, identifierRangePosition Range, docRange Range, kind protocol.CompletionItemKind) Function {
return Function{
_type: fType,
Name: name,
ReturnType: "??",
DocumentURI: docId,
identifierRange: identifierRangePosition,
documentRange: docRange,
Kind: kind,
fType: fType,
name: name,
returnType: returnType,
BaseIndexable: BaseIndexable{
documentURI: docId,
identifierRange: identifierRangePosition,
documentRange: docRange,
Kind: kind,
},
Variables: make(map[string]Variable),
Enums: make(map[string]*Enum),
Structs: make(map[string]Struct),
Expand All @@ -49,15 +49,19 @@ func newFunctionType(fType FunctionType, name string, docId string, identifierRa
}

func (f Function) GetName() string {
return f.Name
return f.name
}

func (f Function) GetReturnType() string {
return f.returnType
}

func (f Function) GetKind() protocol.CompletionItemKind {
return f.Kind
}

func (f Function) GetDocumentURI() string {
return f.DocumentURI
return f.documentURI
}

func (f Function) GetDeclarationRange() Range {
Expand All @@ -83,7 +87,7 @@ func (f *Function) AddEnum(enum *Enum) {
}

func (f Function) AddFunction(f2 *Function) {
f.ChildrenFunctions[f2.Name] = f2
f.ChildrenFunctions[f2.name] = f2
}

func (f Function) AddStruct(s Struct) {
Expand Down
4 changes: 2 additions & 2 deletions server/lsp/language.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (l *Language) FindHoverInformation(doc *Document, params *protocol.HoverPar
hover = protocol.Hover{
Contents: protocol.MarkupContent{
Kind: protocol.MarkupKindMarkdown,
Value: fmt.Sprintf("%s %s()", v.ReturnType, v.GetName()),
Value: fmt.Sprintf("%s %s()", v.GetReturnType(), v.GetName()),
},
}
case *indexables.Struct:
Expand Down Expand Up @@ -140,7 +140,7 @@ func findDeepFirst(identifier string, position protocol.Position, function *inde
return nil, depth
}

if identifier == function.Name {
if identifier == function.GetName() {
return function, depth
}

Expand Down
2 changes: 1 addition & 1 deletion server/lsp/language_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func TestLanguage_FindHoverInformationFromDifferentFile(t *testing.T) {
expectedHover := protocol.Hover{
Contents: protocol.MarkupContent{
Kind: protocol.MarkupKindMarkdown,
Value: fmt.Sprintf("?? importedMethod()"),
Value: fmt.Sprintf("void importedMethod()"),
},
}
assert.Equal(t, expectedHover, hover)
Expand Down
14 changes: 3 additions & 11 deletions server/lsp/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,8 @@ func (p *Parser) ExtractSymbols(doc *Document) idx.Function {
case "function_declaration":
identifier := idx.NewFunction(
content,
doc.URI,
idx.NewRangeFromSitterPositions(c.Node.StartPoint(), c.Node.EndPoint()),
idx.NewRangeFromSitterPositions(c.Node.StartPoint(), c.Node.EndPoint()),
protocol.CompletionItemKindFunction)
c.Node.Parent().ChildByFieldName("return_type").Content(sourceCode),
doc.URI, idx.NewRangeFromSitterPositions(c.Node.StartPoint(), c.Node.EndPoint()), idx.NewRangeFromSitterPositions(c.Node.StartPoint(), c.Node.EndPoint()), protocol.CompletionItemKindFunction)
functionsMap[content] = &identifier
scopeTree.AddFunction(&identifier)
}
Expand Down Expand Up @@ -265,13 +263,7 @@ func (p *Parser) FindFunctionDeclarations(doc *Document) []idx.Indexable {
c.Node.Parent().Type()
if _, exists := found[content]; !exists {
found[content] = true
identifier := idx.NewFunction(
content,
doc.URI,
//protocol.Position{c.Node.StartPoint().Row, c.Node.StartPoint().Column},
idx.NewRangeFromSitterPositions(c.Node.StartPoint(), c.Node.EndPoint()),
idx.NewRangeFromSitterPositions(c.Node.StartPoint(), c.Node.EndPoint()),
protocol.CompletionItemKindFunction)
identifier := idx.NewFunction(content, "", doc.URI, idx.NewRangeFromSitterPositions(c.Node.StartPoint(), c.Node.EndPoint()), idx.NewRangeFromSitterPositions(c.Node.StartPoint(), c.Node.EndPoint()), protocol.CompletionItemKindFunction)

identifiers = append(identifiers, identifier)
}
Expand Down
15 changes: 4 additions & 11 deletions server/lsp/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,22 +193,15 @@ func TestExtractSymbols_finds_function_declaration_identifiers(t *testing.T) {
source := `fn void test() {
return 1;
}
fn void test2(){
fn int test2(int number, char ch){
return 2;
}
`
}`
doc := NewDocumentFromString("x", source)
parser := createParser()
tree := parser.ExtractSymbols(&doc)

function1 := idx.NewFunction("test", "x",
idx.NewRange(0, 8, 0, 12),
idx.NewRange(0, 8, 2, 2),
protocol.CompletionItemKindFunction)
function2 := idx.NewFunction("test2", "x",
idx.NewRange(3, 9, 3, 14),
idx.NewRange(3, 9, 5, 2),
protocol.CompletionItemKindFunction)
function1 := idx.NewFunction("test", "void", "x", idx.NewRange(0, 8, 0, 12), idx.NewRange(0, 8, 2, 2), protocol.CompletionItemKindFunction)
function2 := idx.NewFunction("test2", "int", "x", idx.NewRange(3, 8, 3, 13), idx.NewRange(3, 8, 5, 2), protocol.CompletionItemKindFunction)

root := idx.NewAnonymousScopeFunction(
"main",
Expand Down

0 comments on commit 2d356e7

Please sign in to comment.