generator

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2026 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const RuntimeImportAlias = "ghtmxruntime"

RuntimeImportAlias is the alias writeImports gives RuntimeImports[1]. The analyzer's reserved-import check (GHTMX-E0308) flags template imports that would collide with it — keep the two in sync through this constant.

Variables

View Source
var RuntimeImports = []string{
	"github.com/go-monolith/ghtmx",
	"github.com/go-monolith/ghtmx/runtime",
}

RuntimeImports are the packages every generated file imports, in emission order: the root interface package, then the generated-code runtime. The import-isolation gate (internal/importcheck) verifies their combined transitive closure stays standard-library only (NFR-012) — extend both together.

Functions

func HasGoChanged

func HasGoChanged(previous, updated GeneratorOutput) bool

HasGoChanged returns true if the Go code has changed between the previous and updated GeneratorOutput.

func HasTextChanged

func HasTextChanged(previous, updated GeneratorOutput) bool

HasTextChanged returns true if the text literals have changed between the previous and updated GeneratorOutput.

Types

type GenerateOpt

type GenerateOpt func(g *generator) error

func WithEditorBindings added in v0.1.2

func WithEditorBindings(generatedPkg string) GenerateOpt

WithEditorBindings drops the ghtmx.SafeURL type expectation from the hx-* verb attribute expressions the analyzer would have lowered, for the language server only. generatedPkg names the central generated package, which distinguishes the two cases.

When `ghtmx generate` resolves a symbol binding against the route table, the analyzer folds it into the registered path and the generator never sees it. The language server generates straight from the parsed template with no table, so the binding does reach the generator, and the normal emission produces `var v ghtmx.SafeURL = CreateTodo` — a type error against a template that builds perfectly, which is what surfaces in editors as a false IncompatibleAssign.

Only the shapes the analyzer lowers are relaxed: a bare handler identifier, and a handler qualified by a package other than the generated one. A route-constructor call or a generated-package reference is left alone, because those reach the generator in a real build too — their result really must be a ghtmx.SafeURL, and the editor should keep saying so.

Lowering inside the server instead would delete the expression, and with it the position gopls completes, defines, and hovers at inside `{ … }` (FR-081, FR-083). Dropping only the assignment keeps the symbol live for the editor, and the escaping contract stays enforced everywhere the code is actually compiled.

func WithFileName

func WithFileName(name string) GenerateOpt

WithFileName sets the filename of the templ file in template rendering error messages.

func WithSkipCodeGeneratedComment

func WithSkipCodeGeneratedComment() GenerateOpt

WithSkipCodeGeneratedComment skips the code generated comment at the top of the file. gopls disables edit related functionality for generated files, so the templ LSP may wish to skip generation of this comment so that gopls provides expected results.

func WithTimestamp

func WithTimestamp(d time.Time) GenerateOpt

WithTimestamp enables the generated date to be included in the generated code.

func WithVersion

func WithVersion(v string) GenerateOpt

WithVersion enables the version to be included in the generated code.

type GeneratorOptions

type GeneratorOptions struct {
	// Version of ghtmx.
	Version string
	// FileName to include in error messages if string expressions return an error.
	FileName string
	// SkipCodeGeneratedComment skips the code generated comment at the top of the file.
	SkipCodeGeneratedComment bool
	// GeneratedDate to include as a comment.
	GeneratedDate string
	// EditorBindings relaxes the hx-* verb attribute emission for the
	// language server. See WithEditorBindings.
	EditorBindings bool
	// EditorGeneratedPkg is the central generated package's name, which
	// tells a route constructor apart from a handler reference.
	EditorGeneratedPkg string
}

type GeneratorOutput

type GeneratorOutput struct {
	Options   GeneratorOptions  `json:"meta"`
	SourceMap *parser.SourceMap `json:"sourceMap"`
	Literals  []string          `json:"literals"`
}

func Generate

func Generate(template *parser.TemplateFile, w io.Writer, opts ...GenerateOpt) (op GeneratorOutput, err error)

Generate generates Go code from the input template file to w, and returns a map of the location of Go expressions in the template to the location of the generated Go code in the output.

type RangeWriter

type RangeWriter struct {
	Current parser.Position

	Literals []string
	// contains filtered or unexported fields
}

func NewRangeWriter

func NewRangeWriter(w io.Writer) *RangeWriter

func (*RangeWriter) Write

func (rw *RangeWriter) Write(s string) (r parser.Range, err error)

func (*RangeWriter) WriteIndent

func (rw *RangeWriter) WriteIndent(level int, s string) (r parser.Range, err error)

func (*RangeWriter) WriteStringLiteral

func (rw *RangeWriter) WriteStringLiteral(level int, s string) (r parser.Range, err error)

Jump to

Keyboard shortcuts

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