Skip to content

Commit

Permalink
Simplify function parsing. Fix function ranges indexing.
Browse files Browse the repository at this point in the history
  • Loading branch information
pherrymason committed Jan 13, 2024
1 parent ff491c9 commit 414e68c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 33 deletions.
5 changes: 1 addition & 4 deletions server/lsp/indexables/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type Function struct {
fType FunctionType
name string
returnType string
arguments []string // Used to list which variables are defined in function signature. They are fully defined in Variables

Variables map[string]Variable
Enums map[string]*Enum
Expand Down Expand Up @@ -72,10 +73,6 @@ func (f Function) GetDocumentRange() Range {
return f.documentRange
}

func (f *Function) SetEndRange(endPosition Position) {
f.documentRange.End = endPosition
}

func (f *Function) AddVariables(variables []Variable) {
for _, variable := range variables {
f.Variables[variable.Name] = variable
Expand Down
50 changes: 23 additions & 27 deletions server/lsp/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package lsp
//TSLanguage *tree_sitter_c3();
import "C"
import (
"fmt"
idx "github.com/pherrymason/c3-lsp/lsp/indexables"
sitter "github.com/smacker/go-tree-sitter"
"github.com/tliron/commonlog"
Expand All @@ -15,10 +14,7 @@ import (
const VarDeclarationQuery = `(var_declaration
name: (identifier) @variable_name
)`
const FunctionDeclarationQuery = `(function_declaration
name: (identifier) @function_name
body: (_) @body
)`
const FunctionDeclarationQuery = `(function_declaration) @function_dec`
const EnumDeclaration = `(enum_declaration) @enum_dec`
const StructDeclaration = `(struct_declaration) @struct_dec`

Expand Down Expand Up @@ -68,7 +64,7 @@ func (p *Parser) ExtractSymbols(doc *Document) idx.Function {
qc.Exec(q, doc.parsedTree.RootNode())
sourceCode := []byte(doc.Content)

functionsMap := make(map[string]*idx.Function)
//functionsMap := make(map[string]*idx.Function)
scopeTree := idx.NewAnonymousScopeFunction(
"main",
doc.URI,
Expand All @@ -94,35 +90,17 @@ func (p *Parser) ExtractSymbols(doc *Document) idx.Function {
scopeTree.AddVariables([]idx.Variable{
variable,
})
case "function_declaration":
identifier := idx.NewFunction(
content,
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)
}
} else if nodeType == "function_declaration" {
function := p.nodeToFunction(doc, c.Node, sourceCode)
scopeTree.AddFunction(&function)
} else if nodeType == "enum_declaration" {
enum := p.nodeToEnum(doc, c.Node, sourceCode)
scopeTree.AddEnum(&enum)
} else if nodeType == "struct_declaration" {
_struct := p.nodeToStruct(doc, c.Node, sourceCode)
scopeTree.AddStruct(_struct)
} else if nodeType == "compound_statement" {
variables := p.FindVariableDeclarations(doc, c.Node)

// TODO Previous node has the info about which function is belongs to.
idNode := c.Node.Parent().ChildByFieldName("name")
functionName := idNode.Content(sourceCode)

function, ok := functionsMap[functionName]
if !ok {
panic(fmt.Sprint("Could not find definition for ", functionName))
}
function.SetEndRange(idx.NewPositionFromSitterPoint(c.Node.EndPoint()))
function.AddVariables(variables)
}
//fmt.Println(c.Node.String(), content)
}
}

Expand All @@ -144,6 +122,24 @@ func (p *Parser) nodeToVariable(doc *Document, node *sitter.Node, sourceCode []b
return variable
}

func (p *Parser) nodeToFunction(doc *Document, node *sitter.Node, sourceCode []byte) idx.Function {

nameNode := node.ChildByFieldName("name")
symbol := idx.NewFunction(
nameNode.Content(sourceCode),
node.ChildByFieldName("return_type").Content(sourceCode),
doc.URI,
idx.NewRangeFromSitterPositions(nameNode.StartPoint(), nameNode.EndPoint()), idx.NewRangeFromSitterPositions(node.StartPoint(), node.EndPoint()), protocol.CompletionItemKindFunction)

variables := p.FindVariableDeclarations(doc, node)

// TODO Previous node has the info about which function is belongs to.

symbol.AddVariables(variables)

return symbol
}

func (p *Parser) nodeToStruct(doc *Document, node *sitter.Node, sourceCode []byte) idx.Struct {
nameNode := node.Child(1)
name := nameNode.Content(sourceCode)
Expand Down
4 changes: 2 additions & 2 deletions server/lsp/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,8 @@ func TestExtractSymbols_finds_function_declaration_identifiers(t *testing.T) {
parser := createParser()
tree := parser.ExtractSymbols(&doc)

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)
function1 := idx.NewFunction("test", "void", "x", idx.NewRange(0, 8, 0, 12), idx.NewRange(0, 0, 2, 2), protocol.CompletionItemKindFunction)
function2 := idx.NewFunction("test2", "int", "x", idx.NewRange(3, 8, 3, 13), idx.NewRange(3, 1, 5, 2), protocol.CompletionItemKindFunction)

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

0 comments on commit 414e68c

Please sign in to comment.