Skip to content

Commit

Permalink
Update parser to be compatible with def
Browse files Browse the repository at this point in the history
Index simple defs identifiers.
  • Loading branch information
pherrymason committed Jan 14, 2024
1 parent 5ee17d3 commit 9640d90
Show file tree
Hide file tree
Showing 11 changed files with 34,929 additions and 19,885 deletions.
15 changes: 10 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
init:
$(MAKE) clone-tree-sitter
cp tree-sitter-c3/src/parser.c server/c3/parser.c
cp -r tree-sitter-c3/src/tree_sitter server/c3/tree_sitter

$(MAKE) copy-parser

clone-tree-sitter:
[ ! -d "tree-sitter-c3" ] && git clone [email protected]:zweimach/tree-sitter-c3.git tree-sitter-c3 || true


#build-parser:
# cp yacc-2-treesitter/grammar.js ./grammar.js
# tree-sitter generate
build-parser:
cd tree-sitter-c3 && tree-sitter generate
$(MAKE) copy-parser
#cp yacc-2-treesitter/grammar.js ./grammar.js
#tree-sitter generate

copy-parser:
cp -r tree-sitter-c3/src/tree_sitter server/lsp/tree_sitter
cp tree-sitter-c3/src/parser.c server/lsp/parser.c

build-dev:
cd server && go build -gcflags="all=-N -l" -o c3-lsp
Expand Down
11 changes: 10 additions & 1 deletion server/lsp/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func assertSameFunction(t *testing.T, expected *idx.Function, actual *idx.Functi
assert.Equal(t, expected.Enums, actual.Enums, expected.GetName())
assert.Equal(t, expected.Structs, actual.Structs, expected.GetName())

assert.Equal(t, keys(expected.ChildrenFunctions), keys(actual.ChildrenFunctions))
assert.Equal(t, Keys(expected.ChildrenFunctions), Keys(actual.ChildrenFunctions))
for key, value := range expected.ChildrenFunctions {
assertSameFunction(t, value, actual.ChildrenFunctions[key])
}
Expand All @@ -48,3 +48,12 @@ func createStruct(docId string, name string, members []idx.StructMember, idRange
idRange,
)
}

func assertSameVariable(t *testing.T, expected idx.Variable, actual idx.Variable) {
assert.Equal(t, expected.GetName(), actual.GetName())
assert.Equal(t, expected.GetType(), actual.GetType(), expected.GetName())
assert.Equal(t, expected.GetDocumentURI(), actual.GetDocumentURI(), expected.GetName())
assertSameRange(t, expected.GetDeclarationRange(), actual.GetDeclarationRange(), fmt.Sprint("Variable declaration range:", expected.GetName()))
assertSameRange(t, expected.GetDocumentRange(), actual.GetDocumentRange(), fmt.Sprint("Variable document range:", expected.GetName()))
assert.Equal(t, expected.GetKind(), actual.GetKind(), expected.GetName())
}
33 changes: 33 additions & 0 deletions server/lsp/indexables/DefBuilder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package indexables

import protocol "github.com/tliron/glsp/protocol_3_16"

type DefBuilder struct {
def Def
}

func NewDefBuilder(name string, docId string) *DefBuilder {
return &DefBuilder{
def: Def{
name: name,
BaseIndexable: BaseIndexable{
documentURI: docId,
Kind: protocol.CompletionItemKindTypeParameter,
},
},
}
}

func (d *DefBuilder) WithIdentifierRange(lineStart uint, CharStart uint, lineEnd uint, CharEnd uint) *DefBuilder {
d.def.BaseIndexable.identifierRange = NewRange(lineStart, CharStart, lineEnd, CharEnd)
return d
}

func (d *DefBuilder) WithDocumentRange(lineStart uint, CharStart uint, lineEnd uint, CharEnd uint) *DefBuilder {
d.def.BaseIndexable.documentRange = NewRange(lineStart, CharStart, lineEnd, CharEnd)
return d
}

func (d *DefBuilder) Build() Def {
return d.def
}
47 changes: 47 additions & 0 deletions server/lsp/indexables/def.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package indexables

import (
"fmt"
protocol "github.com/tliron/glsp/protocol_3_16"
)

type Def struct {
name string
BaseIndexable
}

func NewDef(name string, docId string, idRange Range, docRange Range) Def {
return Def{
name: name,
BaseIndexable: BaseIndexable{
documentURI: docId,
identifierRange: idRange,
documentRange: docRange,
Kind: protocol.CompletionItemKindTypeParameter,
},
}
}

func (d Def) GetName() string {
return d.name
}

func (d Def) GetKind() protocol.CompletionItemKind {
return d.Kind
}

func (d Def) GetDocumentURI() string {
return d.documentURI
}

func (d Def) GetDeclarationRange() Range {
return d.identifierRange
}

func (d Def) GetDocumentRange() Range {
return d.documentRange
}

func (d Def) GetHoverInfo() string {
return fmt.Sprintf("def %s = ???", d.name)
}
6 changes: 6 additions & 0 deletions server/lsp/indexables/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type Function struct {
Variables map[string]Variable
Enums map[string]*Enum
Structs map[string]Struct
Defs map[string]Def
ChildrenFunctions map[string]*Function

BaseIndexable
Expand Down Expand Up @@ -49,6 +50,7 @@ func newFunctionType(fType FunctionType, name string, returnType string, argumen
Variables: make(map[string]Variable),
Enums: make(map[string]*Enum),
Structs: make(map[string]Struct),
Defs: make(map[string]Def),
ChildrenFunctions: make(map[string]*Function),
}
}
Expand Down Expand Up @@ -106,3 +108,7 @@ func (f Function) AddStruct(s Struct) {
func (f Function) GetHoverInfo() string {
return fmt.Sprintf("%s %s()", f.GetReturnType(), f.GetName())
}

func (f Function) AddDef(def Def) {
f.Defs[def.GetName()] = def
}
8 changes: 4 additions & 4 deletions server/lsp/indexables/variableBuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ func NewVariableBuilder(name string, variableType string, docId string) *Variabl
}
}

func (vb *VariableBuilder) WithDocumentRange(lineStart uint, CharStart uint, lineEnd uint, CharEnd uint) *VariableBuilder {
vb.variable.BaseIndexable.documentRange = NewRange(lineStart, CharStart, lineEnd, CharEnd)
func (vb *VariableBuilder) WithIdentifierRange(lineStart uint, CharStart uint, lineEnd uint, CharEnd uint) *VariableBuilder {
vb.variable.BaseIndexable.identifierRange = NewRange(lineStart, CharStart, lineEnd, CharEnd)
return vb
}

func (vb *VariableBuilder) WithIdentifierRange(lineStart uint, CharStart uint, lineEnd uint, CharEnd uint) *VariableBuilder {
vb.variable.BaseIndexable.identifierRange = NewRange(lineStart, CharStart, lineEnd, CharEnd)
func (vb *VariableBuilder) WithDocumentRange(lineStart uint, CharStart uint, lineEnd uint, CharEnd uint) *VariableBuilder {
vb.variable.BaseIndexable.documentRange = NewRange(lineStart, CharStart, lineEnd, CharEnd)
return vb
}

Expand Down
Loading

0 comments on commit 9640d90

Please sign in to comment.