Skip to content

Commit

Permalink
Port parsing doc comments from 0.4.0.
Browse files Browse the repository at this point in the history
  • Loading branch information
pherrymason committed Jan 28, 2025
1 parent 6421c1d commit 6a44383
Show file tree
Hide file tree
Showing 7 changed files with 347 additions and 143 deletions.
9 changes: 7 additions & 2 deletions server/internal/lsp/analysis/hover.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func GetHoverInfo(document *document.Document, pos lsp.Position, storage *docume

symbol := symbolResult.Get()
var description string
var sizeInfo string
var sizeInfo string // TODO reimplement this
var extraLine string
switch symbol.Kind {
case ast.VAR, ast.CONST:
Expand All @@ -28,7 +28,7 @@ func GetHoverInfo(document *document.Document, pos lsp.Position, storage *docume

case ast.FIELD:
switch symbol.Type.NodeDecl.(type) {
case ast.TypeInfo:
case *ast.TypeInfo:
description = fmt.Sprintf("%s %s", symbol.Type.Name, symbol.Name)
}

Expand All @@ -47,6 +47,11 @@ func GetHoverInfo(document *document.Document, pos lsp.Position, storage *docume
)
}

docComment := symbol.NodeDecl.GetDocComment()
if docComment.IsSome() {
extraLine += "\n\n" + docComment.Get().DisplayBodyWithContracts()
}

isModule := false
if !isModule {
extraLine += "\n\nIn module **[" + symbol.Module.String() + "]**"
Expand Down
36 changes: 11 additions & 25 deletions server/internal/lsp/ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ type Node interface {
EndPosition() lsp.Position
GetRange() lsp.Range
GetId() NodeId
SetDocComment(docComment *DocComment)
GetDocComment() option.Option[*DocComment]
}

type Expression interface {
Expand Down Expand Up @@ -87,12 +89,21 @@ type NodeAttributes struct {
Range lsp.Range
Attributes []string
Id NodeId
DocComment option.Option[*DocComment]
}

func (n NodeAttributes) StartPosition() lsp.Position { return n.Range.Start }
func (n NodeAttributes) EndPosition() lsp.Position { return n.Range.End }
func (n NodeAttributes) GetRange() lsp.Range { return n.Range }
func (n NodeAttributes) GetId() NodeId { return n.Id }
func (n *NodeAttributes) SetDocComment(docComment *DocComment) {
if docComment != nil {
n.DocComment = option.Some(docComment)
} else {
n.DocComment = option.None[*DocComment]()
}
}
func (n *NodeAttributes) GetDocComment() option.Option[*DocComment] { return n.DocComment }

func ChangeNodePosition(n *NodeAttributes, start sitter.Point, end sitter.Point) {
n.Range.Start = lsp.Position{Line: uint(start.Row), Column: uint(start.Column)}
Expand Down Expand Up @@ -145,38 +156,13 @@ type Import struct {

func (*Import) stmtNode() {}

// Deprecated: using GenDecl for enums
type EnumProperty struct {
NodeAttributes
Type TypeInfo
Name Ident
}

// EnumMember
// Deprecated: using GenDecl for enums
type EnumMember struct {
NodeAttributes
Name Ident
Value CompositeLiteral
}

// Deprecated not used
type PropertyValue struct {
NodeAttributes
Name string
Value Expression
}

// StructMemberDecl
// Deprecated
type StructMemberDecl struct {
NodeAttributes
Names []Ident
Type TypeInfo
BitRange option.Option[[2]uint]
IsInlined bool
}

type FaultMember struct {
NodeAttributes
Name *Ident
Expand Down
33 changes: 22 additions & 11 deletions server/internal/lsp/ast/ast_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ast

import (
"github.com/pherrymason/c3-lsp/internal/lsp"
"github.com/pherrymason/c3-lsp/pkg/option"
sitter "github.com/smacker/go-tree-sitter"
)

Expand Down Expand Up @@ -46,9 +47,9 @@ func (d *NodeAttrsBuilder) WithSitterStartEnd(start sitter.Point, end sitter.Poi
return d
}

func (i *NodeAttrsBuilder) WithSitterPos(node *sitter.Node) *NodeAttrsBuilder {
i.WithSitterStartEnd(node.StartPoint(), node.EndPoint())
return i
func (d *NodeAttrsBuilder) WithSitterPos(node *sitter.Node) *NodeAttrsBuilder {
d.WithSitterStartEnd(node.StartPoint(), node.EndPoint())
return d
}

func (d *NodeAttrsBuilder) WithRangePositions(startRow uint, startCol uint, endRow uint, endCol uint) *NodeAttrsBuilder {
Expand All @@ -62,6 +63,14 @@ func (d *NodeAttrsBuilder) WithRange(aRange lsp.Range) *NodeAttrsBuilder {
return d
}

func (d *NodeAttrsBuilder) WithDocComment(docComment string) *NodeAttrsBuilder {
d.bn.DocComment = option.Some(&DocComment{
body: docComment,
contracts: []*DocCommentContract{},
})
return d
}

// --
// IdentifierBuilder
// --
Expand Down Expand Up @@ -203,13 +212,15 @@ func (b *TypeInfoBuilder) Build() *TypeInfo {
// DefDeclBuilder
// --
type DefDeclBuilder struct {
d DefDecl
a NodeAttrsBuilder
def DefDecl
a NodeAttrsBuilder
}

func NewDefDeclBuilder(nodeId NodeId) *DefDeclBuilder {
return &DefDeclBuilder{
d: DefDecl{},
def: DefDecl{
Ident: NewIdentifierBuilder().Build(),
},
a: *NewNodeAttributesBuilder(),
}
}
Expand All @@ -220,23 +231,23 @@ func (b *DefDeclBuilder) WithSitterPos(node *sitter.Node) *DefDeclBuilder {
}

func (b *DefDeclBuilder) WithName(name string) *DefDeclBuilder {
b.d.Name.Name = name
b.def.Ident.Name = name
return b
}
func (b *DefDeclBuilder) WithIdentifierSitterPos(node *sitter.Node) *DefDeclBuilder {
b.d.Name.Range.Start = lsp.Position{uint(node.StartPoint().Row), uint(node.StartPoint().Column)}
b.d.Name.Range.End = lsp.Position{uint(node.EndPoint().Row), uint(node.EndPoint().Column)}
b.def.Ident.Range.Start = lsp.Position{uint(node.StartPoint().Row), uint(node.StartPoint().Column)}
b.def.Ident.Range.End = lsp.Position{uint(node.EndPoint().Row), uint(node.EndPoint().Column)}

return b
}

func (b *DefDeclBuilder) WithExpression(expression Expression) *DefDeclBuilder {
b.d.Expr = expression
b.def.Expr = expression
return b
}

func (b *DefDeclBuilder) Build() DefDecl {
def := b.d
def := b.def
def.NodeAttributes = b.a.Build()

return def
Expand Down
51 changes: 51 additions & 0 deletions server/internal/lsp/ast/doc_comments.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package ast

type DocCommentContract struct {
name string
body string
}

type DocComment struct {
body string
contracts []*DocCommentContract
}

// Creates a doc comment with the given body.
func NewDocComment(body string) *DocComment {
return &DocComment{
body: body,
contracts: []*DocCommentContract{},
}
}

// Creates a contract with the given name and body.
// It is expected that the name begins with '@'.
func NewDocCommentContract(name string, body string) *DocCommentContract {
return &DocCommentContract{
name,
body,
}
}

// Add contracts to the given doc comment.
func (d *DocComment) AddContracts(contracts []*DocCommentContract) {
d.contracts = append(d.contracts, contracts...)
}

func (d *DocComment) GetBody() string {
return d.body
}

// Return a string displaying the body and contracts as markdown.
func (d *DocComment) DisplayBodyWithContracts() string {
out := d.body

for _, c := range d.contracts {
out += "\n\n**" + c.name + "**"
if c.body != "" {
out += " " + c.body
}
}

return out
}
Loading

0 comments on commit 6a44383

Please sign in to comment.