Skip to content

Commit

Permalink
add docs support to builders
Browse files Browse the repository at this point in the history
  • Loading branch information
PgBiel committed Jan 29, 2025
1 parent d3735e5 commit 5931dea
Show file tree
Hide file tree
Showing 13 changed files with 195 additions and 5 deletions.
75 changes: 74 additions & 1 deletion server/cmd/stdlib_indexer/blurp.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,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 @@ -62,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 @@ -89,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 @@ -119,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 @@ -141,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 @@ -190,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 @@ -220,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 @@ -259,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 Down Expand Up @@ -329,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
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
5 changes: 5 additions & 0 deletions server/pkg/symbols/function_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ func (fb *FunctionBuilder) WithDocumentRange(lineStart uint, CharStart uint, lin
return fb
}

func (fb *FunctionBuilder) WithDocs(docComment DocComment) *FunctionBuilder {
fb.function.BaseIndexable.docComment = &docComment
return fb
}

func (fb *FunctionBuilder) WithoutSourceCode() *FunctionBuilder {
fb.function.BaseIndexable.hasSourceCode = false
return fb
Expand Down
8 changes: 8 additions & 0 deletions server/pkg/symbols/interface_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ func (ib *InterfaceBuilder) WithDocumentRange(lineStart uint, CharStart uint, li
return ib
}

func (ib *InterfaceBuilder) WithDocs(docs string) *InterfaceBuilder {
// 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)
ib._interface.BaseIndexable.docComment = &docComment
return ib
}

func (ib *InterfaceBuilder) Build() Interface {
return ib._interface
}
5 changes: 5 additions & 0 deletions server/pkg/symbols/module_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ func (mb *ModuleBuilder) WithoutSourceCode() *ModuleBuilder {
return mb
}

func (mb *ModuleBuilder) WithDocs(docComment DocComment) *ModuleBuilder {
mb.module.BaseIndexable.docComment = &docComment
return mb
}

func (mb ModuleBuilder) Build() *Module {
return mb.module
}
8 changes: 8 additions & 0 deletions server/pkg/symbols/struct_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ func (sb *StructBuilder) WithoutSourceCode() *StructBuilder {
return sb
}

func (sb *StructBuilder) WithDocs(docs string) *StructBuilder {
// 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)
sb.strukt.BaseIndexable.docComment = &docComment
return sb
}

func (b *StructBuilder) WithStructMember(name string, baseType Type, module string, docId string) *StructBuilder {
member := NewStructMember(
name,
Expand Down
8 changes: 8 additions & 0 deletions server/pkg/symbols/variable_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ func (vb *VariableBuilder) WithDocumentRange(lineStart uint, CharStart uint, lin
return vb
}

func (vb *VariableBuilder) WithDocs(docs string) *VariableBuilder {
// 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)
vb.variable.BaseIndexable.docComment = &docComment
return vb
}

func (vb *VariableBuilder) IsVarArg() *VariableBuilder {
vb.variable.Arg.VarArg = true
return vb
Expand Down

0 comments on commit 5931dea

Please sign in to comment.