Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stdlib docs #118

Merged
merged 5 commits into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 79 additions & 2 deletions server/cmd/stdlib_indexer/blurp.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package main

import (
"strings"

"github.com/dave/jennifer/jen"
"github.com/pherrymason/c3-lsp/pkg/cast"
"github.com/pherrymason/c3-lsp/pkg/symbols"
Expand Down Expand Up @@ -34,6 +36,12 @@ func Generate_variable(variable *s.Variable, module *s.Module) jen.Code {
)
}

if variable.GetDocComment() != nil {
varDef.
Dot("WithDocs").
Call(jen.Lit(variable.GetDocComment().GetBody()))
}

varDef.
Dot("Build").
Call()
Expand All @@ -60,6 +68,12 @@ func Generate_struct(strukt *s.Struct, module *s.Module) jen.Code {
)
}

if strukt.GetDocComment() != nil {
def.
Dot("WithDocs").
Call(jen.Lit(strukt.GetDocComment().GetBody()))
}

def.
Dot("WithoutSourceCode").Call().
Dot("Build").Call()
Expand Down Expand Up @@ -87,6 +101,12 @@ func Generate_bitstruct(bitstruct *s.Bitstruct, module *s.Module) jen.Code {
)
}

if bitstruct.GetDocComment() != nil {
def.
Dot("WithDocs").
Call(jen.Lit(bitstruct.GetDocComment().GetBody()))
}

def.
Dot("WithoutSourceCode").Call().
Dot("Build").Call()
Expand Down Expand Up @@ -117,6 +137,12 @@ func Generate_definition(def *s.Def, module *s.Module) jen.Code {
)
}

if def.GetDocComment() != nil {
defDef.
Dot("WithDocs").
Call(jen.Lit(def.GetDocComment().GetBody()))
}

defDef.
Dot("WithoutSourceCode").Call().
Dot("Build").Call()
Expand All @@ -139,7 +165,15 @@ func Generate_distinct(distinct *s.Distinct, module *s.Module) jen.Code {
Dot("WithBaseType").
Call(
Generate_type(distinct.GetBaseType(), module.GetName()),
).
)

if distinct.GetDocComment() != nil {
distinctDef.
Dot("WithDocs").
Call(jen.Lit(distinct.GetDocComment().GetBody()))
}

distinctDef.
Dot("WithoutSourceCode").Call().
Dot("Build").Call()

Expand Down Expand Up @@ -188,6 +222,12 @@ func Generate_enum(enum *s.Enum, module *s.Module) jen.Code {
)
}

if enum.GetDocComment() != nil {
enumDef.
Dot("WithDocs").
Call(jen.Lit(enum.GetDocComment().GetBody()))
}

enumDef.Dot("Build").Call()

return enumDef
Expand Down Expand Up @@ -218,6 +258,12 @@ func Generate_fault(fault *s.Fault, module *s.Module) jen.Code {
)
}

if fault.GetDocComment() != nil {
faultDef.
Dot("WithDocs").
Call(jen.Lit(fault.GetDocComment().GetBody()))
}

faultDef.Dot("Build").Call()

return faultDef
Expand Down Expand Up @@ -257,6 +303,10 @@ func Generate_function(fun *s.Function, mod *s.Module) jen.Code {
funDef.Dot("IsMacro").Call()
}

if fun.GetDocComment() != nil {
funDef.Dot("WithDocs").Call(Generate_doc_comment(fun.GetDocComment()))
}

funDef.
Dot("WithoutSourceCode").Call().
Dot("Build").Call()
Expand All @@ -280,10 +330,12 @@ func Generate_type(type_ *s.Type, mod string) *jen.Statement {
typeModule = mod
}

ptrs := strings.Repeat("*", type_.GetPointerCount())

typeDef := jen.
Qual(PackageName+"symbols", builderName).
Call(
jen.Lit(type_.String()),
jen.Lit(type_.GetName()+ptrs),
jen.Lit(typeModule),
)

Expand Down Expand Up @@ -325,3 +377,28 @@ func Generate_type(type_ *s.Type, mod string) *jen.Statement {

return typeDef
}

func Generate_doc_comment(docComment *s.DocComment) *jen.Statement {
docDef := jen.
Qual(PackageName+"symbols", "NewDocCommentBuilder").
Call(
jen.Lit(docComment.GetBody()),
)

if docComment.HasContracts() {
for _, contract := range docComment.GetContracts() {
docDef.
Dot("WithContract").
Call(
jen.Lit(contract.GetName()),
jen.Lit(contract.GetBody()),
)
}
}

docDef.
Dot("Build").
Call()

return docDef
}
15 changes: 11 additions & 4 deletions server/cmd/stdlib_indexer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,23 @@ func generateCode(symbolsTable *symbols_table.SymbolsTable, c3Version string) {
if !ok {
uniqueModuleNames[mod.GetName()] = true

dict[jen.Lit(mod.GetName())] =
modDef :=
jen.
Qual(PackageName+"symbols", "NewModuleBuilder").
Call(
jen.Lit(mod.GetName()),
jen.Lit(mod.GetDocumentURI()),
).
Dot("WithoutSourceCode").Call().
Dot("Build").Call()
)

if mod.GetDocComment() != nil {
modDef.Dot("WithDocs").Call(Generate_doc_comment(mod.GetDocComment()))
}

modDef.
Dot("WithoutSourceCode").Call().
Dot("Build").Call()

dict[jen.Lit(mod.GetName())] = modDef
}
}
}
Expand Down
326 changes: 163 additions & 163 deletions server/internal/lsp/stdlib/v064.go

