codeBuilder

package
v0.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 17, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// SectionCommentHeaders is the section for file comment headers (at the very top of the file)
	SectionCommentHeaders = "comment_headers"

	// SectionHeaders is the section for file headers (like package declaration, file comments, etc)
	SectionHeaders = "headers"

	// SectionImports is the section for importing other types/modules
	SectionImports = "imports"

	// SectionDeclareNamespace is the section for declaring namespaces, used in languages such as C#,
	// where the namespace declaration MUST BE after the imports and before any type declarations.
	SectionDeclareNamespace = "declare_namespace"
)

Variables

This section is empty.

Functions

func GetDefaultOrderedSections

func GetDefaultOrderedSections() []string

GetDefaultOrderedSections returns the default ordered sections for output.

Types

type CodeBuildResult

type CodeBuildResult struct {
	Code      string
	DebugInfo string
}

CodeBuildResult holds the result of the code building process.

type CodeBuilder

type CodeBuilder struct {
	// contains filtered or unexported fields
}

CodeBuilder is a utility struct for building code strings with proper indentation and formatting. It provides methods to append lines, manage indentation levels, and is chainable.

func NewCodeBuilder

func NewCodeBuilder() *CodeBuilder

NewCodeBuilder creates a new CodeBuilder instance with default options.

func NewCodeBuilderWithOptions

func NewCodeBuilderWithOptions(opts *CodeBuilderOptions) *CodeBuilder

NewCodeBuilderWithOptions creates a new CodeBuilder instance with the given options.

func (*CodeBuilder) AddCommentHeader

func (c *CodeBuilder) AddCommentHeader(comment string) *CodeBuilder

AddCommentHeader adds a comment header to the "comment_headers" section with a new line. This is useful for comment headers such as license headers, "DO NOT EDIT" warnings, etc.

func (*CodeBuilder) AddHeader

func (c *CodeBuilder) AddHeader(strValue string) *CodeBuilder

AddHeader adds a header to the "headers" section with a new line. This is useful for file headers such as package declarations, general file comments, etc. This will come after comment headers but before imports.

func (*CodeBuilder) AddNamespaceDeclaration

func (c *CodeBuilder) AddNamespaceDeclaration(strValue string) *CodeBuilder

AddNamespaceDeclaration adds a namespace declaration to the "namespace_declarations" section with a new line. This is useful for organizing code into different namespaces in languages such as C#. NOTE: this comes after imports and before type declarations.

func (*CodeBuilder) AppendCommentHeader

func (c *CodeBuilder) AppendCommentHeader(comment string) *CodeBuilder

AppendCommentHeader adds a comment header to the "comment_headers" section. This is useful for comment headers such as license headers, "DO NOT EDIT" warnings, etc.

func (*CodeBuilder) AppendHeader

func (c *CodeBuilder) AppendHeader(strValue string) *CodeBuilder

AppendHeader adds a header to the "headers" section. This is useful for file headers such as package declarations, general file comments, etc. This will come after comment headers but before imports.

func (*CodeBuilder) AppendLine

func (c *CodeBuilder) AppendLine(s string) *CodeBuilder

AppendLine appends a line without adding indentation.

func (*CodeBuilder) AppendNamespaceDeclaration

func (c *CodeBuilder) AppendNamespaceDeclaration(strValue string) *CodeBuilder

AppendNamespaceDeclaration adds a namespace declaration to the "namespace_declarations" section. This is useful for organizing code into different namespaces.

func (*CodeBuilder) AppendStr

func (c *CodeBuilder) AppendStr(s string) *CodeBuilder

AppendStr appends a string without adding indentation.

func (*CodeBuilder) BeginSection

func (c *CodeBuilder) BeginSection(section string) *CodeBuilder

BeginSection begins a new section or switches to an existing one. IMPORTANT NOTE: As long as you are in this section, there is a lock held on this object, to avoid deadlocks, you SHOULD call EndSection() when you are done with this section.

func (*CodeBuilder) Build

func (c *CodeBuilder) Build(orderedKeys []string) *CodeBuildResult

Build builds the code and returns the result.

func (*CodeBuilder) DoImport

func (c *CodeBuilder) DoImport(key, importLine string) *CodeBuilder

DoImport marks the given key as imported and appends the import line if not already imported. The import line is written to the "imports" section; this is a special section that does not need to be manually begun; HOWEVER, it does require that a section is active when this method is called.

func (*CodeBuilder) EndSection

func (c *CodeBuilder) EndSection() *CodeBuilder

EndSection ends the current section and releases the lock.

func (*CodeBuilder) Indent

func (c *CodeBuilder) Indent() *CodeBuilder

Indent increases the indentation level.

func (*CodeBuilder) IsImported

func (c *CodeBuilder) IsImported(key string) bool

IsImported checks if the given key has been marked as imported.

func (*CodeBuilder) NewLine

func (c *CodeBuilder) NewLine() *CodeBuilder

NewLine adds a new line.

func (*CodeBuilder) String

func (c *CodeBuilder) String(orderedKeys []string) string

String returns the built string.

func (*CodeBuilder) Unindent

func (c *CodeBuilder) Unindent() *CodeBuilder

Unindent decreases the indentation level.

func (*CodeBuilder) UnindentLine

func (c *CodeBuilder) UnindentLine() *CodeBuilder

UnindentLine decreases the indentation level and adds a new line.

func (*CodeBuilder) WriteLine

func (c *CodeBuilder) WriteLine(s string) *CodeBuilder

WriteLine writes a line with the current indentation. If you don't want indentation, use AppendLine instead.

func (*CodeBuilder) WriteLinef

func (c *CodeBuilder) WriteLinef(format string, args ...any) *CodeBuilder

WriteLinef writes a formatted line with the current indentation.

func (*CodeBuilder) WriteStr

func (c *CodeBuilder) WriteStr(s string) *CodeBuilder

WriteStr writes a string with the current indentation. If you don't want indentation, use AppendStr instead.

type CodeBuilderOptions

type CodeBuilderOptions struct {
	IndentationStr  string
	NewLineStr      string
	EnableDebugInfo bool
}

CodeBuilderOptions holds configuration options for the CodeBuilder.

func GetDefaultCodeBuilderOptions

func GetDefaultCodeBuilderOptions() *CodeBuilderOptions

GetDefaultCodeBuilderOptions returns the default options for CodeBuilder.

type DebugInfo

type DebugInfo struct {
	// SourceFile is the path to the source file that generated this code.
	SourceFile string `json:"source_file"`

	// SourceLine is the line number in the source file.
	SourceLine int `json:"source_line"`

	// GeneratedLine is the line number in the generated code (relative to the section start).
	// This will be adjusted during the final build process.
	GeneratedLine int `json:"generated_line"`

	// SectionOffset is the byte offset in the section's buffer where this debug info starts.
	SectionOffset int `json:"-"`
}

DebugInfo holds information about the source code that generated a specific part of the output code.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL