Skip to content

Commit

Permalink
Simplify enum parsing. Leave it ready to parse optional base type.
Browse files Browse the repository at this point in the history
  • Loading branch information
pherrymason committed Jan 11, 2024
1 parent f720ce1 commit f6866b7
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 49 deletions.
20 changes: 2 additions & 18 deletions server/lsp/indexables/enum.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,20 @@ package indexables

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

type Enumerator struct {
name string
value string
BaseIndexable
}

func NewEnumerator(name string, value string, identifierPosition protocol.Range) Enumerator {
return Enumerator{
name: name,
value: value,
BaseIndexable: BaseIndexable{
identifierRange: identifierPosition,
Kind: protocol.CompletionItemKindEnumMember,
},
}
}

type Enum struct {
name string
baseType string
enumerators []Enumerator
BaseIndexable
}

func NewEnum(name string, baseType string, enumerators []Enumerator, identifierRangePosition protocol.Range, documentRangePosition protocol.Range) Enum {
func NewEnum(name string, baseType string, enumerators []Enumerator, identifierRangePosition protocol.Range, documentRangePosition protocol.Range, docId protocol.DocumentUri) Enum {
return Enum{
name: name,
baseType: baseType,
enumerators: enumerators,
BaseIndexable: BaseIndexable{
documentURI: docId,
identifierRange: identifierRangePosition,
documentRange: documentRangePosition,
Kind: protocol.CompletionItemKindEnum,
Expand Down
40 changes: 40 additions & 0 deletions server/lsp/indexables/enumerator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package indexables

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

type Enumerator struct {
name string
value string
BaseIndexable
}

func NewEnumerator(name string, value string, identifierPosition protocol.Range) Enumerator {
return Enumerator{
name: name,
value: value,
BaseIndexable: BaseIndexable{
identifierRange: identifierPosition,
Kind: protocol.CompletionItemKindEnumMember,
},
}
}

func (e Enumerator) GetName() string {
return e.name
}

func (e Enumerator) GetKind() protocol.CompletionItemKind {
return e.Kind
}

func (e Enumerator) GetDocumentURI() protocol.DocumentUri {
return e.documentURI
}

func (e Enumerator) GetDeclarationRange() protocol.Range {
return e.identifierRange
}

func (e Enumerator) GetDocumentRange() protocol.Range {
return e.documentRange
}
9 changes: 9 additions & 0 deletions server/lsp/indexables/indexable.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,12 @@ type BaseIndexable struct {
documentRange protocol.Range
Kind protocol.CompletionItemKind
}

func NewBaseIndexable(docId protocol.DocumentUri, idRange protocol.Range, docRange protocol.Range, kind protocol.CompletionItemKind) BaseIndexable {
return BaseIndexable{
documentURI: docId,
identifierRange: idRange,
documentRange: docRange,
Kind: kind,
}
}
73 changes: 43 additions & 30 deletions server/lsp/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ const FunctionDeclarationQuery = `(function_declaration
name: (identifier) @function_name
body: (_) @body
)`
const EnumDeclaration = `(enum_declaration
name: (type_identifier) @enum_name
body: (_) @body
)`
const EnumDeclaration = `(enum_declaration) @enum_dec`

// name: (type_identifier) @enum_name
// body: (_) @body
//)`

func getParser() *sitter.Parser {
parser := sitter.NewParser()
Expand Down Expand Up @@ -74,7 +75,7 @@ func FindSymbols(doc *Document) indexables.Function {
protocol.CompletionItemKindModule, // Best value found
)

var tempEnum *indexables.Enum
//var tempEnum *indexables.Enum

for {
m, ok := qc.NextMatch()
Expand Down Expand Up @@ -102,26 +103,9 @@ func FindSymbols(doc *Document) indexables.Function {
functionsMap[content] = &identifier
scopeTree.AddFunction(&identifier)
}
} else if c.Node.Parent().Type() == "enum_declaration" {
if nodeType == "enumerators" {
enumerators := []indexables.Enumerator{}
for i := uint32(0); i < c.Node.ChildCount(); i++ {
enumeratorNode := c.Node.Child(int(i))
if enumeratorNode.Type() == "enumerator" {
enumerators = append(
enumerators,
indexables.NewEnumerator(enumeratorNode.Child(0).Content(sourceCode), "",
treeSitterPoints2Range(enumeratorNode.StartPoint(), enumeratorNode.EndPoint()),
),
)
}
}
tempEnum.AddEnumerators(enumerators)
} else if nodeType == "type_identifier" {
enum := nodeToEnum(doc, c.Node, sourceCode, content)
scopeTree.AddEnum(&enum)
tempEnum = &enum
}
} else if c.Node.Type() == "enum_declaration" {
enum := nodeToEnum(doc, c.Node, sourceCode)
scopeTree.AddEnum(&enum)
} else if nodeType == "compound_statement" {
variables := FindVariableDeclarations(doc, c.Node)

Expand Down Expand Up @@ -158,14 +142,43 @@ func nodeToVariable(doc *Document, node *sitter.Node, sourceCode []byte, content
return variable
}

func nodeToEnum(doc *Document, node *sitter.Node, sourceCode []byte, content string) indexables.Enum {
func nodeToEnum(doc *Document, node *sitter.Node, sourceCode []byte) indexables.Enum {
nodesCount := node.ChildCount()
nameNode := node.Child(1)

baseType := ""
bodyIndex := 3
if nodesCount == 4 {
// Enum without base_type
} else {
// Enum with base_type
baseType = "?"
bodyIndex = 4
}

enumeratorsNode := node.Child(bodyIndex)
enumerators := []indexables.Enumerator{}
for i := uint32(0); i < enumeratorsNode.ChildCount(); i++ {
enumeratorNode := enumeratorsNode.Child(int(i))
if enumeratorNode.Type() == "enumerator" {
enumerators = append(
enumerators,
indexables.NewEnumerator(
enumeratorNode.Child(0).Content(sourceCode),
"",
treeSitterPoints2Range(enumeratorNode.StartPoint(), enumeratorNode.EndPoint()),
),
)
}
}

enum := indexables.NewEnum(
content,
"",
[]indexables.Enumerator{},
NewRange(0, 0, 0, 0),
nameNode.Content(sourceCode),
baseType,
enumerators,
treeSitterPoints2Range(nameNode.StartPoint(), nameNode.EndPoint()),
treeSitterPoints2Range(node.StartPoint(), node.EndPoint()),
doc.URI,
)

return enum
Expand Down
3 changes: 2 additions & 1 deletion server/lsp/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,9 @@ func TestFindSymbols_finds_function_root_and_global_enum_declarations(t *testing
indexables.NewEnumerator("BLUE", "", NewRange(0, 21, 0, 25)),
indexables.NewEnumerator("GREEN", "", NewRange(0, 27, 0, 32)),
},
NewRange(0, 0, 0, 0),
NewRange(0, 5, 0, 11),
NewRange(0, 0, 0, 34),
"x",
)
expectedRoot.AddEnum(&enum)
assert.Equal(t, &enum, symbols.Enums["Colors"])
Expand Down

0 comments on commit f6866b7

Please sign in to comment.