formatter

package
v0.0.21 Latest Latest
Warning

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

Go to latest
Published: Mar 23, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Format

func Format(source string, filename string, opts FormatOptions) (string, error)

Format formats Kukicha source code and returns the formatted result

func FormatCheck

func FormatCheck(source string, filename string, opts FormatOptions) (bool, error)

FormatCheck checks if the source is already formatted Returns true if the source matches the formatted output

func ProcessSource

func ProcessSource(source string) string

ProcessSource is a convenience function to preprocess source code

Types

type Comment

type Comment struct {
	Text       string // The comment text including the # prefix
	Line       int    // Line number where the comment appears
	Column     int    // Column number where the comment starts
	IsTrailing bool   // True if comment is on same line as code
}

Comment represents a comment in the source code

func ExtractComments

func ExtractComments(tokens []lexer.Token) []Comment

ExtractComments extracts all comment tokens from a token stream

type CommentAttachment

type CommentAttachment struct {
	Leading  []Comment // Comments on lines immediately before the node
	Trailing *Comment  // Comment on same line after the node (optional)
}

CommentAttachment holds comments attached to an AST node

type CommentMap

type CommentMap map[ast.Node]*CommentAttachment

CommentMap maps AST nodes to their attached comments

func AttachComments

func AttachComments(comments []Comment, program *ast.Program) CommentMap

AttachComments attaches comments to AST nodes Comments are attached based on line proximity: - Leading comments: on lines immediately before the node (no blank lines between) - Trailing comments: on the same line as the node

type FormatOptions

type FormatOptions struct {
	// PreprocessGoStyle converts Go-style braces/semicolons to Kukicha style
	PreprocessGoStyle bool
}

FormatOptions contains options for formatting

func DefaultOptions

func DefaultOptions() FormatOptions

DefaultOptions returns the default formatting options

type Preprocessor

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

Preprocessor converts Go-style syntax to Kukicha-style indentation

func NewPreprocessor

func NewPreprocessor(source string) *Preprocessor

NewPreprocessor creates a new preprocessor

func (*Preprocessor) Process

func (p *Preprocessor) Process() string

Process converts Go-style braces to Kukicha-style indentation It handles: - Lines ending with { -> remove brace, increase indent for following lines - Lines that are just } -> remove brace, decrease indent - Trailing semicolons -> remove - Struct/map literals are preserved (braces in expressions)

type Printer

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

Printer prints an AST as formatted Kukicha source code

func NewPrinter

func NewPrinter() *Printer

NewPrinter creates a new printer

func (*Printer) Print

func (p *Printer) Print(program *ast.Program) string

Print prints the program and returns the formatted source code

type PrinterWithComments

type PrinterWithComments struct {
	*Printer
	// contains filtered or unexported fields
}

PrinterWithComments extends the basic printer with comment support

func NewPrinterWithComments

func NewPrinterWithComments(comments CommentMap) *PrinterWithComments

NewPrinterWithComments creates a printer that includes comments

func (*PrinterWithComments) Print

func (p *PrinterWithComments) Print(program *ast.Program) string

Print prints the program with comments

Jump to

Keyboard shortcuts

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