Large diffs are not rendered by default.

344 changes: 172 additions & 172 deletions server/internal/lsp/stdlib/v065.go

Large diffs are not rendered by default.

354 changes: 177 additions & 177 deletions server/internal/lsp/stdlib/v066.go

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion server/pkg/parser/node_to_function.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ func (p *Parser) nodeToArgument(argNode *sitter.Node, methodIdentifier string, c

// if identifier is empty (unnamed argument), then use generic $arg{parameterIndex} name
if len(identifier) == 0 {
identifier = fmt.Sprintf("$arg%d", parameterIndex)
identifier = fmt.Sprintf("$arg#%d", parameterIndex)
}

variable := idx.NewVariable(
Expand Down
39 changes: 29 additions & 10 deletions server/pkg/parser/parser_functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,16 +98,16 @@ func TestExtractSymbols_Functions_Declaration(t *testing.T) {
assert.True(t, fn.IsSome(), "Function was not found")
assert.Equal(t, "init_window", fn.Get().GetName(), "Function name")

arg0 := fn.Get().Variables["$arg0"]
assert.Equal(t, "$arg0", arg0.GetName())
arg0 := fn.Get().Variables["$arg#0"]
assert.Equal(t, "$arg#0", arg0.GetName())
assert.Equal(t, "int", arg0.GetType().String())

arg1 := fn.Get().Variables["$arg1"]
assert.Equal(t, "$arg1", arg1.GetName())
arg1 := fn.Get().Variables["$arg#1"]
assert.Equal(t, "$arg#1", arg1.GetName())
assert.Equal(t, "int", arg1.GetType().String())

arg2 := fn.Get().Variables["$arg2"]
assert.Equal(t, "$arg2", arg2.GetName())
arg2 := fn.Get().Variables["$arg#2"]
assert.Equal(t, "$arg#2", arg2.GetName())
assert.Equal(t, "char*", arg2.GetType().String())
})

Expand All @@ -131,8 +131,8 @@ func TestExtractSymbols_Functions_Declaration(t *testing.T) {
assert.Equal(t, "height", arg1.GetName())
assert.Equal(t, "int", arg1.GetType().String())

arg2 := fn.Get().Variables["$arg2"]
assert.Equal(t, "$arg2", arg2.GetName())
arg2 := fn.Get().Variables["$arg#2"]
assert.Equal(t, "$arg#2", arg2.GetName())
assert.Equal(t, "char*", arg2.GetType().String())
})
}
Expand Down Expand Up @@ -287,6 +287,25 @@ Hello world.
assert.Equal(t, idx.NewRange(0, 44, 0, 51), variable.GetIdRange())
assert.Equal(t, idx.NewRange(0, 39, 0, 51), variable.GetDocumentRange())
})

t.Run("Finds function with empty argument names", func(t *testing.T) {
source := `<* func *>
fn void test(int, char, int*, ...);`
docId := "docId"
doc := document.NewDocument(docId, source)
parser := createParser()
symbols, _ := parser.ParseSymbols(&doc)

fn := symbols.Get("docid").GetChildrenFunctionByName("test")
assert.True(t, fn.IsSome(), "Function was not found")
assert.Equal(t, "fn void test(int, char, int*, ...)", fn.Get().GetHoverInfo(), "Function signature")
assert.Equal(t, "test", fn.Get().GetName(), "Function name")
assert.Equal(t, "void", fn.Get().GetReturnType().GetName(), "Return type")
assert.Equal(t, idx.NewRange(1, 10, 1, 14), fn.Get().GetIdRange())
assert.Equal(t, idx.NewRange(1, 2, 1, 37), fn.Get().GetDocumentRange())
assert.Equal(t, "func", fn.Get().GetDocComment().GetBody())
assert.Equal(t, "func", fn.Get().GetDocComment().DisplayBodyWithContracts())
})
}

