generate

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 14, 2024 License: BSD-3-Clause Imports: 18 Imported by: 0

Documentation

Overview

Package generate provides the generation of useful methods, variables, and constants for Cogent Core code.

Index

Constants

This section is empty.

Variables

View Source
var TreeMethodsTmpl = template.Must(template.New("TreeMethods").
	Funcs(template.FuncMap{
		"HasEmbedDirective": HasEmbedDirective,
		"HasNoNewDirective": HasNoNewDirective,
		"DocToComment":      typegen.DocToComment,
		"TreePkg":           TreePkg,
	}).Parse(
	`
	{{if not (HasNoNewDirective .)}}
	// New{{.LocalName}} adds a new [{{.LocalName}}] with the given name to the given parent:
	{{DocToComment .Doc}}
	func New{{.LocalName}}(parent {{TreePkg .}}Node, name ...string) *{{.LocalName}} {
		return parent.NewChild({{.LocalName}}Type, name...).(*{{.LocalName}})
	}
	{{end}}

	// NodeType returns the [*types.Type] of [{{.LocalName}}]
	func (t *{{.LocalName}}) NodeType() *types.Type { return {{.LocalName}}Type }

	// New returns a new [*{{.LocalName}}] value
	func (t *{{.LocalName}}) New() {{TreePkg .}}Node { return &{{.LocalName}}{} }
	
	{{if HasEmbedDirective .}}
	// {{.LocalName}}Embedder is an interface that all types that embed {{.LocalName}} satisfy
	type {{.LocalName}}Embedder interface {
		As{{.LocalName}}() *{{.LocalName}}
	}
	
	// As{{.LocalName}} returns the given value as a value of type {{.LocalName}} if the type
	// of the given value embeds {{.LocalName}}, or nil otherwise
	func As{{.LocalName}}(k {{TreePkg .}}Node) *{{.LocalName}} {
		if k == nil || k.This() == nil {
			return nil
		}
		if t, ok := k.({{.LocalName}}Embedder); ok {
			return t.As{{.LocalName}}()
		}
		return nil
	}
	
	// As{{.LocalName}} satisfies the [{{.LocalName}}Embedder] interface
	func (t *{{.LocalName}}) As{{.LocalName}}() *{{.LocalName}} { return t }
	{{end}}
	`,
))

TreeMethodsTmpl is a template that contains the methods and functions specific to [tree.Node] types.

Functions

func Generate

func Generate(c *config.Config) error

Generate is the main entry point to code generation that does all of the generation according to the given config info. It overrides the [config.Config.Generate.Typegen.InterfaceConfigs] info.

func GetPagesExamples

func GetPagesExamples(c *config.Config) (ordmap.Map[string, []byte], error)

GetPagesExamples collects and returns all of the pages examples.

func HasEmbedDirective

func HasEmbedDirective(typ *typegen.Type) bool

HasEmbedDirective returns whether the given typegen.Type has a "core:embedder" comment directive. This function is used in TreeMethodsTmpl.

func HasNoNewDirective

func HasNoNewDirective(typ *typegen.Type) bool

HasNoNewDirective returns whether the given typegen.Type has a "core:no-new" comment directive. This function is used in TreeMethodsTmpl.

func Pages

func Pages(c *config.Config) error

Pages does any necessary generation for pages.

func ParsePackages

func ParsePackages(cfg *config.Config) ([]*packages.Package, error)

ParsePackages parses the package(s) based on the given config info.

func TreePkg

func TreePkg(typ *typegen.Type) string

TreePkg returns the package identifier for the tree package in the context of the given type ("" if it is already in the tree package, and "tree." otherwise)

func WritePagegen

func WritePagegen(c *config.Config, examples ordmap.Map[string, []byte]) error

WritePagegen constructs the pagegen.go file from the given examples.

Types

This section is empty.

Jump to

Keyboard shortcuts

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