func TestExtractSymbols_MacrosWithArguments(t *testing.T) {
Expand Down Expand Up @@ -684,8 +703,8 @@ func TestExtractSymbols_FunctionsWithVariableArguments(t *testing.T) {
assert.Equal(t, idx.NewRange(0, 7, 0, 31), fn.Get().GetDocumentRange())
assert.Nil(t, fn.Get().GetDocComment())

variable := fn.Get().Variables["$arg0"]
assert.Equal(t, "$arg0", variable.GetName())
variable := fn.Get().Variables["$arg#0"]
assert.Equal(t, "$arg#0", variable.GetName())
assert.Equal(t, "any*[]", variable.GetType().String())
assert.Equal(t, idx.NewRange(0, 0, 0, 0), variable.GetIdRange())
assert.Equal(t, idx.NewRange(0, 26, 0, 29), variable.GetDocumentRange())
Expand Down
8 changes: 8 additions & 0 deletions server/pkg/symbols/bitstruct_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ func (b *BitstructBuilder) WithDocumentRange(lineStart uint, CharStart uint, lin
return b
}

func (b *BitstructBuilder) WithDocs(docs string) *BitstructBuilder {
// Only modules, functions and macros can have contracts, so a string is enough
// Theoretically, there can be custom contracts here, but the stdlib shouldn't be creating them
docComment := NewDocComment(docs)
b.bitstruct.BaseIndexable.docComment = &docComment
return b
}

func (b *BitstructBuilder) ImplementsInterface(interfaceName string) *BitstructBuilder {
b.bitstruct.implements = append(b.bitstruct.implements, interfaceName)

Expand Down
8 changes: 8 additions & 0 deletions server/pkg/symbols/def_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ func (d *DefBuilder) WithDocumentRange(lineStart uint, CharStart uint, lineEnd u
return d
}

func (d *DefBuilder) WithDocs(docs string) *DefBuilder {
// Only modules, functions and macros can have contracts, so a string is enough
// Theoretically, there can be custom contracts here, but the stdlib shouldn't be creating them
docComment := NewDocComment(docs)
d.def.BaseIndexable.docComment = &docComment
return d
}

func (d *DefBuilder) Build() *Def {
return &d.def
}
8 changes: 8 additions & 0 deletions server/pkg/symbols/distinct_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ func (d *DistinctBuilder) WithDocumentRange(lineStart uint, CharStart uint, line
return d
}

func (d *DistinctBuilder) WithDocs(docs string) *DistinctBuilder {
// Only modules, functions and macros can have contracts, so a string is enough
// Theoretically, there can be custom contracts here, but the stdlib shouldn't be creating them
docComment := NewDocComment(docs)
d.distinct.BaseIndexable.docComment = &docComment
return d
}

func (d *DistinctBuilder) Build() *Distinct {
return &d.distinct
}
36 changes: 36 additions & 0 deletions server/pkg/symbols/doc_comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,26 @@ func (d *DocComment) AddContracts(contracts []*DocCommentContract) {
d.contracts = append(d.contracts, contracts...)
}

func (d *DocComment) HasContracts() bool {
return len(d.contracts) > 0
}

func (d *DocComment) GetContracts() []*DocCommentContract {
return d.contracts
}

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

func (c *DocCommentContract) GetName() string {
return c.name
}

func (c *DocCommentContract) GetBody() string {
return c.body
}

// Return a string displaying the body and contracts as markdown.
func (d *DocComment) DisplayBodyWithContracts() string {
out := d.body
Expand All @@ -52,3 +68,23 @@ func (d *DocComment) DisplayBodyWithContracts() string {

return out
}

type DocCommentBuilder struct {
docComment DocComment
}

func NewDocCommentBuilder(body string) *DocCommentBuilder {
return &DocCommentBuilder{
docComment: NewDocComment(body),
}
}

func (b *DocCommentBuilder) WithContract(name string, body string) *DocCommentBuilder {
contract := NewDocCommentContract(name, body)
b.docComment.contracts = append(b.docComment.contracts, &contract)
return b
}

func (b *DocCommentBuilder) Build() DocComment {
return b.docComment
}
8 changes: 8 additions & 0 deletions server/pkg/symbols/enum_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ func (eb *EnumBuilder) WithDocumentRange(lineStart uint, CharStart uint, lineEnd
return eb
}

func (eb *EnumBuilder) WithDocs(docs string) *EnumBuilder {
// Only modules, functions and macros can have contracts, so a string is enough
// Theoretically, there can be custom contracts here, but the stdlib shouldn't be creating them
docComment := NewDocComment(docs)
eb.enum.BaseIndexable.docComment = &docComment
return eb
}

func (eb *EnumBuilder) WithEnumerator(enumerator *Enumerator) *EnumBuilder {
eb.enum.enumerators = append(eb.enum.enumerators, enumerator)

Expand Down
8 changes: 8 additions & 0 deletions server/pkg/symbols/fault_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ func (eb *FaultBuilder) WithDocumentRange(lineStart uint, CharStart uint, lineEn
return eb
}

func (eb *FaultBuilder) WithDocs(docs string) *FaultBuilder {
// Only modules, functions and macros can have contracts, so a string is enough
// Theoretically, there can be custom contracts here, but the stdlib shouldn't be creating them
docComment := NewDocComment(docs)
eb.fault.BaseIndexable.docComment = &docComment
return eb
}

func (eb *FaultBuilder) WithConstant(constant *FaultConstant) *FaultBuilder {
eb.fault.constants = append(eb.fault.constants, constant)

Expand Down
Loading
Loading