// Code generated by templ - DO NOT EDIT.
package testhtml
//lint:file-ignore SA4006 This context is only used if a nested component is present.
import "github.com/a-h/templ"
import templruntime "github.com/a-h/templ/runtime"
func Render(p Person) templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
return templ_7745c5c3_CtxErr
}
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var1 := templ.GetChildren(ctx)
if templ_7745c5c3_Var1 == nil {
templ_7745c5c3_Var1 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 1, "<div><h1>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var2 string
templ_7745c5c3_Var2, templ_7745c5c3_Err = templ.JoinStringErrs(p.Name)
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `benchmarks/templ/template.templ`, Line: 5, Col: 14}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var2))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 2, "</h1><div style=\"font-family: 'sans-serif'\" id=\"test\" data-contents=\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var3 string
templ_7745c5c3_Var3, templ_7745c5c3_Err = templ.JoinStringErrs(`something with "quotes" and a <tag>`)
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `benchmarks/templ/template.templ`, Line: 6, Col: 104}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var3))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 3, "\"><div>email:<a href=\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var4 templ.SafeURL = templ.URL("mailto: " + p.Email)
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(string(templ_7745c5c3_Var4)))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 4, "\">")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var5 string
templ_7745c5c3_Var5, templ_7745c5c3_Err = templ.JoinStringErrs(p.Email)
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `benchmarks/templ/template.templ`, Line: 7, Col: 67}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var5))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 5, "</a></div></div></div><hr")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
if true {
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 6, " noshade")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 7, "><hr optionA")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
if true {
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 8, " optionB")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 9, " optionC=\"other\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
if false {
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 10, " optionD")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 11, "><hr noshade>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return nil
})
}
var _ = templruntime.GeneratedTemplate
package fmtcmd
import (
"bytes"
"fmt"
"io"
"log/slog"
"os"
"runtime"
"sync"
"time"
"github.com/a-h/templ/cmd/templ/imports"
"github.com/a-h/templ/cmd/templ/processor"
parser "github.com/a-h/templ/parser/v2"
"github.com/natefinch/atomic"
)
type Arguments struct {
FailIfChanged bool
ToStdout bool
StdinFilepath string
Files []string
WorkerCount int
}
func Run(log *slog.Logger, stdin io.Reader, stdout io.Writer, args Arguments) (err error) {
// If no files are provided, read from stdin and write to stdout.
if len(args.Files) == 0 {
out, _ := format(writeToWriter(stdout), readFromReader(stdin, args.StdinFilepath), true)
return out
}
process := func(fileName string) (error, bool) {
read := readFromFile(fileName)
write := writeToFile
if args.ToStdout {
write = writeToWriter(stdout)
}
writeIfUnchanged := args.ToStdout
return format(write, read, writeIfUnchanged)
}
dir := args.Files[0]
return NewFormatter(log, dir, process, args.WorkerCount, args.FailIfChanged).Run()
}
type Formatter struct {
Log *slog.Logger
Dir string
Process func(fileName string) (error, bool)
WorkerCount int
FailIfChange bool
}
func NewFormatter(log *slog.Logger, dir string, process func(fileName string) (error, bool), workerCount int, failIfChange bool) *Formatter {
f := &Formatter{
Log: log,
Dir: dir,
Process: process,
WorkerCount: workerCount,
FailIfChange: failIfChange,
}
if f.WorkerCount == 0 {
f.WorkerCount = runtime.NumCPU()
}
return f
}
func (f *Formatter) Run() (err error) {
changesMade := 0
start := time.Now()
results := make(chan processor.Result)
f.Log.Debug("Walking directory", slog.String("path", f.Dir))
go processor.Process(f.Dir, f.Process, f.WorkerCount, results)
var successCount, errorCount int
for r := range results {
if r.ChangesMade {
changesMade += 1
}
if r.Error != nil {
f.Log.Error(r.FileName, slog.Any("error", r.Error))
errorCount++
continue
}
f.Log.Debug(r.FileName, slog.Duration("duration", r.Duration))
successCount++
}
if f.FailIfChange && changesMade > 0 {
f.Log.Error("Templates were valid but not properly formatted", slog.Int("count", successCount+errorCount), slog.Int("changed", changesMade), slog.Int("errors", errorCount), slog.Duration("duration", time.Since(start)))
return fmt.Errorf("templates were not formatted properly")
}
f.Log.Info("Format Complete", slog.Int("count", successCount+errorCount), slog.Int("errors", errorCount), slog.Int("changed", changesMade), slog.Duration("duration", time.Since(start)))
if errorCount > 0 {
return fmt.Errorf("formatting failed")
}
return
}
type reader func() (fileName, src string, err error)
func readFromReader(r io.Reader, stdinFilepath string) func() (fileName, src string, err error) {
return func() (fileName, src string, err error) {
b, err := io.ReadAll(r)
if err != nil {
return "", "", fmt.Errorf("failed to read stdin: %w", err)
}
return stdinFilepath, string(b), nil
}
}
func readFromFile(name string) reader {
return func() (fileName, src string, err error) {
b, err := os.ReadFile(name)
if err != nil {
return "", "", fmt.Errorf("failed to read file %q: %w", fileName, err)
}
return name, string(b), nil
}
}
type writer func(fileName, tgt string) error
var mu sync.Mutex
func writeToWriter(w io.Writer) func(fileName, tgt string) error {
return func(fileName, tgt string) error {
mu.Lock()
defer mu.Unlock()
_, err := w.Write([]byte(tgt))
return err
}
}
func writeToFile(fileName, tgt string) error {
return atomic.WriteFile(fileName, bytes.NewBufferString(tgt))
}
func format(write writer, read reader, writeIfUnchanged bool) (err error, fileChanged bool) {
fileName, src, err := read()
if err != nil {
return err, false
}
t, err := parser.ParseString(src)
if err != nil {
return err, false
}
t.Filepath = fileName
t, err = imports.Process(t)
if err != nil {
return err, false
}
w := new(bytes.Buffer)
if err = t.Write(w); err != nil {
return fmt.Errorf("formatting error: %w", err), false
}
fileChanged = (src != w.String())
if !writeIfUnchanged && !fileChanged {
return nil, fileChanged
}
return write(fileName, w.String()), fileChanged
}
package generatecmd
import (
"context"
"errors"
"fmt"
"log/slog"
"net/http"
"net/url"
"os"
"path"
"path/filepath"
"regexp"
"runtime"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/a-h/templ"
"github.com/a-h/templ/cmd/templ/generatecmd/modcheck"
"github.com/a-h/templ/cmd/templ/generatecmd/proxy"
"github.com/a-h/templ/cmd/templ/generatecmd/run"
"github.com/a-h/templ/cmd/templ/generatecmd/watcher"
"github.com/a-h/templ/generator"
"github.com/cenkalti/backoff/v4"
"github.com/cli/browser"
"github.com/fsnotify/fsnotify"
)
const defaultWatchPattern = `(.+\.go$)|(.+\.templ$)|(.+_templ\.txt$)`
func NewGenerate(log *slog.Logger, args Arguments) (g *Generate, err error) {
g = &Generate{
Log: log,
Args: &args,
}
if g.Args.WorkerCount == 0 {
g.Args.WorkerCount = runtime.NumCPU()
}
if g.Args.WatchPattern == "" {
g.Args.WatchPattern = defaultWatchPattern
}
g.WatchPattern, err = regexp.Compile(g.Args.WatchPattern)
if err != nil {
return nil, fmt.Errorf("failed to compile watch pattern %q: %w", g.Args.WatchPattern, err)
}
return g, nil
}
type Generate struct {
Log *slog.Logger
Args *Arguments
WatchPattern *regexp.Regexp
}
type GenerationEvent struct {
Event fsnotify.Event
Updated bool
GoUpdated bool
TextUpdated bool
}
func (cmd Generate) Run(ctx context.Context) (err error) {
if cmd.Args.NotifyProxy {
return proxy.NotifyProxy(cmd.Args.ProxyBind, cmd.Args.ProxyPort)
}
if cmd.Args.Watch && cmd.Args.FileName != "" {
return fmt.Errorf("cannot watch a single file, remove the -f or -watch flag")
}
writingToWriter := cmd.Args.FileWriter != nil
if cmd.Args.FileName == "" && writingToWriter {
return fmt.Errorf("only a single file can be output to stdout, add the -f flag to specify the file to generate code for")
}
// Default to writing to files.
if cmd.Args.FileWriter == nil {
cmd.Args.FileWriter = FileWriter
}
if cmd.Args.PPROFPort > 0 {
go func() {
_ = http.ListenAndServe(fmt.Sprintf("localhost:%d", cmd.Args.PPROFPort), nil)
}()
}
// Use absolute path.
if !path.IsAbs(cmd.Args.Path) {
cmd.Args.Path, err = filepath.Abs(cmd.Args.Path)
if err != nil {
return fmt.Errorf("failed to get absolute path: %w", err)
}
}
// Configure generator.
var opts []generator.GenerateOpt
if cmd.Args.IncludeVersion {
opts = append(opts, generator.WithVersion(templ.Version()))
}
if cmd.Args.IncludeTimestamp {
opts = append(opts, generator.WithTimestamp(time.Now()))
}
// Check the version of the templ module.
if err := modcheck.Check(cmd.Args.Path); err != nil {
cmd.Log.Warn("templ version check: " + err.Error())
}
fseh := NewFSEventHandler(
cmd.Log,
cmd.Args.Path,
cmd.Args.Watch,
opts,
cmd.Args.GenerateSourceMapVisualisations,
cmd.Args.KeepOrphanedFiles,
cmd.Args.FileWriter,
cmd.Args.Lazy,
)
// If we're processing a single file, don't bother setting up the channels/multithreaing.
if cmd.Args.FileName != "" {
_, err = fseh.HandleEvent(ctx, fsnotify.Event{
Name: cmd.Args.FileName,
Op: fsnotify.Create,
})
return err
}
// Start timer.
start := time.Now()
// Create channels:
// For the initial filesystem walk and subsequent (optional) fsnotify events.
events := make(chan fsnotify.Event)
// Count of events currently being processed by the event handler.
var eventsWG sync.WaitGroup
// Used to check that the event handler has completed.
var eventHandlerWG sync.WaitGroup
// For errs from the watcher.
errs := make(chan error)
// Tracks whether errors occurred during the generation process.
var errorCount atomic.Int64
// For triggering actions after generation has completed.
postGeneration := make(chan *GenerationEvent, 256)
// Used to check that the post-generation handler has completed.
var postGenerationWG sync.WaitGroup
var postGenerationEventsWG sync.WaitGroup
// Waitgroup for the push process.
var pushHandlerWG sync.WaitGroup
// Start process to push events into the channel.
pushHandlerWG.Add(1)
go func() {
defer pushHandlerWG.Done()
defer close(events)
cmd.Log.Debug(
"Walking directory",
slog.String("path", cmd.Args.Path),
slog.Bool("devMode", cmd.Args.Watch),
)
if err := watcher.WalkFiles(ctx, cmd.Args.Path, cmd.WatchPattern, events); err != nil {
cmd.Log.Error("WalkFiles failed, exiting", slog.Any("error", err))
errs <- FatalError{Err: fmt.Errorf("failed to walk files: %w", err)}
return
}
if !cmd.Args.Watch {
cmd.Log.Debug("Dev mode not enabled, process can finish early")
return
}
cmd.Log.Info("Watching files")
rw, err := watcher.Recursive(ctx, cmd.Args.Path, cmd.WatchPattern, events, errs)
if err != nil {
cmd.Log.Error("Recursive watcher setup failed, exiting", slog.Any("error", err))
errs <- FatalError{Err: fmt.Errorf("failed to setup recursive watcher: %w", err)}
return
}
cmd.Log.Debug("Waiting for context to be cancelled to stop watching files")
<-ctx.Done()
cmd.Log.Debug("Context cancelled, closing watcher")
if err := rw.Close(); err != nil {
cmd.Log.Error("Failed to close watcher", slog.Any("error", err))
}
cmd.Log.Debug("Waiting for events to be processed")
eventsWG.Wait()
cmd.Log.Debug(
"All pending events processed, waiting for pending post-generation events to complete",
)
postGenerationEventsWG.Wait()
cmd.Log.Debug(
"All post-generation events processed, deleting watch mode text files",
slog.Int64("errorCount", errorCount.Load()),
)
fileEvents := make(chan fsnotify.Event)
go func() {
if err := watcher.WalkFiles(ctx, cmd.Args.Path, cmd.WatchPattern, fileEvents); err != nil {
cmd.Log.Error("Post dev mode WalkFiles failed", slog.Any("error", err))
errs <- FatalError{Err: fmt.Errorf("failed to walk files: %w", err)}
return
}
close(fileEvents)
}()
for event := range fileEvents {
if strings.HasSuffix(event.Name, "_templ.txt") {
if err = os.Remove(event.Name); err != nil {
cmd.Log.Warn("Failed to remove watch mode text file", slog.Any("error", err))
}
}
}
}()
// Start process to handle events.
eventHandlerWG.Add(1)
sem := make(chan struct{}, cmd.Args.WorkerCount)
go func() {
defer eventHandlerWG.Done()
defer close(postGeneration)
cmd.Log.Debug("Starting event handler")
for event := range events {
eventsWG.Add(1)
sem <- struct{}{}
go func(event fsnotify.Event) {
cmd.Log.Debug("Processing file", slog.String("file", event.Name))
defer eventsWG.Done()
defer func() { <-sem }()
r, err := fseh.HandleEvent(ctx, event)
if err != nil {
errs <- err
}
if !(r.GoUpdated || r.TextUpdated) {
cmd.Log.Debug("File not updated", slog.String("file", event.Name))
return
}
e := &GenerationEvent{
Event: event,
Updated: r.Updated,
GoUpdated: r.GoUpdated,
TextUpdated: r.TextUpdated,
}
cmd.Log.Debug("File updated", slog.String("file", event.Name))
postGeneration <- e
}(event)
}
// Wait for all events to be processed before closing.
eventsWG.Wait()
}()
// Start process to handle post-generation events.
var updates int
postGenerationWG.Add(1)
var firstPostGenerationExecuted bool
go func() {
defer close(errs)
defer postGenerationWG.Done()
cmd.Log.Debug("Starting post-generation handler")
timeout := time.NewTimer(time.Hour * 24 * 365)
var goUpdated, textUpdated bool
var p *proxy.Handler
for {
select {
case ge := <-postGeneration:
if ge == nil {
cmd.Log.Debug("Post-generation event channel closed, exiting")
return
}
goUpdated = goUpdated || ge.GoUpdated
textUpdated = textUpdated || ge.TextUpdated
if goUpdated || textUpdated {
updates++
}
// Reset timer.
if !timeout.Stop() {
<-timeout.C
}
timeout.Reset(time.Millisecond * 100)
case <-timeout.C:
if !goUpdated && !textUpdated {
// Nothing to process, reset timer and wait again.
timeout.Reset(time.Hour * 24 * 365)
break
}
postGenerationEventsWG.Add(1)
if cmd.Args.Command != "" && goUpdated {
cmd.Log.Debug("Executing command", slog.String("command", cmd.Args.Command))
if cmd.Args.Watch {
os.Setenv("TEMPL_DEV_MODE", "true")
}
if _, err := run.Run(ctx, cmd.Args.Path, cmd.Args.Command); err != nil {
cmd.Log.Error("Error executing command", slog.Any("error", err))
}
}
if !firstPostGenerationExecuted {
cmd.Log.Debug("First post-generation event received, starting proxy")
firstPostGenerationExecuted = true
p, err = cmd.StartProxy(ctx)
if err != nil {
cmd.Log.Error("Failed to start proxy", slog.Any("error", err))
}
}
// Send server-sent event.
if p != nil && (textUpdated || goUpdated) {
cmd.Log.Debug("Sending reload event")
p.SendSSE("message", "reload")
}
postGenerationEventsWG.Done()
// Reset timer.
timeout.Reset(time.Millisecond * 100)
textUpdated = false
goUpdated = false
}
}
}()
// Read errors.
for err := range errs {
if err == nil {
continue
}
if errors.Is(err, FatalError{}) {
cmd.Log.Debug("Fatal error, exiting")
return err
}
cmd.Log.Error("Error", slog.Any("error", err))
errorCount.Add(1)
}
// Wait for everything to complete.
cmd.Log.Debug("Waiting for push handler to complete")
pushHandlerWG.Wait()
cmd.Log.Debug("Waiting for event handler to complete")
eventHandlerWG.Wait()
cmd.Log.Debug("Waiting for post-generation handler to complete")
postGenerationWG.Wait()
if cmd.Args.Command != "" {
cmd.Log.Debug("Killing command", slog.String("command", cmd.Args.Command))
if err := run.KillAll(); err != nil {
cmd.Log.Error("Error killing command", slog.Any("error", err))
}
}
// Check for errors after everything has completed.
if errorCount.Load() > 0 {
return fmt.Errorf("generation completed with %d errors", errorCount.Load())
}
cmd.Log.Info(
"Complete",
slog.Int("updates", updates),
slog.Duration("duration", time.Since(start)),
)
return nil
}
func (cmd *Generate) StartProxy(ctx context.Context) (p *proxy.Handler, err error) {
if cmd.Args.Proxy == "" {
cmd.Log.Debug("No proxy URL specified, not starting proxy")
return nil, nil
}
var target *url.URL
target, err = url.Parse(cmd.Args.Proxy)
if err != nil {
return nil, FatalError{Err: fmt.Errorf("failed to parse proxy URL: %w", err)}
}
if cmd.Args.ProxyPort == 0 {
cmd.Args.ProxyPort = 7331
}
if cmd.Args.ProxyBind == "" {
cmd.Args.ProxyBind = "127.0.0.1"
}
p = proxy.New(cmd.Log, cmd.Args.ProxyBind, cmd.Args.ProxyPort, target)
go func() {
cmd.Log.Info("Proxying", slog.String("from", p.URL), slog.String("to", p.Target.String()))
if err := http.ListenAndServe(fmt.Sprintf("%s:%d", cmd.Args.ProxyBind, cmd.Args.ProxyPort), p); err != nil {
cmd.Log.Error("Proxy failed", slog.Any("error", err))
}
}()
if !cmd.Args.OpenBrowser {
cmd.Log.Debug("Not opening browser")
return p, nil
}
go func() {
cmd.Log.Debug("Waiting for proxy to be ready", slog.String("url", p.URL))
backoff := backoff.NewExponentialBackOff()
backoff.InitialInterval = time.Second
var client http.Client
client.Timeout = 1 * time.Second
for {
if _, err := client.Get(p.URL); err == nil {
break
}
d := backoff.NextBackOff()
cmd.Log.Debug(
"Proxy not ready, retrying",
slog.String("url", p.URL),
slog.Any("backoff", d),
)
time.Sleep(d)
}
if err := browser.OpenURL(p.URL); err != nil {
cmd.Log.Error("Failed to open browser", slog.Any("error", err))
}
}()
return p, nil
}
package generatecmd
import (
"bufio"
"bytes"
"context"
"crypto/sha256"
"fmt"
"go/format"
"go/scanner"
"go/token"
"io"
"log/slog"
"os"
"path"
"path/filepath"
"strings"
"sync"
"time"
"github.com/a-h/templ/cmd/templ/visualize"
"github.com/a-h/templ/generator"
"github.com/a-h/templ/parser/v2"
"github.com/fsnotify/fsnotify"
)
type FileWriterFunc func(name string, contents []byte) error
func FileWriter(fileName string, contents []byte) error {
return os.WriteFile(fileName, contents, 0o644)
}
func WriterFileWriter(w io.Writer) FileWriterFunc {
return func(_ string, contents []byte) error {
_, err := w.Write(contents)
return err
}
}
func NewFSEventHandler(
log *slog.Logger,
dir string,
devMode bool,
genOpts []generator.GenerateOpt,
genSourceMapVis bool,
keepOrphanedFiles bool,
fileWriter FileWriterFunc,
lazy bool,
) *FSEventHandler {
if !path.IsAbs(dir) {
dir, _ = filepath.Abs(dir)
}
fseh := &FSEventHandler{
Log: log,
dir: dir,
fileNameToLastModTime: make(map[string]time.Time),
fileNameToLastModTimeMutex: &sync.Mutex{},
fileNameToError: make(map[string]struct{}),
fileNameToErrorMutex: &sync.Mutex{},
fileNameToOutput: make(map[string]generator.GeneratorOutput),
fileNameToOutputMutex: &sync.Mutex{},
devMode: devMode,
hashes: make(map[string][sha256.Size]byte),
hashesMutex: &sync.Mutex{},
genOpts: genOpts,
genSourceMapVis: genSourceMapVis,
keepOrphanedFiles: keepOrphanedFiles,
writer: fileWriter,
lazy: lazy,
}
return fseh
}
type FSEventHandler struct {
Log *slog.Logger
// dir is the root directory being processed.
dir string
fileNameToLastModTime map[string]time.Time
fileNameToLastModTimeMutex *sync.Mutex
fileNameToError map[string]struct{}
fileNameToErrorMutex *sync.Mutex
fileNameToOutput map[string]generator.GeneratorOutput
fileNameToOutputMutex *sync.Mutex
devMode bool
hashes map[string][sha256.Size]byte
hashesMutex *sync.Mutex
genOpts []generator.GenerateOpt
genSourceMapVis bool
Errors []error
keepOrphanedFiles bool
writer func(string, []byte) error
lazy bool
}
type GenerateResult struct {
// Updated indicates that the file was updated.
Updated bool
// GoUpdated indicates that Go expressions were updated.
GoUpdated bool
// TextUpdated indicates that text literals were updated.
TextUpdated bool
}
func (h *FSEventHandler) HandleEvent(ctx context.Context, event fsnotify.Event) (result GenerateResult, err error) {
// Handle _templ.go files.
if !event.Has(fsnotify.Remove) && strings.HasSuffix(event.Name, "_templ.go") {
_, err = os.Stat(strings.TrimSuffix(event.Name, "_templ.go") + ".templ")
if !os.IsNotExist(err) {
return GenerateResult{}, err
}
// File is orphaned.
if h.keepOrphanedFiles {
return GenerateResult{}, nil
}
h.Log.Debug("Deleting orphaned Go file", slog.String("file", event.Name))
if err = os.Remove(event.Name); err != nil {
h.Log.Warn("Failed to remove orphaned file", slog.Any("error", err))
}
return GenerateResult{Updated: true, GoUpdated: true, TextUpdated: false}, nil
}
// Handle _templ.txt files.
if !event.Has(fsnotify.Remove) && strings.HasSuffix(event.Name, "_templ.txt") {
if h.devMode {
// Don't delete the file in dev mode, ignore changes to it, since the .templ file
// must have been updated in order to trigger a change in the _templ.txt file.
return GenerateResult{Updated: false, GoUpdated: false, TextUpdated: false}, nil
}
h.Log.Debug("Deleting watch mode file", slog.String("file", event.Name))
if err = os.Remove(event.Name); err != nil {
h.Log.Warn("Failed to remove watch mode text file", slog.Any("error", err))
return GenerateResult{}, nil
}
return GenerateResult{}, nil
}
// If the file hasn't been updated since the last time we processed it, ignore it.
lastModTime, updatedModTime := h.UpsertLastModTime(event.Name)
if !updatedModTime {
h.Log.Debug("Skipping file because it wasn't updated", slog.String("file", event.Name))
return GenerateResult{}, nil
}
// Process anything that isn't a templ file.
if !strings.HasSuffix(event.Name, ".templ") {
// If it's a Go file, mark it as updated.
if strings.HasSuffix(event.Name, ".go") {
result.GoUpdated = true
}
result.Updated = true
return result, nil
}
// Handle templ files.
// If the go file is newer than the templ file, skip generation, because it's up-to-date.
if h.lazy && goFileIsUpToDate(event.Name, lastModTime) {
h.Log.Debug("Skipping file because the Go file is up-to-date", slog.String("file", event.Name))
return GenerateResult{}, nil
}
// Start a processor.
start := time.Now()
var diag []parser.Diagnostic
result, diag, err = h.generate(ctx, event.Name)
if err != nil {
h.SetError(event.Name, true)
return result, fmt.Errorf("failed to generate code for %q: %w", event.Name, err)
}
if len(diag) > 0 {
for _, d := range diag {
h.Log.Warn(d.Message,
slog.String("from", fmt.Sprintf("%d:%d", d.Range.From.Line, d.Range.From.Col)),
slog.String("to", fmt.Sprintf("%d:%d", d.Range.To.Line, d.Range.To.Col)),
)
}
return result, nil
}
if errorCleared, errorCount := h.SetError(event.Name, false); errorCleared {
h.Log.Info("Error cleared", slog.String("file", event.Name), slog.Int("errors", errorCount))
}
h.Log.Debug("Generated code", slog.String("file", event.Name), slog.Duration("in", time.Since(start)))
return result, nil
}
func goFileIsUpToDate(templFileName string, templFileLastMod time.Time) (upToDate bool) {
goFileName := strings.TrimSuffix(templFileName, ".templ") + "_templ.go"
goFileInfo, err := os.Stat(goFileName)
if err != nil {
return false
}
return goFileInfo.ModTime().After(templFileLastMod)
}
func (h *FSEventHandler) SetError(fileName string, hasError bool) (previouslyHadError bool, errorCount int) {
h.fileNameToErrorMutex.Lock()
defer h.fileNameToErrorMutex.Unlock()
_, previouslyHadError = h.fileNameToError[fileName]
delete(h.fileNameToError, fileName)
if hasError {
h.fileNameToError[fileName] = struct{}{}
}
return previouslyHadError, len(h.fileNameToError)
}
func (h *FSEventHandler) UpsertLastModTime(fileName string) (modTime time.Time, updated bool) {
fileInfo, err := os.Stat(fileName)
if err != nil {
return modTime, false
}
h.fileNameToLastModTimeMutex.Lock()
defer h.fileNameToLastModTimeMutex.Unlock()
previousModTime := h.fileNameToLastModTime[fileName]
currentModTime := fileInfo.ModTime()
if !currentModTime.After(previousModTime) {
return currentModTime, false
}
h.fileNameToLastModTime[fileName] = currentModTime
return currentModTime, true
}
func (h *FSEventHandler) UpsertHash(fileName string, hash [sha256.Size]byte) (updated bool) {
h.hashesMutex.Lock()
defer h.hashesMutex.Unlock()
lastHash := h.hashes[fileName]
if lastHash == hash {
return false
}
h.hashes[fileName] = hash
return true
}
// generate Go code for a single template.
// If a basePath is provided, the filename included in error messages is relative to it.
func (h *FSEventHandler) generate(ctx context.Context, fileName string) (result GenerateResult, diagnostics []parser.Diagnostic, err error) {
t, err := parser.Parse(fileName)
if err != nil {
return GenerateResult{}, nil, fmt.Errorf("%s parsing error: %w", fileName, err)
}
targetFileName := strings.TrimSuffix(fileName, ".templ") + "_templ.go"
// Only use relative filenames to the basepath for filenames in runtime error messages.
absFilePath, err := filepath.Abs(fileName)
if err != nil {
return GenerateResult{}, nil, fmt.Errorf("failed to get absolute path for %q: %w", fileName, err)
}
relFilePath, err := filepath.Rel(h.dir, absFilePath)
if err != nil {
return GenerateResult{}, nil, fmt.Errorf("failed to get relative path for %q: %w", fileName, err)
}
// Convert Windows file paths to Unix-style for consistency.
relFilePath = filepath.ToSlash(relFilePath)
var b bytes.Buffer
generatorOutput, err := generator.Generate(t, &b, append(h.genOpts, generator.WithFileName(relFilePath))...)
if err != nil {
return GenerateResult{}, nil, fmt.Errorf("%s generation error: %w", fileName, err)
}
formattedGoCode, err := format.Source(b.Bytes())
if err != nil {
err = remapErrorList(err, generatorOutput.SourceMap, fileName)
return GenerateResult{}, nil, fmt.Errorf("%s source formatting error %w", fileName, err)
}
// Hash output, and write out the file if the goCodeHash has changed.
goCodeHash := sha256.Sum256(formattedGoCode)
if h.UpsertHash(targetFileName, goCodeHash) {
result.Updated = true
if err = h.writer(targetFileName, formattedGoCode); err != nil {
return result, nil, fmt.Errorf("failed to write target file %q: %w", targetFileName, err)
}
}
// Add the txt file if it has changed.
if h.devMode {
txtFileName := strings.TrimSuffix(fileName, ".templ") + "_templ.txt"
joined := strings.Join(generatorOutput.Literals, "\n")
txtHash := sha256.Sum256([]byte(joined))
if h.UpsertHash(txtFileName, txtHash) {
result.TextUpdated = true
if err = os.WriteFile(txtFileName, []byte(joined), 0o644); err != nil {
return result, nil, fmt.Errorf("failed to write string literal file %q: %w", txtFileName, err)
}
}
// Check whether the change would require a recompilation to take effect.
h.fileNameToOutputMutex.Lock()
defer h.fileNameToOutputMutex.Unlock()
previous := h.fileNameToOutput[fileName]
if generator.HasChanged(previous, generatorOutput) {
result.GoUpdated = true
}
h.fileNameToOutput[fileName] = generatorOutput
}
parsedDiagnostics, err := parser.Diagnose(t)
if err != nil {
return result, nil, fmt.Errorf("%s diagnostics error: %w", fileName, err)
}
if h.genSourceMapVis {
err = generateSourceMapVisualisation(ctx, fileName, targetFileName, generatorOutput.SourceMap)
}
return result, parsedDiagnostics, err
}
// Takes an error from the formatter and attempts to convert the positions reported in the target file to their positions
// in the source file.
func remapErrorList(err error, sourceMap *parser.SourceMap, fileName string) error {
list, ok := err.(scanner.ErrorList)
if !ok || len(list) == 0 {
return err
}
for i, e := range list {
// The positions in the source map are off by one line because of the package definition.
srcPos, ok := sourceMap.SourcePositionFromTarget(uint32(e.Pos.Line-1), uint32(e.Pos.Column))
if !ok {
continue
}
list[i].Pos = token.Position{
Filename: fileName,
Offset: int(srcPos.Index),
Line: int(srcPos.Line) + 1,
Column: int(srcPos.Col),
}
}
return list
}
func generateSourceMapVisualisation(ctx context.Context, templFileName, goFileName string, sourceMap *parser.SourceMap) error {
if err := ctx.Err(); err != nil {
return err
}
var templContents, goContents []byte
var templErr, goErr error
var wg sync.WaitGroup
wg.Add(2)
go func() {
defer wg.Done()
templContents, templErr = os.ReadFile(templFileName)
}()
go func() {
defer wg.Done()
goContents, goErr = os.ReadFile(goFileName)
}()
wg.Wait()
if templErr != nil {
return templErr
}
if goErr != nil {
return templErr
}
targetFileName := strings.TrimSuffix(templFileName, ".templ") + "_templ_sourcemap.html"
w, err := os.Create(targetFileName)
if err != nil {
return fmt.Errorf("%s sourcemap visualisation error: %w", templFileName, err)
}
defer w.Close()
b := bufio.NewWriter(w)
defer b.Flush()
return visualize.HTML(templFileName, string(templContents), string(goContents), sourceMap).Render(ctx, b)
}
package generatecmd
type FatalError struct {
Err error
}
func (e FatalError) Error() string {
return e.Err.Error()
}
func (e FatalError) Unwrap() error {
return e.Err
}
func (e FatalError) Is(target error) bool {
_, ok := target.(FatalError)
return ok
}
func (e FatalError) As(target any) bool {
_, ok := target.(*FatalError)
return ok
}
package generatecmd
import (
"context"
_ "embed"
"log/slog"
_ "net/http/pprof"
)
type Arguments struct {
FileName string
FileWriter FileWriterFunc
Path string
Watch bool
WatchPattern string
OpenBrowser bool
Command string
ProxyBind string
ProxyPort int
Proxy string
NotifyProxy bool
WorkerCount int
GenerateSourceMapVisualisations bool
IncludeVersion bool
IncludeTimestamp bool
// PPROFPort is the port to run the pprof server on.
PPROFPort int
KeepOrphanedFiles bool
Lazy bool
}
func Run(ctx context.Context, log *slog.Logger, args Arguments) (err error) {
g, err := NewGenerate(log, args)
if err != nil {
return err
}
return g.Run(ctx)
}
package modcheck
import (
"fmt"
"os"
"path/filepath"
"github.com/a-h/templ"
"golang.org/x/mod/modfile"
"golang.org/x/mod/semver"
)
// WalkUp the directory tree, starting at dir, until we find a directory containing
// a go.mod file.
func WalkUp(dir string) (string, error) {
dir, err := filepath.Abs(dir)
if err != nil {
return "", fmt.Errorf("failed to get absolute path: %w", err)
}
var modFile string
for {
modFile = filepath.Join(dir, "go.mod")
_, err := os.Stat(modFile)
if err != nil && !os.IsNotExist(err) {
return "", fmt.Errorf("failed to stat go.mod file: %w", err)
}
if os.IsNotExist(err) {
// Move up.
prev := dir
dir = filepath.Dir(dir)
if dir == prev {
break
}
continue
}
break
}
// No file found.
if modFile == "" {
return dir, fmt.Errorf("could not find go.mod file")
}
return dir, nil
}
func Check(dir string) error {
dir, err := WalkUp(dir)
if err != nil {
return err
}
// Found a go.mod file.
// Read it and find the templ version.
modFile := filepath.Join(dir, "go.mod")
m, err := os.ReadFile(modFile)
if err != nil {
return fmt.Errorf("failed to read go.mod file: %w", err)
}
mf, err := modfile.Parse(modFile, m, nil)
if err != nil {
return fmt.Errorf("failed to parse go.mod file: %w", err)
}
if mf.Module.Mod.Path == "github.com/a-h/templ" {
// The go.mod file is for templ itself.
return nil
}
for _, r := range mf.Require {
if r.Mod.Path == "github.com/a-h/templ" {
cmp := semver.Compare(r.Mod.Version, templ.Version())
if cmp < 0 {
return fmt.Errorf("generator %v is newer than templ version %v found in go.mod file, consider running `go get -u github.com/a-h/templ` to upgrade", templ.Version(), r.Mod.Version)
}
if cmp > 0 {
return fmt.Errorf("generator %v is older than templ version %v found in go.mod file, consider upgrading templ CLI", templ.Version(), r.Mod.Version)
}
return nil
}
}
return fmt.Errorf("templ not found in go.mod file, run `go get github.com/a-h/templ` to install it")
}
package proxy
import (
"bytes"
"compress/gzip"
"fmt"
"html"
"io"
stdlog "log"
"log/slog"
"math"
"net/http"
"net/http/httputil"
"net/url"
"os"
"strconv"
"strings"
"time"
"github.com/PuerkitoBio/goquery"
"github.com/a-h/templ/cmd/templ/generatecmd/sse"
"github.com/andybalholm/brotli"
_ "embed"
)
//go:embed script.js
var script string
type Handler struct {
log *slog.Logger
URL string
Target *url.URL
p *httputil.ReverseProxy
sse *sse.Handler
}
func getScriptTag(nonce string) string {
if nonce != "" {
var sb strings.Builder
sb.WriteString(`<script src="/_templ/reload/script.js" nonce="`)
sb.WriteString(html.EscapeString(nonce))
sb.WriteString(`"></script>`)
return sb.String()
}
return `<script src="/_templ/reload/script.js"></script>`
}
func insertScriptTagIntoBody(nonce, body string) (updated string) {
doc, err := goquery.NewDocumentFromReader(strings.NewReader(body))
if err != nil {
return strings.Replace(body, "</body>", getScriptTag(nonce)+"</body>", -1)
}
doc.Find("body").AppendHtml(getScriptTag(nonce))
r, err := doc.Html()
if err != nil {
return strings.Replace(body, "</body>", getScriptTag(nonce)+"</body>", -1)
}
return r
}
type passthroughWriteCloser struct {
io.Writer
}
func (pwc passthroughWriteCloser) Close() error {
return nil
}
const unsupportedContentEncoding = "Unsupported content encoding, hot reload script not inserted."
func (h *Handler) modifyResponse(r *http.Response) error {
log := h.log.With(slog.String("url", r.Request.URL.String()))
if r.Header.Get("templ-skip-modify") == "true" {
log.Debug("Skipping response modification because templ-skip-modify header is set")
return nil
}
if contentType := r.Header.Get("Content-Type"); !strings.HasPrefix(contentType, "text/html") {
log.Debug("Skipping response modification because content type is not text/html", slog.String("content-type", contentType))
return nil
}
// Set up readers and writers.
newReader := func(in io.Reader) (out io.Reader, err error) {
return in, nil
}
newWriter := func(out io.Writer) io.WriteCloser {
return passthroughWriteCloser{out}
}
switch r.Header.Get("Content-Encoding") {
case "gzip":
newReader = func(in io.Reader) (out io.Reader, err error) {
return gzip.NewReader(in)
}
newWriter = func(out io.Writer) io.WriteCloser {
return gzip.NewWriter(out)
}
case "br":
newReader = func(in io.Reader) (out io.Reader, err error) {
return brotli.NewReader(in), nil
}
newWriter = func(out io.Writer) io.WriteCloser {
return brotli.NewWriter(out)
}
case "":
log.Debug("No content encoding header found")
default:
h.log.Warn(unsupportedContentEncoding, slog.String("encoding", r.Header.Get("Content-Encoding")))
}
// Read the encoded body.
encr, err := newReader(r.Body)
if err != nil {
return err
}
defer r.Body.Close()
body, err := io.ReadAll(encr)
if err != nil {
return err
}
// Update it.
csp := r.Header.Get("Content-Security-Policy")
updated := insertScriptTagIntoBody(parseNonce(csp), string(body))
if log.Enabled(r.Request.Context(), slog.LevelDebug) {
if len(updated) == len(body) {
log.Debug("Reload script not inserted")
} else {
log.Debug("Reload script inserted")
}
}
// Encode the response.
var buf bytes.Buffer
encw := newWriter(&buf)
_, err = encw.Write([]byte(updated))
if err != nil {
return err
}
err = encw.Close()
if err != nil {
return err
}
// Update the response.
r.Body = io.NopCloser(&buf)
r.ContentLength = int64(buf.Len())
r.Header.Set("Content-Length", strconv.Itoa(buf.Len()))
return nil
}
func parseNonce(csp string) (nonce string) {
outer:
for _, rawDirective := range strings.Split(csp, ";") {
parts := strings.Fields(rawDirective)
if len(parts) < 2 {
continue
}
if parts[0] != "script-src" {
continue
}
for _, source := range parts[1:] {
source = strings.TrimPrefix(source, "'")
source = strings.TrimSuffix(source, "'")
if strings.HasPrefix(source, "nonce-") {
nonce = source[6:]
break outer
}
}
}
return nonce
}
func New(log *slog.Logger, bind string, port int, target *url.URL) (h *Handler) {
p := httputil.NewSingleHostReverseProxy(target)
p.ErrorLog = stdlog.New(os.Stderr, "Proxy to target error: ", 0)
p.Transport = &roundTripper{
maxRetries: 20,
initialDelay: 100 * time.Millisecond,
backoffExponent: 1.5,
}
h = &Handler{
log: log,
URL: fmt.Sprintf("http://%s:%d", bind, port),
Target: target,
p: p,
sse: sse.New(),
}
p.ModifyResponse = h.modifyResponse
return h
}
func (p *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/_templ/reload/script.js" {
// Provides a script that reloads the page.
w.Header().Add("Content-Type", "text/javascript")
_, err := io.WriteString(w, script)
if err != nil {
fmt.Printf("failed to write script: %v\n", err)
}
return
}
if r.URL.Path == "/_templ/reload/events" {
switch r.Method {
case http.MethodGet:
// Provides a list of messages including a reload message.
p.sse.ServeHTTP(w, r)
return
case http.MethodPost:
// Send a reload message to all connected clients.
p.sse.Send("message", "reload")
return
}
http.Error(w, "only GET or POST method allowed", http.StatusMethodNotAllowed)
return
}
p.p.ServeHTTP(w, r)
}
func (p *Handler) SendSSE(eventType string, data string) {
p.sse.Send(eventType, data)
}
type roundTripper struct {
maxRetries int
initialDelay time.Duration
backoffExponent float64
}
func (rt *roundTripper) setShouldSkipResponseModificationHeader(r *http.Request, resp *http.Response) {
// Instruct the modifyResponse function to skip modifying the response if the
// HTTP request has come from HTMX.
if r.Header.Get("HX-Request") != "true" {
return
}
resp.Header.Set("templ-skip-modify", "true")
}
func (rt *roundTripper) RoundTrip(r *http.Request) (*http.Response, error) {
// Read and buffer the body.
var bodyBytes []byte
if r.Body != nil && r.Body != http.NoBody {
var err error
bodyBytes, err = io.ReadAll(r.Body)
if err != nil {
return nil, err
}
r.Body.Close()
}
// Retry logic.
var resp *http.Response
var err error
for retries := 0; retries < rt.maxRetries; retries++ {
// Clone the request and set the body.
req := r.Clone(r.Context())
if bodyBytes != nil {
req.Body = io.NopCloser(bytes.NewReader(bodyBytes))
}
// Execute the request.
resp, err = http.DefaultTransport.RoundTrip(req)
if err != nil {
time.Sleep(rt.initialDelay * time.Duration(math.Pow(rt.backoffExponent, float64(retries))))
continue
}
rt.setShouldSkipResponseModificationHeader(r, resp)
return resp, nil
}
return nil, fmt.Errorf("max retries reached: %q", r.URL.String())
}
func NotifyProxy(host string, port int) error {
urlStr := fmt.Sprintf("http://%s:%d/_templ/reload/events", host, port)
req, err := http.NewRequest(http.MethodPost, urlStr, nil)
if err != nil {
return err
}
_, err = http.DefaultClient.Do(req)
return err
}
//go:build unix
package run
import (
"context"
"errors"
"fmt"
"os"
"os/exec"
"strings"
"sync"
"syscall"
"time"
)
var (
m = &sync.Mutex{}
running = map[string]*exec.Cmd{}
)
func KillAll() (err error) {
m.Lock()
defer m.Unlock()
var errs []error
for _, cmd := range running {
if err := kill(cmd); err != nil {
errs = append(errs, fmt.Errorf("failed to kill process %d: %w", cmd.Process.Pid, err))
}
}
running = map[string]*exec.Cmd{}
return errors.Join(errs...)
}
func kill(cmd *exec.Cmd) (err error) {
errs := make([]error, 4)
errs[0] = ignoreExited(cmd.Process.Signal(syscall.SIGINT))
errs[1] = ignoreExited(cmd.Process.Signal(syscall.SIGTERM))
errs[2] = ignoreExited(cmd.Wait())
errs[3] = ignoreExited(syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL))
return errors.Join(errs...)
}
func ignoreExited(err error) error {
if errors.Is(err, syscall.ESRCH) {
return nil
}
// Ignore *exec.ExitError
if _, ok := err.(*exec.ExitError); ok {
return nil
}
return err
}
func Run(ctx context.Context, workingDir string, input string) (cmd *exec.Cmd, err error) {
m.Lock()
defer m.Unlock()
cmd, ok := running[input]
if ok {
if err := kill(cmd); err != nil {
return cmd, fmt.Errorf("failed to kill process %d: %w", cmd.Process.Pid, err)
}
delete(running, input)
}
parts := strings.Fields(input)
executable := parts[0]
args := []string{}
if len(parts) > 1 {
args = append(args, parts[1:]...)
}
cmd = exec.CommandContext(ctx, executable, args...)
// Wait for the process to finish gracefully before termination.
cmd.WaitDelay = time.Second * 3
cmd.Env = os.Environ()
cmd.Dir = workingDir
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
running[input] = cmd
err = cmd.Start()
return
}
package sse
import (
_ "embed"
"fmt"
"net/http"
"sync"
"sync/atomic"
"time"
)
func New() *Handler {
return &Handler{
m: new(sync.Mutex),
requests: map[int64]chan event{},
}
}
type Handler struct {
m *sync.Mutex
counter int64
requests map[int64]chan event
}
type event struct {
Type string
Data string
}
// Send an event to all connected clients.
func (s *Handler) Send(eventType string, data string) {
s.m.Lock()
defer s.m.Unlock()
for _, f := range s.requests {
f := f
go func(f chan event) {
f <- event{
Type: eventType,
Data: data,
}
}(f)
}
}
func (s *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
w.Header().Set("Content-Type", "text/event-stream")
w.Header().Set("Cache-Control", "no-cache")
w.Header().Set("Connection", "keep-alive")
id := atomic.AddInt64(&s.counter, 1)
s.m.Lock()
events := make(chan event)
s.requests[id] = events
s.m.Unlock()
defer func() {
s.m.Lock()
defer s.m.Unlock()
delete(s.requests, id)
close(events)
}()
timer := time.NewTimer(0)
loop:
for {
select {
case <-timer.C:
if _, err := fmt.Fprintf(w, "event: message\ndata: ping\n\n"); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
timer.Reset(time.Second * 5)
case e := <-events:
if _, err := fmt.Fprintf(w, "event: %s\ndata: %s\n\n", e.Type, e.Data); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
case <-r.Context().Done():
break loop
}
w.(http.Flusher).Flush()
}
}
package watcher
import (
"context"
"io/fs"
"os"
"path"
"path/filepath"
"regexp"
"strings"
"sync"
"time"
"github.com/fsnotify/fsnotify"
)
func Recursive(
ctx context.Context,
path string,
watchPattern *regexp.Regexp,
out chan fsnotify.Event,
errors chan error,
) (w *RecursiveWatcher, err error) {
fsnw, err := fsnotify.NewWatcher()
if err != nil {
return nil, err
}
w = NewRecursiveWatcher(ctx, fsnw, watchPattern, out, errors)
go w.loop()
return w, w.Add(path)
}
func NewRecursiveWatcher(ctx context.Context, w *fsnotify.Watcher, watchPattern *regexp.Regexp, events chan fsnotify.Event, errors chan error) *RecursiveWatcher {
return &RecursiveWatcher{
ctx: ctx,
w: w,
WatchPattern: watchPattern,
Events: events,
Errors: errors,
timers: make(map[timerKey]*time.Timer),
}
}
// WalkFiles walks the file tree rooted at path, sending a Create event for each
// file it encounters.
func WalkFiles(ctx context.Context, path string, watchPattern *regexp.Regexp, out chan fsnotify.Event) (err error) {
rootPath := path
fileSystem := os.DirFS(rootPath)
return fs.WalkDir(fileSystem, ".", func(path string, info os.DirEntry, err error) error {
if err != nil {
return nil
}
absPath, err := filepath.Abs(filepath.Join(rootPath, path))
if err != nil {
return nil
}
if info.IsDir() && shouldSkipDir(absPath) {
return filepath.SkipDir
}
if !watchPattern.MatchString(absPath) {
return nil
}
out <- fsnotify.Event{
Name: absPath,
Op: fsnotify.Create,
}
return nil
})
}
type RecursiveWatcher struct {
ctx context.Context
w *fsnotify.Watcher
WatchPattern *regexp.Regexp
Events chan fsnotify.Event
Errors chan error
timerMu sync.Mutex
timers map[timerKey]*time.Timer
}
type timerKey struct {
name string
op fsnotify.Op
}
func timerKeyFromEvent(event fsnotify.Event) timerKey {
return timerKey{
name: event.Name,
op: event.Op,
}
}
func (w *RecursiveWatcher) Close() error {
return w.w.Close()
}
func (w *RecursiveWatcher) loop() {
for {
select {
case <-w.ctx.Done():
return
case event, ok := <-w.w.Events:
if !ok {
return
}
if event.Has(fsnotify.Create) {
if err := w.Add(event.Name); err != nil {
w.Errors <- err
}
}
// Only notify on templ related files.
if !w.WatchPattern.MatchString(event.Name) {
continue
}
tk := timerKeyFromEvent(event)
w.timerMu.Lock()
t, ok := w.timers[tk]
w.timerMu.Unlock()
if !ok {
t = time.AfterFunc(100*time.Millisecond, func() {
w.Events <- event
})
w.timerMu.Lock()
w.timers[tk] = t
w.timerMu.Unlock()
continue
}
t.Reset(100 * time.Millisecond)
case err, ok := <-w.w.Errors:
if !ok {
return
}
w.Errors <- err
}
}
}
func (w *RecursiveWatcher) Add(dir string) error {
return filepath.WalkDir(dir, func(dir string, info os.DirEntry, err error) error {
if err != nil {
return nil
}
if !info.IsDir() {
return nil
}
if shouldSkipDir(dir) {
return filepath.SkipDir
}
return w.w.Add(dir)
})
}
func shouldSkipDir(dir string) bool {
if dir == "." {
return false
}
if dir == "vendor" || dir == "node_modules" {
return true
}
_, name := path.Split(dir)
// These directories are ignored by the Go tool.
if strings.HasPrefix(name, ".") || strings.HasPrefix(name, "_") {
return true
}
return false
}
package imports
import (
"bytes"
"fmt"
"go/ast"
"go/format"
"go/token"
"path"
"slices"
"strconv"
"strings"
goparser "go/parser"
"golang.org/x/sync/errgroup"
"golang.org/x/tools/go/ast/astutil"
"golang.org/x/tools/imports"
"github.com/a-h/templ/generator"
"github.com/a-h/templ/parser/v2"
)
var internalImports = []string{"github.com/a-h/templ", "github.com/a-h/templ/runtime"}
func convertTemplToGoURI(templURI string) (isTemplFile bool, goURI string) {
base, fileName := path.Split(templURI)
if !strings.HasSuffix(fileName, ".templ") {
return
}
return true, base + (strings.TrimSuffix(fileName, ".templ") + "_templ.go")
}
var fset = token.NewFileSet()
func updateImports(name, src string) (updated []*ast.ImportSpec, err error) {
// Apply auto imports.
updatedGoCode, err := imports.Process(name, []byte(src), nil)
if err != nil {
return updated, fmt.Errorf("failed to process go code %q: %w", src, err)
}
// Get updated imports.
gofile, err := goparser.ParseFile(fset, name, updatedGoCode, goparser.ImportsOnly)
if err != nil {
return updated, fmt.Errorf("failed to get imports from updated go code: %w", err)
}
for _, imp := range gofile.Imports {
if !slices.Contains(internalImports, strings.Trim(imp.Path.Value, "\"")) {
updated = append(updated, imp)
}
}
return updated, nil
}
func Process(t parser.TemplateFile) (parser.TemplateFile, error) {
if t.Filepath == "" {
return t, nil
}
isTemplFile, fileName := convertTemplToGoURI(t.Filepath)
if !isTemplFile {
return t, fmt.Errorf("invalid filepath: %s", t.Filepath)
}
// The first node always contains existing imports.
// If there isn't one, create it.
if len(t.Nodes) == 0 {
t.Nodes = append(t.Nodes, parser.TemplateFileGoExpression{})
}
// If there is one, ensure it is a Go expression.
if _, ok := t.Nodes[0].(parser.TemplateFileGoExpression); !ok {
t.Nodes = append([]parser.TemplateFileNode{parser.TemplateFileGoExpression{}}, t.Nodes...)
}
// Find all existing imports.
importsNode := t.Nodes[0].(parser.TemplateFileGoExpression)
// Generate code.
gw := bytes.NewBuffer(nil)
var updatedImports []*ast.ImportSpec
var eg errgroup.Group
eg.Go(func() (err error) {
if _, err := generator.Generate(t, gw); err != nil {
return fmt.Errorf("failed to generate go code: %w", err)
}
updatedImports, err = updateImports(fileName, gw.String())
if err != nil {
return fmt.Errorf("failed to get imports from generated go code: %w", err)
}
return nil
})
var firstGoNodeInTemplate *ast.File
// Update the template with the imports.
// Ensure that there is a Go expression to add the imports to as the first node.
eg.Go(func() (err error) {
firstGoNodeInTemplate, err = goparser.ParseFile(fset, fileName, t.Package.Expression.Value+"\n"+importsNode.Expression.Value, goparser.AllErrors|goparser.ParseComments)
if err != nil {
return fmt.Errorf("failed to parse imports section: %w", err)
}
return nil
})
// Wait for completion of both parts.
if err := eg.Wait(); err != nil {
return t, err
}
// Delete unused imports.
for _, imp := range firstGoNodeInTemplate.Imports {
if !containsImport(updatedImports, imp) {
name, path, err := getImportDetails(imp)
if err != nil {
return t, err
}
astutil.DeleteNamedImport(fset, firstGoNodeInTemplate, name, path)
}
}
// Add imports, if there are any to add.
for _, imp := range updatedImports {
if !containsImport(firstGoNodeInTemplate.Imports, imp) {
name, path, err := getImportDetails(imp)
if err != nil {
return t, err
}
astutil.AddNamedImport(fset, firstGoNodeInTemplate, name, path)
}
}
// Edge case: reinsert the import to use import syntax without parentheses.
if len(firstGoNodeInTemplate.Imports) == 1 {
name, path, err := getImportDetails(firstGoNodeInTemplate.Imports[0])
if err != nil {
return t, err
}
astutil.DeleteNamedImport(fset, firstGoNodeInTemplate, name, path)
astutil.AddNamedImport(fset, firstGoNodeInTemplate, name, path)
}
// Write out the Go code with the imports.
updatedGoCode := new(strings.Builder)
err := format.Node(updatedGoCode, fset, firstGoNodeInTemplate)
if err != nil {
return t, fmt.Errorf("failed to write updated go code: %w", err)
}
// Remove the package statement from the node, by cutting the first line of the file.
importsNode.Expression.Value = strings.TrimSpace(strings.SplitN(updatedGoCode.String(), "\n", 2)[1])
if len(updatedImports) == 0 && importsNode.Expression.Value == "" {
t.Nodes = t.Nodes[1:]
return t, nil
}
t.Nodes[0] = importsNode
return t, nil
}
func getImportDetails(imp *ast.ImportSpec) (name, importPath string, err error) {
if imp.Name != nil {
name = imp.Name.Name
}
if imp.Path != nil {
importPath, err = strconv.Unquote(imp.Path.Value)
if err != nil {
err = fmt.Errorf("failed to unquote package path %s: %w", imp.Path.Value, err)
return
}
}
return name, importPath, nil
}
func containsImport(imports []*ast.ImportSpec, spec *ast.ImportSpec) bool {
for _, imp := range imports {
if imp.Path.Value == spec.Path.Value {
return true
}
}
return false
}
package infocmd
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"log/slog"
"os"
"os/exec"
"runtime"
"strings"
"github.com/a-h/templ"
"github.com/a-h/templ/cmd/templ/lspcmd/pls"
)
type Arguments struct {
JSON bool `flag:"json" help:"Output info as JSON."`
}
type Info struct {
OS struct {
GOOS string `json:"goos"`
GOARCH string `json:"goarch"`
} `json:"os"`
Go ToolInfo `json:"go"`
Gopls ToolInfo `json:"gopls"`
Templ ToolInfo `json:"templ"`
}
type ToolInfo struct {
Location string `json:"location"`
Version string `json:"version"`
OK bool `json:"ok"`
Message string `json:"message,omitempty"`
}
func getGoInfo() (d ToolInfo) {
// Find Go.
var err error
d.Location, err = exec.LookPath("go")
if err != nil {
d.Message = fmt.Sprintf("failed to find go: %v", err)
return
}
// Run go to find the version.
cmd := exec.Command(d.Location, "version")
v, err := cmd.Output()
if err != nil {
d.Message = fmt.Sprintf("failed to get go version, check that Go is installed: %v", err)
return
}
d.Version = strings.TrimSpace(string(v))
d.OK = true
return
}
func getGoplsInfo() (d ToolInfo) {
var err error
d.Location, err = pls.FindGopls()
if err != nil {
d.Message = fmt.Sprintf("failed to find gopls: %v", err)
return
}
cmd := exec.Command(d.Location, "version")
v, err := cmd.Output()
if err != nil {
d.Message = fmt.Sprintf("failed to get gopls version: %v", err)
return
}
d.Version = strings.TrimSpace(string(v))
d.OK = true
return
}
func getTemplInfo() (d ToolInfo) {
// Find templ.
var err error
d.Location, err = findTempl()
if err != nil {
d.Message = err.Error()
return
}
// Run templ to find the version.
cmd := exec.Command(d.Location, "version")
v, err := cmd.Output()
if err != nil {
d.Message = fmt.Sprintf("failed to get templ version: %v", err)
return
}
d.Version = strings.TrimSpace(string(v))
if d.Version != templ.Version() {
d.Message = fmt.Sprintf("version mismatch - you're running %q at the command line, but the version in the path is %q", templ.Version(), d.Version)
return
}
d.OK = true
return
}
func findTempl() (location string, err error) {
executableName := "templ"
if runtime.GOOS == "windows" {
executableName = "templ.exe"
}
executableName, err = exec.LookPath(executableName)
if err == nil {
// Found on the path.
return executableName, nil
}
// Unexpected error.
if !errors.Is(err, exec.ErrNotFound) {
return "", fmt.Errorf("unexpected error looking for templ: %w", err)
}
return "", fmt.Errorf("templ is not in the path (%q). You can install templ with `go install github.com/a-h/templ/cmd/templ@latest`", os.Getenv("PATH"))
}
func getInfo() (d Info) {
d.OS.GOOS = runtime.GOOS
d.OS.GOARCH = runtime.GOARCH
d.Go = getGoInfo()
d.Gopls = getGoplsInfo()
d.Templ = getTemplInfo()
return
}
func Run(ctx context.Context, log *slog.Logger, stdout io.Writer, args Arguments) (err error) {
info := getInfo()
if args.JSON {
enc := json.NewEncoder(stdout)
enc.SetIndent("", " ")
return enc.Encode(info)
}
log.Info("os", slog.String("goos", info.OS.GOOS), slog.String("goarch", info.OS.GOARCH))
logInfo(ctx, log, "go", info.Go)
logInfo(ctx, log, "gopls", info.Gopls)
logInfo(ctx, log, "templ", info.Templ)
return nil
}
func logInfo(ctx context.Context, log *slog.Logger, name string, ti ToolInfo) {
level := slog.LevelInfo
if !ti.OK {
level = slog.LevelError
}
args := []any{
slog.String("location", ti.Location),
slog.String("version", ti.Version),
}
if ti.Message != "" {
args = append(args, slog.String("message", ti.Message))
}
log.Log(ctx, level, name, args...)
}
package httpdebug
import (
"encoding/json"
"io"
"log/slog"
"net/http"
"net/url"
"github.com/a-h/templ"
"github.com/a-h/templ/cmd/templ/lspcmd/proxy"
"github.com/a-h/templ/cmd/templ/visualize"
)
var log *slog.Logger
func NewHandler(l *slog.Logger, s *proxy.Server) http.Handler {
m := http.NewServeMux()
log = l
m.HandleFunc("/templ", func(w http.ResponseWriter, r *http.Request) {
uri := r.URL.Query().Get("uri")
c, ok := s.TemplSource.Get(uri)
if !ok {
Error(w, "uri not found", http.StatusNotFound)
return
}
String(w, c.String())
})
m.HandleFunc("/sourcemap", func(w http.ResponseWriter, r *http.Request) {
uri := r.URL.Query().Get("uri")
sm, ok := s.SourceMapCache.Get(uri)
if !ok {
Error(w, "uri not found", http.StatusNotFound)
return
}
JSON(w, sm.SourceLinesToTarget)
})
m.HandleFunc("/go", func(w http.ResponseWriter, r *http.Request) {
uri := r.URL.Query().Get("uri")
c, ok := s.GoSource[uri]
if !ok {
Error(w, "uri not found", http.StatusNotFound)
return
}
String(w, c)
})
m.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
uri := r.URL.Query().Get("uri")
if uri == "" {
// List all URIs.
if err := list(s.TemplSource.URIs()).Render(r.Context(), w); err != nil {
Error(w, "failed to list URIs", http.StatusInternalServerError)
}
return
}
// Assume we've got a URI.
templSource, ok := s.TemplSource.Get(uri)
if !ok {
if !ok {
Error(w, "uri not found in document contents", http.StatusNotFound)
return
}
}
goSource, ok := s.GoSource[uri]
if !ok {
if !ok {
Error(w, "uri not found in document contents", http.StatusNotFound)
return
}
}
sm, ok := s.SourceMapCache.Get(uri)
if !ok {
Error(w, "uri not found", http.StatusNotFound)
return
}
if err := visualize.HTML(uri, templSource.String(), goSource, sm).Render(r.Context(), w); err != nil {
Error(w, "failed to visualize HTML", http.StatusInternalServerError)
}
})
return m
}
func getMapURL(uri string) templ.SafeURL {
return withQuery("/", uri)
}
func getSourceMapURL(uri string) templ.SafeURL {
return withQuery("/sourcemap", uri)
}
func getTemplURL(uri string) templ.SafeURL {
return withQuery("/templ", uri)
}
func getGoURL(uri string) templ.SafeURL {
return withQuery("/go", uri)
}
func withQuery(path, uri string) templ.SafeURL {
q := make(url.Values)
q.Set("uri", uri)
u := &url.URL{
Path: path,
RawPath: path,
RawQuery: q.Encode(),
}
return templ.SafeURL(u.String())
}
func JSON(w http.ResponseWriter, v any) {
w.Header().Set("Content-Type", "application/json")
enc := json.NewEncoder(w)
enc.SetIndent("", " ")
if err := enc.Encode(v); err != nil {
log.Error("failed to write JSON response", slog.Any("error", err))
}
}
func String(w http.ResponseWriter, s string) {
if _, err := io.WriteString(w, s); err != nil {
log.Error("failed to write string response", slog.Any("error", err))
}
}
func Error(w http.ResponseWriter, msg string, status int) {
w.WriteHeader(status)
if _, err := io.WriteString(w, msg); err != nil {
log.Error("failed to write error response", slog.Any("error", err))
}
}
// Code generated by templ - DO NOT EDIT.
package httpdebug
//lint:file-ignore SA4006 This context is only used if a nested component is present.
import "github.com/a-h/templ"
import templruntime "github.com/a-h/templ/runtime"
func list(uris []string) templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
return templ_7745c5c3_CtxErr
}
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var1 := templ.GetChildren(ctx)
if templ_7745c5c3_Var1 == nil {
templ_7745c5c3_Var1 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 1, "<table><tr><th>File</th><th></th><th></th><th></th><th></th></tr>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
for _, uri := range uris {
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 2, "<tr><td>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var2 string
templ_7745c5c3_Var2, templ_7745c5c3_Err = templ.JoinStringErrs(uri)
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `cmd/templ/lspcmd/httpdebug/list.templ`, Line: 14, Col: 13}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var2))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 3, "</td><td><a href=\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var3 templ.SafeURL = getMapURL(uri)
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(string(templ_7745c5c3_Var3)))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 4, "\">Mapping</a></td><td><a href=\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var4 templ.SafeURL = getSourceMapURL(uri)
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(string(templ_7745c5c3_Var4)))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 5, "\">Source Map</a></td><td><a href=\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var5 templ.SafeURL = getTemplURL(uri)
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(string(templ_7745c5c3_Var5)))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 6, "\">Templ</a></td><td><a href=\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var6 templ.SafeURL = getGoURL(uri)
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(string(templ_7745c5c3_Var6)))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 7, "\">Go</a></td></tr>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 8, "</table>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return nil
})
}
var _ = templruntime.GeneratedTemplate
package lspdiff
import (
"github.com/a-h/templ/lsp/protocol"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
)
// This package provides a way to compare LSP protocol messages, ignoring irrelevant fields.
func Hover(expected, actual protocol.Hover) string {
return cmp.Diff(expected, actual,
cmpopts.IgnoreFields(protocol.Hover{}, "Range"),
cmpopts.IgnoreFields(protocol.MarkupContent{}, "Kind"),
)
}
func CodeAction(expected, actual []protocol.CodeAction) string {
return cmp.Diff(expected, actual)
}
func CompletionList(expected, actual *protocol.CompletionList) string {
return cmp.Diff(expected, actual,
cmpopts.IgnoreFields(protocol.CompletionList{}, "IsIncomplete"),
)
}
func References(expected, actual []protocol.Location) string {
return cmp.Diff(expected, actual)
}
func CompletionListContainsText(cl *protocol.CompletionList, text string) bool {
if cl == nil {
return false
}
for _, item := range cl.Items {
if item.Label == text {
return true
}
}
return false
}
package lspcmd
import (
"context"
"fmt"
"io"
"log/slog"
"net/http"
"os"
"os/signal"
"github.com/a-h/templ/cmd/templ/lspcmd/httpdebug"
"github.com/a-h/templ/cmd/templ/lspcmd/pls"
"github.com/a-h/templ/cmd/templ/lspcmd/proxy"
"github.com/a-h/templ/lsp/jsonrpc2"
"github.com/a-h/templ/lsp/protocol"
_ "net/http/pprof"
)
type Arguments struct {
Log string
GoplsLog string
GoplsRPCTrace bool
// PPROF sets whether to start a profiling server on localhost:9999
PPROF bool
// HTTPDebug sets the HTTP endpoint to listen on. Leave empty for no web debug.
HTTPDebug string
}
func Run(stdin io.Reader, stdout, stderr io.Writer, args Arguments) (err error) {
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, os.Interrupt)
defer func() {
signal.Stop(signalChan)
cancel()
}()
if args.PPROF {
go func() {
_ = http.ListenAndServe("localhost:9999", nil)
}()
}
go func() {
select {
case <-signalChan: // First signal, cancel context.
cancel()
case <-ctx.Done():
}
<-signalChan // Second signal, hard exit.
os.Exit(2)
}()
log := slog.New(slog.NewJSONHandler(io.Discard, nil))
if args.Log != "" {
file, err := os.OpenFile(args.Log, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0600)
if err != nil {
return fmt.Errorf("failed to open log file: %w", err)
}
defer file.Close()
// Create a new logger with a file writer
log = slog.New(slog.NewJSONHandler(file, nil))
log.Debug("Logging to file", slog.String("file", args.Log))
}
templStream := jsonrpc2.NewStream(newStdRwc(log, "templStream", stdout, stdin))
return run(ctx, log, templStream, args)
}
func run(ctx context.Context, log *slog.Logger, templStream jsonrpc2.Stream, args Arguments) (err error) {
log.Info("lsp: starting up...")
defer func() {
if r := recover(); r != nil {
log.Error("handled panic", slog.Any("recovered", r))
}
}()
log.Info("lsp: starting gopls...")
rwc, err := pls.NewGopls(ctx, log, pls.Options{
Log: args.GoplsLog,
RPCTrace: args.GoplsRPCTrace,
})
if err != nil {
log.Error("failed to start gopls", slog.Any("error", err))
os.Exit(1)
}
cache := proxy.NewSourceMapCache()
diagnosticCache := proxy.NewDiagnosticCache()
log.Info("creating gopls client")
clientProxy, clientInit := proxy.NewClient(log, cache, diagnosticCache)
_, goplsConn, goplsServer := protocol.NewClient(ctx, clientProxy, jsonrpc2.NewStream(rwc), log)
defer goplsConn.Close()
log.Info("creating proxy")
// Create the proxy to sit between.
serverProxy := proxy.NewServer(log, goplsServer, cache, diagnosticCache)
// Create templ server.
log.Info("creating templ server")
_, templConn, templClient := protocol.NewServer(context.Background(), serverProxy, templStream, log)
defer templConn.Close()
// Allow both the server and the client to initiate outbound requests.
clientInit(templClient)
// Start the web server if required.
if args.HTTPDebug != "" {
log.Info("starting debug http server", slog.String("addr", args.HTTPDebug))
h := httpdebug.NewHandler(log, serverProxy)
go func() {
if err := http.ListenAndServe(args.HTTPDebug, h); err != nil {
log.Error("web server failed", slog.Any("error", err))
}
}()
}
log.Info("listening")
select {
case <-ctx.Done():
log.Info("context closed")
case <-templConn.Done():
log.Info("templConn closed")
case <-goplsConn.Done():
log.Info("goplsConn closed")
}
log.Info("shutdown complete")
return
}
package pls
import (
"context"
"errors"
"fmt"
"io"
"log/slog"
"os"
"os/exec"
"path"
"runtime"
)
// Options for the gopls client.
type Options struct {
Log string
RPCTrace bool
}
// AsArguments converts the options into command line arguments for gopls.
func (opts Options) AsArguments() []string {
var args []string
if opts.Log != "" {
args = append(args, "-logfile", opts.Log)
}
if opts.RPCTrace {
args = append(args, "-rpc.trace")
}
return args
}
func FindGopls() (location string, err error) {
executableName := "gopls"
if runtime.GOOS == "windows" {
executableName = "gopls.exe"
}
pathLocation, err := exec.LookPath(executableName)
if err == nil {
// Found on the path.
return pathLocation, nil
}
// Unexpected error.
if !errors.Is(err, exec.ErrNotFound) {
return "", fmt.Errorf("unexpected error looking for gopls: %w", err)
}
home, err := os.UserHomeDir()
if err != nil {
return "", fmt.Errorf("unexpected error looking for gopls: %w", err)
}
// Probe standard locations.
locations := []string{
path.Join(home, "go", "bin", executableName),
path.Join(home, ".local", "bin", executableName),
}
for _, location := range locations {
_, err = os.Stat(location)
if err != nil {
continue
}
// Found in a standard location.
return location, nil
}
return "", fmt.Errorf("cannot find gopls on the path (%q), in $HOME/go/bin or $HOME/.local/bin/gopls. You can install gopls with `go install golang.org/x/tools/gopls@latest`", os.Getenv("PATH"))
}
// NewGopls starts gopls and opens up a jsonrpc2 connection to it.
func NewGopls(ctx context.Context, log *slog.Logger, opts Options) (rwc io.ReadWriteCloser, err error) {
location, err := FindGopls()
if err != nil {
return nil, err
}
cmd := exec.Command(location, opts.AsArguments()...)
return newProcessReadWriteCloser(log, cmd)
}
// newProcessReadWriteCloser creates a processReadWriteCloser to allow stdin/stdout to be used as
// a JSON RPC 2.0 transport.
func newProcessReadWriteCloser(logger *slog.Logger, cmd *exec.Cmd) (rwc processReadWriteCloser, err error) {
stdin, err := cmd.StdinPipe()
if err != nil {
return
}
stdout, err := cmd.StdoutPipe()
if err != nil {
return
}
rwc = processReadWriteCloser{
in: stdin,
out: stdout,
}
go func() {
if err := cmd.Run(); err != nil {
logger.Error("gopls command error", slog.Any("error", err))
}
}()
return
}
type processReadWriteCloser struct {
in io.WriteCloser
out io.ReadCloser
}
func (prwc processReadWriteCloser) Read(p []byte) (n int, err error) {
return prwc.out.Read(p)
}
func (prwc processReadWriteCloser) Write(p []byte) (n int, err error) {
return prwc.in.Write(p)
}
func (prwc processReadWriteCloser) Close() error {
errInClose := prwc.in.Close()
errOutClose := prwc.out.Close()
if errInClose != nil || errOutClose != nil {
return fmt.Errorf("error closing process - in: %v, out: %v", errInClose, errOutClose)
}
return nil
}
package proxy
import (
"context"
"fmt"
"log/slog"
"strings"
lsp "github.com/a-h/templ/lsp/protocol"
)
// Client is responsible for rewriting messages that are
// originated from gopls, and are sent to the client.
//
// Since `gopls` is working on Go files, and this is the `templ` LSP,
// the job of this code is to rewrite incoming requests to adjust the
// file name from `*_templ.go` to `*.templ`, and to remap the char
// positions where required.
type Client struct {
Log *slog.Logger
Target lsp.Client
SourceMapCache *SourceMapCache
DiagnosticCache *DiagnosticCache
}
func NewClient(log *slog.Logger, cache *SourceMapCache, diagnosticCache *DiagnosticCache) (c *Client, init func(lsp.Client)) {
c = &Client{
Log: log,
SourceMapCache: cache,
DiagnosticCache: diagnosticCache,
}
return c, func(target lsp.Client) {
c.Target = target
}
}
func (p Client) Progress(ctx context.Context, params *lsp.ProgressParams) (err error) {
p.Log.Info("client <- server: Progress")
return p.Target.Progress(ctx, params)
}
func (p Client) WorkDoneProgressCreate(ctx context.Context, params *lsp.WorkDoneProgressCreateParams) (err error) {
p.Log.Info("client <- server: WorkDoneProgressCreate")
return p.Target.WorkDoneProgressCreate(ctx, params)
}
func (p Client) LogMessage(ctx context.Context, params *lsp.LogMessageParams) (err error) {
p.Log.Info("client <- server: LogMessage", slog.String("message", params.Message))
return p.Target.LogMessage(ctx, params)
}
func (p Client) PublishDiagnostics(ctx context.Context, params *lsp.PublishDiagnosticsParams) (err error) {
p.Log.Info("client <- server: PublishDiagnostics")
if strings.HasSuffix(string(params.URI), "go.mod") {
p.Log.Info("client <- server: PublishDiagnostics: skipping go.mod diagnostics")
return nil
}
// Log diagnostics.
for i, diagnostic := range params.Diagnostics {
p.Log.Info(fmt.Sprintf("client <- server: PublishDiagnostics: [%d]", i), slog.Any("diagnostic", diagnostic))
}
// Get the sourcemap from the cache.
uri := strings.TrimSuffix(string(params.URI), "_templ.go") + ".templ"
sourceMap, ok := p.SourceMapCache.Get(uri)
if !ok {
p.Log.Error("unable to complete because the sourcemap for the URI doesn't exist in the cache", slog.String("uri", uri))
return fmt.Errorf("unable to complete because the sourcemap for %q doesn't exist in the cache, has the didOpen notification been sent yet?", uri)
}
params.URI = lsp.DocumentURI(uri)
// Rewrite the positions.
for i := 0; i < len(params.Diagnostics); i++ {
item := params.Diagnostics[i]
start, ok := sourceMap.SourcePositionFromTarget(item.Range.Start.Line, item.Range.Start.Character)
if !ok {
continue
}
if item.Range.Start.Line == item.Range.End.Line {
length := item.Range.End.Character - item.Range.Start.Character
item.Range.Start.Line = start.Line
item.Range.Start.Character = start.Col
item.Range.End.Line = start.Line
item.Range.End.Character = start.Col + length
params.Diagnostics[i] = item
p.Log.Info(fmt.Sprintf("diagnostic [%d] rewritten", i), slog.Any("diagnostic", item))
continue
}
end, ok := sourceMap.SourcePositionFromTarget(item.Range.End.Line, item.Range.End.Character)
if !ok {
continue
}
item.Range.Start.Line = start.Line
item.Range.Start.Character = start.Col
item.Range.End.Line = end.Line
item.Range.End.Character = end.Col
params.Diagnostics[i] = item
p.Log.Info(fmt.Sprintf("diagnostic [%d] rewritten", i), slog.Any("diagnostic", item))
}
params.Diagnostics = p.DiagnosticCache.AddTemplDiagnostics(uri, params.Diagnostics)
err = p.Target.PublishDiagnostics(ctx, params)
return err
}
func (p Client) ShowMessage(ctx context.Context, params *lsp.ShowMessageParams) (err error) {
p.Log.Info("client <- server: ShowMessage", slog.String("message", params.Message))
if strings.HasPrefix(params.Message, "Do not edit this file!") {
return
}
return p.Target.ShowMessage(ctx, params)
}
func (p Client) ShowMessageRequest(ctx context.Context, params *lsp.ShowMessageRequestParams) (result *lsp.MessageActionItem, err error) {
p.Log.Info("client <- server: ShowMessageRequest", slog.String("message", params.Message))
return p.Target.ShowMessageRequest(ctx, params)
}
func (p Client) Telemetry(ctx context.Context, params any) (err error) {
p.Log.Info("client <- server: Telemetry")
return p.Target.Telemetry(ctx, params)
}
func (p Client) RegisterCapability(ctx context.Context, params *lsp.RegistrationParams) (err error) {
p.Log.Info("client <- server: RegisterCapability")
return p.Target.RegisterCapability(ctx, params)
}
func (p Client) UnregisterCapability(ctx context.Context, params *lsp.UnregistrationParams) (err error) {
p.Log.Info("client <- server: UnregisterCapability")
return p.Target.UnregisterCapability(ctx, params)
}
func (p Client) ApplyEdit(ctx context.Context, params *lsp.ApplyWorkspaceEditParams) (result *lsp.ApplyWorkspaceEditResponse, err error) {
p.Log.Info("client <- server: ApplyEdit")
return p.Target.ApplyEdit(ctx, params)
}
func (p Client) Configuration(ctx context.Context, params *lsp.ConfigurationParams) (result []any, err error) {
p.Log.Info("client <- server: Configuration")
return p.Target.Configuration(ctx, params)
}
func (p Client) WorkspaceFolders(ctx context.Context) (result []lsp.WorkspaceFolder, err error) {
p.Log.Info("client <- server: WorkspaceFolders")
return p.Target.WorkspaceFolders(ctx)
}
package proxy
import (
"sync"
lsp "github.com/a-h/templ/lsp/protocol"
)
func NewDiagnosticCache() *DiagnosticCache {
return &DiagnosticCache{
m: &sync.Mutex{},
cache: make(map[string]fileDiagnostic),
}
}
type fileDiagnostic struct {
templDiagnostics []lsp.Diagnostic
goplsDiagnostics []lsp.Diagnostic
}
type DiagnosticCache struct {
m *sync.Mutex
cache map[string]fileDiagnostic
}
func zeroLengthSliceIfNil(diags []lsp.Diagnostic) []lsp.Diagnostic {
if diags == nil {
return make([]lsp.Diagnostic, 0)
}
return diags
}
func (dc *DiagnosticCache) AddTemplDiagnostics(uri string, goDiagnostics []lsp.Diagnostic) []lsp.Diagnostic {
goDiagnostics = zeroLengthSliceIfNil(goDiagnostics)
dc.m.Lock()
defer dc.m.Unlock()
diag := dc.cache[uri]
diag.goplsDiagnostics = goDiagnostics
diag.templDiagnostics = zeroLengthSliceIfNil(diag.templDiagnostics)
dc.cache[uri] = diag
return append(diag.templDiagnostics, goDiagnostics...)
}
func (dc *DiagnosticCache) ClearTemplDiagnostics(uri string) {
dc.m.Lock()
defer dc.m.Unlock()
diag := dc.cache[uri]
diag.templDiagnostics = make([]lsp.Diagnostic, 0)
dc.cache[uri] = diag
}
func (dc *DiagnosticCache) AddGoDiagnostics(uri string, templDiagnostics []lsp.Diagnostic) []lsp.Diagnostic {
templDiagnostics = zeroLengthSliceIfNil(templDiagnostics)
dc.m.Lock()
defer dc.m.Unlock()
diag := dc.cache[uri]
diag.templDiagnostics = templDiagnostics
diag.goplsDiagnostics = zeroLengthSliceIfNil(diag.goplsDiagnostics)
dc.cache[uri] = diag
return append(diag.goplsDiagnostics, templDiagnostics...)
}
package proxy
import (
"fmt"
"log/slog"
"strings"
"sync"
lsp "github.com/a-h/templ/lsp/protocol"
)
// newDocumentContents creates a document content processing tool.
func newDocumentContents(log *slog.Logger) *DocumentContents {
return &DocumentContents{
m: new(sync.Mutex),
uriToContents: make(map[string]*Document),
log: log,
}
}
type DocumentContents struct {
m *sync.Mutex
uriToContents map[string]*Document
log *slog.Logger
}
// Set the contents of a document.
func (dc *DocumentContents) Set(uri string, d *Document) {
dc.m.Lock()
defer dc.m.Unlock()
dc.uriToContents[uri] = d
}
// Get the contents of a document.
func (dc *DocumentContents) Get(uri string) (d *Document, ok bool) {
dc.m.Lock()
defer dc.m.Unlock()
d, ok = dc.uriToContents[uri]
return
}
// Delete a document from memory.
func (dc *DocumentContents) Delete(uri string) {
dc.m.Lock()
defer dc.m.Unlock()
delete(dc.uriToContents, uri)
}
func (dc *DocumentContents) URIs() (uris []string) {
dc.m.Lock()
defer dc.m.Unlock()
uris = make([]string, len(dc.uriToContents))
var i int
for k := range dc.uriToContents {
uris[i] = k
i++
}
return uris
}
// Apply changes to the document from the client, and return a list of change requests to send back to the client.
func (dc *DocumentContents) Apply(uri string, changes []lsp.TextDocumentContentChangeEvent) (d *Document, err error) {
dc.m.Lock()
defer dc.m.Unlock()
var ok bool
d, ok = dc.uriToContents[uri]
if !ok {
err = fmt.Errorf("document not found")
return
}
for _, change := range changes {
d.Apply(change.Range, change.Text)
}
return
}
func NewDocument(log *slog.Logger, s string) *Document {
return &Document{
Log: log,
Lines: strings.Split(s, "\n"),
}
}
type Document struct {
Log *slog.Logger
Lines []string
}
func (d *Document) LineLengths() (lens []int) {
lens = make([]int, len(d.Lines))
for i, l := range d.Lines {
lens[i] = len(l)
}
return
}
func (d *Document) Len() (line, col int) {
line = len(d.Lines)
col = len(d.Lines[len(d.Lines)-1])
return
}
func (d *Document) Overwrite(fromLine, fromCol, toLine, toCol int, lines []string) {
suffix := d.Lines[toLine][toCol:]
toLen := d.LineLengths()[toLine]
d.Delete(fromLine, fromCol, toLine, toLen)
lines[len(lines)-1] = lines[len(lines)-1] + suffix
d.Insert(fromLine, fromCol, lines)
}
func (d *Document) Insert(line, col int, lines []string) {
prefix := d.Lines[line][:col]
suffix := d.Lines[line][col:]
lines[0] = prefix + lines[0]
d.Lines[line] = lines[0]
if len(lines) > 1 {
d.InsertLines(line+1, lines[1:])
}
d.Lines[line+len(lines)-1] = lines[len(lines)-1] + suffix
}
func (d *Document) InsertLines(i int, withLines []string) {
d.Lines = append(d.Lines[:i], append(withLines, d.Lines[i:]...)...)
}
func (d *Document) Delete(fromLine, fromCol, toLine, toCol int) {
prefix := d.Lines[fromLine][:fromCol]
suffix := d.Lines[toLine][toCol:]
// Delete intermediate lines.
deleteFrom := fromLine
deleteTo := fromLine + (toLine - fromLine)
d.DeleteLines(deleteFrom, deleteTo)
// Merge the contents of the final line.
d.Lines[fromLine] = prefix + suffix
}
func (d *Document) DeleteLines(i, j int) {
d.Lines = append(d.Lines[:i], d.Lines[j:]...)
}
func (d *Document) String() string {
return strings.Join(d.Lines, "\n")
}
func (d *Document) Replace(with string) {
d.Lines = strings.Split(with, "\n")
}
func (d *Document) Apply(r *lsp.Range, with string) {
withLines := strings.Split(with, "\n")
d.normalize(r)
if d.isWholeDocument(r) {
d.Lines = withLines
return
}
if d.isInsert(r, with) {
d.Insert(int(r.Start.Line), int(r.Start.Character), withLines)
return
}
if d.isDelete(r, with) {
d.Delete(int(r.Start.Line), int(r.Start.Character), int(r.End.Line), int(r.End.Character))
return
}
if d.isOverwrite(r, with) {
d.Overwrite(int(r.Start.Line), int(r.Start.Character), int(r.End.Line), int(r.End.Character), withLines)
}
}
func (d *Document) normalize(r *lsp.Range) {
if r == nil {
return
}
lens := d.LineLengths()
if r.Start.Line >= uint32(len(lens)) {
r.Start.Line = uint32(len(lens) - 1)
r.Start.Character = uint32(lens[r.Start.Line])
}
if r.Start.Character > uint32(lens[r.Start.Line]) {
r.Start.Character = uint32(lens[r.Start.Line])
}
if r.End.Line >= uint32(len(lens)) {
r.End.Line = uint32(len(lens) - 1)
r.End.Character = uint32(lens[r.End.Line])
}
if r.End.Character > uint32(lens[r.End.Line]) {
r.End.Character = uint32(lens[r.End.Line])
}
}
func (d *Document) isOverwrite(r *lsp.Range, with string) bool {
return (r.End.Line != r.Start.Line || r.Start.Character != r.End.Character) && with != ""
}
func (d *Document) isInsert(r *lsp.Range, with string) bool {
return r.End.Line == r.Start.Line && r.Start.Character == r.End.Character && with != ""
}
func (d *Document) isDelete(r *lsp.Range, with string) bool {
return (r.End.Line != r.Start.Line || r.Start.Character != r.End.Character) && with == ""
}
func (d *Document) isWholeDocument(r *lsp.Range) bool {
if r == nil {
return true
}
if r.Start.Line != 0 || r.Start.Character != 0 {
return false
}
l, c := d.Len()
return r.End.Line == uint32(l) || r.End.Character == uint32(c)
}
package proxy
import (
"path"
"strings"
lsp "github.com/a-h/templ/lsp/protocol"
)
func convertTemplToGoURI(templURI lsp.DocumentURI) (isTemplFile bool, goURI lsp.DocumentURI) {
base, fileName := path.Split(string(templURI))
if !strings.HasSuffix(fileName, ".templ") {
return
}
return true, lsp.DocumentURI(base + (strings.TrimSuffix(fileName, ".templ") + "_templ.go"))
}
func convertTemplGoToTemplURI(goURI lsp.DocumentURI) (isTemplGoFile bool, templURI lsp.DocumentURI) {
base, fileName := path.Split(string(goURI))
if !strings.HasSuffix(fileName, "_templ.go") {
return
}
return true, lsp.DocumentURI(base + (strings.TrimSuffix(fileName, "_templ.go") + ".templ"))
}
package proxy
import (
"context"
"fmt"
"log/slog"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/a-h/parse"
lsp "github.com/a-h/templ/lsp/protocol"
"github.com/a-h/templ/lsp/uri"
"github.com/a-h/templ"
"github.com/a-h/templ/cmd/templ/imports"
"github.com/a-h/templ/generator"
"github.com/a-h/templ/parser/v2"
)
// Server is responsible for rewriting messages that are
// originated from the text editor, and need to be sent to gopls.
//
// Since the editor is working on `templ` files, and `gopls` works
// on Go files, the job of this code is to rewrite incoming requests
// to adjust the file names from `*.templ` to `*_templ.go` and to
// remap the line/character positions in the `templ` files to their
// corresponding locations in the Go file.
//
// This allows gopls to operate as usual.
//
// This code also rewrites the responses back from gopls to do the
// inverse operation - to put the file names back, and readjust any
// character positions.
type Server struct {
Log *slog.Logger
Target lsp.Server
SourceMapCache *SourceMapCache
DiagnosticCache *DiagnosticCache
TemplSource *DocumentContents
GoSource map[string]string
preLoadURIs []*lsp.DidOpenTextDocumentParams
}
func NewServer(log *slog.Logger, target lsp.Server, cache *SourceMapCache, diagnosticCache *DiagnosticCache) (s *Server) {
return &Server{
Log: log,
Target: target,
SourceMapCache: cache,
DiagnosticCache: diagnosticCache,
TemplSource: newDocumentContents(log),
GoSource: make(map[string]string),
}
}
// updatePosition maps positions and filenames from source templ files into the target *.go files.
func (p *Server) updatePosition(templURI lsp.DocumentURI, current lsp.Position) (ok bool, goURI lsp.DocumentURI, updated lsp.Position) {
log := p.Log.With(slog.String("uri", string(templURI)))
var isTemplFile bool
if isTemplFile, goURI = convertTemplToGoURI(templURI); !isTemplFile {
return false, templURI, current
}
sourceMap, ok := p.SourceMapCache.Get(string(templURI))
if !ok {
log.Warn("completion: sourcemap not found in cache, it could be that didOpen was not called")
return
}
// Map from the source position to target Go position.
to, ok := sourceMap.TargetPositionFromSource(current.Line, current.Character)
if !ok {
log.Info("updatePosition: not found", slog.String("from", fmt.Sprintf("%d:%d", current.Line, current.Character)))
return false, templURI, current
}
log.Info("updatePosition: found", slog.String("fromTempl", fmt.Sprintf("%d:%d", current.Line, current.Character)),
slog.String("toGo", fmt.Sprintf("%d:%d", to.Line, to.Col)))
updated.Line = to.Line
updated.Character = to.Col
return true, goURI, updated
}
func (p *Server) convertTemplRangeToGoRange(templURI lsp.DocumentURI, input lsp.Range) (output lsp.Range, ok bool) {
output = input
var sourceMap *parser.SourceMap
sourceMap, ok = p.SourceMapCache.Get(string(templURI))
if !ok {
p.Log.Warn("templ->go: sourcemap not found in cache")
return
}
// Map from the source position to target Go position.
start, ok := sourceMap.TargetPositionFromSource(input.Start.Line, input.Start.Character)
if ok {
output.Start.Line = start.Line
output.Start.Character = start.Col
}
end, ok := sourceMap.TargetPositionFromSource(input.End.Line, input.End.Character)
if ok {
output.End.Line = end.Line
output.End.Character = end.Col
}
return
}
func (p *Server) convertGoRangeToTemplRange(templURI lsp.DocumentURI, input lsp.Range) (output lsp.Range) {
output = input
sourceMap, ok := p.SourceMapCache.Get(string(templURI))
if !ok {
p.Log.Warn("go->templ: sourcemap not found in cache")
return
}
// Map from the source position to target Go position.
start, startPositionMapped := sourceMap.SourcePositionFromTarget(input.Start.Line, input.Start.Character)
if startPositionMapped {
output.Start.Line = start.Line
output.Start.Character = start.Col
}
end, endPositionMapped := sourceMap.SourcePositionFromTarget(input.End.Line, input.End.Character)
if endPositionMapped {
output.End.Line = end.Line
output.End.Character = end.Col
}
if !startPositionMapped || !endPositionMapped {
p.Log.Warn("go->templ: range not found in sourcemap", slog.Any("range", input))
}
return
}
// parseTemplate parses the templ file content, and notifies the end user via the LSP about how it went.
func (p *Server) parseTemplate(ctx context.Context, uri uri.URI, templateText string) (template parser.TemplateFile, ok bool, err error) {
template, err = parser.ParseString(templateText)
if err != nil {
msg := &lsp.PublishDiagnosticsParams{
URI: uri,
Diagnostics: []lsp.Diagnostic{
{
Severity: lsp.DiagnosticSeverityError,
Code: "",
Source: "templ",
Message: err.Error(),
},
},
}
if pe, isParserError := err.(parse.ParseError); isParserError {
msg.Diagnostics[0].Range = lsp.Range{
Start: lsp.Position{
Line: uint32(pe.Pos.Line),
Character: uint32(pe.Pos.Col),
},
End: lsp.Position{
Line: uint32(pe.Pos.Line),
Character: uint32(pe.Pos.Col),
},
}
}
msg.Diagnostics = p.DiagnosticCache.AddGoDiagnostics(string(uri), msg.Diagnostics)
err = lsp.ClientFromContext(ctx).PublishDiagnostics(ctx, msg)
if err != nil {
p.Log.Error("failed to publish error diagnostics", slog.Any("error", err))
}
return
}
template.Filepath = string(uri)
parsedDiagnostics, err := parser.Diagnose(template)
if err != nil {
return
}
ok = true
if len(parsedDiagnostics) > 0 {
msg := &lsp.PublishDiagnosticsParams{
URI: uri,
}
for _, d := range parsedDiagnostics {
msg.Diagnostics = append(msg.Diagnostics, lsp.Diagnostic{
Severity: lsp.DiagnosticSeverityWarning,
Code: "",
Source: "templ",
Message: d.Message,
Range: lsp.Range{
Start: lsp.Position{
Line: uint32(d.Range.From.Line),
Character: uint32(d.Range.From.Col),
},
End: lsp.Position{
Line: uint32(d.Range.To.Line),
Character: uint32(d.Range.To.Col),
},
},
})
}
msg.Diagnostics = p.DiagnosticCache.AddGoDiagnostics(string(uri), msg.Diagnostics)
err = lsp.ClientFromContext(ctx).PublishDiagnostics(ctx, msg)
if err != nil {
p.Log.Error("failed to publish error diagnostics", slog.Any("error", err))
}
return
}
// Clear templ diagnostics.
p.DiagnosticCache.ClearTemplDiagnostics(string(uri))
err = lsp.ClientFromContext(ctx).PublishDiagnostics(ctx, &lsp.PublishDiagnosticsParams{
URI: uri,
// Cannot be nil as per https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#publishDiagnosticsParams
Diagnostics: []lsp.Diagnostic{},
})
if err != nil {
p.Log.Error("failed to publish diagnostics", slog.Any("error", err))
return
}
return
}
func (p *Server) Initialize(ctx context.Context, params *lsp.InitializeParams) (result *lsp.InitializeResult, err error) {
p.Log.Info("client -> server: Initialize")
defer p.Log.Info("client -> server: Initialize end")
result, err = p.Target.Initialize(ctx, params)
if err != nil {
p.Log.Error("Initialize failed", slog.Any("error", err))
}
// Add the '<' and '{' trigger so that we can do snippets for tags.
if result.Capabilities.CompletionProvider == nil {
result.Capabilities.CompletionProvider = &lsp.CompletionOptions{}
}
result.Capabilities.CompletionProvider.TriggerCharacters = append(result.Capabilities.CompletionProvider.TriggerCharacters, "{", "<")
// Remove all the gopls commands.
if result.Capabilities.ExecuteCommandProvider == nil {
result.Capabilities.ExecuteCommandProvider = &lsp.ExecuteCommandOptions{}
}
result.Capabilities.ExecuteCommandProvider.Commands = []string{}
result.Capabilities.DocumentFormattingProvider = true
result.Capabilities.SemanticTokensProvider = nil
result.Capabilities.DocumentRangeFormattingProvider = false
result.Capabilities.TextDocumentSync = lsp.TextDocumentSyncOptions{
OpenClose: true,
Change: lsp.TextDocumentSyncKindFull,
WillSave: false,
WillSaveWaitUntil: false,
Save: &lsp.SaveOptions{IncludeText: true},
}
for _, c := range params.WorkspaceFolders {
path := strings.TrimPrefix(c.URI, "file://")
werr := filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
p.Log.Info("found file", slog.String("path", path))
uri := uri.URI("file://" + path)
isTemplFile, goURI := convertTemplToGoURI(uri)
if !isTemplFile {
return nil
}
b, err := os.ReadFile(path)
if err != nil {
return err
}
p.TemplSource.Set(string(uri), NewDocument(p.Log, string(b)))
// Parse the template.
template, ok, err := p.parseTemplate(ctx, uri, string(b))
if err != nil {
p.Log.Error("parseTemplate failure", slog.Any("error", err))
}
if !ok {
p.Log.Info("parsing template did not succeed", slog.String("uri", string(uri)))
return nil
}
w := new(strings.Builder)
generatorOutput, err := generator.Generate(template, w)
if err != nil {
return fmt.Errorf("generate failure: %w", err)
}
p.Log.Info("setting source map cache contents", slog.String("uri", string(uri)))
p.SourceMapCache.Set(string(uri), generatorOutput.SourceMap)
// Set the Go contents.
p.GoSource[string(uri)] = w.String()
didOpenParams := &lsp.DidOpenTextDocumentParams{
TextDocument: lsp.TextDocumentItem{
URI: goURI,
Text: w.String(),
Version: 1,
LanguageID: "go",
},
}
p.preLoadURIs = append(p.preLoadURIs, didOpenParams)
return nil
})
if werr != nil {
p.Log.Error("walk error", slog.Any("error", werr))
}
}
result.ServerInfo.Name = "templ-lsp"
result.ServerInfo.Version = templ.Version()
return result, err
}
func (p *Server) Initialized(ctx context.Context, params *lsp.InitializedParams) (err error) {
p.Log.Info("client -> server: Initialized")
defer p.Log.Info("client -> server: Initialized end")
goInitErr := p.Target.Initialized(ctx, params)
for i, doParams := range p.preLoadURIs {
doErr := p.Target.DidOpen(ctx, doParams)
if doErr != nil {
return doErr
}
p.preLoadURIs[i] = nil
}
return goInitErr
}
func (p *Server) Shutdown(ctx context.Context) (err error) {
p.Log.Info("client -> server: Shutdown")
defer p.Log.Info("client -> server: Shutdown end")
return p.Target.Shutdown(ctx)
}
func (p *Server) Exit(ctx context.Context) (err error) {
p.Log.Info("client -> server: Exit")
defer p.Log.Info("client -> server: Exit end")
return p.Target.Exit(ctx)
}
func (p *Server) WorkDoneProgressCancel(ctx context.Context, params *lsp.WorkDoneProgressCancelParams) (err error) {
p.Log.Info("client -> server: WorkDoneProgressCancel")
defer p.Log.Info("client -> server: WorkDoneProgressCancel end")
return p.Target.WorkDoneProgressCancel(ctx, params)
}
func (p *Server) LogTrace(ctx context.Context, params *lsp.LogTraceParams) (err error) {
p.Log.Info("client -> server: LogTrace", slog.String("message", params.Message))
defer p.Log.Info("client -> server: LogTrace end")
return p.Target.LogTrace(ctx, params)
}
func (p *Server) SetTrace(ctx context.Context, params *lsp.SetTraceParams) (err error) {
p.Log.Info("client -> server: SetTrace")
defer p.Log.Info("client -> server: SetTrace end")
return p.Target.SetTrace(ctx, params)
}
var supportedCodeActions = map[string]bool{}
func (p *Server) CodeAction(ctx context.Context, params *lsp.CodeActionParams) (result []lsp.CodeAction, err error) {
p.Log.Info("client -> server: CodeAction", slog.Any("params", params))
defer p.Log.Info("client -> server: CodeAction end")
isTemplFile, goURI := convertTemplToGoURI(params.TextDocument.URI)
if !isTemplFile {
return p.Target.CodeAction(ctx, params)
}
templURI := params.TextDocument.URI
var ok bool
if params.Range, ok = p.convertTemplRangeToGoRange(templURI, params.Range); !ok {
// Don't pass the request to gopls if the range is not within a Go code block.
return
}
params.TextDocument.URI = goURI
result, err = p.Target.CodeAction(ctx, params)
if err != nil {
return
}
var updatedResults []lsp.CodeAction
// Filter out commands that are not yet supported.
// For example, "Fill Struct" runs the `gopls.apply_fix` command.
// This command has a set of arguments, including Fix, Range and URI.
// However, these are just a map[string]any so for each command that we want to support,
// we need to know what the arguments are so that we can rewrite them.
for i := 0; i < len(result); i++ {
if !supportedCodeActions[result[i].Title] {
continue
}
r := result[i]
// Rewrite the Diagnostics range field.
for di := 0; di < len(r.Diagnostics); di++ {
r.Diagnostics[di].Range = p.convertGoRangeToTemplRange(templURI, r.Diagnostics[di].Range)
}
// Rewrite the DocumentChanges.
if r.Edit != nil {
for dci := 0; dci < len(r.Edit.DocumentChanges); dci++ {
dc := r.Edit.DocumentChanges[0]
for ei := 0; ei < len(dc.Edits); ei++ {
dc.Edits[ei].Range = p.convertGoRangeToTemplRange(templURI, dc.Edits[ei].Range)
}
dc.TextDocument.URI = templURI
r.Edit.DocumentChanges[dci] = dc
}
}
updatedResults = append(updatedResults, r)
}
return updatedResults, nil
}
func (p *Server) CodeLens(ctx context.Context, params *lsp.CodeLensParams) (result []lsp.CodeLens, err error) {
p.Log.Info("client -> server: CodeLens")
defer p.Log.Info("client -> server: CodeLens end")
isTemplFile, goURI := convertTemplToGoURI(params.TextDocument.URI)
if !isTemplFile {
return p.Target.CodeLens(ctx, params)
}
templURI := params.TextDocument.URI
params.TextDocument.URI = goURI
result, err = p.Target.CodeLens(ctx, params)
if err != nil {
return
}
if result == nil {
return
}
for i := 0; i < len(result); i++ {
cl := result[i]
cl.Range = p.convertGoRangeToTemplRange(templURI, cl.Range)
result[i] = cl
}
return
}
func (p *Server) CodeLensResolve(ctx context.Context, params *lsp.CodeLens) (result *lsp.CodeLens, err error) {
p.Log.Info("client -> server: CodeLensResolve")
defer p.Log.Info("client -> server: CodeLensResolve end")
return p.Target.CodeLensResolve(ctx, params)
}
func (p *Server) ColorPresentation(ctx context.Context, params *lsp.ColorPresentationParams) (result []lsp.ColorPresentation, err error) {
p.Log.Info("client -> server: ColorPresentation ColorPresentation")
defer p.Log.Info("client -> server: ColorPresentation end")
isTemplFile, goURI := convertTemplToGoURI(params.TextDocument.URI)
if !isTemplFile {
return p.Target.ColorPresentation(ctx, params)
}
templURI := params.TextDocument.URI
params.TextDocument.URI = goURI
result, err = p.Target.ColorPresentation(ctx, params)
if err != nil {
return
}
if result == nil {
return
}
for i := 0; i < len(result); i++ {
r := result[i]
if r.TextEdit != nil {
r.TextEdit.Range = p.convertGoRangeToTemplRange(templURI, r.TextEdit.Range)
}
result[i] = r
}
return
}
func (p *Server) Completion(ctx context.Context, params *lsp.CompletionParams) (result *lsp.CompletionList, err error) {
p.Log.Info("client -> server: Completion")
defer p.Log.Info("client -> server: Completion end")
if params.Context != nil && params.Context.TriggerCharacter == "<" {
result = &lsp.CompletionList{
Items: htmlSnippets,
}
return
}
// Get the sourcemap from the cache.
templURI := params.TextDocument.URI
var ok bool
ok, params.TextDocument.URI, params.TextDocumentPositionParams.Position = p.updatePosition(templURI, params.TextDocumentPositionParams.Position)
if !ok {
return nil, nil
}
// Ensure that Go source is available.
gosrc := strings.Split(p.GoSource[string(templURI)], "\n")
if len(gosrc) < int(params.TextDocumentPositionParams.Position.Line) {
p.Log.Info("completion: line position out of range")
return nil, nil
}
if len(gosrc[params.TextDocumentPositionParams.Position.Line]) < int(params.TextDocumentPositionParams.Position.Character) {
p.Log.Info("completion: col position out of range")
return nil, nil
}
// Call the target.
result, err = p.Target.Completion(ctx, params)
if err != nil {
p.Log.Warn("completion: got gopls error", slog.Any("error", err))
return
}
if result == nil {
return
}
// Rewrite the result positions.
p.Log.Info("completion: received items", slog.Int("count", len(result.Items)))
for i := 0; i < len(result.Items); i++ {
item := result.Items[i]
if item.TextEdit != nil {
if item.TextEdit.TextEdit != nil {
item.TextEdit.TextEdit.Range = p.convertGoRangeToTemplRange(templURI, item.TextEdit.TextEdit.Range)
}
if item.TextEdit.InsertReplaceEdit != nil {
item.TextEdit.InsertReplaceEdit.Insert = p.convertGoRangeToTemplRange(templURI, item.TextEdit.InsertReplaceEdit.Insert)
item.TextEdit.InsertReplaceEdit.Replace = p.convertGoRangeToTemplRange(templURI, item.TextEdit.InsertReplaceEdit.Replace)
}
}
if len(item.AdditionalTextEdits) > 0 {
doc, ok := p.TemplSource.Get(string(templURI))
if !ok {
continue
}
pkg := getPackageFromItemDetail(item.Detail)
imp := addImport(doc.Lines, pkg)
item.AdditionalTextEdits = []lsp.TextEdit{
{
Range: lsp.Range{
Start: lsp.Position{Line: uint32(imp.LineIndex), Character: 0},
End: lsp.Position{Line: uint32(imp.LineIndex), Character: 0},
},
NewText: imp.Text,
},
}
}
result.Items[i] = item
}
// Add templ snippet.
result.Items = append(result.Items, snippet...)
return
}
var completionWithImport = regexp.MustCompile(`^.*\(from\s(".+")\)$`)
func getPackageFromItemDetail(pkg string) string {
if m := completionWithImport.FindStringSubmatch(pkg); len(m) == 2 {
return m[1]
}
return pkg
}
type importInsert struct {
Text string
LineIndex int
}
var nonImportKeywordRegexp = regexp.MustCompile(`^(?:templ|func|css|script|var|const|type)\s`)
func addImport(lines []string, pkg string) (result importInsert) {
var isInMultiLineImport bool
lastSingleLineImportIndex := -1
for lineIndex, line := range lines {
if strings.HasPrefix(line, "import (") {
isInMultiLineImport = true
continue
}
if strings.HasPrefix(line, "import \"") {
lastSingleLineImportIndex = lineIndex
continue
}
if isInMultiLineImport && strings.HasPrefix(line, ")") {
return importInsert{
LineIndex: lineIndex,
Text: fmt.Sprintf("\t%s\n", pkg),
}
}
// Only add import statements before templates, functions, css, and script templates.
if nonImportKeywordRegexp.MatchString(line) {
break
}
}
var suffix string
if lastSingleLineImportIndex == -1 {
lastSingleLineImportIndex = 1
suffix = "\n"
}
return importInsert{
LineIndex: lastSingleLineImportIndex + 1,
Text: fmt.Sprintf("import %s\n%s", pkg, suffix),
}
}
func (p *Server) CompletionResolve(ctx context.Context, params *lsp.CompletionItem) (result *lsp.CompletionItem, err error) {
p.Log.Info("client -> server: CompletionResolve")
defer p.Log.Info("client -> server: CompletionResolve end")
return p.Target.CompletionResolve(ctx, params)
}
func (p *Server) Declaration(ctx context.Context, params *lsp.DeclarationParams) (result []lsp.Location /* Declaration | DeclarationLink[] | null */, err error) {
p.Log.Info("client -> server: Declaration")
defer p.Log.Info("client -> server: Declaration end")
// Rewrite the request.
templURI := params.TextDocument.URI
var ok bool
ok, params.TextDocument.URI, params.Position = p.updatePosition(templURI, params.Position)
if !ok {
return nil, nil
}
// Call gopls and get the result.
result, err = p.Target.Declaration(ctx, params)
if err != nil {
return
}
if result == nil {
return
}
for i := 0; i < len(result); i++ {
if isTemplGoFile, templURI := convertTemplGoToTemplURI(result[i].URI); isTemplGoFile {
result[i].URI = templURI
result[i].Range = p.convertGoRangeToTemplRange(templURI, result[i].Range)
}
}
return
}
func (p *Server) Definition(ctx context.Context, params *lsp.DefinitionParams) (result []lsp.Location /* Definition | DefinitionLink[] | null */, err error) {
p.Log.Info("client -> server: Definition")
defer p.Log.Info("client -> server: Definition end")
// Rewrite the request.
templURI := params.TextDocument.URI
var ok bool
ok, params.TextDocument.URI, params.Position = p.updatePosition(templURI, params.Position)
if !ok {
return result, nil
}
// Call gopls and get the result.
result, err = p.Target.Definition(ctx, params)
if err != nil {
return
}
if result == nil {
return
}
for i := 0; i < len(result); i++ {
if isTemplGoFile, templURI := convertTemplGoToTemplURI(result[i].URI); isTemplGoFile {
result[i].URI = templURI
result[i].Range = p.convertGoRangeToTemplRange(templURI, result[i].Range)
}
}
return
}
func (p *Server) DidChange(ctx context.Context, params *lsp.DidChangeTextDocumentParams) (err error) {
p.Log.Info("client -> server: DidChange", slog.Any("params", params))
defer p.Log.Info("client -> server: DidChange end")
isTemplFile, goURI := convertTemplToGoURI(params.TextDocument.URI)
if !isTemplFile {
p.Log.Error("not a templ file")
return
}
// Apply content changes to the cached template.
d, err := p.TemplSource.Apply(string(params.TextDocument.URI), params.ContentChanges)
if err != nil {
p.Log.Error("error applying changes", slog.Any("error", err))
return
}
// Update the Go code.
p.Log.Info("parsing template")
template, ok, err := p.parseTemplate(ctx, params.TextDocument.URI, d.String())
if err != nil {
p.Log.Error("parseTemplate failure", slog.Any("error", err))
}
if !ok {
return
}
w := new(strings.Builder)
// In future updates, we may pass `WithSkipCodeGeneratedComment` to the generator.
// This will enable a number of actions within gopls that it doesn't currently apply because
// it recognises templ code as being auto-generated.
//
// This change would increase the surface area of gopls that we use, so may surface a number of issues
// if enabled.
generatorOutput, err := generator.Generate(template, w)
if err != nil {
p.Log.Error("generate failure", slog.Any("error", err))
return
}
// Cache the sourcemap.
p.Log.Info("setting cache", slog.String("uri", string(params.TextDocument.URI)))
p.SourceMapCache.Set(string(params.TextDocument.URI), generatorOutput.SourceMap)
p.GoSource[string(params.TextDocument.URI)] = w.String()
// Change the path.
params.TextDocument.URI = goURI
params.TextDocument.TextDocumentIdentifier.URI = goURI
// Overwrite all the Go contents.
params.ContentChanges = []lsp.TextDocumentContentChangeEvent{{
Text: w.String(),
}}
return p.Target.DidChange(ctx, params)
}
func (p *Server) DidChangeConfiguration(ctx context.Context, params *lsp.DidChangeConfigurationParams) (err error) {
p.Log.Info("client -> server: DidChangeConfiguration")
defer p.Log.Info("client -> server: DidChangeConfiguration end")
return p.Target.DidChangeConfiguration(ctx, params)
}
func (p *Server) DidChangeWatchedFiles(ctx context.Context, params *lsp.DidChangeWatchedFilesParams) (err error) {
p.Log.Info("client -> server: DidChangeWatchedFiles")
defer p.Log.Info("client -> server: DidChangeWatchedFiles end")
return p.Target.DidChangeWatchedFiles(ctx, params)
}
func (p *Server) DidChangeWorkspaceFolders(ctx context.Context, params *lsp.DidChangeWorkspaceFoldersParams) (err error) {
p.Log.Info("client -> server: DidChangeWorkspaceFolders")
defer p.Log.Info("client -> server: DidChangeWorkspaceFolders end")
return p.Target.DidChangeWorkspaceFolders(ctx, params)
}
func (p *Server) DidClose(ctx context.Context, params *lsp.DidCloseTextDocumentParams) (err error) {
p.Log.Info("client -> server: DidClose")
defer p.Log.Info("client -> server: DidClose end")
isTemplFile, goURI := convertTemplToGoURI(params.TextDocument.URI)
if !isTemplFile {
return p.Target.DidClose(ctx, params)
}
// Delete the template and sourcemaps from caches.
p.TemplSource.Delete(string(params.TextDocument.URI))
p.SourceMapCache.Delete(string(params.TextDocument.URI))
// Get gopls to delete the Go file from its cache.
params.TextDocument.URI = goURI
return p.Target.DidClose(ctx, params)
}
func (p *Server) DidOpen(ctx context.Context, params *lsp.DidOpenTextDocumentParams) (err error) {
p.Log.Info("client -> server: DidOpen", slog.String("uri", string(params.TextDocument.URI)))
defer p.Log.Info("client -> server: DidOpen end")
isTemplFile, goURI := convertTemplToGoURI(params.TextDocument.URI)
if !isTemplFile {
return p.Target.DidOpen(ctx, params)
}
// Cache the template doc.
p.TemplSource.Set(string(params.TextDocument.URI), NewDocument(p.Log, params.TextDocument.Text))
// Parse the template.
template, ok, err := p.parseTemplate(ctx, params.TextDocument.URI, params.TextDocument.Text)
if err != nil {
p.Log.Error("parseTemplate failure", slog.Any("error", err))
}
if !ok {
p.Log.Info("parsing template did not succeed", slog.String("uri", string(params.TextDocument.URI)))
return nil
}
// Generate the output code and cache the source map and Go contents to use during completion
// requests.
w := new(strings.Builder)
generatorOutput, err := generator.Generate(template, w)
if err != nil {
return
}
p.Log.Info("setting source map cache contents", slog.String("uri", string(params.TextDocument.URI)))
p.SourceMapCache.Set(string(params.TextDocument.URI), generatorOutput.SourceMap)
// Set the Go contents.
params.TextDocument.Text = w.String()
p.GoSource[string(params.TextDocument.URI)] = params.TextDocument.Text
// Change the path.
params.TextDocument.URI = goURI
return p.Target.DidOpen(ctx, params)
}
func (p *Server) DidSave(ctx context.Context, params *lsp.DidSaveTextDocumentParams) (err error) {
p.Log.Info("client -> server: DidSave")
defer p.Log.Info("client -> server: DidSave end")
if isTemplFile, goURI := convertTemplToGoURI(params.TextDocument.URI); isTemplFile {
params.TextDocument.URI = goURI
}
return p.Target.DidSave(ctx, params)
}
func (p *Server) DocumentColor(ctx context.Context, params *lsp.DocumentColorParams) (result []lsp.ColorInformation, err error) {
p.Log.Info("client -> server: DocumentColor")
defer p.Log.Info("client -> server: DocumentColor end")
isTemplFile, goURI := convertTemplToGoURI(params.TextDocument.URI)
if !isTemplFile {
return p.Target.DocumentColor(ctx, params)
}
templURI := params.TextDocument.URI
params.TextDocument.URI = goURI
result, err = p.Target.DocumentColor(ctx, params)
if err != nil {
return
}
if result == nil {
return
}
for i := 0; i < len(result); i++ {
result[i].Range = p.convertGoRangeToTemplRange(templURI, result[i].Range)
}
return
}
func (p *Server) DocumentHighlight(ctx context.Context, params *lsp.DocumentHighlightParams) (result []lsp.DocumentHighlight, err error) {
p.Log.Info("client -> server: DocumentHighlight")
defer p.Log.Info("client -> server: DocumentHighlight end")
return
}
func (p *Server) DocumentLink(ctx context.Context, params *lsp.DocumentLinkParams) (result []lsp.DocumentLink, err error) {
p.Log.Info("client -> server: DocumentLink", slog.String("uri", string(params.TextDocument.URI)))
defer p.Log.Info("client -> server: DocumentLink end")
return
}
func (p *Server) DocumentLinkResolve(ctx context.Context, params *lsp.DocumentLink) (result *lsp.DocumentLink, err error) {
p.Log.Info("client -> server: DocumentLinkResolve")
defer p.Log.Info("client -> server: DocumentLinkResolve end")
isTemplFile, goURI := convertTemplToGoURI(params.Target)
if !isTemplFile {
return p.Target.DocumentLinkResolve(ctx, params)
}
templURI := params.Target
params.Target = goURI
var ok bool
if params.Range, ok = p.convertTemplRangeToGoRange(templURI, params.Range); !ok {
return
}
// Rewrite the result.
result, err = p.Target.DocumentLinkResolve(ctx, params)
if err != nil {
return
}
if result == nil {
return
}
result.Target = templURI
result.Range = p.convertGoRangeToTemplRange(templURI, result.Range)
return
}
func (p *Server) DocumentSymbol(ctx context.Context, params *lsp.DocumentSymbolParams) (result []lsp.SymbolInformationOrDocumentSymbol, err error) {
p.Log.Info("client -> server: DocumentSymbol")
defer p.Log.Info("client -> server: DocumentSymbol end")
isTemplFile, goURI := convertTemplToGoURI(params.TextDocument.URI)
if !isTemplFile {
return p.Target.DocumentSymbol(ctx, params)
}
templURI := params.TextDocument.URI
params.TextDocument.URI = goURI
symbols, err := p.Target.DocumentSymbol(ctx, params)
if err != nil {
return nil, err
}
for _, s := range symbols {
if s.DocumentSymbol != nil {
p.convertSymbolRange(templURI, s.DocumentSymbol)
result = append(result, s)
}
if s.SymbolInformation != nil {
s.SymbolInformation.Location.URI = templURI
s.SymbolInformation.Location.Range = p.convertGoRangeToTemplRange(templURI, s.SymbolInformation.Location.Range)
result = append(result, s)
}
}
return result, err
}
func (p *Server) convertSymbolRange(templURI lsp.DocumentURI, s *lsp.DocumentSymbol) {
sourceMap, ok := p.SourceMapCache.Get(string(templURI))
if !ok {
p.Log.Warn("go->templ: sourcemap not found in cache")
return
}
src, ok := sourceMap.SymbolSourceRangeFromTarget(s.Range.Start.Line, s.Range.Start.Character)
if !ok {
p.Log.Warn("go->templ: symbol range not found", slog.Any("symbol", s), slog.Any("choices", sourceMap.TargetSymbolRangeToSource))
return
}
s.Range = lsp.Range{
Start: lsp.Position{
Line: uint32(src.From.Line),
Character: uint32(src.From.Col),
},
End: lsp.Position{
Line: uint32(src.To.Line),
Character: uint32(src.To.Col),
},
}
// Within the symbol, we can select sub-sections.
// These are Go expressions, in the standard source map.
s.SelectionRange = p.convertGoRangeToTemplRange(templURI, s.SelectionRange)
for i := 0; i < len(s.Children); i++ {
p.convertSymbolRange(templURI, &s.Children[i])
if !isRangeWithin(s.Range, s.Children[i].Range) {
p.Log.Error("child symbol range not within parent range", slog.Any("symbol", s.Children[i]), slog.Int("index", i))
}
}
if !isRangeWithin(s.Range, s.SelectionRange) {
p.Log.Error("selection range not within range", slog.Any("symbol", s))
}
}
func isRangeWithin(parent, child lsp.Range) bool {
if child.Start.Line < parent.Start.Line || child.End.Line > parent.End.Line {
return false
}
if child.Start.Line == parent.Start.Line && child.Start.Character < parent.Start.Character {
return false
}
if child.End.Line == parent.End.Line && child.End.Character > parent.End.Character {
return false
}
return true
}
func (p *Server) ExecuteCommand(ctx context.Context, params *lsp.ExecuteCommandParams) (result any, err error) {
p.Log.Info("client -> server: ExecuteCommand")
defer p.Log.Info("client -> server: ExecuteCommand end")
return p.Target.ExecuteCommand(ctx, params)
}
func (p *Server) FoldingRanges(ctx context.Context, params *lsp.FoldingRangeParams) (result []lsp.FoldingRange, err error) {
p.Log.Info("client -> server: FoldingRanges")
defer p.Log.Info("client -> server: FoldingRanges end")
// There are no folding ranges in templ files.
// return p.Target.FoldingRanges(ctx, params)
return []lsp.FoldingRange{}, nil
}
func (p *Server) Formatting(ctx context.Context, params *lsp.DocumentFormattingParams) (result []lsp.TextEdit, err error) {
p.Log.Info("client -> server: Formatting")
defer p.Log.Info("client -> server: Formatting end")
// Format the current document.
d, _ := p.TemplSource.Get(string(params.TextDocument.URI))
template, ok, err := p.parseTemplate(ctx, params.TextDocument.URI, d.String())
if err != nil {
p.Log.Error("parseTemplate failure", slog.Any("error", err))
return
}
if !ok {
return
}
p.Log.Info("attempting to organise imports", slog.String("uri", template.Filepath))
template, err = imports.Process(template)
if err != nil {
p.Log.Error("organise imports failure", slog.Any("error", err))
return
}
w := new(strings.Builder)
err = template.Write(w)
if err != nil {
p.Log.Error("handleFormatting: faled to write template", slog.Any("error", err))
return
}
// Replace everything.
result = append(result, lsp.TextEdit{
Range: lsp.Range{
Start: lsp.Position{},
End: lsp.Position{Line: uint32(len(d.Lines)), Character: 0},
},
NewText: w.String(),
})
d.Replace(w.String())
return
}
func (p *Server) Hover(ctx context.Context, params *lsp.HoverParams) (result *lsp.Hover, err error) {
p.Log.Info("client -> server: Hover")
defer p.Log.Info("client -> server: Hover end")
// Rewrite the request.
templURI := params.TextDocument.URI
var ok bool
ok, params.TextDocument.URI, params.Position = p.updatePosition(params.TextDocument.URI, params.Position)
if !ok {
return nil, nil
}
// Call gopls.
result, err = p.Target.Hover(ctx, params)
if err != nil {
return
}
// Rewrite the response.
if result != nil && result.Range != nil {
p.Log.Info("hover: result returned")
r := p.convertGoRangeToTemplRange(templURI, *result.Range)
p.Log.Info("hover: setting range")
result.Range = &r
}
return
}
func (p *Server) Implementation(ctx context.Context, params *lsp.ImplementationParams) (result []lsp.Location, err error) {
p.Log.Info("client -> server: Implementation")
defer p.Log.Info("client -> server: Implementation end")
templURI := params.TextDocument.URI
// Rewrite the request.
var ok bool
ok, params.TextDocument.URI, params.Position = p.updatePosition(params.TextDocument.URI, params.Position)
if !ok {
return nil, nil
}
result, err = p.Target.Implementation(ctx, params)
if err != nil {
return
}
if result == nil {
return
}
// Rewrite the response.
for i := 0; i < len(result); i++ {
r := result[i]
r.URI = templURI
r.Range = p.convertGoRangeToTemplRange(templURI, r.Range)
result[i] = r
}
return
}
func (p *Server) OnTypeFormatting(ctx context.Context, params *lsp.DocumentOnTypeFormattingParams) (result []lsp.TextEdit, err error) {
p.Log.Info("client -> server: OnTypeFormatting")
defer p.Log.Info("client -> server: OnTypeFormatting end")
templURI := params.TextDocument.URI
// Rewrite the request.
var ok bool
ok, params.TextDocument.URI, params.Position = p.updatePosition(params.TextDocument.URI, params.Position)
if !ok {
return nil, nil
}
// Get the response.
result, err = p.Target.OnTypeFormatting(ctx, params)
if err != nil {
return
}
if result == nil {
return
}
// Rewrite the response.
for i := 0; i < len(result); i++ {
r := result[i]
r.Range = p.convertGoRangeToTemplRange(templURI, r.Range)
result[i] = r
}
return
}
func (p *Server) PrepareRename(ctx context.Context, params *lsp.PrepareRenameParams) (result *lsp.Range, err error) {
p.Log.Info("client -> server: PrepareRename")
defer p.Log.Info("client -> server: PrepareRename end")
templURI := params.TextDocument.URI
// Rewrite the request.
var ok bool
ok, params.TextDocument.URI, params.Position = p.updatePosition(params.TextDocument.URI, params.Position)
if !ok {
return nil, nil
}
// Get the response.
result, err = p.Target.PrepareRename(ctx, params)
if err != nil {
return
}
if result == nil {
return
}
// Rewrite the response.
output := p.convertGoRangeToTemplRange(templURI, *result)
return &output, nil
}
func (p *Server) RangeFormatting(ctx context.Context, params *lsp.DocumentRangeFormattingParams) (result []lsp.TextEdit, err error) {
p.Log.Info("client -> server: RangeFormatting")
defer p.Log.Info("client -> server: RangeFormatting end")
templURI := params.TextDocument.URI
// Rewrite the request.
var isTemplURI bool
isTemplURI, params.TextDocument.URI = convertTemplToGoURI(params.TextDocument.URI)
if !isTemplURI {
err = fmt.Errorf("not a templ file")
return
}
// Call gopls.
result, err = p.Target.RangeFormatting(ctx, params)
if err != nil {
return
}
// Rewrite the response.
for i := 0; i < len(result); i++ {
r := result[i]
r.Range = p.convertGoRangeToTemplRange(templURI, r.Range)
result[i] = r
}
return result, err
}
func (p *Server) References(ctx context.Context, params *lsp.ReferenceParams) (result []lsp.Location, err error) {
p.Log.Info("client -> server: References")
defer p.Log.Info("client -> server: References end")
// Rewrite the request.
var ok bool
ok, params.TextDocument.URI, params.Position = p.updatePosition(params.TextDocument.URI, params.Position)
if !ok {
return nil, nil
}
// Call gopls.
result, err = p.Target.References(ctx, params)
if err != nil {
return
}
// Rewrite the response.
for i := 0; i < len(result); i++ {
r := result[i]
isTemplURI, templURI := convertTemplGoToTemplURI(r.URI)
if isTemplURI {
p.Log.Info(fmt.Sprintf("references-%d - range conversion for %s", i, r.URI))
r.URI, r.Range = templURI, p.convertGoRangeToTemplRange(templURI, r.Range)
}
p.Log.Info(fmt.Sprintf("references-%d: %+v", i, r))
result[i] = r
}
return result, err
}
func (p *Server) Rename(ctx context.Context, params *lsp.RenameParams) (result *lsp.WorkspaceEdit, err error) {
p.Log.Info("client -> server: Rename")
defer p.Log.Info("client -> server: Rename end")
return p.Target.Rename(ctx, params)
}
func (p *Server) SignatureHelp(ctx context.Context, params *lsp.SignatureHelpParams) (result *lsp.SignatureHelp, err error) {
p.Log.Info("client -> server: SignatureHelp")
defer p.Log.Info("client -> server: SignatureHelp end")
var ok bool
ok, params.TextDocument.URI, params.Position = p.updatePosition(params.TextDocument.URI, params.Position)
if !ok {
return nil, nil
}
return p.Target.SignatureHelp(ctx, params)
}
func (p *Server) Symbols(ctx context.Context, params *lsp.WorkspaceSymbolParams) (result []lsp.SymbolInformation, err error) {
p.Log.Info("client -> server: Symbols")
defer p.Log.Info("client -> server: Symbols end")
return p.Target.Symbols(ctx, params)
}
func (p *Server) TypeDefinition(ctx context.Context, params *lsp.TypeDefinitionParams) (result []lsp.Location, err error) {
p.Log.Info("client -> server: TypeDefinition")
defer p.Log.Info("client -> server: TypeDefinition end")
var ok bool
ok, params.TextDocument.URI, params.Position = p.updatePosition(params.TextDocument.URI, params.Position)
if !ok {
return nil, nil
}
return p.Target.TypeDefinition(ctx, params)
}
func (p *Server) WillSave(ctx context.Context, params *lsp.WillSaveTextDocumentParams) (err error) {
p.Log.Info("client -> server: WillSave")
defer p.Log.Info("client -> server: WillSave end")
var ok bool
ok, params.TextDocument.URI = convertTemplToGoURI(params.TextDocument.URI)
if !ok {
p.Log.Error("not a templ file")
return nil
}
return p.Target.WillSave(ctx, params)
}
func (p *Server) WillSaveWaitUntil(ctx context.Context, params *lsp.WillSaveTextDocumentParams) (result []lsp.TextEdit, err error) {
p.Log.Info("client -> server: WillSaveWaitUntil")
defer p.Log.Info("client -> server: WillSaveWaitUntil end")
return p.Target.WillSaveWaitUntil(ctx, params)
}
func (p *Server) ShowDocument(ctx context.Context, params *lsp.ShowDocumentParams) (result *lsp.ShowDocumentResult, err error) {
p.Log.Info("client -> server: ShowDocument")
defer p.Log.Info("client -> server: ShowDocument end")
return p.Target.ShowDocument(ctx, params)
}
func (p *Server) WillCreateFiles(ctx context.Context, params *lsp.CreateFilesParams) (result *lsp.WorkspaceEdit, err error) {
p.Log.Info("client -> server: WillCreateFiles")
defer p.Log.Info("client -> server: WillCreateFiles end")
return p.Target.WillCreateFiles(ctx, params)
}
func (p *Server) DidCreateFiles(ctx context.Context, params *lsp.CreateFilesParams) (err error) {
p.Log.Info("client -> server: DidCreateFiles")
defer p.Log.Info("client -> server: DidCreateFiles end")
return p.Target.DidCreateFiles(ctx, params)
}
func (p *Server) WillRenameFiles(ctx context.Context, params *lsp.RenameFilesParams) (result *lsp.WorkspaceEdit, err error) {
p.Log.Info("client -> server: WillRenameFiles")
defer p.Log.Info("client -> server: WillRenameFiles end")
return p.Target.WillRenameFiles(ctx, params)
}
func (p *Server) DidRenameFiles(ctx context.Context, params *lsp.RenameFilesParams) (err error) {
p.Log.Info("client -> server: DidRenameFiles")
defer p.Log.Info("client -> server: DidRenameFiles end")
return p.Target.DidRenameFiles(ctx, params)
}
func (p *Server) WillDeleteFiles(ctx context.Context, params *lsp.DeleteFilesParams) (result *lsp.WorkspaceEdit, err error) {
p.Log.Info("client -> server: WillDeleteFiles")
defer p.Log.Info("client -> server: WillDeleteFiles end")
return p.Target.WillDeleteFiles(ctx, params)
}
func (p *Server) DidDeleteFiles(ctx context.Context, params *lsp.DeleteFilesParams) (err error) {
p.Log.Info("client -> server: DidDeleteFiles")
defer p.Log.Info("client -> server: DidDeleteFiles end")
return p.Target.DidDeleteFiles(ctx, params)
}
func (p *Server) CodeLensRefresh(ctx context.Context) (err error) {
p.Log.Info("client -> server: CodeLensRefresh")
defer p.Log.Info("client -> server: CodeLensRefresh end")
return p.Target.CodeLensRefresh(ctx)
}
func (p *Server) PrepareCallHierarchy(ctx context.Context, params *lsp.CallHierarchyPrepareParams) (result []lsp.CallHierarchyItem, err error) {
p.Log.Info("client -> server: PrepareCallHierarchy")
defer p.Log.Info("client -> server: PrepareCallHierarchy end")
return p.Target.PrepareCallHierarchy(ctx, params)
}
func (p *Server) IncomingCalls(ctx context.Context, params *lsp.CallHierarchyIncomingCallsParams) (result []lsp.CallHierarchyIncomingCall, err error) {
p.Log.Info("client -> server: IncomingCalls")
defer p.Log.Info("client -> server: IncomingCalls end")
return p.Target.IncomingCalls(ctx, params)
}
func (p *Server) OutgoingCalls(ctx context.Context, params *lsp.CallHierarchyOutgoingCallsParams) (result []lsp.CallHierarchyOutgoingCall, err error) {
p.Log.Info("client -> server: OutgoingCalls")
defer p.Log.Info("client -> server: OutgoingCalls end")
return p.Target.OutgoingCalls(ctx, params)
}
func (p *Server) SemanticTokensFull(ctx context.Context, params *lsp.SemanticTokensParams) (result *lsp.SemanticTokens, err error) {
p.Log.Info("client -> server: SemanticTokensFull")
defer p.Log.Info("client -> server: SemanticTokensFull end")
isTemplFile, goURI := convertTemplToGoURI(params.TextDocument.URI)
if !isTemplFile {
return nil, nil
}
params.TextDocument.URI = goURI
return p.Target.SemanticTokensFull(ctx, params)
}
func (p *Server) SemanticTokensFullDelta(ctx context.Context, params *lsp.SemanticTokensDeltaParams) (result any /* SemanticTokens | SemanticTokensDelta */, err error) {
p.Log.Info("client -> server: SemanticTokensFullDelta")
defer p.Log.Info("client -> server: SemanticTokensFullDelta end")
isTemplFile, goURI := convertTemplToGoURI(params.TextDocument.URI)
if !isTemplFile {
return nil, nil
}
params.TextDocument.URI = goURI
return p.Target.SemanticTokensFullDelta(ctx, params)
}
func (p *Server) SemanticTokensRange(ctx context.Context, params *lsp.SemanticTokensRangeParams) (result *lsp.SemanticTokens, err error) {
p.Log.Info("client -> server: SemanticTokensRange")
defer p.Log.Info("client -> server: SemanticTokensRange end")
isTemplFile, goURI := convertTemplToGoURI(params.TextDocument.URI)
if !isTemplFile {
return nil, nil
}
params.TextDocument.URI = goURI
return p.Target.SemanticTokensRange(ctx, params)
}
func (p *Server) SemanticTokensRefresh(ctx context.Context) (err error) {
p.Log.Info("client -> server: SemanticTokensRefresh")
defer p.Log.Info("client -> server: SemanticTokensRefresh end")
return p.Target.SemanticTokensRefresh(ctx)
}
func (p *Server) LinkedEditingRange(ctx context.Context, params *lsp.LinkedEditingRangeParams) (result *lsp.LinkedEditingRanges, err error) {
p.Log.Info("client -> server: LinkedEditingRange")
defer p.Log.Info("client -> server: LinkedEditingRange end")
return p.Target.LinkedEditingRange(ctx, params)
}
func (p *Server) Moniker(ctx context.Context, params *lsp.MonikerParams) (result []lsp.Moniker, err error) {
p.Log.Info("client -> server: Moniker")
defer p.Log.Info("client -> server: Moniker end")
templURI := params.TextDocument.URI
var ok bool
ok, params.TextDocument.URI, params.TextDocumentPositionParams.Position = p.updatePosition(templURI, params.TextDocumentPositionParams.Position)
if !ok {
return nil, nil
}
return p.Target.Moniker(ctx, params)
}
func (p *Server) Request(ctx context.Context, method string, params any) (result any, err error) {
p.Log.Info("client -> server: Request")
defer p.Log.Info("client -> server: Request end")
return p.Target.Request(ctx, method, params)
}
package proxy
import (
"sync"
"github.com/a-h/templ/parser/v2"
)
// NewSourceMapCache creates a cache of .templ file URIs to the source map.
func NewSourceMapCache() *SourceMapCache {
return &SourceMapCache{
m: new(sync.Mutex),
uriToSourceMap: make(map[string]*parser.SourceMap),
}
}
// SourceMapCache is a cache of .templ file URIs to the source map.
type SourceMapCache struct {
m *sync.Mutex
uriToSourceMap map[string]*parser.SourceMap
}
func (fc *SourceMapCache) Set(uri string, m *parser.SourceMap) {
fc.m.Lock()
defer fc.m.Unlock()
fc.uriToSourceMap[uri] = m
}
func (fc *SourceMapCache) Get(uri string) (m *parser.SourceMap, ok bool) {
fc.m.Lock()
defer fc.m.Unlock()
m, ok = fc.uriToSourceMap[uri]
return
}
func (fc *SourceMapCache) Delete(uri string) {
fc.m.Lock()
defer fc.m.Unlock()
delete(fc.uriToSourceMap, uri)
}
func (fc *SourceMapCache) URIs() (uris []string) {
fc.m.Lock()
defer fc.m.Unlock()
uris = make([]string, len(fc.uriToSourceMap))
var i int
for k := range fc.uriToSourceMap {
uris[i] = k
i++
}
return uris
}
package lspcmd
import (
"errors"
"io"
"log/slog"
)
// stdrwc (standard read/write closer) reads from stdin, and writes to stdout.
func newStdRwc(log *slog.Logger, name string, w io.Writer, r io.Reader) stdrwc {
return stdrwc{
log: log,
name: name,
w: w,
r: r,
}
}
type stdrwc struct {
log *slog.Logger
name string
w io.Writer
r io.Reader
}
func (s stdrwc) Read(p []byte) (int, error) {
return s.r.Read(p)
}
func (s stdrwc) Write(p []byte) (int, error) {
return s.w.Write(p)
}
func (s stdrwc) Close() error {
s.log.Info("rwc: closing", slog.String("name", s.name))
var errs []error
if closer, isCloser := s.r.(io.Closer); isCloser {
if err := closer.Close(); err != nil {
s.log.Error("rwc: error closing reader", slog.String("name", s.name), slog.Any("error", err))
errs = append(errs, err)
}
}
if closer, isCloser := s.w.(io.Closer); isCloser {
if err := closer.Close(); err != nil {
s.log.Error("rwc: error closing writer", slog.String("name", s.name), slog.Any("error", err))
errs = append(errs, err)
}
}
return errors.Join(errs...)
}
package main
import (
"context"
"flag"
"fmt"
"io"
"log/slog"
"os"
"os/signal"
"runtime"
"github.com/a-h/templ"
"github.com/a-h/templ/cmd/templ/fmtcmd"
"github.com/a-h/templ/cmd/templ/generatecmd"
"github.com/a-h/templ/cmd/templ/infocmd"
"github.com/a-h/templ/cmd/templ/lspcmd"
"github.com/a-h/templ/cmd/templ/sloghandler"
"github.com/fatih/color"
)
func main() {
code := run(os.Stdin, os.Stdout, os.Stderr, os.Args)
if code != 0 {
os.Exit(code)
}
}
const usageText = `usage: templ <command> [<args>...]
templ - build HTML UIs with Go
See docs at https://templ.guide
commands:
generate Generates Go code from templ files
fmt Formats templ files
lsp Starts a language server for templ files
info Displays information about the templ environment
version Prints the version
`
func run(stdin io.Reader, stdout, stderr io.Writer, args []string) (code int) {
if len(args) < 2 {
fmt.Fprint(stderr, usageText)
return 64 // EX_USAGE
}
switch args[1] {
case "info":
return infoCmd(stdout, stderr, args[2:])
case "generate":
return generateCmd(stdout, stderr, args[2:])
case "fmt":
return fmtCmd(stdin, stdout, stderr, args[2:])
case "lsp":
return lspCmd(stdin, stdout, stderr, args[2:])
case "version", "--version":
fmt.Fprintln(stdout, templ.Version())
return 0
case "help", "-help", "--help", "-h":
fmt.Fprint(stdout, usageText)
return 0
}
fmt.Fprint(stderr, usageText)
return 64 // EX_USAGE
}
func newLogger(logLevel string, verbose bool, stderr io.Writer) *slog.Logger {
if verbose {
logLevel = "debug"
}
level := slog.LevelInfo.Level()
switch logLevel {
case "debug":
level = slog.LevelDebug.Level()
case "warn":
level = slog.LevelWarn.Level()
case "error":
level = slog.LevelError.Level()
}
return slog.New(sloghandler.NewHandler(stderr, &slog.HandlerOptions{
AddSource: logLevel == "debug",
Level: level,
}))
}
const infoUsageText = `usage: templ info [<args>...]
Displays information about the templ environment.
Args:
-json
Output information in JSON format to stdout. (default false)
-v
Set log verbosity level to "debug". (default "info")
-log-level
Set log verbosity level. (default "info", options: "debug", "info", "warn", "error")
-help
Print help and exit.
`
func infoCmd(stdout, stderr io.Writer, args []string) (code int) {
cmd := flag.NewFlagSet("diagnose", flag.ExitOnError)
jsonFlag := cmd.Bool("json", false, "")
verboseFlag := cmd.Bool("v", false, "")
logLevelFlag := cmd.String("log-level", "info", "")
helpFlag := cmd.Bool("help", false, "")
err := cmd.Parse(args)
if err != nil {
fmt.Fprint(stderr, infoUsageText)
return 64 // EX_USAGE
}
if *helpFlag {
fmt.Fprint(stdout, infoUsageText)
return
}
log := newLogger(*logLevelFlag, *verboseFlag, stderr)
ctx, cancel := context.WithCancel(context.Background())
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, os.Interrupt)
go func() {
<-signalChan
fmt.Fprintln(stderr, "Stopping...")
cancel()
}()
err = infocmd.Run(ctx, log, stdout, infocmd.Arguments{
JSON: *jsonFlag,
})
if err != nil {
color.New(color.FgRed).Fprint(stderr, "(✗) ")
fmt.Fprintln(stderr, "Command failed: "+err.Error())
return 1
}
return 0
}
const generateUsageText = `usage: templ generate [<args>...]
Generates Go code from templ files.
Args:
-path <path>
Generates code for all files in path. (default .)
-f <file>
Optionally generates code for a single file, e.g. -f header.templ
-stdout
Prints to stdout instead of writing generated files to the filesystem.
Only applicable when -f is used.
-source-map-visualisations
Set to true to generate HTML files to visualise the templ code and its corresponding Go code.
-include-version
Set to false to skip inclusion of the templ version in the generated code. (default true)
-include-timestamp
Set to true to include the current time in the generated code.
-watch
Set to true to watch the path for changes and regenerate code.
-watch-pattern <regexp>
Set the regexp pattern of files that will be watched for changes. (default: '(.+\.go$)|(.+\.templ$)|(.+_templ\.txt$)')
-cmd <cmd>
Set the command to run after generating code.
-proxy
Set the URL to proxy after generating code and executing the command.
-proxyport
The port the proxy will listen on. (default 7331)
-proxybind
The address the proxy will listen on. (default 127.0.0.1)
-notify-proxy
If present, the command will issue a reload event to the proxy 127.0.0.1:7331, or use proxyport and proxybind to specify a different address.
-w
Number of workers to use when generating code. (default runtime.NumCPUs)
-lazy
Only generate .go files if the source .templ file is newer.
-pprof
Port to run the pprof server on.
-keep-orphaned-files
Keeps orphaned generated templ files. (default false)
-v
Set log verbosity level to "debug". (default "info")
-log-level
Set log verbosity level. (default "info", options: "debug", "info", "warn", "error")
-help
Print help and exit.
Examples:
Generate code for all files in the current directory and subdirectories:
templ generate
Generate code for a single file:
templ generate -f header.templ
Watch the current directory and subdirectories for changes and regenerate code:
templ generate -watch
`
func generateCmd(stdout, stderr io.Writer, args []string) (code int) {
cmd := flag.NewFlagSet("generate", flag.ExitOnError)
fileNameFlag := cmd.String("f", "", "")
pathFlag := cmd.String("path", ".", "")
toStdoutFlag := cmd.Bool("stdout", false, "")
sourceMapVisualisationsFlag := cmd.Bool("source-map-visualisations", false, "")
includeVersionFlag := cmd.Bool("include-version", true, "")
includeTimestampFlag := cmd.Bool("include-timestamp", false, "")
watchFlag := cmd.Bool("watch", false, "")
watchPatternFlag := cmd.String("watch-pattern", "(.+\\.go$)|(.+\\.templ$)|(.+_templ\\.txt$)", "")
openBrowserFlag := cmd.Bool("open-browser", true, "")
cmdFlag := cmd.String("cmd", "", "")
proxyFlag := cmd.String("proxy", "", "")
proxyPortFlag := cmd.Int("proxyport", 7331, "")
proxyBindFlag := cmd.String("proxybind", "127.0.0.1", "")
notifyProxyFlag := cmd.Bool("notify-proxy", false, "")
workerCountFlag := cmd.Int("w", runtime.NumCPU(), "")
pprofPortFlag := cmd.Int("pprof", 0, "")
keepOrphanedFilesFlag := cmd.Bool("keep-orphaned-files", false, "")
verboseFlag := cmd.Bool("v", false, "")
logLevelFlag := cmd.String("log-level", "info", "")
lazyFlag := cmd.Bool("lazy", false, "")
helpFlag := cmd.Bool("help", false, "")
err := cmd.Parse(args)
if err != nil {
fmt.Fprint(stderr, generateUsageText)
return 64 // EX_USAGE
}
if *helpFlag {
fmt.Fprint(stdout, generateUsageText)
return
}
log := newLogger(*logLevelFlag, *verboseFlag, stderr)
ctx, cancel := context.WithCancel(context.Background())
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, os.Interrupt)
go func() {
<-signalChan
fmt.Fprintln(stderr, "Stopping...")
cancel()
}()
var fw generatecmd.FileWriterFunc
if *toStdoutFlag {
fw = generatecmd.WriterFileWriter(stdout)
}
err = generatecmd.Run(ctx, log, generatecmd.Arguments{
FileName: *fileNameFlag,
Path: *pathFlag,
FileWriter: fw,
Watch: *watchFlag,
WatchPattern: *watchPatternFlag,
OpenBrowser: *openBrowserFlag,
Command: *cmdFlag,
Proxy: *proxyFlag,
ProxyPort: *proxyPortFlag,
ProxyBind: *proxyBindFlag,
NotifyProxy: *notifyProxyFlag,
WorkerCount: *workerCountFlag,
GenerateSourceMapVisualisations: *sourceMapVisualisationsFlag,
IncludeVersion: *includeVersionFlag,
IncludeTimestamp: *includeTimestampFlag,
PPROFPort: *pprofPortFlag,
KeepOrphanedFiles: *keepOrphanedFilesFlag,
Lazy: *lazyFlag,
})
if err != nil {
color.New(color.FgRed).Fprint(stderr, "(✗) ")
fmt.Fprintln(stderr, "Command failed: "+err.Error())
return 1
}
return 0
}
const fmtUsageText = `usage: templ fmt [<args> ...]
Format all files in directory:
templ fmt .
Format stdin to stdout:
templ fmt < header.templ
Format file or directory to stdout:
templ fmt -stdout FILE
Args:
-stdout
Prints to stdout instead of in-place format
-stdin-filepath
Provides the formatter with filepath context when using -stdout.
Required for organising imports.
-v
Set log verbosity level to "debug". (default "info")
-log-level
Set log verbosity level. (default "info", options: "debug", "info", "warn", "error")
-w
Number of workers to use when formatting code. (default runtime.NumCPUs).
-fail
Fails with exit code 1 if files are changed. (e.g. in CI)
-help
Print help and exit.
`
func fmtCmd(stdin io.Reader, stdout, stderr io.Writer, args []string) (code int) {
cmd := flag.NewFlagSet("fmt", flag.ExitOnError)
helpFlag := cmd.Bool("help", false, "")
workerCountFlag := cmd.Int("w", runtime.NumCPU(), "")
verboseFlag := cmd.Bool("v", false, "")
logLevelFlag := cmd.String("log-level", "info", "")
failIfChanged := cmd.Bool("fail", false, "")
stdoutFlag := cmd.Bool("stdout", false, "")
stdinFilepath := cmd.String("stdin-filepath", "", "")
err := cmd.Parse(args)
if err != nil {
fmt.Fprint(stderr, fmtUsageText)
return 64 // EX_USAGE
}
if *helpFlag {
fmt.Fprint(stdout, fmtUsageText)
return
}
log := newLogger(*logLevelFlag, *verboseFlag, stderr)
err = fmtcmd.Run(log, stdin, stdout, fmtcmd.Arguments{
ToStdout: *stdoutFlag,
Files: cmd.Args(),
WorkerCount: *workerCountFlag,
StdinFilepath: *stdinFilepath,
FailIfChanged: *failIfChanged,
})
if err != nil {
return 1
}
return 0
}
const lspUsageText = `usage: templ lsp [<args> ...]
Starts a language server for templ.
Args:
-log string
The file to log templ LSP output to, or leave empty to disable logging.
-goplsLog string
The file to log gopls output, or leave empty to disable logging.
-goplsRPCTrace
Set gopls to log input and output messages.
-help
Print help and exit.
-pprof
Enable pprof web server (default address is localhost:9999)
-http string
Enable http debug server by setting a listen address (e.g. localhost:7474)
`
func lspCmd(stdin io.Reader, stdout, stderr io.Writer, args []string) (code int) {
cmd := flag.NewFlagSet("lsp", flag.ExitOnError)
logFlag := cmd.String("log", "", "")
goplsLog := cmd.String("goplsLog", "", "")
goplsRPCTrace := cmd.Bool("goplsRPCTrace", false, "")
helpFlag := cmd.Bool("help", false, "")
pprofFlag := cmd.Bool("pprof", false, "")
httpDebugFlag := cmd.String("http", "", "")
err := cmd.Parse(args)
if err != nil {
fmt.Fprint(stderr, lspUsageText)
return 64 // EX_USAGE
}
if *helpFlag {
fmt.Fprint(stdout, lspUsageText)
return
}
err = lspcmd.Run(stdin, stdout, stderr, lspcmd.Arguments{
Log: *logFlag,
GoplsLog: *goplsLog,
GoplsRPCTrace: *goplsRPCTrace,
PPROF: *pprofFlag,
HTTPDebug: *httpDebugFlag,
})
if err != nil {
fmt.Fprintln(stderr, err.Error())
return 1
}
return 0
}
package processor
import (
"io/fs"
"path"
"path/filepath"
"strings"
"sync"
"time"
)
type Result struct {
FileName string
Duration time.Duration
Error error
ChangesMade bool
}
func Process(dir string, f func(fileName string) (error, bool), workerCount int, results chan<- Result) {
templates := make(chan string)
go func() {
defer close(templates)
if err := FindTemplates(dir, templates); err != nil {
results <- Result{Error: err}
}
}()
ProcessChannel(templates, dir, f, workerCount, results)
}
func shouldSkipDir(dir string) bool {
if dir == "." {
return false
}
if dir == "vendor" || dir == "node_modules" {
return true
}
_, name := path.Split(dir)
// These directories are ignored by the Go tool.
if strings.HasPrefix(name, ".") || strings.HasPrefix(name, "_") {
return true
}
return false
}
func FindTemplates(srcPath string, output chan<- string) (err error) {
return filepath.WalkDir(srcPath, func(currentPath string, info fs.DirEntry, err error) error {
if err != nil {
return err
}
if info.IsDir() && shouldSkipDir(currentPath) {
return filepath.SkipDir
}
if !info.IsDir() && strings.HasSuffix(currentPath, ".templ") {
output <- currentPath
}
return nil
})
}
func ProcessChannel(templates <-chan string, dir string, f func(fileName string) (error, bool), workerCount int, results chan<- Result) {
defer close(results)
var wg sync.WaitGroup
wg.Add(workerCount)
for i := 0; i < workerCount; i++ {
go func() {
defer wg.Done()
for sourceFileName := range templates {
start := time.Now()
outErr, outChanged := f(sourceFileName)
results <- Result{
FileName: sourceFileName,
Error: outErr,
Duration: time.Since(start),
ChangesMade: outChanged,
}
}
}()
}
wg.Wait()
}
package sloghandler
import (
"context"
"io"
"log/slog"
"strings"
"sync"
"github.com/fatih/color"
)
var _ slog.Handler = &Handler{}
type Handler struct {
h slog.Handler
m *sync.Mutex
w io.Writer
}
var levelToIcon = map[slog.Level]string{
slog.LevelDebug: "(✓)",
slog.LevelInfo: "(✓)",
slog.LevelWarn: "(!)",
slog.LevelError: "(✗)",
}
var levelToColor = map[slog.Level]*color.Color{
slog.LevelDebug: color.New(color.FgCyan),
slog.LevelInfo: color.New(color.FgGreen),
slog.LevelWarn: color.New(color.FgYellow),
slog.LevelError: color.New(color.FgRed),
}
func NewHandler(w io.Writer, opts *slog.HandlerOptions) *Handler {
if opts == nil {
opts = &slog.HandlerOptions{}
}
return &Handler{
w: w,
h: slog.NewTextHandler(w, &slog.HandlerOptions{
Level: opts.Level,
AddSource: opts.AddSource,
ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {
if opts.ReplaceAttr != nil {
a = opts.ReplaceAttr(groups, a)
}
if a.Key == slog.LevelKey {
level, ok := levelToIcon[a.Value.Any().(slog.Level)]
if !ok {
level = a.Value.Any().(slog.Level).String()
}
a.Value = slog.StringValue(level)
return a
}
if a.Key == slog.TimeKey {
return slog.Attr{}
}
return a
},
}),
m: &sync.Mutex{},
}
}
func (h *Handler) Enabled(ctx context.Context, level slog.Level) bool {
return h.h.Enabled(ctx, level)
}
func (h *Handler) WithAttrs(attrs []slog.Attr) slog.Handler {
return &Handler{h: h.h.WithAttrs(attrs), w: h.w, m: h.m}
}
func (h *Handler) WithGroup(name string) slog.Handler {
return &Handler{h: h.h.WithGroup(name), w: h.w, m: h.m}
}
var keyValueColor = color.New(color.Faint & color.FgBlack)
func (h *Handler) Handle(ctx context.Context, r slog.Record) (err error) {
var sb strings.Builder
sb.WriteString(levelToColor[r.Level].Sprint(levelToIcon[r.Level]))
sb.WriteString(" ")
sb.WriteString(r.Message)
if r.NumAttrs() != 0 {
sb.WriteString(" [")
r.Attrs(func(a slog.Attr) bool {
sb.WriteString(keyValueColor.Sprintf(" %s=%s", a.Key, a.Value.String()))
return true
})
sb.WriteString(" ]")
}
sb.WriteString("\n")
h.m.Lock()
defer h.m.Unlock()
_, err = io.WriteString(h.w, sb.String())
return err
}
package testproject
import (
"bytes"
"embed"
"fmt"
"os"
"path/filepath"
"strings"
)
//go:embed testdata/*
var testdata embed.FS
func Create(moduleRoot string) (dir string, err error) {
dir, err = os.MkdirTemp("", "templ_test_*")
if err != nil {
return dir, fmt.Errorf("failed to make test dir: %w", err)
}
files, err := testdata.ReadDir("testdata")
if err != nil {
return dir, fmt.Errorf("failed to read embedded dir: %w", err)
}
for _, file := range files {
if file.IsDir() {
if err = os.MkdirAll(filepath.Join(dir, file.Name()), 0777); err != nil {
return dir, fmt.Errorf("failed to create dir: %w", err)
}
continue
}
src := filepath.Join("testdata", file.Name())
data, err := testdata.ReadFile(src)
if err != nil {
return dir, fmt.Errorf("failed to read file: %w", err)
}
target := filepath.Join(dir, file.Name())
if file.Name() == "go.mod.embed" {
data = bytes.ReplaceAll(data, []byte("{moduleRoot}"), []byte(moduleRoot))
target = filepath.Join(dir, "go.mod")
}
err = os.WriteFile(target, data, 0660)
if err != nil {
return dir, fmt.Errorf("failed to copy file: %w", err)
}
}
files, err = testdata.ReadDir("testdata/css-classes")
if err != nil {
return dir, fmt.Errorf("failed to read embedded dir: %w", err)
}
for _, file := range files {
src := filepath.Join("testdata", "css-classes", file.Name())
data, err := testdata.ReadFile(src)
if err != nil {
return dir, fmt.Errorf("failed to read file: %w", err)
}
target := filepath.Join(dir, "css-classes", file.Name())
err = os.WriteFile(target, data, 0660)
if err != nil {
return dir, fmt.Errorf("failed to copy file: %w", err)
}
}
return dir, nil
}
func MustReplaceLine(file string, line int, replacement string) string {
lines := strings.Split(file, "\n")
lines[line-1] = replacement
return strings.Join(lines, "\n")
}
// Code generated by templ - DO NOT EDIT.
package visualize
//lint:file-ignore SA4006 This context is only used if a nested component is present.
import "github.com/a-h/templ"
import templruntime "github.com/a-h/templ/runtime"
func row() templ.CSSClass {
templ_7745c5c3_CSSBuilder := templruntime.GetBuilder()
templ_7745c5c3_CSSBuilder.WriteString(`display:flex;`)
templ_7745c5c3_CSSID := templ.CSSID(`row`, templ_7745c5c3_CSSBuilder.String())
return templ.ComponentCSSClass{
ID: templ_7745c5c3_CSSID,
Class: templ.SafeCSS(`.` + templ_7745c5c3_CSSID + `{` + templ_7745c5c3_CSSBuilder.String() + `}`),
}
}
func column() templ.CSSClass {
templ_7745c5c3_CSSBuilder := templruntime.GetBuilder()
templ_7745c5c3_CSSBuilder.WriteString(`flex:50%;`)
templ_7745c5c3_CSSBuilder.WriteString(`overflow-y:scroll;`)
templ_7745c5c3_CSSBuilder.WriteString(`max-height:100vh;`)
templ_7745c5c3_CSSID := templ.CSSID(`column`, templ_7745c5c3_CSSBuilder.String())
return templ.ComponentCSSClass{
ID: templ_7745c5c3_CSSID,
Class: templ.SafeCSS(`.` + templ_7745c5c3_CSSID + `{` + templ_7745c5c3_CSSBuilder.String() + `}`),
}
}
func code() templ.CSSClass {
templ_7745c5c3_CSSBuilder := templruntime.GetBuilder()
templ_7745c5c3_CSSBuilder.WriteString(`font-family:monospace;`)
templ_7745c5c3_CSSID := templ.CSSID(`code`, templ_7745c5c3_CSSBuilder.String())
return templ.ComponentCSSClass{
ID: templ_7745c5c3_CSSID,
Class: templ.SafeCSS(`.` + templ_7745c5c3_CSSID + `{` + templ_7745c5c3_CSSBuilder.String() + `}`),
}
}
func combine(templFileName string, left, right templ.Component) templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
return templ_7745c5c3_CtxErr
}
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var1 := templ.GetChildren(ctx)
if templ_7745c5c3_Var1 == nil {
templ_7745c5c3_Var1 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 1, "<html><head><title>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var2 string
templ_7745c5c3_Var2, templ_7745c5c3_Err = templ.JoinStringErrs(templFileName)
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `cmd/templ/visualize/sourcemapvisualisation.templ`, Line: 20, Col: 25}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var2))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 2, "- Source Map Visualisation</title><style type=\"text/css\">\n\t\t\t\t.mapped { background-color: green }\n\t\t\t\t.highlighted { background-color: yellow }\n\t\t\t</style></head><body><h1>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var3 string
templ_7745c5c3_Var3, templ_7745c5c3_Err = templ.JoinStringErrs(templFileName)
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `cmd/templ/visualize/sourcemapvisualisation.templ`, Line: 27, Col: 22}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var3))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 3, "</h1>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var4 = []any{templ.Classes(row())}
templ_7745c5c3_Err = templ.RenderCSSItems(ctx, templ_7745c5c3_Buffer, templ_7745c5c3_Var4...)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 4, "<div class=\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var5 string
templ_7745c5c3_Var5, templ_7745c5c3_Err = templ.JoinStringErrs(templ.CSSClasses(templ_7745c5c3_Var4).String())
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `cmd/templ/visualize/sourcemapvisualisation.templ`, Line: 1, Col: 0}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var5))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 5, "\">")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var6 = []any{templ.Classes(column(), code())}
templ_7745c5c3_Err = templ.RenderCSSItems(ctx, templ_7745c5c3_Buffer, templ_7745c5c3_Var6...)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 6, "<div class=\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var7 string
templ_7745c5c3_Var7, templ_7745c5c3_Err = templ.JoinStringErrs(templ.CSSClasses(templ_7745c5c3_Var6).String())
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `cmd/templ/visualize/sourcemapvisualisation.templ`, Line: 1, Col: 0}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var7))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 7, "\">")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = left.Render(ctx, templ_7745c5c3_Buffer)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 8, "</div>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var8 = []any{templ.Classes(column(), code())}
templ_7745c5c3_Err = templ.RenderCSSItems(ctx, templ_7745c5c3_Buffer, templ_7745c5c3_Var8...)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 9, "<div class=\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var9 string
templ_7745c5c3_Var9, templ_7745c5c3_Err = templ.JoinStringErrs(templ.CSSClasses(templ_7745c5c3_Var8).String())
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `cmd/templ/visualize/sourcemapvisualisation.templ`, Line: 1, Col: 0}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var9))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 10, "\">")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = right.Render(ctx, templ_7745c5c3_Buffer)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 11, "</div></div></body></html>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return nil
})
}
func highlight(sourceId, targetId string) templ.ComponentScript {
return templ.ComponentScript{
Name: `__templ_highlight_ae80`,
Function: `function __templ_highlight_ae80(sourceId, targetId){let items = document.getElementsByClassName(sourceId);
for(let i = 0; i < items.length; i ++) {
items[i].classList.add("highlighted");
}
items = document.getElementsByClassName(targetId);
for(let i = 0; i < items.length; i ++) {
items[i].classList.add("highlighted");
}
}`,
Call: templ.SafeScript(`__templ_highlight_ae80`, sourceId, targetId),
CallInline: templ.SafeScriptInline(`__templ_highlight_ae80`, sourceId, targetId),
}
}
func removeHighlight(sourceId, targetId string) templ.ComponentScript {
return templ.ComponentScript{
Name: `__templ_removeHighlight_58f2`,
Function: `function __templ_removeHighlight_58f2(sourceId, targetId){let items = document.getElementsByClassName(sourceId);
for(let i = 0; i < items.length; i ++) {
items[i].classList.remove("highlighted");
}
items = document.getElementsByClassName(targetId);
for(let i = 0; i < items.length; i ++) {
items[i].classList.remove("highlighted");
}
}`,
Call: templ.SafeScript(`__templ_removeHighlight_58f2`, sourceId, targetId),
CallInline: templ.SafeScriptInline(`__templ_removeHighlight_58f2`, sourceId, targetId),
}
}
func mappedCharacter(s string, sourceID, targetID string) templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
return templ_7745c5c3_CtxErr
}
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var10 := templ.GetChildren(ctx)
if templ_7745c5c3_Var10 == nil {
templ_7745c5c3_Var10 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
var templ_7745c5c3_Var11 = []any{templ.Classes(templ.Class("mapped"), templ.Class(sourceID), templ.Class(targetID))}
templ_7745c5c3_Err = templ.RenderCSSItems(ctx, templ_7745c5c3_Buffer, templ_7745c5c3_Var11...)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templ.RenderScriptItems(ctx, templ_7745c5c3_Buffer, highlight(sourceID, targetID), removeHighlight(sourceID, targetID))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 12, "<span class=\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var12 string
templ_7745c5c3_Var12, templ_7745c5c3_Err = templ.JoinStringErrs(templ.CSSClasses(templ_7745c5c3_Var11).String())
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `cmd/templ/visualize/sourcemapvisualisation.templ`, Line: 1, Col: 0}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var12))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 13, "\" onMouseOver=\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var13 templ.ComponentScript = highlight(sourceID, targetID)
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ_7745c5c3_Var13.Call)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 14, "\" onMouseOut=\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var14 templ.ComponentScript = removeHighlight(sourceID, targetID)
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ_7745c5c3_Var14.Call)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 15, "\">")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var15 string
templ_7745c5c3_Var15, templ_7745c5c3_Err = templ.JoinStringErrs(s)
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `cmd/templ/visualize/sourcemapvisualisation.templ`, Line: 63, Col: 200}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var15))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 16, "</span>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return nil
})
}
var _ = templruntime.GeneratedTemplate
package visualize
import (
"context"
"fmt"
"html"
"io"
"strconv"
"strings"
"github.com/a-h/templ"
"github.com/a-h/templ/parser/v2"
)
func HTML(templFileName string, templContents, goContents string, sourceMap *parser.SourceMap) templ.Component {
tl := templLines{contents: string(templContents), sourceMap: sourceMap}
gl := goLines{contents: string(goContents), sourceMap: sourceMap}
return combine(templFileName, tl, gl)
}
type templLines struct {
contents string
sourceMap *parser.SourceMap
}
func (tl templLines) Render(ctx context.Context, w io.Writer) (err error) {
templLines := strings.Split(tl.contents, "\n")
for lineIndex, line := range templLines {
if _, err = w.Write([]byte("<span>" + strconv.Itoa(lineIndex) + " </span>\n")); err != nil {
return
}
for colIndex, c := range line {
if tgt, ok := tl.sourceMap.TargetPositionFromSource(uint32(lineIndex), uint32(colIndex)); ok {
sourceID := fmt.Sprintf("src_%d_%d", lineIndex, colIndex)
targetID := fmt.Sprintf("tgt_%d_%d", tgt.Line, tgt.Col)
if err := mappedCharacter(string(c), sourceID, targetID).Render(ctx, w); err != nil {
return err
}
} else {
s := html.EscapeString(string(c))
s = strings.ReplaceAll(s, "\t", " ")
s = strings.ReplaceAll(s, " ", " ")
if _, err := w.Write([]byte(s)); err != nil {
return err
}
}
}
if _, err = w.Write([]byte("\n<br/>\n")); err != nil {
return
}
}
return nil
}
type goLines struct {
contents string
sourceMap *parser.SourceMap
}
func (gl goLines) Render(ctx context.Context, w io.Writer) (err error) {
templLines := strings.Split(gl.contents, "\n")
for lineIndex, line := range templLines {
if _, err = w.Write([]byte("<span>" + strconv.Itoa(lineIndex) + " </span>\n")); err != nil {
return
}
for colIndex, c := range line {
if src, ok := gl.sourceMap.SourcePositionFromTarget(uint32(lineIndex), uint32(colIndex)); ok {
sourceID := fmt.Sprintf("src_%d_%d", src.Line, src.Col)
targetID := fmt.Sprintf("tgt_%d_%d", lineIndex, colIndex)
if err := mappedCharacter(string(c), sourceID, targetID).Render(ctx, w); err != nil {
return err
}
} else {
s := html.EscapeString(string(c))
s = strings.ReplaceAll(s, "\t", " ")
s = strings.ReplaceAll(s, " ", " ")
if _, err := w.Write([]byte(s)); err != nil {
return err
}
}
}
if _, err = w.Write([]byte("\n<br/>\n")); err != nil {
return
}
}
return nil
}
package main
import (
"fmt"
"log"
"net/http"
"github.com/a-h/templ"
)
func main() {
// Use a template that doesn't take parameters.
http.Handle("/", templ.Handler(home()))
// Use a template that accesses data or handles form posts.
http.Handle("/posts", NewPostsHandler())
// Start the server.
fmt.Println("listening on http://localhost:8000")
if err := http.ListenAndServe("localhost:8000", nil); err != nil {
log.Printf("error listening: %v", err)
}
}
func NewPostsHandler() PostsHandler {
// Replace this in-memory function with a call to a database.
postsGetter := func() (posts []Post, err error) {
return []Post{{Name: "templ", Author: "author"}}, nil
}
return PostsHandler{
GetPosts: postsGetter,
Log: log.Default(),
}
}
type PostsHandler struct {
Log *log.Logger
GetPosts func() ([]Post, error)
}
func (ph PostsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ps, err := ph.GetPosts()
if err != nil {
ph.Log.Printf("failed to get posts: %v", err)
http.Error(w, "failed to retrieve posts", http.StatusInternalServerError)
return
}
templ.Handler(posts(ps)).ServeHTTP(w, r)
}
type Post struct {
Name string
Author string
}
// Code generated by templ - DO NOT EDIT.
package main
//lint:file-ignore SA4006 This context is only used if a nested component is present.
import "github.com/a-h/templ"
import templruntime "github.com/a-h/templ/runtime"
import (
"fmt"
"time"
)
func headerTemplate(name string) templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
return templ_7745c5c3_CtxErr
}
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var1 := templ.GetChildren(ctx)
if templ_7745c5c3_Var1 == nil {
templ_7745c5c3_Var1 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 1, "<header data-testid=\"headerTemplate\"><h1>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var2 string
templ_7745c5c3_Var2, templ_7745c5c3_Err = templ.JoinStringErrs(name)
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `examples/blog/posts.templ`, Line: 10, Col: 12}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var2))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 2, "</h1></header>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return nil
})
}
func footerTemplate() templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
return templ_7745c5c3_CtxErr
}
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var3 := templ.GetChildren(ctx)
if templ_7745c5c3_Var3 == nil {
templ_7745c5c3_Var3 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 3, "<footer data-testid=\"footerTemplate\"><div>© ")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var4 string
templ_7745c5c3_Var4, templ_7745c5c3_Err = templ.JoinStringErrs(fmt.Sprintf("%d", time.Now().Year()))
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `examples/blog/posts.templ`, Line: 16, Col: 52}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var4))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 4, "</div></footer>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return nil
})
}
func navTemplate() templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
return templ_7745c5c3_CtxErr
}
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var5 := templ.GetChildren(ctx)
if templ_7745c5c3_Var5 == nil {
templ_7745c5c3_Var5 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 5, "<nav data-testid=\"navTemplate\"><ul><li><a href=\"/\">Home</a></li><li><a href=\"/posts\">Posts</a></li></ul></nav>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return nil
})
}
func layout(name string) templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
return templ_7745c5c3_CtxErr
}
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var6 := templ.GetChildren(ctx)
if templ_7745c5c3_Var6 == nil {
templ_7745c5c3_Var6 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 6, "<html><head><title>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var7 string
templ_7745c5c3_Var7, templ_7745c5c3_Err = templ.JoinStringErrs(name)
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `examples/blog/posts.templ`, Line: 31, Col: 21}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var7))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 7, "</title></head><body>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = headerTemplate(name).Render(ctx, templ_7745c5c3_Buffer)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = navTemplate().Render(ctx, templ_7745c5c3_Buffer)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 8, "<main>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templ_7745c5c3_Var6.Render(ctx, templ_7745c5c3_Buffer)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 9, "</main></body>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = footerTemplate().Render(ctx, templ_7745c5c3_Buffer)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 10, "</html>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return nil
})
}
func postsTemplate(posts []Post) templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
return templ_7745c5c3_CtxErr
}
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var8 := templ.GetChildren(ctx)
if templ_7745c5c3_Var8 == nil {
templ_7745c5c3_Var8 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 11, "<div data-testid=\"postsTemplate\">")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
for _, p := range posts {
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 12, "<div data-testid=\"postsTemplatePost\"><div data-testid=\"postsTemplatePostName\">")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var9 string
templ_7745c5c3_Var9, templ_7745c5c3_Err = templ.JoinStringErrs(p.Name)
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `examples/blog/posts.templ`, Line: 47, Col: 53}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var9))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 13, "</div><div data-testid=\"postsTemplatePostAuthor\">")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var10 string
templ_7745c5c3_Var10, templ_7745c5c3_Err = templ.JoinStringErrs(p.Author)
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `examples/blog/posts.templ`, Line: 48, Col: 57}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var10))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 14, "</div></div>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 15, "</div>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return nil
})
}
func home() templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
return templ_7745c5c3_CtxErr
}
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var11 := templ.GetChildren(ctx)
if templ_7745c5c3_Var11 == nil {
templ_7745c5c3_Var11 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
templ_7745c5c3_Var12 := templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 16, "<div data-testid=\"homeTemplate\">Welcome to my website.</div>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return nil
})
templ_7745c5c3_Err = layout("Home").Render(templ.WithChildren(ctx, templ_7745c5c3_Var12), templ_7745c5c3_Buffer)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return nil
})
}
func posts(posts []Post) templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
return templ_7745c5c3_CtxErr
}
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var13 := templ.GetChildren(ctx)
if templ_7745c5c3_Var13 == nil {
templ_7745c5c3_Var13 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
templ_7745c5c3_Var14 := templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Err = postsTemplate(posts).Render(ctx, templ_7745c5c3_Buffer)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return nil
})
templ_7745c5c3_Err = layout("Posts").Render(templ.WithChildren(ctx, templ_7745c5c3_Var14), templ_7745c5c3_Buffer)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return nil
})
}
var _ = templruntime.GeneratedTemplate
package templ
import (
"context"
"io"
)
// Flush flushes the output buffer after all its child components have been rendered.
func Flush() FlushComponent {
return FlushComponent{}
}
type FlushComponent struct {
}
type flusherError interface {
Flush() error
}
type flusher interface {
Flush()
}
func (f FlushComponent) Render(ctx context.Context, w io.Writer) (err error) {
if err = GetChildren(ctx).Render(ctx, w); err != nil {
return err
}
switch w := w.(type) {
case flusher:
w.Flush()
return nil
case flusherError:
return w.Flush()
}
return nil
}
package generator
import (
"crypto/sha256"
"encoding/hex"
"fmt"
"html"
"io"
"path/filepath"
"reflect"
"strconv"
"strings"
"time"
"unicode"
_ "embed"
"github.com/a-h/templ/parser/v2"
)
type GenerateOpt func(g *generator) error
// WithVersion enables the version to be included in the generated code.
func WithVersion(v string) GenerateOpt {
return func(g *generator) error {
g.options.Version = v
return nil
}
}
// WithTimestamp enables the generated date to be included in the generated code.
func WithTimestamp(d time.Time) GenerateOpt {
return func(g *generator) error {
g.options.GeneratedDate = d.Format(time.RFC3339)
return nil
}
}
// WithFileName sets the filename of the templ file in template rendering error messages.
func WithFileName(name string) GenerateOpt {
return func(g *generator) error {
if filepath.IsAbs(name) {
_, g.options.FileName = filepath.Split(name)
return nil
}
g.options.FileName = name
return nil
}
}
// 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 WithSkipCodeGeneratedComment() GenerateOpt {
return func(g *generator) error {
g.options.SkipCodeGeneratedComment = true
return nil
}
}
type GeneratorOutput struct {
Options GeneratorOptions `json:"meta"`
SourceMap *parser.SourceMap `json:"sourceMap"`
Literals []string `json:"literals"`
}
type GeneratorOptions struct {
// Version of templ.
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
}
// HasChanged returns true if the generated file should be written to disk, and therefore, also
// requires a recompilation.
func HasChanged(previous, updated GeneratorOutput) bool {
// If generator options have changed, we need to recompile.
if previous.Options.Version != updated.Options.Version {
return true
}
if previous.Options.FileName != updated.Options.FileName {
return true
}
if previous.Options.SkipCodeGeneratedComment != updated.Options.SkipCodeGeneratedComment {
return true
}
// We don't check the generated date as it's not used for determining if the file has changed.
// If the number of literals has changed, we need to recompile.
if len(previous.Literals) != len(updated.Literals) {
return true
}
// If the Go code has changed, we need to recompile.
if len(previous.SourceMap.Expressions) != len(updated.SourceMap.Expressions) {
return true
}
for i, prev := range previous.SourceMap.Expressions {
if prev != updated.SourceMap.Expressions[i] {
return true
}
}
return false
}
// 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.
func Generate(template parser.TemplateFile, w io.Writer, opts ...GenerateOpt) (op GeneratorOutput, err error) {
g := &generator{
tf: template,
w: NewRangeWriter(w),
sourceMap: parser.NewSourceMap(),
}
for _, opt := range opts {
if err = opt(g); err != nil {
return
}
}
err = g.generate()
if err != nil {
return op, err
}
op.Options = g.options
op.SourceMap = g.sourceMap
op.Literals = g.w.Literals
return op, nil
}
type generator struct {
tf parser.TemplateFile
w *RangeWriter
sourceMap *parser.SourceMap
variableID int
childrenVar string
options GeneratorOptions
}
func (g *generator) generate() (err error) {
if err = g.writeCodeGeneratedComment(); err != nil {
return
}
if err = g.writeVersionComment(); err != nil {
return
}
if err = g.writeGeneratedDateComment(); err != nil {
return
}
if err = g.writeHeader(); err != nil {
return
}
if err = g.writePackage(); err != nil {
return
}
if err = g.writeImports(); err != nil {
return
}
if err = g.writeTemplateNodes(); err != nil {
return
}
if err = g.writeBlankAssignmentForRuntimeImport(); err != nil {
return
}
return err
}
// See https://pkg.go.dev/cmd/go#hdr-Generate_Go_files_by_processing_source
// Automatically generated files have a comment in the header that instructs the LSP
// to stop operating.
func (g *generator) writeCodeGeneratedComment() (err error) {
if g.options.SkipCodeGeneratedComment {
// Write an empty comment so that the file is the same shape.
_, err = g.w.Write("//\n\n")
return err
}
_, err = g.w.Write("// Code generated by templ - DO NOT EDIT.\n\n")
return err
}
func (g *generator) writeVersionComment() (err error) {
if g.options.Version != "" {
_, err = g.w.Write("// templ: version: " + g.options.Version + "\n")
}
return err
}
func (g *generator) writeGeneratedDateComment() (err error) {
if g.options.GeneratedDate != "" {
_, err = g.w.Write("// templ: generated: " + g.options.GeneratedDate + "\n")
}
return err
}
func (g *generator) writeHeader() (err error) {
if len(g.tf.Header) == 0 {
return nil
}
for _, n := range g.tf.Header {
if err := g.writeGoExpression(n); err != nil {
return err
}
}
return err
}
func (g *generator) writePackage() error {
var r parser.Range
var err error
// package ...
if r, err = g.w.Write(g.tf.Package.Expression.Value + "\n\n"); err != nil {
return err
}
g.sourceMap.Add(g.tf.Package.Expression, r)
if _, err = g.w.Write("//lint:file-ignore SA4006 This context is only used if a nested component is present.\n\n"); err != nil {
return err
}
return nil
}
func (g *generator) writeImports() error {
var err error
// Always import templ because it's the interface type of all templates.
if _, err = g.w.Write("import \"github.com/a-h/templ\"\n"); err != nil {
return err
}
if _, err = g.w.Write("import templruntime \"github.com/a-h/templ/runtime\"\n"); err != nil {
return err
}
if _, err = g.w.Write("\n"); err != nil {
return err
}
return nil
}
func (g *generator) writeTemplateNodes() error {
for i := 0; i < len(g.tf.Nodes); i++ {
switch n := g.tf.Nodes[i].(type) {
case parser.TemplateFileGoExpression:
if err := g.writeGoExpression(n); err != nil {
return err
}
case parser.HTMLTemplate:
if err := g.writeTemplate(i, n); err != nil {
return err
}
case parser.CSSTemplate:
if err := g.writeCSS(n); err != nil {
return err
}
case parser.ScriptTemplate:
if err := g.writeScript(n); err != nil {
return err
}
default:
return fmt.Errorf("unknown node type: %v", reflect.TypeOf(n))
}
}
return nil
}
func (g *generator) writeCSS(n parser.CSSTemplate) error {
var r parser.Range
var tgtSymbolRange parser.Range
var err error
var indentLevel int
// func
if r, err = g.w.Write("func "); err != nil {
return err
}
tgtSymbolRange.From = r.From
if r, err = g.w.Write(n.Expression.Value); err != nil {
return err
}
g.sourceMap.Add(n.Expression, r)
// templ.CSSClass {
if _, err = g.w.Write(" templ.CSSClass {\n"); err != nil {
return err
}
{
indentLevel++
// templ_7745c5c3_CSSBuilder := templruntim.GetBuilder()
if _, err = g.w.WriteIndent(indentLevel, "templ_7745c5c3_CSSBuilder := templruntime.GetBuilder()\n"); err != nil {
return err
}
for i := 0; i < len(n.Properties); i++ {
switch p := n.Properties[i].(type) {
case parser.ConstantCSSProperty:
// Constant CSS property values are not sanitized.
if _, err = g.w.WriteIndent(indentLevel, "templ_7745c5c3_CSSBuilder.WriteString("+createGoString(p.String(true))+")\n"); err != nil {
return err
}
case parser.ExpressionCSSProperty:
// templ_7745c5c3_CSSBuilder.WriteString(templ.SanitizeCSS('name', p.Expression()))
if _, err = g.w.WriteIndent(indentLevel, fmt.Sprintf("templ_7745c5c3_CSSBuilder.WriteString(string(templ.SanitizeCSS(`%s`, ", p.Name)); err != nil {
return err
}
if r, err = g.w.Write(p.Value.Expression.Value); err != nil {
return err
}
g.sourceMap.Add(p.Value.Expression, r)
if _, err = g.w.Write(")))\n"); err != nil {
return err
}
default:
return fmt.Errorf("unknown CSS property type: %v", reflect.TypeOf(p))
}
}
if _, err = g.w.WriteIndent(indentLevel, fmt.Sprintf("templ_7745c5c3_CSSID := templ.CSSID(`%s`, templ_7745c5c3_CSSBuilder.String())\n", n.Name)); err != nil {
return err
}
// return templ.CSS {
if _, err = g.w.WriteIndent(indentLevel, "return templ.ComponentCSSClass{\n"); err != nil {
return err
}
{
indentLevel++
// ID: templ_7745c5c3_CSSID,
if _, err = g.w.WriteIndent(indentLevel, "ID: templ_7745c5c3_CSSID,\n"); err != nil {
return err
}
// Class: templ.SafeCSS(".cssID{" + templ.CSSBuilder.String() + "}"),
if _, err = g.w.WriteIndent(indentLevel, "Class: templ.SafeCSS(`.` + templ_7745c5c3_CSSID + `{` + templ_7745c5c3_CSSBuilder.String() + `}`),\n"); err != nil {
return err
}
indentLevel--
}
if _, err = g.w.WriteIndent(indentLevel, "}\n"); err != nil {
return err
}
indentLevel--
}
// }
if r, err = g.w.WriteIndent(indentLevel, "}\n\n"); err != nil {
return err
}
// Keep a track of symbol ranges for the LSP.
tgtSymbolRange.To = r.To
g.sourceMap.AddSymbolRange(n.Range, tgtSymbolRange)
return nil
}
func (g *generator) writeGoExpression(n parser.TemplateFileGoExpression) (err error) {
var tgtSymbolRange parser.Range
r, err := g.w.Write(n.Expression.Value)
if err != nil {
return err
}
tgtSymbolRange.From = r.From
g.sourceMap.Add(n.Expression, r)
v := n.Expression.Value
lineSlice := strings.Split(v, "\n")
lastLine := lineSlice[len(lineSlice)-1]
if strings.HasPrefix(lastLine, "//") {
if _, err = g.w.WriteIndent(0, "\n"); err != nil {
return err
}
return err
}
if r, err = g.w.WriteIndent(0, "\n\n"); err != nil {
return err
}
// Keep a track of symbol ranges for the LSP.
tgtSymbolRange.To = r.To
g.sourceMap.AddSymbolRange(n.Expression.Range, tgtSymbolRange)
return err
}
func (g *generator) writeTemplBuffer(indentLevel int) (err error) {
// templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if _, err = g.w.WriteIndent(indentLevel, "templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)\n"); err != nil {
return err
}
// if !templ_7745c5c3_IsBuffer {
// defer func() {
// templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
// if templ_7745c5c3_Err == nil {
// templ_7745c5c3_Err = templ_7745c5c3_BufErr
// }
// }()
// }
if _, err = g.w.WriteIndent(indentLevel, "if !templ_7745c5c3_IsBuffer {\n"); err != nil {
return err
}
{
indentLevel++
if _, err = g.w.WriteIndent(indentLevel, "defer func() {\n"); err != nil {
return err
}
{
indentLevel++
if _, err = g.w.WriteIndent(indentLevel, "templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)\n"); err != nil {
return err
}
if _, err = g.w.WriteIndent(indentLevel, "if templ_7745c5c3_Err == nil {\n"); err != nil {
return err
}
{
indentLevel++
if _, err = g.w.WriteIndent(indentLevel, "templ_7745c5c3_Err = templ_7745c5c3_BufErr\n"); err != nil {
return err
}
indentLevel--
}
if _, err = g.w.WriteIndent(indentLevel, "}\n"); err != nil {
return err
}
indentLevel--
}
if _, err = g.w.WriteIndent(indentLevel, "}()\n"); err != nil {
return err
}
indentLevel--
}
if _, err = g.w.WriteIndent(indentLevel, "}\n"); err != nil {
return err
}
return
}
func (g *generator) writeTemplate(nodeIdx int, t parser.HTMLTemplate) error {
var r parser.Range
var tgtSymbolRange parser.Range
var err error
var indentLevel int
// func
if r, err = g.w.Write("func "); err != nil {
return err
}
tgtSymbolRange.From = r.From
// (r *Receiver) Name(params []string)
if r, err = g.w.Write(t.Expression.Value); err != nil {
return err
}
g.sourceMap.Add(t.Expression, r)
// templ.Component {
if _, err = g.w.Write(" templ.Component {\n"); err != nil {
return err
}
indentLevel++
// return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
if _, err = g.w.WriteIndent(indentLevel, "return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {\n"); err != nil {
return err
}
{
indentLevel++
if _, err = g.w.WriteIndent(indentLevel, "templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context\n"); err != nil {
return err
}
if _, err = g.w.WriteIndent(indentLevel, "if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {\n"); err != nil {
return err
}
{
indentLevel++
if _, err = g.w.WriteIndent(indentLevel, "return templ_7745c5c3_CtxErr"); err != nil {
return err
}
indentLevel--
}
if _, err = g.w.WriteIndent(indentLevel, "}\n"); err != nil {
return err
}
if err := g.writeTemplBuffer(indentLevel); err != nil {
return err
}
// ctx = templ.InitializeContext(ctx)
if _, err = g.w.WriteIndent(indentLevel, "ctx = templ.InitializeContext(ctx)\n"); err != nil {
return err
}
g.childrenVar = g.createVariableName()
// templ_7745c5c3_Var1 := templ.GetChildren(ctx)
// if templ_7745c5c3_Var1 == nil {
// templ_7745c5c3_Var1 = templ.NopComponent
// }
if _, err = g.w.WriteIndent(indentLevel, fmt.Sprintf("%s := templ.GetChildren(ctx)\n", g.childrenVar)); err != nil {
return err
}
if _, err = g.w.WriteIndent(indentLevel, fmt.Sprintf("if %s == nil {\n", g.childrenVar)); err != nil {
return err
}
{
indentLevel++
if _, err = g.w.WriteIndent(indentLevel, fmt.Sprintf("%s = templ.NopComponent\n", g.childrenVar)); err != nil {
return err
}
indentLevel--
}
if _, err = g.w.WriteIndent(indentLevel, "}\n"); err != nil {
return err
}
// ctx = templ.ClearChildren(children)
if _, err = g.w.WriteIndent(indentLevel, "ctx = templ.ClearChildren(ctx)\n"); err != nil {
return err
}
// Nodes.
if err = g.writeNodes(indentLevel, stripWhitespace(t.Children), nil); err != nil {
return err
}
// return nil
if _, err = g.w.WriteIndent(indentLevel, "return nil\n"); err != nil {
return err
}
indentLevel--
}
// })
if _, err = g.w.WriteIndent(indentLevel, "})\n"); err != nil {
return err
}
indentLevel--
// }
// Note: gofmt wants to remove a single empty line at the end of a file
// so we have to make sure we don't output one if this is the last node.
closingBrace := "}\n\n"
if nodeIdx+1 >= len(g.tf.Nodes) {
closingBrace = "}\n"
}
if r, err = g.w.WriteIndent(indentLevel, closingBrace); err != nil {
return err
}
// Keep a track of symbol ranges for the LSP.
tgtSymbolRange.To = r.To
g.sourceMap.AddSymbolRange(t.Range, tgtSymbolRange)
return nil
}
func stripWhitespace(input []parser.Node) (output []parser.Node) {
for i, n := range input {
if _, isWhiteSpace := n.(parser.Whitespace); !isWhiteSpace {
output = append(output, input[i])
}
}
return output
}
func stripLeadingWhitespace(nodes []parser.Node) []parser.Node {
for i := 0; i < len(nodes); i++ {
n := nodes[i]
if _, isWhiteSpace := n.(parser.Whitespace); !isWhiteSpace {
return nodes[i:]
}
}
return []parser.Node{}
}
func stripTrailingWhitespace(nodes []parser.Node) []parser.Node {
for i := len(nodes) - 1; i >= 0; i-- {
n := nodes[i]
if _, isWhiteSpace := n.(parser.Whitespace); !isWhiteSpace {
return nodes[0 : i+1]
}
}
return []parser.Node{}
}
func stripLeadingAndTrailingWhitespace(nodes []parser.Node) []parser.Node {
return stripTrailingWhitespace(stripLeadingWhitespace(nodes))
}
func (g *generator) writeNodes(indentLevel int, nodes []parser.Node, next parser.Node) error {
for i, curr := range nodes {
var nextNode parser.Node
if i+1 < len(nodes) {
nextNode = nodes[i+1]
}
if nextNode == nil {
nextNode = next
}
if err := g.writeNode(indentLevel, curr, nextNode); err != nil {
return err
}
}
return nil
}
func (g *generator) writeNode(indentLevel int, current parser.Node, next parser.Node) (err error) {
switch n := current.(type) {
case parser.DocType:
err = g.writeDocType(indentLevel, n)
case parser.Element:
err = g.writeElement(indentLevel, n)
case parser.HTMLComment:
err = g.writeComment(indentLevel, n)
case parser.ChildrenExpression:
err = g.writeChildrenExpression(indentLevel)
case parser.RawElement:
err = g.writeRawElement(indentLevel, n)
case parser.ForExpression:
err = g.writeForExpression(indentLevel, n, next)
case parser.CallTemplateExpression:
err = g.writeCallTemplateExpression(indentLevel, n)
case parser.TemplElementExpression:
err = g.writeTemplElementExpression(indentLevel, n)
case parser.IfExpression:
err = g.writeIfExpression(indentLevel, n, next)
case parser.SwitchExpression:
err = g.writeSwitchExpression(indentLevel, n, next)
case parser.StringExpression:
err = g.writeStringExpression(indentLevel, n.Expression)
case parser.GoCode:
err = g.writeGoCode(indentLevel, n.Expression)
case parser.Whitespace:
err = g.writeWhitespace(indentLevel, n)
case parser.Text:
err = g.writeText(indentLevel, n)
case parser.GoComment:
// Do not render Go comments in the output HTML.
return
default:
return fmt.Errorf("unhandled type: %v", reflect.TypeOf(n))
}
// Write trailing whitespace, if there is a next node that might need the space.
// If the next node is inline or text, we might need it.
// If the current node is a block element, we don't need it.
needed := (isInlineOrText(current) && isInlineOrText(next))
if ws, ok := current.(parser.WhitespaceTrailer); ok && needed {
if err := g.writeWhitespaceTrailer(indentLevel, ws.Trailing()); err != nil {
return err
}
}
return
}
func isInlineOrText(next parser.Node) bool {
// While these are formatted as blocks when they're written in the HTML template.
// They're inline - i.e. there's no whitespace rendered around them at runtime for minification.
if next == nil {
return false
}
switch n := next.(type) {
case parser.IfExpression:
return true
case parser.SwitchExpression:
return true
case parser.ForExpression:
return true
case parser.Element:
return !n.IsBlockElement()
case parser.Text:
return true
case parser.StringExpression:
return true
}
return false
}
func (g *generator) writeWhitespaceTrailer(indentLevel int, n parser.TrailingSpace) (err error) {
if n == parser.SpaceNone {
return nil
}
// Normalize whitespace for minified output. In HTML, a single space is equivalent to
// any number of spaces, tabs, or newlines.
if n == parser.SpaceVertical {
n = parser.SpaceHorizontal
}
if _, err = g.w.WriteStringLiteral(indentLevel, string(n)); err != nil {
return err
}
return nil
}
func (g *generator) writeDocType(indentLevel int, n parser.DocType) (err error) {
if _, err = g.w.WriteStringLiteral(indentLevel, fmt.Sprintf("<!doctype %s>", n.Value)); err != nil {
return err
}
return nil
}
func (g *generator) writeIfExpression(indentLevel int, n parser.IfExpression, nextNode parser.Node) (err error) {
var r parser.Range
// if
if _, err = g.w.WriteIndent(indentLevel, `if `); err != nil {
return err
}
// x == y {
if r, err = g.w.Write(n.Expression.Value); err != nil {
return err
}
g.sourceMap.Add(n.Expression, r)
// {
if _, err = g.w.Write(` {` + "\n"); err != nil {
return err
}
{
indentLevel++
if err = g.writeNodes(indentLevel, stripLeadingAndTrailingWhitespace(n.Then), nextNode); err != nil {
return err
}
indentLevel--
}
for _, elseIf := range n.ElseIfs {
// } else if {
if _, err = g.w.WriteIndent(indentLevel, `} else if `); err != nil {
return err
}
// x == y {
if r, err = g.w.Write(elseIf.Expression.Value); err != nil {
return err
}
g.sourceMap.Add(elseIf.Expression, r)
// {
if _, err = g.w.Write(` {` + "\n"); err != nil {
return err
}
{
indentLevel++
if err = g.writeNodes(indentLevel, stripLeadingAndTrailingWhitespace(elseIf.Then), nextNode); err != nil {
return err
}
indentLevel--
}
}
if len(n.Else) > 0 {
// } else {
if _, err = g.w.WriteIndent(indentLevel, `} else {`+"\n"); err != nil {
return err
}
{
indentLevel++
if err = g.writeNodes(indentLevel, stripLeadingAndTrailingWhitespace(n.Else), nextNode); err != nil {
return err
}
indentLevel--
}
}
// }
if _, err = g.w.WriteIndent(indentLevel, `}`+"\n"); err != nil {
return err
}
return nil
}
func (g *generator) writeSwitchExpression(indentLevel int, n parser.SwitchExpression, next parser.Node) (err error) {
var r parser.Range
// switch
if _, err = g.w.WriteIndent(indentLevel, `switch `); err != nil {
return err
}
// val
if r, err = g.w.Write(n.Expression.Value); err != nil {
return err
}
g.sourceMap.Add(n.Expression, r)
// {
if _, err = g.w.Write(` {` + "\n"); err != nil {
return err
}
if len(n.Cases) > 0 {
for _, c := range n.Cases {
// case x:
// default:
if r, err = g.w.WriteIndent(indentLevel, c.Expression.Value); err != nil {
return err
}
g.sourceMap.Add(c.Expression, r)
indentLevel++
if err = g.writeNodes(indentLevel, stripLeadingAndTrailingWhitespace(c.Children), next); err != nil {
return err
}
indentLevel--
}
}
// }
if _, err = g.w.WriteIndent(indentLevel, `}`+"\n"); err != nil {
return err
}
return nil
}
func (g *generator) writeChildrenExpression(indentLevel int) (err error) {
if _, err = g.w.WriteIndent(indentLevel, fmt.Sprintf("templ_7745c5c3_Err = %s.Render(ctx, templ_7745c5c3_Buffer)\n", g.childrenVar)); err != nil {
return err
}
if err = g.writeErrorHandler(indentLevel); err != nil {
return err
}
return nil
}
func (g *generator) writeTemplElementExpression(indentLevel int, n parser.TemplElementExpression) (err error) {
if len(n.Children) == 0 {
return g.writeSelfClosingTemplElementExpression(indentLevel, n)
}
return g.writeBlockTemplElementExpression(indentLevel, n)
}
func (g *generator) writeBlockTemplElementExpression(indentLevel int, n parser.TemplElementExpression) (err error) {
var r parser.Range
childrenName := g.createVariableName()
if _, err = g.w.WriteIndent(indentLevel, childrenName+" := templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {\n"); err != nil {
return err
}
indentLevel++
if _, err = g.w.WriteIndent(indentLevel, "templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context\n"); err != nil {
return err
}
if err := g.writeTemplBuffer(indentLevel); err != nil {
return err
}
// ctx = templ.InitializeContext(ctx)
if _, err = g.w.WriteIndent(indentLevel, "ctx = templ.InitializeContext(ctx)\n"); err != nil {
return err
}
if err = g.writeNodes(indentLevel, stripLeadingAndTrailingWhitespace(n.Children), nil); err != nil {
return err
}
// return nil
if _, err = g.w.WriteIndent(indentLevel, "return nil\n"); err != nil {
return err
}
indentLevel--
if _, err = g.w.WriteIndent(indentLevel, "})\n"); err != nil {
return err
}
if _, err = g.w.WriteIndent(indentLevel, `templ_7745c5c3_Err = `); err != nil {
return err
}
if r, err = g.w.Write(n.Expression.Value); err != nil {
return err
}
g.sourceMap.Add(n.Expression, r)
// .Render(templ.WithChildren(ctx, children), templ_7745c5c3_Buffer)
if _, err = g.w.Write(".Render(templ.WithChildren(ctx, " + childrenName + "), templ_7745c5c3_Buffer)\n"); err != nil {
return err
}
if err = g.writeErrorHandler(indentLevel); err != nil {
return err
}
return nil
}
func (g *generator) writeSelfClosingTemplElementExpression(indentLevel int, n parser.TemplElementExpression) (err error) {
if _, err = g.w.WriteIndent(indentLevel, `templ_7745c5c3_Err = `); err != nil {
return err
}
// Template expression.
var r parser.Range
if r, err = g.w.Write(n.Expression.Value); err != nil {
return err
}
g.sourceMap.Add(n.Expression, r)
// .Render(ctx, templ_7745c5c3_Buffer)
if _, err = g.w.Write(".Render(ctx, templ_7745c5c3_Buffer)\n"); err != nil {
return err
}
if err = g.writeErrorHandler(indentLevel); err != nil {
return err
}
return nil
}
func (g *generator) writeCallTemplateExpression(indentLevel int, n parser.CallTemplateExpression) (err error) {
if _, err = g.w.WriteIndent(indentLevel, `templ_7745c5c3_Err = `); err != nil {
return err
}
// Template expression.
var r parser.Range
if r, err = g.w.Write(n.Expression.Value); err != nil {
return err
}
g.sourceMap.Add(n.Expression, r)
// .Render(ctx, templ_7745c5c3_Buffer)
if _, err = g.w.Write(".Render(ctx, templ_7745c5c3_Buffer)\n"); err != nil {
return err
}
if err = g.writeErrorHandler(indentLevel); err != nil {
return err
}
return nil
}
func (g *generator) writeForExpression(indentLevel int, n parser.ForExpression, next parser.Node) (err error) {
var r parser.Range
// for
if _, err = g.w.WriteIndent(indentLevel, `for `); err != nil {
return err
}
// i, v := range p.Stuff
if r, err = g.w.Write(n.Expression.Value); err != nil {
return err
}
g.sourceMap.Add(n.Expression, r)
// {
if _, err = g.w.Write(` {` + "\n"); err != nil {
return err
}
// Children.
indentLevel++
if err = g.writeNodes(indentLevel, stripLeadingAndTrailingWhitespace(n.Children), next); err != nil {
return err
}
indentLevel--
// }
if _, err = g.w.WriteIndent(indentLevel, `}`+"\n"); err != nil {
return err
}
return nil
}
func (g *generator) writeErrorHandler(indentLevel int) (err error) {
_, err = g.w.WriteIndent(indentLevel, "if templ_7745c5c3_Err != nil {\n")
if err != nil {
return err
}
indentLevel++
_, err = g.w.WriteIndent(indentLevel, "return templ_7745c5c3_Err\n")
if err != nil {
return err
}
indentLevel--
_, err = g.w.WriteIndent(indentLevel, "}\n")
if err != nil {
return err
}
return err
}
func (g *generator) writeExpressionErrorHandler(indentLevel int, expression parser.Expression) (err error) {
_, err = g.w.WriteIndent(indentLevel, "if templ_7745c5c3_Err != nil {\n")
if err != nil {
return err
}
indentLevel++
line := int(expression.Range.To.Line + 1)
col := int(expression.Range.To.Col)
_, err = g.w.WriteIndent(indentLevel, "return templ.Error{Err: templ_7745c5c3_Err, FileName: "+createGoString(g.options.FileName)+", Line: "+strconv.Itoa(line)+", Col: "+strconv.Itoa(col)+"}\n")
if err != nil {
return err
}
indentLevel--
_, err = g.w.WriteIndent(indentLevel, "}\n")
if err != nil {
return err
}
return err
}
func copyAttributes(attr []parser.Attribute) []parser.Attribute {
o := make([]parser.Attribute, len(attr))
for i, a := range attr {
if c, ok := a.(parser.ConditionalAttribute); ok {
c.Then = copyAttributes(c.Then)
c.Else = copyAttributes(c.Else)
o[i] = c
continue
}
o[i] = a
}
return o
}
func (g *generator) writeElement(indentLevel int, n parser.Element) (err error) {
if len(n.Attributes) == 0 {
// <div>
if _, err = g.w.WriteStringLiteral(indentLevel, fmt.Sprintf(`<%s>`, html.EscapeString(n.Name))); err != nil {
return err
}
} else {
attrs := copyAttributes(n.Attributes)
// <style type="text/css"></style>
if err = g.writeElementCSS(indentLevel, attrs); err != nil {
return err
}
// <script></script>
if err = g.writeElementScript(indentLevel, attrs); err != nil {
return err
}
// <div
if _, err = g.w.WriteStringLiteral(indentLevel, fmt.Sprintf(`<%s`, html.EscapeString(n.Name))); err != nil {
return err
}
if err = g.writeElementAttributes(indentLevel, n.Name, attrs); err != nil {
return err
}
// >
if _, err = g.w.WriteStringLiteral(indentLevel, `>`); err != nil {
return err
}
}
// Skip children and close tag for void elements.
if n.IsVoidElement() && len(n.Children) == 0 {
return nil
}
// Children.
if err = g.writeNodes(indentLevel, stripWhitespace(n.Children), nil); err != nil {
return err
}
// </div>
if _, err = g.w.WriteStringLiteral(indentLevel, fmt.Sprintf(`</%s>`, html.EscapeString(n.Name))); err != nil {
return err
}
return err
}
func (g *generator) writeAttributeCSS(indentLevel int, attr parser.ExpressionAttribute) (result parser.ExpressionAttribute, ok bool, err error) {
var r parser.Range
name := html.EscapeString(attr.Name)
if name != "class" {
ok = false
return
}
// Create a class name for the style.
// The expression can either be expecting a templ.Classes call, or an expression that returns
// var templ_7745c5c3_CSSClasses = []any{
classesName := g.createVariableName()
if _, err = g.w.WriteIndent(indentLevel, "var "+classesName+" = []any{"); err != nil {
return
}
// p.Name()
if r, err = g.w.Write(attr.Expression.Value); err != nil {
return
}
g.sourceMap.Add(attr.Expression, r)
// }\n
if _, err = g.w.Write("}\n"); err != nil {
return
}
// Render the CSS before the element if required.
// templ_7745c5c3_Err = templ.RenderCSSItems(ctx, templ_7745c5c3_Buffer, templ_7745c5c3_CSSClasses...)
if _, err = g.w.WriteIndent(indentLevel, "templ_7745c5c3_Err = templ.RenderCSSItems(ctx, templ_7745c5c3_Buffer, "+classesName+"...)\n"); err != nil {
return
}
if err = g.writeErrorHandler(indentLevel); err != nil {
return
}
// Rewrite the ExpressionAttribute to point at the new variable.
attr.Expression = parser.Expression{
Value: "templ.CSSClasses(" + classesName + ").String()",
}
return attr, true, nil
}
func (g *generator) writeAttributesCSS(indentLevel int, attrs []parser.Attribute) (err error) {
for i := 0; i < len(attrs); i++ {
if attr, ok := attrs[i].(parser.ExpressionAttribute); ok {
attr, ok, err = g.writeAttributeCSS(indentLevel, attr)
if err != nil {
return err
}
if ok {
attrs[i] = attr
}
}
if cattr, ok := attrs[i].(parser.ConditionalAttribute); ok {
err = g.writeAttributesCSS(indentLevel, cattr.Then)
if err != nil {
return err
}
err = g.writeAttributesCSS(indentLevel, cattr.Else)
if err != nil {
return err
}
attrs[i] = cattr
}
}
return nil
}
func (g *generator) writeElementCSS(indentLevel int, attrs []parser.Attribute) (err error) {
return g.writeAttributesCSS(indentLevel, attrs)
}
func isScriptAttribute(name string) bool {
for _, prefix := range []string{"on", "hx-on:"} {
if strings.HasPrefix(name, prefix) {
return true
}
}
return false
}
func (g *generator) writeElementScript(indentLevel int, attrs []parser.Attribute) (err error) {
var scriptExpressions []string
for _, attr := range attrs {
scriptExpressions = append(scriptExpressions, getAttributeScripts(attr)...)
}
if len(scriptExpressions) == 0 {
return
}
// Render the scripts before the element if required.
// templ_7745c5c3_Err = templ.RenderScriptItems(ctx, templ_7745c5c3_Buffer, a, b, c)
if _, err = g.w.WriteIndent(indentLevel, "templ_7745c5c3_Err = templ.RenderScriptItems(ctx, templ_7745c5c3_Buffer, "+strings.Join(scriptExpressions, ", ")+")\n"); err != nil {
return err
}
if err = g.writeErrorHandler(indentLevel); err != nil {
return err
}
return err
}
func getAttributeScripts(attr parser.Attribute) (scripts []string) {
if attr, ok := attr.(parser.ConditionalAttribute); ok {
for _, attr := range attr.Then {
scripts = append(scripts, getAttributeScripts(attr)...)
}
for _, attr := range attr.Else {
scripts = append(scripts, getAttributeScripts(attr)...)
}
}
if attr, ok := attr.(parser.ExpressionAttribute); ok {
name := html.EscapeString(attr.Name)
if isScriptAttribute(name) {
scripts = append(scripts, attr.Expression.Value)
}
}
return scripts
}
func (g *generator) writeBoolConstantAttribute(indentLevel int, attr parser.BoolConstantAttribute) (err error) {
name := html.EscapeString(attr.Name)
if _, err = g.w.WriteStringLiteral(indentLevel, fmt.Sprintf(` %s`, name)); err != nil {
return err
}
return nil
}
func (g *generator) writeConstantAttribute(indentLevel int, attr parser.ConstantAttribute) (err error) {
name := html.EscapeString(attr.Name)
value := html.EscapeString(attr.Value)
value = strconv.Quote(value)
value = value[1 : len(value)-1]
if _, err = g.w.WriteStringLiteral(indentLevel, fmt.Sprintf(` %s=\"%s\"`, name, value)); err != nil {
return err
}
return nil
}
func (g *generator) writeBoolExpressionAttribute(indentLevel int, attr parser.BoolExpressionAttribute) (err error) {
name := html.EscapeString(attr.Name)
// if
if _, err = g.w.WriteIndent(indentLevel, `if `); err != nil {
return err
}
// x == y
var r parser.Range
if r, err = g.w.Write(attr.Expression.Value); err != nil {
return err
}
g.sourceMap.Add(attr.Expression, r)
// {
if _, err = g.w.Write(` {` + "\n"); err != nil {
return err
}
{
indentLevel++
if _, err = g.w.WriteStringLiteral(indentLevel, fmt.Sprintf(` %s`, name)); err != nil {
return err
}
indentLevel--
}
// }
if _, err = g.w.WriteIndent(indentLevel, `}`+"\n"); err != nil {
return err
}
return nil
}
func (g *generator) writeExpressionAttributeValueURL(indentLevel int, attr parser.ExpressionAttribute) (err error) {
vn := g.createVariableName()
// var vn templ.SafeURL =
if _, err = g.w.WriteIndent(indentLevel, "var "+vn+" templ.SafeURL = "); err != nil {
return err
}
// p.Name()
var r parser.Range
if r, err = g.w.Write(attr.Expression.Value); err != nil {
return err
}
g.sourceMap.Add(attr.Expression, r)
if _, err = g.w.Write("\n"); err != nil {
return err
}
if _, err = g.w.WriteIndent(indentLevel, "_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(string("+vn+")))\n"); err != nil {
return err
}
return g.writeErrorHandler(indentLevel)
}
func (g *generator) writeExpressionAttributeValueScript(indentLevel int, attr parser.ExpressionAttribute) (err error) {
// It's a JavaScript handler, and requires special handling, because we expect a JavaScript expression.
vn := g.createVariableName()
// var vn templ.ComponentScript =
if _, err = g.w.WriteIndent(indentLevel, "var "+vn+" templ.ComponentScript = "); err != nil {
return err
}
// p.Name()
var r parser.Range
if r, err = g.w.Write(attr.Expression.Value); err != nil {
return err
}
g.sourceMap.Add(attr.Expression, r)
if _, err = g.w.Write("\n"); err != nil {
return err
}
if _, err = g.w.WriteIndent(indentLevel, "_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("+vn+".Call)\n"); err != nil {
return err
}
return g.writeErrorHandler(indentLevel)
}
func (g *generator) writeExpressionAttributeValueDefault(indentLevel int, attr parser.ExpressionAttribute) (err error) {
var r parser.Range
vn := g.createVariableName()
// var vn string
if _, err = g.w.WriteIndent(indentLevel, "var "+vn+" string\n"); err != nil {
return err
}
// vn, templ_7745c5c3_Err = templ.JoinStringErrs(
if _, err = g.w.WriteIndent(indentLevel, vn+", templ_7745c5c3_Err = templ.JoinStringErrs("); err != nil {
return err
}
// p.Name()
if r, err = g.w.Write(attr.Expression.Value); err != nil {
return err
}
g.sourceMap.Add(attr.Expression, r)
// )
if _, err = g.w.Write(")\n"); err != nil {
return err
}
// Attribute expression error handler.
err = g.writeExpressionErrorHandler(indentLevel, attr.Expression)
if err != nil {
return err
}
// _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(vn)
if _, err = g.w.WriteIndent(indentLevel, "_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString("+vn+"))\n"); err != nil {
return err
}
return g.writeErrorHandler(indentLevel)
}
func (g *generator) writeExpressionAttributeValueStyle(indentLevel int, attr parser.ExpressionAttribute) (err error) {
var r parser.Range
vn := g.createVariableName()
// var vn string
if _, err = g.w.WriteIndent(indentLevel, "var "+vn+" string\n"); err != nil {
return err
}
// vn, templ_7745c5c3_Err = templruntime.SanitizeStyleAttributeValues(
if _, err = g.w.WriteIndent(indentLevel, vn+", templ_7745c5c3_Err = templruntime.SanitizeStyleAttributeValues("); err != nil {
return err
}
// value
if r, err = g.w.Write(attr.Expression.Value); err != nil {
return err
}
g.sourceMap.Add(attr.Expression, r)
// )
if _, err = g.w.Write(")\n"); err != nil {
return err
}
// Attribute expression error handler.
err = g.writeExpressionErrorHandler(indentLevel, attr.Expression)
if err != nil {
return err
}
// _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(vn))
if _, err = g.w.WriteIndent(indentLevel, "_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString("+vn+"))\n"); err != nil {
return err
}
return g.writeErrorHandler(indentLevel)
}
func (g *generator) writeExpressionAttribute(indentLevel int, elementName string, attr parser.ExpressionAttribute) (err error) {
attrName := html.EscapeString(attr.Name)
// Name
if _, err = g.w.WriteStringLiteral(indentLevel, fmt.Sprintf(` %s=`, attrName)); err != nil {
return err
}
// Open quote.
if _, err = g.w.WriteStringLiteral(indentLevel, `\"`); err != nil {
return err
}
// Value.
if (elementName == "a" && attr.Name == "href") || (elementName == "form" && attr.Name == "action") {
if err := g.writeExpressionAttributeValueURL(indentLevel, attr); err != nil {
return err
}
} else if isScriptAttribute(attr.Name) {
if err := g.writeExpressionAttributeValueScript(indentLevel, attr); err != nil {
return err
}
} else if attr.Name == "style" {
if err := g.writeExpressionAttributeValueStyle(indentLevel, attr); err != nil {
return err
}
} else {
if err := g.writeExpressionAttributeValueDefault(indentLevel, attr); err != nil {
return err
}
}
// Close quote.
if _, err = g.w.WriteStringLiteral(indentLevel, `\"`); err != nil {
return err
}
return nil
}
func (g *generator) writeSpreadAttributes(indentLevel int, attr parser.SpreadAttributes) (err error) {
// templ.RenderAttributes(ctx, w, spreadAttrs)
if _, err = g.w.WriteIndent(indentLevel, `templ_7745c5c3_Err = templ.RenderAttributes(ctx, templ_7745c5c3_Buffer, `); err != nil {
return err
}
// spreadAttrs
var r parser.Range
if r, err = g.w.Write(attr.Expression.Value); err != nil {
return err
}
g.sourceMap.Add(attr.Expression, r)
// )
if _, err = g.w.Write(")\n"); err != nil {
return err
}
if err = g.writeErrorHandler(indentLevel); err != nil {
return err
}
return nil
}
func (g *generator) writeConditionalAttribute(indentLevel int, elementName string, attr parser.ConditionalAttribute) (err error) {
// if
if _, err = g.w.WriteIndent(indentLevel, `if `); err != nil {
return err
}
// x == y
var r parser.Range
if r, err = g.w.Write(attr.Expression.Value); err != nil {
return err
}
g.sourceMap.Add(attr.Expression, r)
// {
if _, err = g.w.Write(` {` + "\n"); err != nil {
return err
}
{
indentLevel++
if err = g.writeElementAttributes(indentLevel, elementName, attr.Then); err != nil {
return err
}
indentLevel--
}
if len(attr.Else) > 0 {
// } else {
if _, err = g.w.WriteIndent(indentLevel, `} else {`+"\n"); err != nil {
return err
}
{
indentLevel++
if err = g.writeElementAttributes(indentLevel, elementName, attr.Else); err != nil {
return err
}
indentLevel--
}
}
// }
if _, err = g.w.WriteIndent(indentLevel, `}`+"\n"); err != nil {
return err
}
return nil
}
func (g *generator) writeElementAttributes(indentLevel int, name string, attrs []parser.Attribute) (err error) {
for i := 0; i < len(attrs); i++ {
switch attr := attrs[i].(type) {
case parser.BoolConstantAttribute:
err = g.writeBoolConstantAttribute(indentLevel, attr)
case parser.ConstantAttribute:
err = g.writeConstantAttribute(indentLevel, attr)
case parser.BoolExpressionAttribute:
err = g.writeBoolExpressionAttribute(indentLevel, attr)
case parser.ExpressionAttribute:
err = g.writeExpressionAttribute(indentLevel, name, attr)
case parser.SpreadAttributes:
err = g.writeSpreadAttributes(indentLevel, attr)
case parser.ConditionalAttribute:
err = g.writeConditionalAttribute(indentLevel, name, attr)
default:
err = fmt.Errorf("unknown attribute type %s", reflect.TypeOf(attrs[i]))
}
}
return
}
func (g *generator) writeRawElement(indentLevel int, n parser.RawElement) (err error) {
if len(n.Attributes) == 0 {
// <div>
if _, err = g.w.WriteStringLiteral(indentLevel, fmt.Sprintf(`<%s>`, html.EscapeString(n.Name))); err != nil {
return err
}
} else {
// <script></script>
if err = g.writeElementScript(indentLevel, n.Attributes); err != nil {
return err
}
// <div
if _, err = g.w.WriteStringLiteral(indentLevel, fmt.Sprintf(`<%s`, html.EscapeString(n.Name))); err != nil {
return err
}
if err = g.writeElementAttributes(indentLevel, n.Name, n.Attributes); err != nil {
return err
}
// >
if _, err = g.w.WriteStringLiteral(indentLevel, `>`); err != nil {
return err
}
}
// Contents.
if err = g.writeText(indentLevel, parser.Text{Value: n.Contents}); err != nil {
return err
}
// </div>
if _, err = g.w.WriteStringLiteral(indentLevel, fmt.Sprintf(`</%s>`, html.EscapeString(n.Name))); err != nil {
return err
}
return err
}
func (g *generator) writeComment(indentLevel int, c parser.HTMLComment) (err error) {
// <!--
if _, err = g.w.WriteStringLiteral(indentLevel, "<!--"); err != nil {
return err
}
// Contents.
if err = g.writeText(indentLevel, parser.Text{Value: c.Contents}); err != nil {
return err
}
// -->
if _, err = g.w.WriteStringLiteral(indentLevel, "-->"); err != nil {
return err
}
return err
}
func (g *generator) createVariableName() string {
g.variableID++
return "templ_7745c5c3_Var" + strconv.Itoa(g.variableID)
}
func (g *generator) writeGoCode(indentLevel int, e parser.Expression) (err error) {
if strings.TrimSpace(e.Value) == "" {
return
}
var r parser.Range
if r, err = g.w.WriteIndent(indentLevel, e.Value+"\n"); err != nil {
return err
}
g.sourceMap.Add(e, r)
return nil
}
func (g *generator) writeStringExpression(indentLevel int, e parser.Expression) (err error) {
if strings.TrimSpace(e.Value) == "" {
return
}
var r parser.Range
vn := g.createVariableName()
// var vn string
if _, err = g.w.WriteIndent(indentLevel, "var "+vn+" string\n"); err != nil {
return err
}
// vn, templ_7745c5c3_Err = templ.JoinStringErrs(
if _, err = g.w.WriteIndent(indentLevel, vn+", templ_7745c5c3_Err = templ.JoinStringErrs("); err != nil {
return err
}
// p.Name()
if r, err = g.w.Write(e.Value); err != nil {
return err
}
g.sourceMap.Add(e, r)
// )
if _, err = g.w.Write(")\n"); err != nil {
return err
}
// String expression error handler.
err = g.writeExpressionErrorHandler(indentLevel, e)
if err != nil {
return err
}
// _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(vn)
if _, err = g.w.WriteIndent(indentLevel, "_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString("+vn+"))\n"); err != nil {
return err
}
if err = g.writeErrorHandler(indentLevel); err != nil {
return err
}
return nil
}
func (g *generator) writeWhitespace(indentLevel int, n parser.Whitespace) (err error) {
if len(n.Value) == 0 {
return
}
// _, err = templ_7745c5c3_Buffer.WriteString(` `)
if _, err = g.w.WriteStringLiteral(indentLevel, " "); err != nil {
return err
}
return nil
}
func (g *generator) writeText(indentLevel int, n parser.Text) (err error) {
quoted := strconv.Quote(n.Value)
_, err = g.w.WriteStringLiteral(indentLevel, quoted[1:len(quoted)-1])
return err
}
func createGoString(s string) string {
var sb strings.Builder
sb.WriteRune('`')
sects := strings.Split(s, "`")
for i := 0; i < len(sects); i++ {
sb.WriteString(sects[i])
if len(sects) > i+1 {
sb.WriteString("` + \"`\" + `")
}
}
sb.WriteRune('`')
return sb.String()
}
func (g *generator) writeScript(t parser.ScriptTemplate) error {
var r parser.Range
var tgtSymbolRange parser.Range
var err error
var indentLevel int
// func
if r, err = g.w.Write("func "); err != nil {
return err
}
tgtSymbolRange.From = r.From
if r, err = g.w.Write(t.Name.Value); err != nil {
return err
}
g.sourceMap.Add(t.Name, r)
// (
if _, err = g.w.Write("("); err != nil {
return err
}
// Write parameters.
if r, err = g.w.Write(t.Parameters.Value); err != nil {
return err
}
g.sourceMap.Add(t.Parameters, r)
// ) templ.ComponentScript {
if _, err = g.w.Write(") templ.ComponentScript {\n"); err != nil {
return err
}
indentLevel++
// return templ.ComponentScript{
if _, err = g.w.WriteIndent(indentLevel, "return templ.ComponentScript{\n"); err != nil {
return err
}
{
indentLevel++
fn := functionName(t.Name.Value, t.Value)
goFn := createGoString(fn)
// Name: "scriptName",
if _, err = g.w.WriteIndent(indentLevel, "Name: "+goFn+",\n"); err != nil {
return err
}
// Function: `function scriptName(a, b, c){` + `constantScriptValue` + `}`,
prefix := "function " + fn + "(" + stripTypes(t.Parameters.Value) + "){"
body := strings.TrimLeftFunc(t.Value, unicode.IsSpace)
suffix := "}"
if _, err = g.w.WriteIndent(indentLevel, "Function: "+createGoString(prefix+body+suffix)+",\n"); err != nil {
return err
}
// Call: templ.SafeScript(scriptName, a, b, c)
if _, err = g.w.WriteIndent(indentLevel, "Call: templ.SafeScript("+goFn+", "+stripTypes(t.Parameters.Value)+"),\n"); err != nil {
return err
}
// CallInline: templ.SafeScriptInline(scriptName, a, b, c)
if _, err = g.w.WriteIndent(indentLevel, "CallInline: templ.SafeScriptInline("+goFn+", "+stripTypes(t.Parameters.Value)+"),\n"); err != nil {
return err
}
indentLevel--
}
// }
if _, err = g.w.WriteIndent(indentLevel, "}\n"); err != nil {
return err
}
indentLevel--
// }
if r, err = g.w.WriteIndent(indentLevel, "}\n\n"); err != nil {
return err
}
// Keep track of the symbol range for the LSP.
tgtSymbolRange.To = r.To
g.sourceMap.AddSymbolRange(t.Range, tgtSymbolRange)
return nil
}
// writeBlankAssignmentForRuntimeImport writes out a blank identifier assignment.
// This ensures that even if the github.com/a-h/templ/runtime package is not used in the generated code,
// the Go compiler will not complain about the unused import.
func (g *generator) writeBlankAssignmentForRuntimeImport() error {
var err error
if _, err = g.w.Write("var _ = templruntime.GeneratedTemplate"); err != nil {
return err
}
return nil
}
func functionName(name string, body string) string {
h := sha256.New()
h.Write([]byte(body))
hp := hex.EncodeToString(h.Sum(nil))[0:4]
return "__templ_" + name + "_" + hp
}
func stripTypes(parameters string) string {
variableNames := []string{}
params := strings.Split(parameters, ",")
for i := 0; i < len(params); i++ {
p := strings.Split(strings.TrimSpace(params[i]), " ")
variableNames = append(variableNames, strings.TrimSpace(p[0]))
}
return strings.Join(variableNames, ", ")
}
package htmldiff
import (
"context"
"errors"
"fmt"
"io"
"strings"
"sync"
"github.com/a-h/htmlformat"
"github.com/a-h/templ"
"github.com/google/go-cmp/cmp"
)
func DiffStrings(expected, actual string) (diff string, err error) {
// Format both strings.
var wg sync.WaitGroup
wg.Add(2)
var errs []error
// Format expected.
go func() {
defer wg.Done()
e := new(strings.Builder)
err := htmlformat.Fragment(e, strings.NewReader(expected))
if err != nil {
errs = append(errs, fmt.Errorf("expected html formatting error: %w", err))
}
expected = e.String()
}()
// Format actual.
go func() {
defer wg.Done()
a := new(strings.Builder)
err := htmlformat.Fragment(a, strings.NewReader(actual))
if err != nil {
errs = append(errs, fmt.Errorf("actual html formatting error: %w", err))
}
actual = a.String()
}()
// Wait for processing.
wg.Wait()
return cmp.Diff(expected, actual), errors.Join(errs...)
}
func Diff(input templ.Component, expected string) (diff string, err error) {
_, diff, err = DiffCtx(context.Background(), input, expected)
return diff, err
}
func DiffCtx(ctx context.Context, input templ.Component, expected string) (formattedInput, diff string, err error) {
var wg sync.WaitGroup
wg.Add(2)
var errs []error
// Format the expected value.
go func() {
defer wg.Done()
e := new(strings.Builder)
err := htmlformat.Fragment(e, strings.NewReader(expected))
if err != nil {
errs = append(errs, fmt.Errorf("expected html formatting error: %w", err))
}
expected = e.String()
}()
// Pipe via the HTML formatter.
actual := new(strings.Builder)
r, w := io.Pipe()
go func() {
defer wg.Done()
err := htmlformat.Fragment(actual, r)
if err != nil {
errs = append(errs, fmt.Errorf("actual html formatting error: %w", err))
}
}()
// Render the component.
err = input.Render(ctx, w)
if err != nil {
errs = append(errs, fmt.Errorf("failed to render component: %w", err))
}
w.Close()
// Wait for processing.
wg.Wait()
return actual.String(), cmp.Diff(expected, actual.String()), errors.Join(errs...)
}
package generator
import (
"io"
"strconv"
"strings"
"unicode/utf8"
"github.com/a-h/templ/parser/v2"
)
func NewRangeWriter(w io.Writer) *RangeWriter {
return &RangeWriter{
w: w,
builder: &strings.Builder{},
}
}
type RangeWriter struct {
Current parser.Position
inLiteral bool
w io.Writer
// Extract strings.
index int
builder *strings.Builder
Literals []string
}
func (rw *RangeWriter) closeLiteral(indent int) (r parser.Range, err error) {
rw.inLiteral = false
rw.index++
var sb strings.Builder
sb.WriteString(strings.Repeat("\t", indent))
sb.WriteString(`templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, `)
sb.WriteString(strconv.Itoa(rw.index))
sb.WriteString(`, "`)
literal := rw.builder.String()
rw.Literals = append(rw.Literals, literal)
sb.WriteString(literal)
rw.builder.Reset()
sb.WriteString(`")`)
sb.WriteString("\n")
if _, err := rw.write(sb.String()); err != nil {
return r, err
}
err = rw.writeErrorHandler(indent)
return
}
func (rw *RangeWriter) WriteIndent(level int, s string) (r parser.Range, err error) {
if rw.inLiteral {
if _, err = rw.closeLiteral(level); err != nil {
return
}
}
_, err = rw.write(strings.Repeat("\t", level))
if err != nil {
return
}
return rw.write(s)
}
func (rw *RangeWriter) WriteStringLiteral(level int, s string) (r parser.Range, err error) {
rw.inLiteral = true
rw.builder.WriteString(s)
return
}
func (rw *RangeWriter) Write(s string) (r parser.Range, err error) {
if rw.inLiteral {
if _, err = rw.closeLiteral(0); err != nil {
return
}
}
return rw.write(s)
}
func (rw *RangeWriter) write(s string) (r parser.Range, err error) {
r.From = parser.Position{
Index: rw.Current.Index,
Line: rw.Current.Line,
Col: rw.Current.Col,
}
utf8Bytes := make([]byte, 4)
for _, c := range s {
rlen := utf8.EncodeRune(utf8Bytes, c)
rw.Current.Col += uint32(rlen)
if c == '\n' {
rw.Current.Line++
rw.Current.Col = 0
}
_, err = rw.w.Write(utf8Bytes[:rlen])
rw.Current.Index += int64(rlen)
if err != nil {
return r, err
}
}
r.To = rw.Current
return r, err
}
func (rw *RangeWriter) writeErrorHandler(indentLevel int) (err error) {
_, err = rw.WriteIndent(indentLevel, "if templ_7745c5c3_Err != nil {\n")
if err != nil {
return err
}
indentLevel++
_, err = rw.WriteIndent(indentLevel, "return templ_7745c5c3_Err\n")
if err != nil {
return err
}
indentLevel--
_, err = rw.WriteIndent(indentLevel, "}\n")
if err != nil {
return err
}
return err
}
// Code generated by templ - DO NOT EDIT.
package testahref
//lint:file-ignore SA4006 This context is only used if a nested component is present.
import "github.com/a-h/templ"
import templruntime "github.com/a-h/templ/runtime"
func render() templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
return templ_7745c5c3_CtxErr
}
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var1 := templ.GetChildren(ctx)
if templ_7745c5c3_Var1 == nil {
templ_7745c5c3_Var1 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 1, "<a href=\"javascript:alert('unaffected');\">Ignored</a> <a href=\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var2 templ.SafeURL = templ.URL("javascript:alert('should be sanitized')")
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(string(templ_7745c5c3_Var2)))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 2, "\">Sanitized</a> <a href=\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var3 templ.SafeURL = templ.SafeURL("javascript:alert('should not be sanitized')")
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(string(templ_7745c5c3_Var3)))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 3, "\">Unsanitized</a>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return nil
})
}
var _ = templruntime.GeneratedTemplate
// Code generated by templ - DO NOT EDIT.
package testattrerrs
//lint:file-ignore SA4006 This context is only used if a nested component is present.
import "github.com/a-h/templ"
import templruntime "github.com/a-h/templ/runtime"
func funcWithNoError() (s string) {
return "OK"
}
func funcWithError(in error) (s string, err error) {
if in != nil {
return "", in
}
return "OK2", nil
}
func TestComponent(err error) templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
return templ_7745c5c3_CtxErr
}
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var1 := templ.GetChildren(ctx)
if templ_7745c5c3_Var1 == nil {
templ_7745c5c3_Var1 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 1, "<ul><li data-attr=\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var2 string
templ_7745c5c3_Var2, templ_7745c5c3_Err = templ.JoinStringErrs("raw")
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `generator/test-attribute-errors/template.templ`, Line: 16, Col: 23}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var2))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 2, "\"></li><li data-attr=\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var3 string
templ_7745c5c3_Var3, templ_7745c5c3_Err = templ.JoinStringErrs(funcWithNoError())
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `generator/test-attribute-errors/template.templ`, Line: 17, Col: 35}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var3))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 3, "\"></li><li data-attr=\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var4 string
templ_7745c5c3_Var4, templ_7745c5c3_Err = templ.JoinStringErrs(funcWithError(err))
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `generator/test-attribute-errors/template.templ`, Line: 18, Col: 36}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var4))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 4, "\"></li></ul>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return nil
})
}
var _ = templruntime.GeneratedTemplate
// Code generated by templ - DO NOT EDIT.
package testhtml
//lint:file-ignore SA4006 This context is only used if a nested component is present.
import "github.com/a-h/templ"
import templruntime "github.com/a-h/templ/runtime"
func BasicTemplate(url string) templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
return templ_7745c5c3_CtxErr
}
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var1 := templ.GetChildren(ctx)
if templ_7745c5c3_Var1 == nil {
templ_7745c5c3_Var1 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 1, "<div><a href=\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var2 templ.SafeURL = templ.URL(url)
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(string(templ_7745c5c3_Var2)))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 2, "\">text</a></div><div><button hx-post=\"/click\" hx-trigger=\"click\" hx-vals=\"{"val":"Value"}\">Click</button></div>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return nil
})
}
var _ = templruntime.GeneratedTemplate
// Code generated by templ - DO NOT EDIT.
package testcall
//lint:file-ignore SA4006 This context is only used if a nested component is present.
import "github.com/a-h/templ"
import templruntime "github.com/a-h/templ/runtime"
func showAll() templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
return templ_7745c5c3_CtxErr
}
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var1 := templ.GetChildren(ctx)
if templ_7745c5c3_Var1 == nil {
templ_7745c5c3_Var1 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
templ_7745c5c3_Err = a().Render(ctx, templ_7745c5c3_Buffer)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = b(c("C")).Render(ctx, templ_7745c5c3_Buffer)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = d().Render(ctx, templ_7745c5c3_Buffer)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = showOne(e()).Render(ctx, templ_7745c5c3_Buffer)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Var2 := templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 1, "<div>Child content</div>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return nil
})
templ_7745c5c3_Err = wrapChildren().Render(templ.WithChildren(ctx, templ_7745c5c3_Var2), templ_7745c5c3_Buffer)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return nil
})
}
func a() templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
return templ_7745c5c3_CtxErr
}
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var3 := templ.GetChildren(ctx)
if templ_7745c5c3_Var3 == nil {
templ_7745c5c3_Var3 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 2, "<div>A</div>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return nil
})
}
func b(child templ.Component) templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
return templ_7745c5c3_CtxErr
}
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var4 := templ.GetChildren(ctx)
if templ_7745c5c3_Var4 == nil {
templ_7745c5c3_Var4 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 3, "<div>B</div>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = child.Render(ctx, templ_7745c5c3_Buffer)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return nil
})
}
func c(text string) templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
return templ_7745c5c3_CtxErr
}
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var5 := templ.GetChildren(ctx)
if templ_7745c5c3_Var5 == nil {
templ_7745c5c3_Var5 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 4, "<div>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var6 string
templ_7745c5c3_Var6, templ_7745c5c3_Err = templ.JoinStringErrs(text)
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `generator/test-call/template.templ`, Line: 23, Col: 12}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var6))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 5, "</div>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return nil
})
}
func d() templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
return templ_7745c5c3_CtxErr
}
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var7 := templ.GetChildren(ctx)
if templ_7745c5c3_Var7 == nil {
templ_7745c5c3_Var7 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 6, "<div>Legacy call style</div>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return nil
})
}
func e() templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
return templ_7745c5c3_CtxErr
}
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var8 := templ.GetChildren(ctx)
if templ_7745c5c3_Var8 == nil {
templ_7745c5c3_Var8 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 7, "e")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return nil
})
}
func showOne(component templ.Component) templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
return templ_7745c5c3_CtxErr
}
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var9 := templ.GetChildren(ctx)
if templ_7745c5c3_Var9 == nil {
templ_7745c5c3_Var9 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 8, "<div>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = component.Render(ctx, templ_7745c5c3_Buffer)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 9, "</div>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return nil
})
}
func wrapChildren() templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
return templ_7745c5c3_CtxErr
}
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var10 := templ.GetChildren(ctx)
if templ_7745c5c3_Var10 == nil {
templ_7745c5c3_Var10 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 10, "<div id=\"wrapper\">")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templ_7745c5c3_Var10.Render(ctx, templ_7745c5c3_Buffer)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 11, "</div>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return nil
})
}
var _ = templruntime.GeneratedTemplate
// Code generated by templ - DO NOT EDIT.
package testcancelledcontext
//lint:file-ignore SA4006 This context is only used if a nested component is present.
import "github.com/a-h/templ"
import templruntime "github.com/a-h/templ/runtime"
func EmptyComponent() templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
return templ_7745c5c3_CtxErr
}
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var1 := templ.GetChildren(ctx)
if templ_7745c5c3_Var1 == nil {
templ_7745c5c3_Var1 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
return nil
})
}
var _ = templruntime.GeneratedTemplate
// Code generated by templ - DO NOT EDIT.
package testcomplexattributes
//lint:file-ignore SA4006 This context is only used if a nested component is present.
import "github.com/a-h/templ"
import templruntime "github.com/a-h/templ/runtime"
func ComplexAttributes() templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
return templ_7745c5c3_CtxErr
}
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var1 := templ.GetChildren(ctx)
if templ_7745c5c3_Var1 == nil {
templ_7745c5c3_Var1 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 1, "<div x-data=\"{darkMode: localStorage.getItem('darkMode') || localStorage.setItem('darkMode', 'system')}\" x-init=\"$watch('darkMode', val => localStorage.setItem('darkMode', val))\" :class=\"{'dark': darkMode === 'dark' || (darkMode === 'system' && window.matchMedia('(prefers-color-scheme: dark)').matches)}\"></div><div x-data=\"{ count: 0 }\"><button x-on:click=\"count++\">Increment</button> <span x-text=\"count\"></span></div><div x-data=\"{ count: 0 }\"><button @click=\"count++\">Increment</button> <span x-text=\"count\"></span></div>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return nil
})
}
var _ = templruntime.GeneratedTemplate
// Code generated by templ - DO NOT EDIT.
package testconstantattributeescaping
//lint:file-ignore SA4006 This context is only used if a nested component is present.
import "github.com/a-h/templ"
import templruntime "github.com/a-h/templ/runtime"
func BasicTemplate() templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
return templ_7745c5c3_CtxErr
}
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var1 := templ.GetChildren(ctx)
if templ_7745c5c3_Var1 == nil {
templ_7745c5c3_Var1 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 1, "<div><!-- valid go escape sequences --><input pattern=\"\\a\"> <input pattern=\"\\b\"> <input pattern=\"\\f\"> <input pattern=\"\\n\"> <input pattern=\"\\r\"> <input pattern=\"\\t\"> <input pattern=\"\\v\"> <input pattern=\"\\\\\"> <input pattern=\"\\777\"> <input pattern=\"\\xFF\"> <input pattern=\"\\u00FF\"> <input pattern=\"\\u00FF\\u00FF\\u00FF\"><!-- invalid go escape sequences --><input pattern=\"\\s\"></div>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return nil
})
}
var _ = templruntime.GeneratedTemplate
// Code generated by templ - DO NOT EDIT.
package testcontext
//lint:file-ignore SA4006 This context is only used if a nested component is present.
import "github.com/a-h/templ"
import templruntime "github.com/a-h/templ/runtime"
type contextKey string
var contextKeyName contextKey = "name"
func render() templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
return templ_7745c5c3_CtxErr
}
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var1 := templ.GetChildren(ctx)
if templ_7745c5c3_Var1 == nil {
templ_7745c5c3_Var1 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 1, "<ul><li>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var2 string
templ_7745c5c3_Var2, templ_7745c5c3_Err = templ.JoinStringErrs(ctx.Value(contextKeyName).(string))
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `generator/test-context/template.templ`, Line: 9, Col: 42}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var2))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 2, "</li>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
if ctx.Value(contextKeyName).(string) == "test" {
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 3, "<li>the if passed</li>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
}
if ctx.Value(contextKeyName).(string) != "test" {
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 4, "<li>the else if failed</li>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
} else if ctx.Value(contextKeyName).(string) == "test" {
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 5, "<li>the else if passed</li>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 6, "</ul>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return nil
})
}
var _ = templruntime.GeneratedTemplate
// Code generated by templ - DO NOT EDIT.
package testcssexpression
//lint:file-ignore SA4006 This context is only used if a nested component is present.
import "github.com/a-h/templ"
import templruntime "github.com/a-h/templ/runtime"
func className() templ.CSSClass {
templ_7745c5c3_CSSBuilder := templruntime.GetBuilder()
templ_7745c5c3_CSSBuilder.WriteString(`background-color:#ffffff;`)
templ_7745c5c3_CSSBuilder.WriteString(`max-height:calc(100vh - 170px);`)
templ_7745c5c3_CSSBuilder.WriteString(string(templ.SanitizeCSS(`color`, red)))
templ_7745c5c3_CSSID := templ.CSSID(`className`, templ_7745c5c3_CSSBuilder.String())
return templ.ComponentCSSClass{
ID: templ_7745c5c3_CSSID,
Class: templ.SafeCSS(`.` + templ_7745c5c3_CSSID + `{` + templ_7745c5c3_CSSBuilder.String() + `}`),
}
}
var _ = templruntime.GeneratedTemplate
// Code generated by templ - DO NOT EDIT.
package testcssmiddleware
//lint:file-ignore SA4006 This context is only used if a nested component is present.
import "github.com/a-h/templ"
import templruntime "github.com/a-h/templ/runtime"
func red() templ.CSSClass {
templ_7745c5c3_CSSBuilder := templruntime.GetBuilder()
templ_7745c5c3_CSSBuilder.WriteString(`color:red;`)
templ_7745c5c3_CSSID := templ.CSSID(`red`, templ_7745c5c3_CSSBuilder.String())
return templ.ComponentCSSClass{
ID: templ_7745c5c3_CSSID,
Class: templ.SafeCSS(`.` + templ_7745c5c3_CSSID + `{` + templ_7745c5c3_CSSBuilder.String() + `}`),
}
}
func render(s string) templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
return templ_7745c5c3_CtxErr
}
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var1 := templ.GetChildren(ctx)
if templ_7745c5c3_Var1 == nil {
templ_7745c5c3_Var1 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
var templ_7745c5c3_Var2 = []any{red}
templ_7745c5c3_Err = templ.RenderCSSItems(ctx, templ_7745c5c3_Buffer, templ_7745c5c3_Var2...)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 1, "<div class=\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var3 string
templ_7745c5c3_Var3, templ_7745c5c3_Err = templ.JoinStringErrs(templ.CSSClasses(templ_7745c5c3_Var2).String())
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `generator/test-css-middleware/template.templ`, Line: 1, Col: 0}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var3))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 2, "\">")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var4 string
templ_7745c5c3_Var4, templ_7745c5c3_Err = templ.JoinStringErrs(s)
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `generator/test-css-middleware/template.templ`, Line: 8, Col: 23}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var4))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 3, "</div>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return nil
})
}
var _ = templruntime.GeneratedTemplate
// Code generated by templ - DO NOT EDIT.
package testcssusage
//lint:file-ignore SA4006 This context is only used if a nested component is present.
import "github.com/a-h/templ"
import templruntime "github.com/a-h/templ/runtime"
import (
"fmt"
"math"
)
func StyleTagsAreSupported() templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
return templ_7745c5c3_CtxErr
}
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var1 := templ.GetChildren(ctx)
if templ_7745c5c3_Var1 == nil {
templ_7745c5c3_Var1 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 1, "<style>\n\t.test {\n\t\tcolor: #ff0000;\n\t}\n\t</style><div class=\"test\">Style tags are supported</div>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return nil
})
}
// CSS components.
const red = "#00ff00"
func cssComponentGreen() templ.CSSClass {
templ_7745c5c3_CSSBuilder := templruntime.GetBuilder()
templ_7745c5c3_CSSBuilder.WriteString(string(templ.SanitizeCSS(`color`, red)))
templ_7745c5c3_CSSID := templ.CSSID(`cssComponentGreen`, templ_7745c5c3_CSSBuilder.String())
return templ.ComponentCSSClass{
ID: templ_7745c5c3_CSSID,
Class: templ.SafeCSS(`.` + templ_7745c5c3_CSSID + `{` + templ_7745c5c3_CSSBuilder.String() + `}`),
}
}
func CSSComponentsAreSupported() templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
return templ_7745c5c3_CtxErr
}
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var2 := templ.GetChildren(ctx)
if templ_7745c5c3_Var2 == nil {
templ_7745c5c3_Var2 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
var templ_7745c5c3_Var3 = []any{cssComponentGreen()}
templ_7745c5c3_Err = templ.RenderCSSItems(ctx, templ_7745c5c3_Buffer, templ_7745c5c3_Var3...)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 2, "<div class=\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var4 string
templ_7745c5c3_Var4, templ_7745c5c3_Err = templ.JoinStringErrs(templ.CSSClasses(templ_7745c5c3_Var3).String())
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `generator/test-css-usage/template.templ`, Line: 1, Col: 0}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var4))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 3, "\">CSS components are supported</div>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return nil
})
}
// Both CSS components and constants are supported.
// Only string names are really required. There is no need to use templ.Class or templ.SafeClass.
func CSSComponentsAndConstantsAreSupported() templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
return templ_7745c5c3_CtxErr
}
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var5 := templ.GetChildren(ctx)
if templ_7745c5c3_Var5 == nil {
templ_7745c5c3_Var5 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
var templ_7745c5c3_Var6 = []any{cssComponentGreen(), "classA", templ.Class("&&&classB"), templ.SafeClass("classC"), "d e"}
templ_7745c5c3_Err = templ.RenderCSSItems(ctx, templ_7745c5c3_Buffer, templ_7745c5c3_Var6...)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 4, "<div class=\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var7 string
templ_7745c5c3_Var7, templ_7745c5c3_Err = templ.JoinStringErrs(templ.CSSClasses(templ_7745c5c3_Var6).String())
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `generator/test-css-usage/template.templ`, Line: 1, Col: 0}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var7))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 5, "\" type=\"button\">Both CSS components and constants are supported</div>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var8 = []any{templ.Classes(cssComponentGreen(), "classA", templ.Class("&&&classB"), templ.SafeClass("classC")), "d e"}
templ_7745c5c3_Err = templ.RenderCSSItems(ctx, templ_7745c5c3_Buffer, templ_7745c5c3_Var8...)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 6, "<div class=\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var9 string
templ_7745c5c3_Var9, templ_7745c5c3_Err = templ.JoinStringErrs(templ.CSSClasses(templ_7745c5c3_Var8).String())
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `generator/test-css-usage/template.templ`, Line: 1, Col: 0}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var9))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 7, "\" type=\"button\">Both CSS components and constants are supported</div>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return nil
})
}
// Maps can be used to determine if a class should be added or not.
func MapsCanBeUsedToConditionallySetClasses() templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
return templ_7745c5c3_CtxErr
}
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var10 := templ.GetChildren(ctx)
if templ_7745c5c3_Var10 == nil {
templ_7745c5c3_Var10 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
var templ_7745c5c3_Var11 = []any{map[string]bool{"a": true, "b": false, "c": true}}
templ_7745c5c3_Err = templ.RenderCSSItems(ctx, templ_7745c5c3_Buffer, templ_7745c5c3_Var11...)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 8, "<div class=\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var12 string
templ_7745c5c3_Var12, templ_7745c5c3_Err = templ.JoinStringErrs(templ.CSSClasses(templ_7745c5c3_Var11).String())
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `generator/test-css-usage/template.templ`, Line: 1, Col: 0}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var12))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 9, "\">Maps can be used to determine if a class should be added or not.</div>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return nil
})
}
// The templ.KV function can be used to add a class if a condition is true.
func d() templ.CSSClass {
templ_7745c5c3_CSSBuilder := templruntime.GetBuilder()
templ_7745c5c3_CSSBuilder.WriteString(`font-size:12pt;`)
templ_7745c5c3_CSSID := templ.CSSID(`d`, templ_7745c5c3_CSSBuilder.String())
return templ.ComponentCSSClass{
ID: templ_7745c5c3_CSSID,
Class: templ.SafeCSS(`.` + templ_7745c5c3_CSSID + `{` + templ_7745c5c3_CSSBuilder.String() + `}`),
}
}
func e() templ.CSSClass {
templ_7745c5c3_CSSBuilder := templruntime.GetBuilder()
templ_7745c5c3_CSSBuilder.WriteString(`font-size:14pt;`)
templ_7745c5c3_CSSID := templ.CSSID(`e`, templ_7745c5c3_CSSBuilder.String())
return templ.ComponentCSSClass{
ID: templ_7745c5c3_CSSID,
Class: templ.SafeCSS(`.` + templ_7745c5c3_CSSID + `{` + templ_7745c5c3_CSSBuilder.String() + `}`),
}
}
func KVCanBeUsedToConditionallySetClasses() templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
return templ_7745c5c3_CtxErr
}
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var13 := templ.GetChildren(ctx)
if templ_7745c5c3_Var13 == nil {
templ_7745c5c3_Var13 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
var templ_7745c5c3_Var14 = []any{"a", templ.KV("b", false), "c", templ.KV(d(), false), templ.KV(e(), true)}
templ_7745c5c3_Err = templ.RenderCSSItems(ctx, templ_7745c5c3_Buffer, templ_7745c5c3_Var14...)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 10, "<div class=\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var15 string
templ_7745c5c3_Var15, templ_7745c5c3_Err = templ.JoinStringErrs(templ.CSSClasses(templ_7745c5c3_Var14).String())
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `generator/test-css-usage/template.templ`, Line: 1, Col: 0}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var15))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 11, "\">KV can be used to conditionally set classes.</div>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return nil
})
}
// Pseudo attributes can be used without any special syntax.
func PseudoAttributesAndComplexClassNamesAreSupported() templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
return templ_7745c5c3_CtxErr
}
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var16 := templ.GetChildren(ctx)
if templ_7745c5c3_Var16 == nil {
templ_7745c5c3_Var16 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
var templ_7745c5c3_Var17 = []any{"bg-violet-500", "hover:bg-red-600", "hover:bg-sky-700", "text-[#50d71e]", "w-[calc(100%-4rem)"}
templ_7745c5c3_Err = templ.RenderCSSItems(ctx, templ_7745c5c3_Buffer, templ_7745c5c3_Var17...)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 12, "<div class=\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var18 string
templ_7745c5c3_Var18, templ_7745c5c3_Err = templ.JoinStringErrs(templ.CSSClasses(templ_7745c5c3_Var17).String())
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `generator/test-css-usage/template.templ`, Line: 1, Col: 0}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var18))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 13, "\">Pseudo attributes and complex class names are supported.</div>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return nil
})
}
// Class names are HTML escaped.
func ClassNamesAreHTMLEscaped() templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
return templ_7745c5c3_CtxErr
}
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var19 := templ.GetChildren(ctx)
if templ_7745c5c3_Var19 == nil {
templ_7745c5c3_Var19 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
var templ_7745c5c3_Var20 = []any{"a\" onClick=\"alert('hello')\""}
templ_7745c5c3_Err = templ.RenderCSSItems(ctx, templ_7745c5c3_Buffer, templ_7745c5c3_Var20...)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 14, "<div class=\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var21 string
templ_7745c5c3_Var21, templ_7745c5c3_Err = templ.JoinStringErrs(templ.CSSClasses(templ_7745c5c3_Var20).String())
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `generator/test-css-usage/template.templ`, Line: 1, Col: 0}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var21))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 15, "\">Class names are HTML escaped.</div>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return nil
})
}
// CSS components can be used with arguments.
func loading(percent int) templ.CSSClass {
templ_7745c5c3_CSSBuilder := templruntime.GetBuilder()
templ_7745c5c3_CSSBuilder.WriteString(string(templ.SanitizeCSS(`width`, fmt.Sprintf("%d%%", percent))))
templ_7745c5c3_CSSID := templ.CSSID(`loading`, templ_7745c5c3_CSSBuilder.String())
return templ.ComponentCSSClass{
ID: templ_7745c5c3_CSSID,
Class: templ.SafeCSS(`.` + templ_7745c5c3_CSSID + `{` + templ_7745c5c3_CSSBuilder.String() + `}`),
}
}
func CSSComponentsCanBeUsedWithArguments() templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
return templ_7745c5c3_CtxErr
}
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var22 := templ.GetChildren(ctx)
if templ_7745c5c3_Var22 == nil {
templ_7745c5c3_Var22 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
var templ_7745c5c3_Var23 = []any{loading(50)}
templ_7745c5c3_Err = templ.RenderCSSItems(ctx, templ_7745c5c3_Buffer, templ_7745c5c3_Var23...)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 16, "<div class=\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var24 string
templ_7745c5c3_Var24, templ_7745c5c3_Err = templ.JoinStringErrs(templ.CSSClasses(templ_7745c5c3_Var23).String())
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `generator/test-css-usage/template.templ`, Line: 1, Col: 0}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var24))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 17, "\">CSS components can be used with arguments.</div>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var25 = []any{loading(100)}
templ_7745c5c3_Err = templ.RenderCSSItems(ctx, templ_7745c5c3_Buffer, templ_7745c5c3_Var25...)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 18, "<div class=\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var26 string
templ_7745c5c3_Var26, templ_7745c5c3_Err = templ.JoinStringErrs(templ.CSSClasses(templ_7745c5c3_Var25).String())
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `generator/test-css-usage/template.templ`, Line: 1, Col: 0}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var26))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 19, "\">CSS components can be used with arguments.</div>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return nil
})
}
func windVaneRotation(degrees float64) templ.CSSClass {
templ_7745c5c3_CSSBuilder := templruntime.GetBuilder()
templ_7745c5c3_CSSBuilder.WriteString(string(templ.SanitizeCSS(`transform`, templ.SafeCSSProperty(fmt.Sprintf("rotate(%ddeg)", int(math.Round(degrees)))))))
templ_7745c5c3_CSSID := templ.CSSID(`windVaneRotation`, templ_7745c5c3_CSSBuilder.String())
return templ.ComponentCSSClass{
ID: templ_7745c5c3_CSSID,
Class: templ.SafeCSS(`.` + templ_7745c5c3_CSSID + `{` + templ_7745c5c3_CSSBuilder.String() + `}`),
}
}
func Rotate(degrees float64) templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
return templ_7745c5c3_CtxErr
}
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var27 := templ.GetChildren(ctx)
if templ_7745c5c3_Var27 == nil {
templ_7745c5c3_Var27 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
var templ_7745c5c3_Var28 = []any{windVaneRotation(degrees)}
templ_7745c5c3_Err = templ.RenderCSSItems(ctx, templ_7745c5c3_Buffer, templ_7745c5c3_Var28...)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 20, "<div class=\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var29 string
templ_7745c5c3_Var29, templ_7745c5c3_Err = templ.JoinStringErrs(templ.CSSClasses(templ_7745c5c3_Var28).String())
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `generator/test-css-usage/template.templ`, Line: 1, Col: 0}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var29))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 21, "\">Rotate</div>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return nil
})
}
// Combine all tests.
func TestComponent() templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
return templ_7745c5c3_CtxErr
}
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var30 := templ.GetChildren(ctx)
if templ_7745c5c3_Var30 == nil {
templ_7745c5c3_Var30 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
templ_7745c5c3_Err = StyleTagsAreSupported().Render(ctx, templ_7745c5c3_Buffer)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = CSSComponentsAreSupported().Render(ctx, templ_7745c5c3_Buffer)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = CSSComponentsAndConstantsAreSupported().Render(ctx, templ_7745c5c3_Buffer)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = MapsCanBeUsedToConditionallySetClasses().Render(ctx, templ_7745c5c3_Buffer)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = KVCanBeUsedToConditionallySetClasses().Render(ctx, templ_7745c5c3_Buffer)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = PseudoAttributesAndComplexClassNamesAreSupported().Render(ctx, templ_7745c5c3_Buffer)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = ClassNamesAreHTMLEscaped().Render(ctx, templ_7745c5c3_Buffer)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = CSSComponentsCanBeUsedWithArguments().Render(ctx, templ_7745c5c3_Buffer)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = Rotate(45).Render(ctx, templ_7745c5c3_Buffer)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return nil
})
}
var _ = templruntime.GeneratedTemplate
// Code generated by templ - DO NOT EDIT.
package testdoctype
//lint:file-ignore SA4006 This context is only used if a nested component is present.
import "github.com/a-h/templ"
import templruntime "github.com/a-h/templ/runtime"
func Layout(title, content string) templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
return templ_7745c5c3_CtxErr
}
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var1 := templ.GetChildren(ctx)
if templ_7745c5c3_Var1 == nil {
templ_7745c5c3_Var1 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 1, "<!doctype html><html lang=\"en\"><head><meta charset=\"UTF-8\"><meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\"><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"><title>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var2 string
templ_7745c5c3_Var2, templ_7745c5c3_Err = templ.JoinStringErrs(title)
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `generator/test-doctype/template.templ`, Line: 10, Col: 17}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var2))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 2, "</title></head><body>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var3 string
templ_7745c5c3_Var3, templ_7745c5c3_Err = templ.JoinStringErrs(content)
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `generator/test-doctype/template.templ`, Line: 12, Col: 17}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var3))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 3, "</body></html>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return nil
})
}
var _ = templruntime.GeneratedTemplate
// Code generated by templ - DO NOT EDIT.
package testelementattributes
//lint:file-ignore SA4006 This context is only used if a nested component is present.
import "github.com/a-h/templ"
import templruntime "github.com/a-h/templ/runtime"
func important() templ.CSSClass {
templ_7745c5c3_CSSBuilder := templruntime.GetBuilder()
templ_7745c5c3_CSSBuilder.WriteString(`width:100;`)
templ_7745c5c3_CSSID := templ.CSSID(`important`, templ_7745c5c3_CSSBuilder.String())
return templ.ComponentCSSClass{
ID: templ_7745c5c3_CSSID,
Class: templ.SafeCSS(`.` + templ_7745c5c3_CSSID + `{` + templ_7745c5c3_CSSBuilder.String() + `}`),
}
}
func unimportant() templ.CSSClass {
templ_7745c5c3_CSSBuilder := templruntime.GetBuilder()
templ_7745c5c3_CSSBuilder.WriteString(`width:50;`)
templ_7745c5c3_CSSID := templ.CSSID(`unimportant`, templ_7745c5c3_CSSBuilder.String())
return templ.ComponentCSSClass{
ID: templ_7745c5c3_CSSID,
Class: templ.SafeCSS(`.` + templ_7745c5c3_CSSID + `{` + templ_7745c5c3_CSSBuilder.String() + `}`),
}
}
func render(p person) templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
return templ_7745c5c3_CtxErr
}
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var1 := templ.GetChildren(ctx)
if templ_7745c5c3_Var1 == nil {
templ_7745c5c3_Var1 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
var templ_7745c5c3_Var2 = []any{important()}
templ_7745c5c3_Err = templ.RenderCSSItems(ctx, templ_7745c5c3_Buffer, templ_7745c5c3_Var2...)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 1, "<div style=\"width: 100;\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
if p.important {
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 2, " class=\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var3 string
templ_7745c5c3_Var3, templ_7745c5c3_Err = templ.JoinStringErrs(templ.CSSClasses(templ_7745c5c3_Var2).String())
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `generator/test-element-attributes/template.templ`, Line: 1, Col: 0}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var3))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 3, "\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 4, ">Important</div>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var4 = []any{unimportant}
templ_7745c5c3_Err = templ.RenderCSSItems(ctx, templ_7745c5c3_Buffer, templ_7745c5c3_Var4...)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 5, "<div style=\"width: 100;\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
if !p.important {
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 6, " class=\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var5 string
templ_7745c5c3_Var5, templ_7745c5c3_Err = templ.JoinStringErrs(templ.CSSClasses(templ_7745c5c3_Var4).String())
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `generator/test-element-attributes/template.templ`, Line: 1, Col: 0}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var5))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 7, "\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 8, ">Unimportant</div>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var6 = []any{important}
templ_7745c5c3_Err = templ.RenderCSSItems(ctx, templ_7745c5c3_Buffer, templ_7745c5c3_Var6...)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var7 = []any{unimportant}
templ_7745c5c3_Err = templ.RenderCSSItems(ctx, templ_7745c5c3_Buffer, templ_7745c5c3_Var7...)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 9, "<div style=\"width: 100;\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
if p.important {
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 10, " class=\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var8 string
templ_7745c5c3_Var8, templ_7745c5c3_Err = templ.JoinStringErrs(templ.CSSClasses(templ_7745c5c3_Var6).String())
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `generator/test-element-attributes/template.templ`, Line: 1, Col: 0}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var8))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 11, "\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
} else {
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 12, " class=\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var9 string
templ_7745c5c3_Var9, templ_7745c5c3_Err = templ.JoinStringErrs(templ.CSSClasses(templ_7745c5c3_Var7).String())
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `generator/test-element-attributes/template.templ`, Line: 1, Col: 0}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var9))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 13, "\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 14, ">Else</div><div data-script=\"on click\n do something\n end\"></div><h2>HTMX Wildcard attribute</h2><form hx-post=\"/api/secret/unlock\" hx-target=\"#secret\" hx-target-*=\"#errors\" hx-indicator=\"#loading-indicator\"><input type=\"button\" value=\"Unlock\"></form>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return nil
})
}
var _ = templruntime.GeneratedTemplate
package elseif
type data struct {
}
func (d data) IsTrue() bool {
return false
}
// Code generated by templ - DO NOT EDIT.
package elseif
//lint:file-ignore SA4006 This context is only used if a nested component is present.
import "github.com/a-h/templ"
import templruntime "github.com/a-h/templ/runtime"
func render(d data) templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
return templ_7745c5c3_CtxErr
}
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var1 := templ.GetChildren(ctx)
if templ_7745c5c3_Var1 == nil {
templ_7745c5c3_Var1 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 1, "<div>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
if d.IsTrue() {
var templ_7745c5c3_Var2 string
templ_7745c5c3_Var2, templ_7745c5c3_Err = templ.JoinStringErrs("True")
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `generator/test-elseif/template.templ`, Line: 6, Col: 11}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var2))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
} else if !d.IsTrue() {
var templ_7745c5c3_Var3 string
templ_7745c5c3_Var3, templ_7745c5c3_Err = templ.JoinStringErrs("False")
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `generator/test-elseif/template.templ`, Line: 8, Col: 12}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var3))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
} else {
var templ_7745c5c3_Var4 string
templ_7745c5c3_Var4, templ_7745c5c3_Err = templ.JoinStringErrs("Else")
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `generator/test-elseif/template.templ`, Line: 10, Col: 11}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var4))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 2, "</div><div>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
if 1 == 2 {
var templ_7745c5c3_Var5 string
templ_7745c5c3_Var5, templ_7745c5c3_Err = templ.JoinStringErrs("If")
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `generator/test-elseif/template.templ`, Line: 15, Col: 9}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var5))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
} else if 1 == 1 {
var templ_7745c5c3_Var6 string
templ_7745c5c3_Var6, templ_7745c5c3_Err = templ.JoinStringErrs("ElseIf")
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `generator/test-elseif/template.templ`, Line: 17, Col: 13}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var6))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 3, "</div><div>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
if 1 == 2 {
var templ_7745c5c3_Var7 string
templ_7745c5c3_Var7, templ_7745c5c3_Err = templ.JoinStringErrs("If")
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `generator/test-elseif/template.templ`, Line: 22, Col: 9}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var7))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
} else if 1 == 3 {
var templ_7745c5c3_Var8 string
templ_7745c5c3_Var8, templ_7745c5c3_Err = templ.JoinStringErrs("ElseIf")
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `generator/test-elseif/template.templ`, Line: 24, Col: 13}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var8))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
} else if 1 == 4 {
var templ_7745c5c3_Var9 string
templ_7745c5c3_Var9, templ_7745c5c3_Err = templ.JoinStringErrs("ElseIf")
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `generator/test-elseif/template.templ`, Line: 26, Col: 13}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var9))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
} else if 1 == 1 {
var templ_7745c5c3_Var10 string
templ_7745c5c3_Var10, templ_7745c5c3_Err = templ.JoinStringErrs("OK")
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `generator/test-elseif/template.templ`, Line: 28, Col: 9}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var10))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 4, "</div>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return nil
})
}
var _ = templruntime.GeneratedTemplate
// Code generated by templ - DO NOT EDIT.
package testfor
//lint:file-ignore SA4006 This context is only used if a nested component is present.
import "github.com/a-h/templ"
import templruntime "github.com/a-h/templ/runtime"
func render(items []string) templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
return templ_7745c5c3_CtxErr
}
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var1 := templ.GetChildren(ctx)
if templ_7745c5c3_Var1 == nil {
templ_7745c5c3_Var1 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
for _, item := range items {
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 1, "<div>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var2 string
templ_7745c5c3_Var2, templ_7745c5c3_Err = templ.JoinStringErrs(item)
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `generator/test-for/template.templ`, Line: 5, Col: 13}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var2))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 2, "</div>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
}
return nil
})
}
var _ = templruntime.GeneratedTemplate
// Code generated by templ - DO NOT EDIT.
package testahref
//lint:file-ignore SA4006 This context is only used if a nested component is present.
import "github.com/a-h/templ"
import templruntime "github.com/a-h/templ/runtime"
func render() templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
return templ_7745c5c3_CtxErr
}
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var1 := templ.GetChildren(ctx)
if templ_7745c5c3_Var1 == nil {
templ_7745c5c3_Var1 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 1, "<form action=\"javascript:alert('unaffected');\">Ignored</form><form action=\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var2 templ.SafeURL = templ.URL("javascript:alert('should be sanitized')")
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(string(templ_7745c5c3_Var2)))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 2, "\">Sanitized</form><form action=\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var3 templ.SafeURL = templ.SafeURL("javascript:alert('should not be sanitized')")
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(string(templ_7745c5c3_Var3)))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 3, "\">Unsanitized</form>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return nil
})
}
var _ = templruntime.GeneratedTemplate
// Code generated by templ - DO NOT EDIT.
package testcomment
//lint:file-ignore SA4006 This context is only used if a nested component is present.
import "github.com/a-h/templ"
import templruntime "github.com/a-h/templ/runtime"
func render(content string) templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
return templ_7745c5c3_CtxErr
}
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var1 := templ.GetChildren(ctx)
if templ_7745c5c3_Var1 == nil {
templ_7745c5c3_Var1 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 1, "<p>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var2 string
templ_7745c5c3_Var2, templ_7745c5c3_Err = templ.JoinStringErrs(content)
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `generator/test-go-comments/template.templ`, Line: 5, Col: 13}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var2))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 2, "</p>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return nil
})
}
var _ = templruntime.GeneratedTemplate
// Code generated by templ - DO NOT EDIT.
package testgotemplates
//lint:file-ignore SA4006 This context is only used if a nested component is present.
import "github.com/a-h/templ"
import templruntime "github.com/a-h/templ/runtime"
import "html/template"
var goTemplate = template.Must(template.New("example").Parse("<div>{{ . }}</div>"))
func Example() templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
return templ_7745c5c3_CtxErr
}
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var1 := templ.GetChildren(ctx)
if templ_7745c5c3_Var1 == nil {
templ_7745c5c3_Var1 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 1, "<!doctype html><html><body>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templ.FromGoHTML(goTemplate, "Hello, World!").Render(ctx, templ_7745c5c3_Buffer)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 2, "</body></html>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return nil
})
}
var _ = templruntime.GeneratedTemplate
// Code generated by templ - DO NOT EDIT.
package testcomment
//lint:file-ignore SA4006 This context is only used if a nested component is present.
import "github.com/a-h/templ"
import templruntime "github.com/a-h/templ/runtime"
func render(content string) templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
return templ_7745c5c3_CtxErr
}
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var1 := templ.GetChildren(ctx)
if templ_7745c5c3_Var1 == nil {
templ_7745c5c3_Var1 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 1, "<!-- simple html comment -->")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = paragraph(content).Render(ctx, templ_7745c5c3_Buffer)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 2, "<!--\n\t\tmultiline\n\t\tcomment\n\t-->")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = paragraph("second paragraph").Render(ctx, templ_7745c5c3_Buffer)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 3, "<!--\n\t\t@paragraph(\"commented out composed element\")\n\t-->")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = paragraph("third paragraph").Render(ctx, templ_7745c5c3_Buffer)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 4, "<!-- commented out string expression: { content } --><span>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var2 string
templ_7745c5c3_Var2, templ_7745c5c3_Err = templ.JoinStringErrs(content)
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `generator/test-html-comment/template.templ`, Line: 16, Col: 16}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var2))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 5, "</span><!-- <div>comment with html</div> -->")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return nil
})
}
func paragraph(content string) templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
return templ_7745c5c3_CtxErr
}
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var3 := templ.GetChildren(ctx)
if templ_7745c5c3_Var3 == nil {
templ_7745c5c3_Var3 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 6, "<p>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var4 string
templ_7745c5c3_Var4, templ_7745c5c3_Err = templ.JoinStringErrs(content)
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `generator/test-html-comment/template.templ`, Line: 21, Col: 13}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var4))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 7, "</p>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return nil
})
}
var _ = templruntime.GeneratedTemplate
// Code generated by templ - DO NOT EDIT.
package testhtml
//lint:file-ignore SA4006 This context is only used if a nested component is present.
import "github.com/a-h/templ"
import templruntime "github.com/a-h/templ/runtime"
func render(p person) templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
return templ_7745c5c3_CtxErr
}
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var1 := templ.GetChildren(ctx)
if templ_7745c5c3_Var1 == nil {
templ_7745c5c3_Var1 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 1, "<div><h1>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var2 string
templ_7745c5c3_Var2, templ_7745c5c3_Err = templ.JoinStringErrs(p.name)
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `generator/test-html/template.templ`, Line: 5, Col: 14}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var2))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 2, "</h1><div style=\"font-family: 'sans-serif'\" id=\"test\" data-contents=\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var3 string
templ_7745c5c3_Var3, templ_7745c5c3_Err = templ.JoinStringErrs(`something with "quotes" and a <tag>`)
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `generator/test-html/template.templ`, Line: 6, Col: 104}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var3))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 3, "\"><div>email:<a href=\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var4 templ.SafeURL = templ.URL("mailto: " + p.email)
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(string(templ_7745c5c3_Var4)))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 4, "\">")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var5 string
templ_7745c5c3_Var5, templ_7745c5c3_Err = templ.JoinStringErrs(p.email)
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `generator/test-html/template.templ`, Line: 7, Col: 67}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var5))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 5, "</a></div></div></div><hr")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
if true {
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 6, " noshade")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 7, "><hr optionA")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
if true {
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 8, " optionB")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 9, " optionC=\"other\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
if false {
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 10, " optionD")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 11, "><hr noshade><input name=\"test\">Text")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return nil
})
}
var _ = templruntime.GeneratedTemplate
package testif
type data struct {
}
func (d data) IsTrue() bool {
return true
}
// Code generated by templ - DO NOT EDIT.
package testif
//lint:file-ignore SA4006 This context is only used if a nested component is present.
import "github.com/a-h/templ"
import templruntime "github.com/a-h/templ/runtime"
func render(d data) templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
return templ_7745c5c3_CtxErr
}
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var1 := templ.GetChildren(ctx)
if templ_7745c5c3_Var1 == nil {
templ_7745c5c3_Var1 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
if d.IsTrue() {
var templ_7745c5c3_Var2 string
templ_7745c5c3_Var2, templ_7745c5c3_Err = templ.JoinStringErrs("True")
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `generator/test-if/template.templ`, Line: 5, Col: 10}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var2))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
} else {
var templ_7745c5c3_Var3 string
templ_7745c5c3_Var3, templ_7745c5c3_Err = templ.JoinStringErrs("False")
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `generator/test-if/template.templ`, Line: 7, Col: 11}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var3))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
}
return nil
})
}
var _ = templruntime.GeneratedTemplate
package ifelse
type data struct {
}
func (d data) IsTrue() bool {
return false
}
// Code generated by templ - DO NOT EDIT.
package ifelse
//lint:file-ignore SA4006 This context is only used if a nested component is present.
import "github.com/a-h/templ"
import templruntime "github.com/a-h/templ/runtime"
func render(d data) templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
return templ_7745c5c3_CtxErr
}
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var1 := templ.GetChildren(ctx)
if templ_7745c5c3_Var1 == nil {
templ_7745c5c3_Var1 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
if d.IsTrue() {
var templ_7745c5c3_Var2 string
templ_7745c5c3_Var2, templ_7745c5c3_Err = templ.JoinStringErrs("True")
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `generator/test-ifelse/template.templ`, Line: 5, Col: 10}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var2))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
} else {
var templ_7745c5c3_Var3 string
templ_7745c5c3_Var3, templ_7745c5c3_Err = templ.JoinStringErrs("False")
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `generator/test-ifelse/template.templ`, Line: 7, Col: 11}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var3))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
}
return nil
})
}
var _ = templruntime.GeneratedTemplate
// Code generated by templ - DO NOT EDIT.
package testimport
//lint:file-ignore SA4006 This context is only used if a nested component is present.
import "github.com/a-h/templ"
import templruntime "github.com/a-h/templ/runtime"
func listItem() templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
return templ_7745c5c3_CtxErr
}
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var1 := templ.GetChildren(ctx)
if templ_7745c5c3_Var1 == nil {
templ_7745c5c3_Var1 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 1, "<li>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templ_7745c5c3_Var1.Render(ctx, templ_7745c5c3_Buffer)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 2, "</li>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return nil
})
}
func list() templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
return templ_7745c5c3_CtxErr
}
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var2 := templ.GetChildren(ctx)
if templ_7745c5c3_Var2 == nil {
templ_7745c5c3_Var2 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 3, "<ul>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templ_7745c5c3_Var2.Render(ctx, templ_7745c5c3_Buffer)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 4, "</ul>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return nil
})
}
func main() templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
return templ_7745c5c3_CtxErr
}
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var3 := templ.GetChildren(ctx)
if templ_7745c5c3_Var3 == nil {
templ_7745c5c3_Var3 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
templ_7745c5c3_Var4 := templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var5 := templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 5, "<u>Item 1</u>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return nil
})
templ_7745c5c3_Err = listItem().Render(templ.WithChildren(ctx, templ_7745c5c3_Var5), templ_7745c5c3_Buffer)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 6, " ")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Var6 := templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 7, "<u>Item 2</u>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return nil
})
templ_7745c5c3_Err = listItem().Render(templ.WithChildren(ctx, templ_7745c5c3_Var6), templ_7745c5c3_Buffer)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 8, " ")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Var7 := templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 9, "<u>Item 3</u>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return nil
})
templ_7745c5c3_Err = listItem().Render(templ.WithChildren(ctx, templ_7745c5c3_Var7), templ_7745c5c3_Buffer)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return nil
})
templ_7745c5c3_Err = list().Render(templ.WithChildren(ctx, templ_7745c5c3_Var4), templ_7745c5c3_Buffer)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return nil
})
}
var _ = templruntime.GeneratedTemplate
// Code generated by templ - DO NOT EDIT.
package testjsunsafeusage
//lint:file-ignore SA4006 This context is only used if a nested component is present.
import "github.com/a-h/templ"
import templruntime "github.com/a-h/templ/runtime"
func TestComponent() templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
return templ_7745c5c3_CtxErr
}
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var1 := templ.GetChildren(ctx)
if templ_7745c5c3_Var1 == nil {
templ_7745c5c3_Var1 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
templ_7745c5c3_Err = templ.RenderScriptItems(ctx, templ_7745c5c3_Buffer, templ.JSUnsafeFuncCall("anythingILike('blah')"))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 1, "<button onClick=\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var2 templ.ComponentScript = templ.JSUnsafeFuncCall("anythingILike('blah')")
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ_7745c5c3_Var2.Call)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 2, "\">Click me</button>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templ.JSUnsafeFuncCall("// Arbitrary JS code").Render(ctx, templ_7745c5c3_Buffer)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return nil
})
}
var _ = templruntime.GeneratedTemplate
// Code generated by templ - DO NOT EDIT.
package testjsusage
//lint:file-ignore SA4006 This context is only used if a nested component is present.
import "github.com/a-h/templ"
import templruntime "github.com/a-h/templ/runtime"
import "time"
var onceHandle = templ.NewOnceHandle()
func TestComponent() templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
return templ_7745c5c3_CtxErr
}
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var1 := templ.GetChildren(ctx)
if templ_7745c5c3_Var1 == nil {
templ_7745c5c3_Var1 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
templ_7745c5c3_Err = templ.RenderScriptItems(ctx, templ_7745c5c3_Buffer, templ.JSFuncCall("alert", "Hello, World!"))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 1, "<button onClick=\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var2 templ.ComponentScript = templ.JSFuncCall("alert", "Hello, World!")
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ_7745c5c3_Var2.Call)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 2, "\">Click me</button>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Var3 := templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 3, "<script>\n\t\t\tfunction customAlert(msg, date) {\n\t\t\t\talert(msg + \" \" + date);\n\t\t\t}\n\t\t</script>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return nil
})
templ_7745c5c3_Err = onceHandle.Once().Render(templ.WithChildren(ctx, templ_7745c5c3_Var3), templ_7745c5c3_Buffer)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templ.RenderScriptItems(ctx, templ_7745c5c3_Buffer, templ.JSFuncCall("customAlert", "Hello, custom alert 1: ", time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC)))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 4, "<button onClick=\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var4 templ.ComponentScript = templ.JSFuncCall("customAlert", "Hello, custom alert 1: ", time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC))
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ_7745c5c3_Var4.Call)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 5, "\">Click me</button> ")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templ.RenderScriptItems(ctx, templ_7745c5c3_Buffer, templ.JSFuncCall("customAlert", "Hello, custom alert 2: ", time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC)))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 6, "<button onClick=\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var5 templ.ComponentScript = templ.JSFuncCall("customAlert", "Hello, custom alert 2: ", time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC))
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ_7745c5c3_Var5.Call)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 7, "\">Click me</button>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templ.JSFuncCall("customAlert", "Runs on page load", time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC)).Render(ctx, templ_7745c5c3_Buffer)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 8, "<script>\n\t\tfunction onClickEventHandler(event, data) {\n\t\t\talert(event.type);\n\t\t\talert(data)\n\t\t\tevent.preventDefault();\n\t\t}\n\t</script>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templ.RenderScriptItems(ctx, templ_7745c5c3_Buffer, templ.JSFuncCall("onClickEventHandler", templ.JSExpression("event"), "1234"))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 9, "<button onclick=\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var6 templ.ComponentScript = templ.JSFuncCall("onClickEventHandler", templ.JSExpression("event"), "1234")
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ_7745c5c3_Var6.Call)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 10, "\">Pass event handler</button>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return nil
})
}
var _ = templruntime.GeneratedTemplate
// Code generated by templ - DO NOT EDIT.
package testmethod
//lint:file-ignore SA4006 This context is only used if a nested component is present.
import "github.com/a-h/templ"
import templruntime "github.com/a-h/templ/runtime"
type Data struct {
message string
}
func (d Data) Method() templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
return templ_7745c5c3_CtxErr
}
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var1 := templ.GetChildren(ctx)
if templ_7745c5c3_Var1 == nil {
templ_7745c5c3_Var1 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 1, "<div>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var2 string
templ_7745c5c3_Var2, templ_7745c5c3_Err = templ.JoinStringErrs(d.message)
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `generator/test-method/template.templ`, Line: 8, Col: 17}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var2))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 2, "</div>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return nil
})
}
var _ = templruntime.GeneratedTemplate
// Code generated by templ - DO NOT EDIT.
package once
//lint:file-ignore SA4006 This context is only used if a nested component is present.
import "github.com/a-h/templ"
import templruntime "github.com/a-h/templ/runtime"
var helloHandle = templ.NewOnceHandle()
func hello(label, name string) templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
return templ_7745c5c3_CtxErr
}
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var1 := templ.GetChildren(ctx)
if templ_7745c5c3_Var1 == nil {
templ_7745c5c3_Var1 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
templ_7745c5c3_Var2 := templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 1, "<script>\n\t\t\tfunction hello(name) {\n\t\t\t\talert('Hello, ' + name + '!');\n\t\t\t}\n\t\t</script>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return nil
})
templ_7745c5c3_Err = helloHandle.Once().Render(templ.WithChildren(ctx, templ_7745c5c3_Var2), templ_7745c5c3_Buffer)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 2, "<input type=\"button\" value=\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var3 string
templ_7745c5c3_Var3, templ_7745c5c3_Err = templ.JoinStringErrs(label)
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `generator/test-once/template.templ`, Line: 13, Col: 35}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var3))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 3, "\" data-name=\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var4 string
templ_7745c5c3_Var4, templ_7745c5c3_Err = templ.JoinStringErrs(name)
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `generator/test-once/template.templ`, Line: 13, Col: 54}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var4))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 4, "\" onclick=\"hello(this.getAttribute('data-name'))\">")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return nil
})
}
func render() templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
return templ_7745c5c3_CtxErr
}
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var5 := templ.GetChildren(ctx)
if templ_7745c5c3_Var5 == nil {
templ_7745c5c3_Var5 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
templ_7745c5c3_Err = hello("Hello User", "user").Render(ctx, templ_7745c5c3_Buffer)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = hello("Hello World", "world").Render(ctx, templ_7745c5c3_Buffer)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return nil
})
}
var _ = templruntime.GeneratedTemplate
// Code generated by templ - DO NOT EDIT.
package onlyscripts
//lint:file-ignore SA4006 This context is only used if a nested component is present.
import "github.com/a-h/templ"
import templruntime "github.com/a-h/templ/runtime"
func withParameters(a string, b string, c int) templ.ComponentScript {
return templ.ComponentScript{
Name: `__templ_withParameters_1056`,
Function: `function __templ_withParameters_1056(a, b, c){console.log(a, b, c);
}`,
Call: templ.SafeScript(`__templ_withParameters_1056`, a, b, c),
CallInline: templ.SafeScriptInline(`__templ_withParameters_1056`, a, b, c),
}
}
var _ = templruntime.GeneratedTemplate
// Code generated by templ - DO NOT EDIT.
package testrawelements
//lint:file-ignore SA4006 This context is only used if a nested component is present.
import "github.com/a-h/templ"
import templruntime "github.com/a-h/templ/runtime"
func Example() templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
return templ_7745c5c3_CtxErr
}
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var1 := templ.GetChildren(ctx)
if templ_7745c5c3_Var1 == nil {
templ_7745c5c3_Var1 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 1, "<html><head></head><body><style><!-- Some stuff --></style><style>\n .customClass {\n border: 1px solid black;\n }\n </style><script>\n $(\"div\").marquee();\n function test() {\n window.open(\"https://example.com\")\n }\n </script><h1>Hello</h1>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templ.Raw("<div>World</div>").Render(ctx, templ_7745c5c3_Buffer)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 2, "</body></html>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return nil
})
}
var _ = templruntime.GeneratedTemplate
// Code generated by templ - DO NOT EDIT.
package testscriptinline
//lint:file-ignore SA4006 This context is only used if a nested component is present.
import "github.com/a-h/templ"
import templruntime "github.com/a-h/templ/runtime"
func withParameters(a string, b string, c int) templ.ComponentScript {
return templ.ComponentScript{
Name: `__templ_withParameters_1056`,
Function: `function __templ_withParameters_1056(a, b, c){console.log(a, b, c);
}`,
Call: templ.SafeScript(`__templ_withParameters_1056`, a, b, c),
CallInline: templ.SafeScriptInline(`__templ_withParameters_1056`, a, b, c),
}
}
func withoutParameters() templ.ComponentScript {
return templ.ComponentScript{
Name: `__templ_withoutParameters_6bbf`,
Function: `function __templ_withoutParameters_6bbf(){alert("hello");
}`,
Call: templ.SafeScript(`__templ_withoutParameters_6bbf`),
CallInline: templ.SafeScriptInline(`__templ_withoutParameters_6bbf`),
}
}
func InlineJavascript(a string) templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
return templ_7745c5c3_CtxErr
}
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var1 := templ.GetChildren(ctx)
if templ_7745c5c3_Var1 == nil {
templ_7745c5c3_Var1 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
templ_7745c5c3_Err = withoutParameters().Render(ctx, templ_7745c5c3_Buffer)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = withParameters(a, "test", 123).Render(ctx, templ_7745c5c3_Buffer)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = withoutParameters().Render(ctx, templ_7745c5c3_Buffer)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = withParameters(a, "test", 123).Render(ctx, templ_7745c5c3_Buffer)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return nil
})
}
var _ = templruntime.GeneratedTemplate
// Code generated by templ - DO NOT EDIT.
package testscriptusage
//lint:file-ignore SA4006 This context is only used if a nested component is present.
import "github.com/a-h/templ"
import templruntime "github.com/a-h/templ/runtime"
func withParameters(a string, b string, c int) templ.ComponentScript {
return templ.ComponentScript{
Name: `__templ_withParameters_1056`,
Function: `function __templ_withParameters_1056(a, b, c){console.log(a, b, c);
}`,
Call: templ.SafeScript(`__templ_withParameters_1056`, a, b, c),
CallInline: templ.SafeScriptInline(`__templ_withParameters_1056`, a, b, c),
}
}
func withoutParameters() templ.ComponentScript {
return templ.ComponentScript{
Name: `__templ_withoutParameters_6bbf`,
Function: `function __templ_withoutParameters_6bbf(){alert("hello");
}`,
Call: templ.SafeScript(`__templ_withoutParameters_6bbf`),
CallInline: templ.SafeScriptInline(`__templ_withoutParameters_6bbf`),
}
}
func onClick() templ.ComponentScript {
return templ.ComponentScript{
Name: `__templ_onClick_657d`,
Function: `function __templ_onClick_657d(){alert("clicked");
}`,
Call: templ.SafeScript(`__templ_onClick_657d`),
CallInline: templ.SafeScriptInline(`__templ_onClick_657d`),
}
}
func Button(text string) templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
return templ_7745c5c3_CtxErr
}
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var1 := templ.GetChildren(ctx)
if templ_7745c5c3_Var1 == nil {
templ_7745c5c3_Var1 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
templ_7745c5c3_Err = templ.RenderScriptItems(ctx, templ_7745c5c3_Buffer, withParameters("test", text, 123), withoutParameters())
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 1, "<button onClick=\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var2 templ.ComponentScript = withParameters("test", text, 123)
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ_7745c5c3_Var2.Call)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 2, "\" onMouseover=\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var3 templ.ComponentScript = withoutParameters()
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ_7745c5c3_Var3.Call)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 3, "\" type=\"button\">")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var4 string
templ_7745c5c3_Var4, templ_7745c5c3_Err = templ.JoinStringErrs(text)
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `generator/test-script-usage-nonce/template.templ`, Line: 16, Col: 111}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var4))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 4, "</button>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return nil
})
}
func withComment() templ.ComponentScript {
return templ.ComponentScript{
Name: `__templ_withComment_9cf8`,
Function: `function __templ_withComment_9cf8(){//'
}`,
Call: templ.SafeScript(`__templ_withComment_9cf8`),
CallInline: templ.SafeScriptInline(`__templ_withComment_9cf8`),
}
}
func ThreeButtons() templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
return templ_7745c5c3_CtxErr
}
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var5 := templ.GetChildren(ctx)
if templ_7745c5c3_Var5 == nil {
templ_7745c5c3_Var5 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
templ_7745c5c3_Err = Button("A").Render(ctx, templ_7745c5c3_Buffer)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = Button("B").Render(ctx, templ_7745c5c3_Buffer)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 5, "<button onMouseover=\"console.log('mouseover')\" type=\"button\">Button C</button> <button hx-on::click=\"alert('clicked inline')\" type=\"button\">Button D</button> ")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templ.RenderScriptItems(ctx, templ_7745c5c3_Buffer, onClick())
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 6, "<button hx-on::click=\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var6 templ.ComponentScript = onClick()
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ_7745c5c3_Var6.Call)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 7, "\" type=\"button\">Button E</button>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = Conditional(true).Render(ctx, templ_7745c5c3_Buffer)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return nil
})
}
func conditionalScript() templ.ComponentScript {
return templ.ComponentScript{
Name: `__templ_conditionalScript_de41`,
Function: `function __templ_conditionalScript_de41(){alert("conditional");
}`,
Call: templ.SafeScript(`__templ_conditionalScript_de41`),
CallInline: templ.SafeScriptInline(`__templ_conditionalScript_de41`),
}
}
func Conditional(show bool) templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
return templ_7745c5c3_CtxErr
}
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var7 := templ.GetChildren(ctx)
if templ_7745c5c3_Var7 == nil {
templ_7745c5c3_Var7 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
templ_7745c5c3_Err = templ.RenderScriptItems(ctx, templ_7745c5c3_Buffer, conditionalScript())
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 8, "<input type=\"button\" value=\"Click me\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
if show {
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 9, " onclick=\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var8 templ.ComponentScript = conditionalScript()
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ_7745c5c3_Var8.Call)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 10, "\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 11, ">")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return nil
})
}
var _ = templruntime.GeneratedTemplate
// Code generated by templ - DO NOT EDIT.
package testscriptusage
//lint:file-ignore SA4006 This context is only used if a nested component is present.
import "github.com/a-h/templ"
import templruntime "github.com/a-h/templ/runtime"
func withParameters(a string, b string, c int) templ.ComponentScript {
return templ.ComponentScript{
Name: `__templ_withParameters_1056`,
Function: `function __templ_withParameters_1056(a, b, c){console.log(a, b, c);
}`,
Call: templ.SafeScript(`__templ_withParameters_1056`, a, b, c),
CallInline: templ.SafeScriptInline(`__templ_withParameters_1056`, a, b, c),
}
}
func withoutParameters() templ.ComponentScript {
return templ.ComponentScript{
Name: `__templ_withoutParameters_6bbf`,
Function: `function __templ_withoutParameters_6bbf(){alert("hello");
}`,
Call: templ.SafeScript(`__templ_withoutParameters_6bbf`),
CallInline: templ.SafeScriptInline(`__templ_withoutParameters_6bbf`),
}
}
func onClick() templ.ComponentScript {
return templ.ComponentScript{
Name: `__templ_onClick_657d`,
Function: `function __templ_onClick_657d(){alert("clicked");
}`,
Call: templ.SafeScript(`__templ_onClick_657d`),
CallInline: templ.SafeScriptInline(`__templ_onClick_657d`),
}
}
func Button(text string) templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
return templ_7745c5c3_CtxErr
}
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var1 := templ.GetChildren(ctx)
if templ_7745c5c3_Var1 == nil {
templ_7745c5c3_Var1 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
templ_7745c5c3_Err = templ.RenderScriptItems(ctx, templ_7745c5c3_Buffer, withParameters("test", text, 123), withoutParameters())
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 1, "<button onClick=\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var2 templ.ComponentScript = withParameters("test", text, 123)
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ_7745c5c3_Var2.Call)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 2, "\" onMouseover=\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var3 templ.ComponentScript = withoutParameters()
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ_7745c5c3_Var3.Call)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 3, "\" type=\"button\">")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var4 string
templ_7745c5c3_Var4, templ_7745c5c3_Err = templ.JoinStringErrs(text)
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `generator/test-script-usage/template.templ`, Line: 16, Col: 111}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var4))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 4, "</button>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return nil
})
}
func withComment() templ.ComponentScript {
return templ.ComponentScript{
Name: `__templ_withComment_9cf8`,
Function: `function __templ_withComment_9cf8(){//'
}`,
Call: templ.SafeScript(`__templ_withComment_9cf8`),
CallInline: templ.SafeScriptInline(`__templ_withComment_9cf8`),
}
}
func whenButtonIsClicked(event templ.JSExpression) templ.ComponentScript {
return templ.ComponentScript{
Name: `__templ_whenButtonIsClicked_253e`,
Function: `function __templ_whenButtonIsClicked_253e(event){console.log(event.target)
}`,
Call: templ.SafeScript(`__templ_whenButtonIsClicked_253e`, event),
CallInline: templ.SafeScriptInline(`__templ_whenButtonIsClicked_253e`, event),
}
}
func ThreeButtons() templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
return templ_7745c5c3_CtxErr
}
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var5 := templ.GetChildren(ctx)
if templ_7745c5c3_Var5 == nil {
templ_7745c5c3_Var5 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
templ_7745c5c3_Err = Button("A").Render(ctx, templ_7745c5c3_Buffer)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = Button("B").Render(ctx, templ_7745c5c3_Buffer)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 5, "<button onMouseover=\"console.log('mouseover')\" type=\"button\">Button C</button> <button hx-on::click=\"alert('clicked inline')\" type=\"button\">Button D</button> ")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templ.RenderScriptItems(ctx, templ_7745c5c3_Buffer, onClick())
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 6, "<button hx-on::click=\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var6 templ.ComponentScript = onClick()
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ_7745c5c3_Var6.Call)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 7, "\" type=\"button\">Button E</button> ")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templ.RenderScriptItems(ctx, templ_7745c5c3_Buffer, whenButtonIsClicked(templ.JSExpression("event")))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 8, "<button onclick=\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var7 templ.ComponentScript = whenButtonIsClicked(templ.JSExpression("event"))
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ_7745c5c3_Var7.Call)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 9, "\">Button F</button>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = Conditional(true).Render(ctx, templ_7745c5c3_Buffer)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = ScriptOnLoad().Render(ctx, templ_7745c5c3_Buffer)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return nil
})
}
func conditionalScript() templ.ComponentScript {
return templ.ComponentScript{
Name: `__templ_conditionalScript_de41`,
Function: `function __templ_conditionalScript_de41(){alert("conditional");
}`,
Call: templ.SafeScript(`__templ_conditionalScript_de41`),
CallInline: templ.SafeScriptInline(`__templ_conditionalScript_de41`),
}
}
func Conditional(show bool) templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
return templ_7745c5c3_CtxErr
}
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var8 := templ.GetChildren(ctx)
if templ_7745c5c3_Var8 == nil {
templ_7745c5c3_Var8 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
templ_7745c5c3_Err = templ.RenderScriptItems(ctx, templ_7745c5c3_Buffer, conditionalScript())
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 10, "<input type=\"button\" value=\"Click me\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
if show {
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 11, " onclick=\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var9 templ.ComponentScript = conditionalScript()
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ_7745c5c3_Var9.Call)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 12, "\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 13, ">")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return nil
})
}
func alertTest() templ.ComponentScript {
return templ.ComponentScript{
Name: `__templ_alertTest_eadf`,
Function: `function __templ_alertTest_eadf(){alert('testing');
}`,
Call: templ.SafeScript(`__templ_alertTest_eadf`),
CallInline: templ.SafeScriptInline(`__templ_alertTest_eadf`),
}
}
func ScriptOnLoad() templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
return templ_7745c5c3_CtxErr
}
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var10 := templ.GetChildren(ctx)
if templ_7745c5c3_Var10 == nil {
templ_7745c5c3_Var10 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
templ_7745c5c3_Err = templ.RenderScriptItems(ctx, templ_7745c5c3_Buffer, alertTest())
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 14, "<script async crossorigin=\"true\" onload=\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var11 templ.ComponentScript = alertTest()
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ_7745c5c3_Var11.Call)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 15, "\" src=\"url.to.some.script\"></script>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return nil
})
}
var _ = templruntime.GeneratedTemplate
// Code generated by templ - DO NOT EDIT.
package testspreadattributes
//lint:file-ignore SA4006 This context is only used if a nested component is present.
import "github.com/a-h/templ"
import templruntime "github.com/a-h/templ/runtime"
func BasicTemplate(spread templ.Attributes) templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
return templ_7745c5c3_CtxErr
}
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var1 := templ.GetChildren(ctx)
if templ_7745c5c3_Var1 == nil {
templ_7745c5c3_Var1 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 1, "<div><a")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templ.RenderAttributes(ctx, templ_7745c5c3_Buffer, spread)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 2, ">text</a><div")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
if true {
templ_7745c5c3_Err = templ.RenderAttributes(ctx, templ_7745c5c3_Buffer, spread)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 3, ">text2</div><div")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
if false {
templ_7745c5c3_Err = templ.RenderAttributes(ctx, templ_7745c5c3_Buffer, spread)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 4, ">text3</div></div>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return nil
})
}
var _ = templruntime.GeneratedTemplate
// Code generated by templ - DO NOT EDIT.
package teststringerrs
//lint:file-ignore SA4006 This context is only used if a nested component is present.
import "github.com/a-h/templ"
import templruntime "github.com/a-h/templ/runtime"
func funcWithNoError() (s string) {
return "OK"
}
func funcWithError(in error) (s string, err error) {
if in != nil {
return "", in
}
return "OK2", nil
}
func TestComponent(err error) templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
return templ_7745c5c3_CtxErr
}
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var1 := templ.GetChildren(ctx)
if templ_7745c5c3_Var1 == nil {
templ_7745c5c3_Var1 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 1, "<ul><li>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var2 string
templ_7745c5c3_Var2, templ_7745c5c3_Err = templ.JoinStringErrs("raw")
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `generator/test-string-errors/template.templ`, Line: 16, Col: 13}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var2))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 2, "</li><li>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var3 string
templ_7745c5c3_Var3, templ_7745c5c3_Err = templ.JoinStringErrs(funcWithNoError())
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `generator/test-string-errors/template.templ`, Line: 17, Col: 25}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var3))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 3, "</li><li>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var4 string
templ_7745c5c3_Var4, templ_7745c5c3_Err = templ.JoinStringErrs(funcWithError(err))
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `generator/test-string-errors/template.templ`, Line: 18, Col: 26}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var4))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 4, "</li></ul>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return nil
})
}
var _ = templruntime.GeneratedTemplate
// Code generated by templ - DO NOT EDIT.
package teststring
//lint:file-ignore SA4006 This context is only used if a nested component is present.
import "github.com/a-h/templ"
import templruntime "github.com/a-h/templ/runtime"
func render(s string) templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
return templ_7745c5c3_CtxErr
}
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var1 := templ.GetChildren(ctx)
if templ_7745c5c3_Var1 == nil {
templ_7745c5c3_Var1 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 1, "<ul><li></li><li>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var2 string
templ_7745c5c3_Var2, templ_7745c5c3_Err = templ.JoinStringErrs(s)
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `generator/test-string/template.templ`, Line: 6, Col: 9}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var2))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 2, "</li><li>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var3 string
templ_7745c5c3_Var3, templ_7745c5c3_Err = templ.JoinStringErrs("Spaces")
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `generator/test-string/template.templ`, Line: 7, Col: 16}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var3))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 3, " ")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var4 string
templ_7745c5c3_Var4, templ_7745c5c3_Err = templ.JoinStringErrs("are")
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `generator/test-string/template.templ`, Line: 7, Col: 26}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var4))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 4, " ")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var5 string
templ_7745c5c3_Var5, templ_7745c5c3_Err = templ.JoinStringErrs("preserved.")
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `generator/test-string/template.templ`, Line: 7, Col: 43}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var5))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 5, "</li></ul>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return nil
})
}
var _ = templruntime.GeneratedTemplate
// Code generated by templ - DO NOT EDIT.
package teststyleattribute
//lint:file-ignore SA4006 This context is only used if a nested component is present.
import "github.com/a-h/templ"
import templruntime "github.com/a-h/templ/runtime"
func Button[T any](style T, text string) templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
return templ_7745c5c3_CtxErr
}
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var1 := templ.GetChildren(ctx)
if templ_7745c5c3_Var1 == nil {
templ_7745c5c3_Var1 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 1, "<button style=\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var2 string
templ_7745c5c3_Var2, templ_7745c5c3_Err = templruntime.SanitizeStyleAttributeValues(style)
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `generator/test-style-attribute/template.templ`, Line: 4, Col: 22}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var2))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 2, "\">")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var3 string
templ_7745c5c3_Var3, templ_7745c5c3_Err = templ.JoinStringErrs(text)
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `generator/test-style-attribute/template.templ`, Line: 4, Col: 31}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var3))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 3, "</button> <button style=\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var4 string
templ_7745c5c3_Var4, templ_7745c5c3_Err = templruntime.SanitizeStyleAttributeValues(getFunctionResult())
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `generator/test-style-attribute/template.templ`, Line: 5, Col: 36}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var4))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 4, "\">")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var5 string
templ_7745c5c3_Var5, templ_7745c5c3_Err = templ.JoinStringErrs(text)
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `generator/test-style-attribute/template.templ`, Line: 5, Col: 45}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var5))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 5, "</button>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return nil
})
}
func getFunctionResult() (string, error) {
return "background-color: red", nil
}
var _ = templruntime.GeneratedTemplate
// Code generated by templ - DO NOT EDIT.
package testswitch
//lint:file-ignore SA4006 This context is only used if a nested component is present.
import "github.com/a-h/templ"
import templruntime "github.com/a-h/templ/runtime"
func render(input string) templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
return templ_7745c5c3_CtxErr
}
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var1 := templ.GetChildren(ctx)
if templ_7745c5c3_Var1 == nil {
templ_7745c5c3_Var1 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
switch input {
case "a":
var templ_7745c5c3_Var2 string
templ_7745c5c3_Var2, templ_7745c5c3_Err = templ.JoinStringErrs("it was 'a'")
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `generator/test-switch/template.templ`, Line: 6, Col: 17}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var2))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
default:
var templ_7745c5c3_Var3 string
templ_7745c5c3_Var3, templ_7745c5c3_Err = templ.JoinStringErrs("it was something else")
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `generator/test-switch/template.templ`, Line: 8, Col: 28}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var3))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
}
return nil
})
}
var _ = templruntime.GeneratedTemplate
// Code generated by templ - DO NOT EDIT.
package testswitchdefault
//lint:file-ignore SA4006 This context is only used if a nested component is present.
import "github.com/a-h/templ"
import templruntime "github.com/a-h/templ/runtime"
func template(input string) templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
return templ_7745c5c3_CtxErr
}
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var1 := templ.GetChildren(ctx)
if templ_7745c5c3_Var1 == nil {
templ_7745c5c3_Var1 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
switch input {
case "a":
var templ_7745c5c3_Var2 string
templ_7745c5c3_Var2, templ_7745c5c3_Err = templ.JoinStringErrs("it was 'a'")
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `generator/test-switchdefault/template.templ`, Line: 6, Col: 17}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var2))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
default:
var templ_7745c5c3_Var3 string
templ_7745c5c3_Var3, templ_7745c5c3_Err = templ.JoinStringErrs("it was something else")
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `generator/test-switchdefault/template.templ`, Line: 8, Col: 28}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var3))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
}
return nil
})
}
var _ = templruntime.GeneratedTemplate
// Code generated by templ - DO NOT EDIT.
package testtemplelement
//lint:file-ignore SA4006 This context is only used if a nested component is present.
import "github.com/a-h/templ"
import templruntime "github.com/a-h/templ/runtime"
import "fmt"
func wrapper(index int) templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
return templ_7745c5c3_CtxErr
}
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var1 := templ.GetChildren(ctx)
if templ_7745c5c3_Var1 == nil {
templ_7745c5c3_Var1 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 1, "<div id=\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var2 string
templ_7745c5c3_Var2, templ_7745c5c3_Err = templ.JoinStringErrs(fmt.Sprint(index))
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `generator/test-templ-element/template.templ`, Line: 6, Col: 28}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var2))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 2, "\">")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templ_7745c5c3_Var1.Render(ctx, templ_7745c5c3_Buffer)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 3, "</div>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return nil
})
}
func template() templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
return templ_7745c5c3_CtxErr
}
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var3 := templ.GetChildren(ctx)
if templ_7745c5c3_Var3 == nil {
templ_7745c5c3_Var3 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
templ_7745c5c3_Var4 := templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 4, "child1")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Var5 := templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 5, "child2")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Var6 := templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 6, "child3")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = wrapper(4).Render(ctx, templ_7745c5c3_Buffer)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return nil
})
templ_7745c5c3_Err = wrapper(3).Render(templ.WithChildren(ctx, templ_7745c5c3_Var6), templ_7745c5c3_Buffer)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return nil
})
templ_7745c5c3_Err = wrapper(2).Render(templ.WithChildren(ctx, templ_7745c5c3_Var5), templ_7745c5c3_Buffer)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return nil
})
templ_7745c5c3_Err = wrapper(1).Render(templ.WithChildren(ctx, templ_7745c5c3_Var4), templ_7745c5c3_Buffer)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return nil
})
}
var _ = templruntime.GeneratedTemplate
// Code generated by templ - DO NOT EDIT.
package testgotemplates
//lint:file-ignore SA4006 This context is only used if a nested component is present.
import "github.com/a-h/templ"
import templruntime "github.com/a-h/templ/runtime"
import "html/template"
var example = template.Must(template.New("example").Parse(`<!DOCTYPE html>
<html>
<body>
{{ . }}
</body>
</html>
`))
func greeting() templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
return templ_7745c5c3_CtxErr
}
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var1 := templ.GetChildren(ctx)
if templ_7745c5c3_Var1 == nil {
templ_7745c5c3_Var1 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 1, "<div>Hello, World!</div>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return nil
})
}
var _ = templruntime.GeneratedTemplate
// Code generated by templ - DO NOT EDIT.
package testtextwhitespace
//lint:file-ignore SA4006 This context is only used if a nested component is present.
import "github.com/a-h/templ"
import templruntime "github.com/a-h/templ/runtime"
func WhitespaceIsAddedWithinTemplStatements() templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
return templ_7745c5c3_CtxErr
}
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var1 := templ.GetChildren(ctx)
if templ_7745c5c3_Var1 == nil {
templ_7745c5c3_Var1 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 1, "<p>This is some text. ")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
if true {
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 2, "So is this.")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 3, "</p>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return nil
})
}
const WhitespaceIsAddedWithinTemplStatementsExpected = `<p>This is some text. So is this.</p>`
func InlineElementsAreNotPadded() templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
return templ_7745c5c3_CtxErr
}
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var2 := templ.GetChildren(ctx)
if templ_7745c5c3_Var2 == nil {
templ_7745c5c3_Var2 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 4, "<p>Inline text <b>is spaced properly</b> without adding extra spaces.</p>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return nil
})
}
const InlineElementsAreNotPaddedExpected = `<p>Inline text <b>is spaced properly</b> without adding extra spaces.</p>`
func WhiteSpaceInHTMLIsNormalised() templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
return templ_7745c5c3_CtxErr
}
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var3 := templ.GetChildren(ctx)
if templ_7745c5c3_Var3 == nil {
templ_7745c5c3_Var3 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 5, "<p>newlines and other whitespace are stripped but it is normalised like HTML.</p>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return nil
})
}
const WhiteSpaceInHTMLIsNormalisedExpected = `<p>newlines and other whitespace are stripped but it is normalised like HTML.</p>`
func WhiteSpaceAroundValues() templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
return templ_7745c5c3_CtxErr
}
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var4 := templ.GetChildren(ctx)
if templ_7745c5c3_Var4 == nil {
templ_7745c5c3_Var4 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 6, "<p>templ allows ")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var5 string
templ_7745c5c3_Var5, templ_7745c5c3_Err = templ.JoinStringErrs("strings")
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `generator/test-text-whitespace/template.templ`, Line: 31, Col: 28}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var5))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 7, " to be included in sentences.</p>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return nil
})
}
const WhiteSpaceAroundValuesExpected = `<p>templ allows strings to be included in sentences.</p>`
const WhiteSpaceAroundTemplatedValuesExpected = `<div>templ allows whitespace around templated values.</div>`
func WhiteSpaceAroundTemplatedValues(prefix, statement string) templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
return templ_7745c5c3_CtxErr
}
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var6 := templ.GetChildren(ctx)
if templ_7745c5c3_Var6 == nil {
templ_7745c5c3_Var6 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 8, "<div>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var7 string
templ_7745c5c3_Var7, templ_7745c5c3_Err = templ.JoinStringErrs(prefix)
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `generator/test-text-whitespace/template.templ`, Line: 39, Col: 14}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var7))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 9, " ")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var8 string
templ_7745c5c3_Var8, templ_7745c5c3_Err = templ.JoinStringErrs(statement)
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `generator/test-text-whitespace/template.templ`, Line: 39, Col: 28}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var8))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 10, "</div>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return nil
})
}
var _ = templruntime.GeneratedTemplate
// Code generated by templ - DO NOT EDIT.
package testtext
//lint:file-ignore SA4006 This context is only used if a nested component is present.
import "github.com/a-h/templ"
import templruntime "github.com/a-h/templ/runtime"
func BasicTemplate(name string) templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
return templ_7745c5c3_CtxErr
}
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var1 := templ.GetChildren(ctx)
if templ_7745c5c3_Var1 == nil {
templ_7745c5c3_Var1 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 1, "<div>Name: ")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var2 string
templ_7745c5c3_Var2, templ_7745c5c3_Err = templ.JoinStringErrs(name)
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `generator/test-text/template.templ`, Line: 4, Col: 18}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var2))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 2, "</div><div>Text `with backticks`</div><div>Text `with backtick</div><div>Text `with backtick alongside variable: ")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var3 string
templ_7745c5c3_Var3, templ_7745c5c3_Err = templ.JoinStringErrs(name)
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `generator/test-text/template.templ`, Line: 7, Col: 52}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var3))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 3, "</div>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return nil
})
}
var _ = templruntime.GeneratedTemplate
// Code generated by templ - DO NOT EDIT.
package testvoid
//lint:file-ignore SA4006 This context is only used if a nested component is present.
import "github.com/a-h/templ"
import templruntime "github.com/a-h/templ/runtime"
func render() templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
return templ_7745c5c3_CtxErr
}
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var1 := templ.GetChildren(ctx)
if templ_7745c5c3_Var1 == nil {
templ_7745c5c3_Var1 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 1, "<br><img src=\"https://example.com/image.png\"><br><br>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return nil
})
}
var _ = templruntime.GeneratedTemplate
// Code generated by templ - DO NOT EDIT.
package testwhitespacearoundgokeywords
//lint:file-ignore SA4006 This context is only used if a nested component is present.
import "github.com/a-h/templ"
import templruntime "github.com/a-h/templ/runtime"
import "fmt"
func WhitespaceIsConsistentInIf(firstIf, secondIf bool) templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
return templ_7745c5c3_CtxErr
}
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var1 := templ.GetChildren(ctx)
if templ_7745c5c3_Var1 == nil {
templ_7745c5c3_Var1 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 1, "<button>Start</button> ")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
if firstIf {
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 2, "<button>If</button> ")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
} else if secondIf {
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 3, "<button>ElseIf</button> ")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
} else {
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 4, "<button>Else</button> ")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 5, "<button>End</button>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return nil
})
}
const WhitespaceIsConsistentInTrueIfExpected = `<button>Start</button> <button>If</button> <button>End</button>`
const WhitespaceIsConsistentInTrueElseIfExpected = `<button>Start</button> <button>ElseIf</button> <button>End</button>`
const WhitespaceIsConsistentInTrueElseExpected = `<button>Start</button> <button>Else</button> <button>End</button>`
func WhitespaceIsConsistentInFalseIf() templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
return templ_7745c5c3_CtxErr
}
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var2 := templ.GetChildren(ctx)
if templ_7745c5c3_Var2 == nil {
templ_7745c5c3_Var2 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 6, "<button>Start</button> ")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
if false {
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 7, "<button>Will Not Render</button> ")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 8, "<button>End</button>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return nil
})
}
const WhitespaceIsConsistentInFalseIfExpected = `<button>Start</button> <button>End</button>`
func WhitespaceIsConsistentInSwitch(i int) templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
return templ_7745c5c3_CtxErr
}
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var3 := templ.GetChildren(ctx)
if templ_7745c5c3_Var3 == nil {
templ_7745c5c3_Var3 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 9, "<button>Start</button> ")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
switch i {
case 1:
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 10, "<button>1</button> ")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
default:
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 11, "<button>default</button> ")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 12, "<button>End</button>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return nil
})
}
const WhitespaceIsConsistentInOneSwitchExpected = `<button>Start</button> <button>1</button> <button>End</button>`
const WhitespaceIsConsistentInDefaultSwitchExpected = `<button>Start</button> <button>default</button> <button>End</button>`
func WhitespaceIsConsistentInSwitchNoDefault() templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
return templ_7745c5c3_CtxErr
}
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var4 := templ.GetChildren(ctx)
if templ_7745c5c3_Var4 == nil {
templ_7745c5c3_Var4 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 13, "<button>Start</button> ")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
switch false {
case true:
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 14, "<button>Will Not Render</button> ")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 15, "<button>End</button>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return nil
})
}
const WhitespaceIsConsistentInSwitchNoDefaultExpected = `<button>Start</button> <button>End</button>`
func WhitespaceIsConsistentInFor(i int) templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
return templ_7745c5c3_CtxErr
}
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var5 := templ.GetChildren(ctx)
if templ_7745c5c3_Var5 == nil {
templ_7745c5c3_Var5 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 16, "<button>Start</button> ")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
for j := 0; j < i; j++ {
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 17, "<button>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var6 string
templ_7745c5c3_Var6, templ_7745c5c3_Err = templ.JoinStringErrs(fmt.Sprint(j))
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `generator/test-whitespace-around-go-keywords/template.templ`, Line: 59, Col: 25}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var6))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 18, "</button> ")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 19, "<button>End</button>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return nil
})
}
const WhitespaceIsConsistentInForZeroExpected = `<button>Start</button> <button>End</button>`
const WhitespaceIsConsistentInForOneExpected = `<button>Start</button> <button>0</button> <button>End</button>`
const WhitespaceIsConsistentInForThreeExpected = `<button>Start</button> <button>0</button> <button>1</button> <button>2</button> <button>End</button>`
var _ = templruntime.GeneratedTemplate
package templ
import "net/http"
// ComponentHandler is a http.Handler that renders components.
type ComponentHandler struct {
Component Component
Status int
ContentType string
ErrorHandler func(r *http.Request, err error) http.Handler
StreamResponse bool
}
const componentHandlerErrorMessage = "templ: failed to render template"
func (ch *ComponentHandler) ServeHTTPBuffered(w http.ResponseWriter, r *http.Request) {
// Since the component may error, write to a buffer first.
// This prevents partial responses from being written to the client.
buf := GetBuffer()
defer ReleaseBuffer(buf)
err := ch.Component.Render(r.Context(), buf)
if err != nil {
if ch.ErrorHandler != nil {
w.Header().Set("Content-Type", ch.ContentType)
ch.ErrorHandler(r, err).ServeHTTP(w, r)
return
}
http.Error(w, componentHandlerErrorMessage, http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", ch.ContentType)
if ch.Status != 0 {
w.WriteHeader(ch.Status)
}
// Ignore write error like http.Error() does, because there is
// no way to recover at this point.
_, _ = w.Write(buf.Bytes())
}
func (ch *ComponentHandler) ServeHTTPStreamed(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", ch.ContentType)
if ch.Status != 0 {
w.WriteHeader(ch.Status)
}
if err := ch.Component.Render(r.Context(), w); err != nil {
if ch.ErrorHandler != nil {
w.Header().Set("Content-Type", ch.ContentType)
ch.ErrorHandler(r, err).ServeHTTP(w, r)
return
}
http.Error(w, componentHandlerErrorMessage, http.StatusInternalServerError)
}
}
// ServeHTTP implements the http.Handler interface.
func (ch ComponentHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if ch.StreamResponse {
ch.ServeHTTPStreamed(w, r)
return
}
ch.ServeHTTPBuffered(w, r)
}
// Handler creates a http.Handler that renders the template.
func Handler(c Component, options ...func(*ComponentHandler)) *ComponentHandler {
ch := &ComponentHandler{
Component: c,
ContentType: "text/html; charset=utf-8",
}
for _, o := range options {
o(ch)
}
return ch
}
// WithStatus sets the HTTP status code returned by the ComponentHandler.
func WithStatus(status int) func(*ComponentHandler) {
return func(ch *ComponentHandler) {
ch.Status = status
}
}
// WithContentType sets the Content-Type header returned by the ComponentHandler.
func WithContentType(contentType string) func(*ComponentHandler) {
return func(ch *ComponentHandler) {
ch.ContentType = contentType
}
}
// WithErrorHandler sets the error handler used if rendering fails.
func WithErrorHandler(eh func(r *http.Request, err error) http.Handler) func(*ComponentHandler) {
return func(ch *ComponentHandler) {
ch.ErrorHandler = eh
}
}
// WithStreaming sets the ComponentHandler to stream the response instead of buffering it.
func WithStreaming() func(*ComponentHandler) {
return func(ch *ComponentHandler) {
ch.StreamResponse = true
}
}
package templ
import (
"context"
"io"
)
// Join returns a single `templ.Component` that will render provided components in order.
// If any of the components return an error the Join component will immediately return with the error.
func Join(components ...Component) Component {
return ComponentFunc(func(ctx context.Context, w io.Writer) (err error) {
for _, c := range components {
if err = c.Render(ctx, w); err != nil {
return err
}
}
return nil
})
}
package templ
import (
"crypto/sha256"
"encoding/hex"
"html"
)
// JSUnsafeFuncCall calls arbitrary JavaScript in the js parameter.
//
// Use of this function presents a security risk - the JavaScript must come
// from a trusted source, because it will be included as-is in the output.
func JSUnsafeFuncCall[T ~string](js T) ComponentScript {
sum := sha256.Sum256([]byte(js))
return ComponentScript{
Name: "jsUnsafeFuncCall_" + hex.EncodeToString(sum[:]),
// Function is empty because the body of the function is defined elsewhere,
// e.g. in a <script> tag within a templ.Once block.
Function: "",
Call: html.EscapeString(string(js)),
CallInline: string(js),
}
}
// JSFuncCall calls a JavaScript function with the given arguments.
//
// It can be used in event handlers, e.g. onclick, onhover, etc. or
// directly in HTML.
func JSFuncCall[T ~string](functionName T, args ...any) ComponentScript {
call := SafeScript(string(functionName), args...)
sum := sha256.Sum256([]byte(call))
return ComponentScript{
Name: "jsFuncCall_" + hex.EncodeToString(sum[:]),
// Function is empty because the body of the function is defined elsewhere,
// e.g. in a <script> tag within a templ.Once block.
Function: "",
Call: call,
CallInline: SafeScriptInline(string(functionName), args...),
}
}
package templ
import (
"context"
"encoding/json"
"fmt"
"io"
)
var _ Component = JSONScriptElement{}
// JSONScript renders a JSON object inside a script element.
// e.g. <script type="application/json">{"foo":"bar"}</script>
func JSONScript(id string, data any) JSONScriptElement {
return JSONScriptElement{
ID: id,
Type: "application/json",
Data: data,
Nonce: GetNonce,
}
}
// WithType sets the value of the type attribute of the script element.
func (j JSONScriptElement) WithType(t string) JSONScriptElement {
j.Type = t
return j
}
// WithNonceFromString sets the value of the nonce attribute of the script element to the given string.
func (j JSONScriptElement) WithNonceFromString(nonce string) JSONScriptElement {
j.Nonce = func(context.Context) string {
return nonce
}
return j
}
// WithNonceFrom sets the value of the nonce attribute of the script element to the value returned by the given function.
func (j JSONScriptElement) WithNonceFrom(f func(context.Context) string) JSONScriptElement {
j.Nonce = f
return j
}
type JSONScriptElement struct {
// ID of the element in the DOM.
ID string
// Type of the script element, defaults to "application/json".
Type string
// Data that will be encoded as JSON.
Data any
// Nonce is a function that returns a CSP nonce.
// Defaults to CSPNonceFromContext.
// See https://content-security-policy.com/nonce for more information.
Nonce func(ctx context.Context) string
}
func (j JSONScriptElement) Render(ctx context.Context, w io.Writer) (err error) {
if _, err = io.WriteString(w, "<script"); err != nil {
return err
}
if j.ID != "" {
if _, err = fmt.Fprintf(w, " id=\"%s\"", EscapeString(j.ID)); err != nil {
return err
}
}
if j.Type != "" {
if _, err = fmt.Fprintf(w, " type=\"%s\"", EscapeString(j.Type)); err != nil {
return err
}
}
if nonce := j.Nonce(ctx); nonce != "" {
if _, err = fmt.Fprintf(w, " nonce=\"%s\"", EscapeString(nonce)); err != nil {
return err
}
}
if _, err = io.WriteString(w, ">"); err != nil {
return err
}
if err = json.NewEncoder(w).Encode(j.Data); err != nil {
return err
}
if _, err = io.WriteString(w, "</script>"); err != nil {
return err
}
return nil
}
package templ
import (
"encoding/json"
)
// JSONString returns a JSON encoded string of v.
func JSONString(v any) (string, error) {
b, err := json.Marshal(v)
if err != nil {
return "", err
}
return string(b), nil
}
// SPDX-FileCopyrightText: 2021 The Go Language Server Authors
// SPDX-License-Identifier: BSD-3-Clause
package jsonrpc2
import (
"bytes"
"context"
"fmt"
"sync"
"sync/atomic"
"encoding/json"
)
// Conn is the common interface to jsonrpc clients and servers.
//
// Conn is bidirectional; it does not have a designated server or client end.
// It manages the jsonrpc2 protocol, connecting responses back to their calls.
type Conn interface {
// Call invokes the target method and waits for a response.
//
// The params will be marshaled to JSON before sending over the wire, and will
// be handed to the method invoked.
//
// The response will be unmarshaled from JSON into the result.
//
// The id returned will be unique from this connection, and can be used for
// logging or tracking.
Call(ctx context.Context, method string, params, result any) (ID, error)
// Notify invokes the target method but does not wait for a response.
//
// The params will be marshaled to JSON before sending over the wire, and will
// be handed to the method invoked.
Notify(ctx context.Context, method string, params any) error
// Go starts a goroutine to handle the connection.
//
// It must be called exactly once for each Conn. It returns immediately.
// Must block on Done() to wait for the connection to shut down.
//
// This is a temporary measure, this should be started automatically in the
// future.
Go(ctx context.Context, handler Handler)
// Close closes the connection and it's underlying stream.
//
// It does not wait for the close to complete, use the Done() channel for
// that.
Close() error
// Done returns a channel that will be closed when the processing goroutine
// has terminated, which will happen if Close() is called or an underlying
// stream is closed.
Done() <-chan struct{}
// Err returns an error if there was one from within the processing goroutine.
//
// If err returns non nil, the connection will be already closed or closing.
Err() error
}
type conn struct {
seq int32 // access atomically
writeMu sync.Mutex // protects writes to the stream
stream Stream // supplied stream
pendingMu sync.Mutex // protects the pending map
pending map[ID]chan *Response // holds the pending response channel with the ID as the key.
done chan struct{} // closed when done
err atomic.Value // holds run error
}
// NewConn creates a new connection object around the supplied stream.
func NewConn(s Stream) Conn {
conn := &conn{
stream: s,
pending: make(map[ID]chan *Response),
done: make(chan struct{}),
}
return conn
}
// Call implements Conn.
func (c *conn) Call(ctx context.Context, method string, params, result any) (id ID, err error) {
// generate a new request identifier
id = NewNumberID(atomic.AddInt32(&c.seq, 1))
call, err := NewCall(id, method, params)
if err != nil {
return id, fmt.Errorf("marshaling call parameters: %w", err)
}
// We have to add ourselves to the pending map before we send, otherwise we
// are racing the response. Also add a buffer to rchan, so that if we get a
// wire response between the time this call is cancelled and id is deleted
// from c.pending, the send to rchan will not block.
rchan := make(chan *Response, 1)
c.pendingMu.Lock()
c.pending[id] = rchan
c.pendingMu.Unlock()
defer func() {
c.pendingMu.Lock()
delete(c.pending, id)
c.pendingMu.Unlock()
}()
// now we are ready to send
_, err = c.write(ctx, call)
if err != nil {
// sending failed, we will never get a response, so don't leave it pending
return id, err
}
// now wait for the response
select {
case resp := <-rchan:
// is it an error response?
if resp.err != nil {
return id, resp.err
}
if result == nil || len(resp.result) == 0 {
return id, nil
}
dec := json.NewDecoder(bytes.NewReader(resp.result))
if err := dec.Decode(result); err != nil {
return id, fmt.Errorf("unmarshaling result: %w", err)
}
return id, nil
case <-ctx.Done():
return id, ctx.Err()
}
}
// Notify implements Conn.
func (c *conn) Notify(ctx context.Context, method string, params any) (err error) {
notify, err := NewNotification(method, params)
if err != nil {
return fmt.Errorf("marshaling notify parameters: %w", err)
}
_, err = c.write(ctx, notify)
return err
}
func (c *conn) replier(req Message) Replier {
return func(ctx context.Context, result any, err error) error {
call, ok := req.(*Call)
if !ok {
// request was a notify, no need to respond
return nil
}
response, err := NewResponse(call.id, result, err)
if err != nil {
return err
}
_, err = c.write(ctx, response)
if err != nil {
// TODO(iancottrell): if a stream write fails, we really need to shut down the whole stream
return err
}
return nil
}
}
func (c *conn) write(ctx context.Context, msg Message) (int64, error) {
c.writeMu.Lock()
n, err := c.stream.Write(ctx, msg)
c.writeMu.Unlock()
if err != nil {
return 0, fmt.Errorf("write to stream: %w", err)
}
return n, nil
}
// Go implements Conn.
func (c *conn) Go(ctx context.Context, handler Handler) {
go c.run(ctx, handler)
}
func (c *conn) run(ctx context.Context, handler Handler) {
defer close(c.done)
for {
// get the next message
msg, _, err := c.stream.Read(ctx)
if err != nil {
// The stream failed, we cannot continue.
c.fail(err)
return
}
switch msg := msg.(type) {
case Request:
if err := handler(ctx, c.replier(msg), msg); err != nil {
c.fail(err)
}
case *Response:
// If method is not set, this should be a response, in which case we must
// have an id to send the response back to the caller.
c.pendingMu.Lock()
rchan, ok := c.pending[msg.id]
c.pendingMu.Unlock()
if ok {
rchan <- msg
}
}
}
}
// Close implements Conn.
func (c *conn) Close() error {
return c.stream.Close()
}
// Done implements Conn.
func (c *conn) Done() <-chan struct{} {
return c.done
}
// Err implements Conn.
func (c *conn) Err() error {
if err := c.err.Load(); err != nil {
return err.(error)
}
return nil
}
// fail sets a failure condition on the stream and closes it.
func (c *conn) fail(err error) {
c.err.Store(err)
c.stream.Close()
}
// SPDX-FileCopyrightText: 2019 The Go Language Server Authors
// SPDX-License-Identifier: BSD-3-Clause
package jsonrpc2
import (
"errors"
"fmt"
"encoding/json"
)
// Error represents a JSON-RPC error.
type Error struct {
// Code a number indicating the error type that occurred.
Code Code `json:"code"`
// Message a string providing a short description of the error.
Message string `json:"message"`
// Data a Primitive or Structured value that contains additional
// information about the error. Can be omitted.
Data *json.RawMessage `json:"data,omitempty"`
}
// compile time check whether the Error implements error interface.
var _ error = (*Error)(nil)
// Error implements error.Error.
func (e *Error) Error() string {
if e == nil {
return ""
}
return e.Message
}
// Unwrap implements errors.Unwrap.
//
// Returns the error underlying the receiver, which may be nil.
func (e *Error) Unwrap() error { return errors.New(e.Message) }
// NewError builds a Error struct for the suppied code and message.
func NewError(c Code, message string) *Error {
return &Error{
Code: c,
Message: message,
}
}
// Errorf builds a Error struct for the suppied code, format and args.
func Errorf(c Code, format string, args ...any) *Error {
return &Error{
Code: c,
Message: fmt.Sprintf(format, args...),
}
}
// constErr represents a error constant.
type constErr string
// compile time check whether the constErr implements error interface.
var _ error = (*constErr)(nil)
// Error implements error.Error.
func (e constErr) Error() string { return string(e) }
const (
// ErrIdleTimeout is returned when serving timed out waiting for new connections.
ErrIdleTimeout = constErr("timed out waiting for new connections")
)
// SPDX-FileCopyrightText: 2019 The Go Language Server Authors
// SPDX-License-Identifier: BSD-3-Clause
package jsonrpc2
import (
"context"
"fmt"
"sync"
)
// Handler is invoked to handle incoming requests.
//
// The Replier sends a reply to the request and must be called exactly once.
type Handler func(ctx context.Context, reply Replier, req Request) error
// Replier is passed to handlers to allow them to reply to the request.
//
// If err is set then result will be ignored.
type Replier func(ctx context.Context, result any, err error) error
// MethodNotFoundHandler is a Handler that replies to all call requests with the
// standard method not found response.
//
// This should normally be the final handler in a chain.
func MethodNotFoundHandler(ctx context.Context, reply Replier, req Request) error {
return reply(ctx, nil, fmt.Errorf("%q: %w", req.Method(), ErrMethodNotFound))
}
// ReplyHandler creates a Handler that panics if the wrapped handler does
// not call Reply for every request that it is passed.
func ReplyHandler(handler Handler) (h Handler) {
h = Handler(func(ctx context.Context, reply Replier, req Request) error {
called := false
err := handler(ctx, func(ctx context.Context, result any, err error) error {
if called {
panic(fmt.Errorf("request %q replied to more than once", req.Method()))
}
called = true
return reply(ctx, result, err)
}, req)
if !called {
panic(fmt.Errorf("request %q was never replied to", req.Method()))
}
return err
})
return h
}
// CancelHandler returns a handler that supports cancellation, and a function
// that can be used to trigger canceling in progress requests.
func CancelHandler(handler Handler) (h Handler, canceller func(id ID)) {
var mu sync.Mutex
handling := make(map[ID]context.CancelFunc)
h = Handler(func(ctx context.Context, reply Replier, req Request) error {
if call, ok := req.(*Call); ok {
cancelCtx, cancel := context.WithCancel(ctx)
ctx = cancelCtx
mu.Lock()
handling[call.ID()] = cancel
mu.Unlock()
innerReply := reply
reply = func(ctx context.Context, result any, err error) error {
mu.Lock()
delete(handling, call.ID())
mu.Unlock()
return innerReply(ctx, result, err)
}
}
return handler(ctx, reply, req)
})
canceller = func(id ID) {
mu.Lock()
cancel, found := handling[id]
mu.Unlock()
if found {
cancel()
}
}
return h, canceller
}
// AsyncHandler returns a handler that processes each request goes in its own
// goroutine.
//
// The handler returns immediately, without the request being processed.
// Each request then waits for the previous request to finish before it starts.
//
// This allows the stream to unblock at the cost of unbounded goroutines
// all stalled on the previous one.
func AsyncHandler(handler Handler) (h Handler) {
nextRequest := make(chan struct{})
close(nextRequest)
h = Handler(func(ctx context.Context, reply Replier, req Request) error {
waitForPrevious := nextRequest
nextRequest = make(chan struct{})
unlockNext := nextRequest
innerReply := reply
reply = func(ctx context.Context, result any, err error) error {
close(unlockNext)
return innerReply(ctx, result, err)
}
go func() {
<-waitForPrevious
_ = handler(ctx, reply, req)
}()
return nil
})
return h
}
// SPDX-FileCopyrightText: 2021 The Go Language Server Authors
// SPDX-License-Identifier: BSD-3-Clause
package jsonrpc2
import (
"bytes"
"errors"
"fmt"
"encoding/json"
)
// Message is the interface to all JSON-RPC message types.
//
// They share no common functionality, but are a closed set of concrete types
// that are allowed to implement this interface.
//
// The message types are *Call, *Response and *Notification.
type Message interface {
// jsonrpc2Message is used to make the set of message implementations a
// closed set.
jsonrpc2Message()
}
// Request is the shared interface to jsonrpc2 messages that request
// a method be invoked.
//
// The request types are a closed set of *Call and *Notification.
type Request interface {
Message
// Method is a string containing the method name to invoke.
Method() string
// Params is either a struct or an array with the parameters of the method.
Params() json.RawMessage
// jsonrpc2Request is used to make the set of request implementations closed.
jsonrpc2Request()
}
// Call is a request that expects a response.
//
// The response will have a matching ID.
type Call struct {
// Method is a string containing the method name to invoke.
method string
// Params is either a struct or an array with the parameters of the method.
params json.RawMessage
// id of this request, used to tie the Response back to the request.
id ID
}
// make sure a Call implements the Request, json.Marshaler and json.Unmarshaler and interfaces.
var (
_ Request = (*Call)(nil)
_ json.Marshaler = (*Call)(nil)
_ json.Unmarshaler = (*Call)(nil)
)
// NewCall constructs a new Call message for the supplied ID, method and
// parameters.
func NewCall(id ID, method string, params any) (*Call, error) {
p, merr := marshalInterface(params)
req := &Call{
id: id,
method: method,
params: p,
}
return req, merr
}
// ID returns the current call id.
func (c *Call) ID() ID { return c.id }
// Method implements Request.
func (c *Call) Method() string { return c.method }
// Params implements Request.
func (c *Call) Params() json.RawMessage { return c.params }
// jsonrpc2Message implements Request.
func (Call) jsonrpc2Message() {}
// jsonrpc2Request implements Request.
func (Call) jsonrpc2Request() {}
// MarshalJSON implements json.Marshaler.
func (c Call) MarshalJSON() ([]byte, error) {
req := wireRequest{
Method: c.method,
Params: &c.params,
ID: &c.id,
}
data, err := json.Marshal(req)
if err != nil {
return data, fmt.Errorf("marshaling call: %w", err)
}
return data, nil
}
// UnmarshalJSON implements json.Unmarshaler.
func (c *Call) UnmarshalJSON(data []byte) error {
var req wireRequest
dec := json.NewDecoder(bytes.NewReader(data))
if err := dec.Decode(&req); err != nil {
return fmt.Errorf("unmarshaling call: %w", err)
}
c.method = req.Method
if req.Params != nil {
c.params = *req.Params
}
if req.ID != nil {
c.id = *req.ID
}
return nil
}
// Response is a reply to a Request.
//
// It will have the same ID as the call it is a response to.
type Response struct {
// result is the content of the response.
result json.RawMessage
// err is set only if the call failed.
err error
// ID of the request this is a response to.
id ID
}
// make sure a Response implements the Message, json.Marshaler and json.Unmarshaler and interfaces.
var (
_ Message = (*Response)(nil)
_ json.Marshaler = (*Response)(nil)
_ json.Unmarshaler = (*Response)(nil)
)
// NewResponse constructs a new Response message that is a reply to the
// supplied. If err is set result may be ignored.
func NewResponse(id ID, result any, err error) (*Response, error) {
r, merr := marshalInterface(result)
resp := &Response{
id: id,
result: r,
err: err,
}
return resp, merr
}
// ID returns the current response id.
func (r *Response) ID() ID { return r.id }
// Result returns the Response result.
func (r *Response) Result() json.RawMessage { return r.result }
// Err returns the Response error.
func (r *Response) Err() error { return r.err }
// jsonrpc2Message implements Message.
func (r *Response) jsonrpc2Message() {}
// MarshalJSON implements json.Marshaler.
func (r Response) MarshalJSON() ([]byte, error) {
resp := &wireResponse{
Error: toError(r.err),
ID: &r.id,
}
if resp.Error == nil {
resp.Result = &r.result
}
data, err := json.Marshal(resp)
if err != nil {
return data, fmt.Errorf("marshaling notification: %w", err)
}
return data, nil
}
// UnmarshalJSON implements json.Unmarshaler.
func (r *Response) UnmarshalJSON(data []byte) error {
var resp wireResponse
dec := json.NewDecoder(bytes.NewReader(data))
if err := dec.Decode(&resp); err != nil {
return fmt.Errorf("unmarshaling jsonrpc response: %w", err)
}
if resp.Result != nil {
r.result = *resp.Result
}
if resp.Error != nil {
r.err = resp.Error
}
if resp.ID != nil {
r.id = *resp.ID
}
return nil
}
func toError(err error) *Error {
if err == nil {
// no error, the response is complete
return nil
}
var wrapped *Error
if errors.As(err, &wrapped) {
// already a wire error, just use it
return wrapped
}
result := &Error{Message: err.Error()}
if errors.As(err, &wrapped) {
// if we wrapped a wire error, keep the code from the wrapped error
// but the message from the outer error
result.Code = wrapped.Code
}
return result
}
// Notification is a request for which a response cannot occur, and as such
// it has not ID.
type Notification struct {
// Method is a string containing the method name to invoke.
method string
params json.RawMessage
}
// make sure a Notification implements the Request, json.Marshaler and json.Unmarshaler and interfaces.
var (
_ Request = (*Notification)(nil)
_ json.Marshaler = (*Notification)(nil)
_ json.Unmarshaler = (*Notification)(nil)
)
// NewNotification constructs a new Notification message for the supplied
// method and parameters.
func NewNotification(method string, params any) (*Notification, error) {
p, merr := marshalInterface(params)
notify := &Notification{
method: method,
params: p,
}
return notify, merr
}
// Method implements Request.
func (n *Notification) Method() string { return n.method }
// Params implements Request.
func (n *Notification) Params() json.RawMessage { return n.params }
// jsonrpc2Message implements Request.
func (Notification) jsonrpc2Message() {}
// jsonrpc2Request implements Request.
func (Notification) jsonrpc2Request() {}
// MarshalJSON implements json.Marshaler.
func (n Notification) MarshalJSON() ([]byte, error) {
req := wireRequest{
Method: n.method,
Params: &n.params,
}
data, err := json.Marshal(req)
if err != nil {
return data, fmt.Errorf("marshaling notification: %w", err)
}
return data, nil
}
// UnmarshalJSON implements json.Unmarshaler.
func (n *Notification) UnmarshalJSON(data []byte) error {
var req wireRequest
dec := json.NewDecoder(bytes.NewReader(data))
if err := dec.Decode(&req); err != nil {
return fmt.Errorf("unmarshaling notification: %w", err)
}
n.method = req.Method
if req.Params != nil {
n.params = *req.Params
}
return nil
}
// DecodeMessage decodes data to Message.
func DecodeMessage(data []byte) (Message, error) {
var msg combined
dec := json.NewDecoder(bytes.NewReader(data))
if err := dec.Decode(&msg); err != nil {
return nil, fmt.Errorf("unmarshaling jsonrpc message: %w", err)
}
if msg.Method == "" {
// no method, should be a response
if msg.ID == nil {
return nil, ErrInvalidRequest
}
resp := &Response{
id: *msg.ID,
}
if msg.Error != nil {
resp.err = msg.Error
}
if msg.Result != nil {
resp.result = *msg.Result
}
return resp, nil
}
// has a method, must be a request
if msg.ID == nil {
// request with no ID is a notify
notify := &Notification{
method: msg.Method,
}
if msg.Params != nil {
notify.params = *msg.Params
}
return notify, nil
}
// request with an ID, must be a call
call := &Call{
method: msg.Method,
id: *msg.ID,
}
if msg.Params != nil {
call.params = *msg.Params
}
return call, nil
}
// marshalInterface marshal obj to json.RawMessage.
func marshalInterface(obj any) (json.RawMessage, error) {
data, err := json.Marshal(obj)
if err != nil {
return json.RawMessage{}, fmt.Errorf("failed to marshal json: %w", err)
}
return json.RawMessage(data), nil
}
// SPDX-FileCopyrightText: 2021 The Go Language Server Authors
// SPDX-License-Identifier: BSD-3-Clause
package jsonrpc2
import (
"context"
"fmt"
"net"
"os"
"time"
)
// NOTE: This file provides an experimental API for serving multiple remote
// jsonrpc2 clients over the network. For now, it is intentionally similar to
// net/http, but that may change in the future as we figure out the correct
// semantics.
// StreamServer is used to serve incoming jsonrpc2 clients communicating over
// a newly created connection.
type StreamServer interface {
ServeStream(context.Context, Conn) error
}
// ServerFunc is an adapter that implements the StreamServer interface
// using an ordinary function.
type ServerFunc func(context.Context, Conn) error
// ServeStream implements StreamServer.
//
// ServeStream calls f(ctx, s).
func (f ServerFunc) ServeStream(ctx context.Context, c Conn) error {
return f(ctx, c)
}
// HandlerServer returns a StreamServer that handles incoming streams using the
// provided handler.
func HandlerServer(h Handler) StreamServer {
return ServerFunc(func(ctx context.Context, conn Conn) error {
conn.Go(ctx, h)
<-conn.Done()
return conn.Err()
})
}
// ListenAndServe starts an jsonrpc2 server on the given address.
//
// If idleTimeout is non-zero, ListenAndServe exits after there are no clients for
// this duration, otherwise it exits only on error.
func ListenAndServe(ctx context.Context, network, addr string, server StreamServer, idleTimeout time.Duration) error {
ln, err := net.Listen(network, addr)
if err != nil {
return fmt.Errorf("failed to listen %s:%s: %w", network, addr, err)
}
defer ln.Close()
if network == "unix" {
defer os.Remove(addr)
}
return Serve(ctx, ln, server, idleTimeout)
}
// Serve accepts incoming connections from the network, and handles them using
// the provided server. If idleTimeout is non-zero, ListenAndServe exits after
// there are no clients for this duration, otherwise it exits only on error.
func Serve(ctx context.Context, ln net.Listener, server StreamServer, idleTimeout time.Duration) error {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
// Max duration: ~290 years; surely that's long enough.
const forever = 1<<63 - 1
if idleTimeout <= 0 {
idleTimeout = forever
}
connTimer := time.NewTimer(idleTimeout)
newConns := make(chan net.Conn)
doneListening := make(chan error)
closedConns := make(chan error)
go func() {
for {
nc, err := ln.Accept()
if err != nil {
select {
case doneListening <- fmt.Errorf("accept: %w", err):
case <-ctx.Done():
}
return
}
newConns <- nc
}
}()
activeConns := 0
for {
select {
case netConn := <-newConns:
activeConns++
connTimer.Stop()
stream := NewStream(netConn)
go func() {
conn := NewConn(stream)
closedConns <- server.ServeStream(ctx, conn)
stream.Close()
}()
case err := <-doneListening:
return err
case <-closedConns:
// if !isClosingError(err) {
// }
activeConns--
if activeConns == 0 {
connTimer.Reset(idleTimeout)
}
case <-connTimer.C:
return ErrIdleTimeout
case <-ctx.Done():
return ctx.Err()
}
}
}
// SPDX-FileCopyrightText: 2018 The Go Language Server Authors
// SPDX-License-Identifier: BSD-3-Clause
package jsonrpc2
import (
"bufio"
"context"
stdjson "encoding/json"
"fmt"
"io"
"strconv"
"strings"
"encoding/json"
)
const (
// HdrContentLength is the HTTP header name of the length of the content part in bytes. This header is required.
// This entity header indicates the size of the entity-body, in bytes, sent to the recipient.
//
// RFC 7230, section 3.3.2: Content-Length:
// https://tools.ietf.org/html/rfc7230#section-3.3.2
HdrContentLength = "Content-Length"
// HeaderContentType is the mime type of the content part. Defaults to "application/vscode-jsonrpc; charset=utf-8".
// This entity header is used to indicate the media type of the resource.
//
// RFC 7231, section 3.1.1.5: Content-Type:
// https://tools.ietf.org/html/rfc7231#section-3.1.1.5
HdrContentType = "Content-Type"
// HeaderContentSeparator is the header and content part separator.
HdrContentSeparator = "\r\n\r\n"
)
// Framer wraps a network connection up into a Stream.
//
// It is responsible for the framing and encoding of messages into wire form.
// NewRawStream and NewStream are implementations of a Framer.
type Framer func(conn io.ReadWriteCloser) Stream
// Stream abstracts the transport mechanics from the JSON RPC protocol.
//
// A Conn reads and writes messages using the stream it was provided on
// construction, and assumes that each call to Read or Write fully transfers
// a single message, or returns an error.
//
// A stream is not safe for concurrent use, it is expected it will be used by
// a single Conn in a safe manner.
type Stream interface {
// Read gets the next message from the stream.
Read(context.Context) (Message, int64, error)
// Write sends a message to the stream.
Write(context.Context, Message) (int64, error)
// Close closes the connection.
// Any blocked Read or Write operations will be unblocked and return errors.
Close() error
}
type rawStream struct {
conn io.ReadWriteCloser
in *stdjson.Decoder
}
// NewRawStream returns a Stream built on top of a io.ReadWriteCloser.
//
// The messages are sent with no wrapping, and rely on json decode consistency
// to determine message boundaries.
func NewRawStream(conn io.ReadWriteCloser) Stream {
return &rawStream{
conn: conn,
in: stdjson.NewDecoder(conn), // TODO(zchee): why test fail using segmentio json.Decoder?
}
}
// Read implements Stream.Read.
func (s *rawStream) Read(ctx context.Context) (Message, int64, error) {
select {
case <-ctx.Done():
return nil, 0, ctx.Err()
default:
}
var raw stdjson.RawMessage
if err := s.in.Decode(&raw); err != nil {
return nil, 0, fmt.Errorf("decoding raw message: %w", err)
}
msg, err := DecodeMessage(raw)
return msg, int64(len(raw)), err
}
// Write implements Stream.Write.
func (s *rawStream) Write(ctx context.Context, msg Message) (int64, error) {
select {
case <-ctx.Done():
return 0, ctx.Err()
default:
}
data, err := json.Marshal(msg)
if err != nil {
return 0, fmt.Errorf("marshaling message: %w", err)
}
n, err := s.conn.Write(data)
if err != nil {
return 0, fmt.Errorf("write to stream: %w", err)
}
return int64(n), nil
}
// Close implements Stream.Close.
func (s *rawStream) Close() error {
return s.conn.Close()
}
type stream struct {
conn io.ReadWriteCloser
in *bufio.Reader
}
// NewStream returns a Stream built on top of a io.ReadWriteCloser.
//
// The messages are sent with HTTP content length and MIME type headers.
// This is the format used by LSP and others.
func NewStream(conn io.ReadWriteCloser) Stream {
return &stream{
conn: conn,
in: bufio.NewReader(conn),
}
}
// Read implements Stream.Read.
func (s *stream) Read(ctx context.Context) (Message, int64, error) {
select {
case <-ctx.Done():
return nil, 0, ctx.Err()
default:
}
var total int64
var length int64
// read the header, stop on the first empty line
for {
line, err := s.in.ReadString('\n')
total += int64(len(line))
if err != nil {
return nil, total, fmt.Errorf("failed reading header line: %w", err)
}
line = strings.TrimSpace(line)
// check we have a header line
if line == "" {
break
}
colon := strings.IndexRune(line, ':')
if colon < 0 {
return nil, total, fmt.Errorf("invalid header line %q", line)
}
name, value := line[:colon], strings.TrimSpace(line[colon+1:])
switch name {
case HdrContentLength:
if length, err = strconv.ParseInt(value, 10, 32); err != nil {
return nil, total, fmt.Errorf("failed parsing %s: %v: %w", HdrContentLength, value, err)
}
if length <= 0 {
return nil, total, fmt.Errorf("invalid %s: %v", HdrContentLength, length)
}
default:
// ignoring unknown headers
}
}
if length == 0 {
return nil, total, fmt.Errorf("missing %s header", HdrContentLength)
}
data := make([]byte, length)
if _, err := io.ReadFull(s.in, data); err != nil {
return nil, total, fmt.Errorf("read full of data: %w", err)
}
total += length
msg, err := DecodeMessage(data)
return msg, total, err
}
// Write implements Stream.Write.
func (s *stream) Write(ctx context.Context, msg Message) (int64, error) {
select {
case <-ctx.Done():
return 0, ctx.Err()
default:
}
data, err := json.Marshal(msg)
if err != nil {
return 0, fmt.Errorf("marshaling message: %w", err)
}
n, err := fmt.Fprintf(s.conn, "%s: %v%s", HdrContentLength, len(data), HdrContentSeparator)
total := int64(n)
if err != nil {
return 0, fmt.Errorf("write data to conn: %w", err)
}
n, err = s.conn.Write(data)
total += int64(n)
if err != nil {
return 0, fmt.Errorf("write data to conn: %w", err)
}
return total, nil
}
// Close implements Stream.Close.
func (s *stream) Close() error {
return s.conn.Close()
}
// SPDX-FileCopyrightText: 2021 The Go Language Server Authors
// SPDX-License-Identifier: BSD-3-Clause
package jsonrpc2
import (
"fmt"
"encoding/json"
)
// Version represents a JSON-RPC version.
const Version = "2.0"
// version is a special 0 sized struct that encodes as the jsonrpc version tag.
//
// It will fail during decode if it is not the correct version tag in the stream.
type version struct{}
// compile time check whether the version implements a json.Marshaler and json.Unmarshaler interfaces.
var (
_ json.Marshaler = (*version)(nil)
_ json.Unmarshaler = (*version)(nil)
)
// MarshalJSON implements json.Marshaler.
func (version) MarshalJSON() ([]byte, error) {
return json.Marshal(Version)
}
// UnmarshalJSON implements json.Unmarshaler.
func (version) UnmarshalJSON(data []byte) error {
version := ""
if err := json.Unmarshal(data, &version); err != nil {
return fmt.Errorf("failed to Unmarshal: %w", err)
}
if version != Version {
return fmt.Errorf("invalid RPC version %v", version)
}
return nil
}
// ID is a Request identifier.
//
// Only one of either the Name or Number members will be set, using the
// number form if the Name is the empty string.
type ID struct {
name string
number int32
}
// compile time check whether the ID implements a fmt.Formatter, json.Marshaler and json.Unmarshaler interfaces.
var (
_ fmt.Formatter = (*ID)(nil)
_ json.Marshaler = (*ID)(nil)
_ json.Unmarshaler = (*ID)(nil)
)
// NewNumberID returns a new number request ID.
func NewNumberID(v int32) ID { return ID{number: v} }
// NewStringID returns a new string request ID.
func NewStringID(v string) ID { return ID{name: v} }
// Format writes the ID to the formatter.
//
// If the rune is q the representation is non ambiguous,
// string forms are quoted, number forms are preceded by a #.
func (id ID) Format(f fmt.State, r rune) {
numF, strF := `%d`, `%s`
if r == 'q' {
numF, strF = `#%d`, `%q`
}
switch {
case id.name != "":
fmt.Fprintf(f, strF, id.name)
default:
fmt.Fprintf(f, numF, id.number)
}
}
// MarshalJSON implements json.Marshaler.
func (id *ID) MarshalJSON() ([]byte, error) {
if id.name != "" {
return json.Marshal(id.name)
}
return json.Marshal(id.number)
}
// UnmarshalJSON implements json.Unmarshaler.
func (id *ID) UnmarshalJSON(data []byte) error {
*id = ID{}
if err := json.Unmarshal(data, &id.number); err == nil {
return nil
}
return json.Unmarshal(data, &id.name)
}
// wireRequest is sent to a server to represent a Call or Notify operaton.
type wireRequest struct {
// VersionTag is always encoded as the string "2.0"
VersionTag version `json:"jsonrpc"`
// Method is a string containing the method name to invoke.
Method string `json:"method"`
// Params is either a struct or an array with the parameters of the method.
Params *json.RawMessage `json:"params,omitempty"`
// The id of this request, used to tie the Response back to the request.
// Will be either a string or a number. If not set, the Request is a notify,
// and no response is possible.
ID *ID `json:"id,omitempty"`
}
// wireResponse is a reply to a Request.
//
// It will always have the ID field set to tie it back to a request, and will
// have either the Result or Error fields set depending on whether it is a
// success or failure wireResponse.
type wireResponse struct {
// VersionTag is always encoded as the string "2.0"
VersionTag version `json:"jsonrpc"`
// Result is the response value, and is required on success.
Result *json.RawMessage `json:"result,omitempty"`
// Error is a structured error response if the call fails.
Error *Error `json:"error,omitempty"`
// ID must be set and is the identifier of the Request this is a response to.
ID *ID `json:"id,omitempty"`
}
// combined has all the fields of both Request and Response.
//
// We can decode this and then work out which it is.
type combined struct {
VersionTag version `json:"jsonrpc"`
ID *ID `json:"id,omitempty"`
Method string `json:"method"`
Params *json.RawMessage `json:"params,omitempty"`
Result *json.RawMessage `json:"result,omitempty"`
Error *Error `json:"error,omitempty"`
}
// SPDX-FileCopyrightText: 2021 The Go Language Server Authors
// SPDX-License-Identifier: BSD-3-Clause
package protocol
import (
"encoding/json"
"fmt"
)
// CancelParams params of cancelRequest.
type CancelParams struct {
// ID is the request id to cancel.
ID any `json:"id"` // int32 | string
}
// ProgressParams params of Progress netification.
//
// @since 3.15.0.
type ProgressParams struct {
// Token is the progress token provided by the client or server.
Token ProgressToken `json:"token"`
// Value is the progress data.
Value any `json:"value"`
}
// ProgressToken is the progress token provided by the client or server.
//
// @since 3.15.0.
type ProgressToken struct {
name string
number int32
}
// compile time check whether the ProgressToken implements a fmt.Formatter, fmt.Stringer, json.Marshaler and json.Unmarshaler interfaces.
var (
_ fmt.Formatter = (*ProgressToken)(nil)
_ fmt.Stringer = (*ProgressToken)(nil)
_ json.Marshaler = (*ProgressToken)(nil)
_ json.Unmarshaler = (*ProgressToken)(nil)
)
// NewProgressToken returns a new ProgressToken.
func NewProgressToken(s string) *ProgressToken {
return &ProgressToken{name: s}
}
// NewNumberProgressToken returns a new number ProgressToken.
func NewNumberProgressToken(n int32) *ProgressToken {
return &ProgressToken{number: n}
}
// Format writes the ProgressToken to the formatter.
//
// If the rune is q the representation is non ambiguous,
// string forms are quoted.
func (v ProgressToken) Format(f fmt.State, r rune) {
const numF = `%d`
strF := `%s`
if r == 'q' {
strF = `%q`
}
switch {
case v.name != "":
fmt.Fprintf(f, strF, v.name)
default:
fmt.Fprintf(f, numF, v.number)
}
}
// String returns a string representation of the ProgressToken.
func (v ProgressToken) String() string {
return fmt.Sprint(v)
}
// MarshalJSON implements json.Marshaler.
func (v *ProgressToken) MarshalJSON() ([]byte, error) {
if v.name != "" {
return json.Marshal(v.name)
}
return json.Marshal(v.number)
}
// UnmarshalJSON implements json.Unmarshaler.
func (v *ProgressToken) UnmarshalJSON(data []byte) error {
*v = ProgressToken{}
if err := json.Unmarshal(data, &v.number); err == nil {
return nil
}
return json.Unmarshal(data, &v.name)
}
// SPDX-FileCopyrightText: 2019 The Go Language Server Authors
// SPDX-License-Identifier: BSD-3-Clause
package protocol
import (
"github.com/a-h/templ/lsp/uri"
)
// DocumentURI represents the URI of a document.
//
// Many of the interfaces contain fields that correspond to the URI of a document.
// For clarity, the type of such a field is declared as a DocumentURI.
// Over the wire, it will still be transferred as a string, but this guarantees
// that the contents of that string can be parsed as a valid URI.
type DocumentURI = uri.URI
// URI a tagging interface for normal non document URIs.
//
// @since 3.16.0.
type URI = uri.URI
// EOL denotes represents the character offset.
var EOL = []string{"\n", "\r\n", "\r"}
// Position represents a text document expressed as zero-based line and zero-based character offset.
//
// The offsets are based on a UTF-16 string representation.
// So a string of the form "a𐐀b" the character offset of the character "a" is 0,
// the character offset of "𐐀" is 1 and the character offset of "b" is 3 since 𐐀 is represented using two code
// units in UTF-16.
//
// Positions are line end character agnostic. So you can not specify a position that
// denotes "\r|\n" or "\n|" where "|" represents the character offset.
//
// Position is between two characters like an "insert" cursor in a editor.
// Special values like for example "-1" to denote the end of a line are not supported.
type Position struct {
// Line position in a document (zero-based).
//
// If a line number is greater than the number of lines in a document, it defaults back to the number of lines in
// the document.
// If a line number is negative, it defaults to 0.
Line uint32 `json:"line"`
// Character offset on a line in a document (zero-based).
//
// Assuming that the line is represented as a string, the Character value represents the gap between the
// "character" and "character + 1".
//
// If the character value is greater than the line length it defaults back to the line length.
// If a line number is negative, it defaults to 0.
Character uint32 `json:"character"`
}
// Range represents a text document expressed as (zero-based) start and end positions.
//
// A range is comparable to a selection in an editor. Therefore the end position is exclusive.
// If you want to specify a range that contains a line including the line ending character(s) then use an end position
// denoting the start of the next line.
type Range struct {
// Start is the range's start position.
Start Position `json:"start"`
// End is the range's end position.
End Position `json:"end"`
}
// Location represents a location inside a resource, such as a line inside a text file.
type Location struct {
URI DocumentURI `json:"uri"`
Range Range `json:"range"`
}
// LocationLink represents a link between a source and a target location.
type LocationLink struct {
// OriginSelectionRange span of the origin of this link.
//
// Used as the underlined span for mouse interaction. Defaults to the word range at the mouse position.
OriginSelectionRange *Range `json:"originSelectionRange,omitempty"`
// TargetURI is the target resource identifier of this link.
TargetURI DocumentURI `json:"targetUri"`
// TargetRange is the full target range of this link.
//
// If the target for example is a symbol then target range is the range enclosing this symbol not including
// leading/trailing whitespace but everything else like comments.
//
// This information is typically used to highlight the range in the editor.
TargetRange Range `json:"targetRange"`
// TargetSelectionRange is the range that should be selected and revealed when this link is being followed,
// e.g the name of a function.
//
// Must be contained by the the TargetRange. See also DocumentSymbol#range
TargetSelectionRange Range `json:"targetSelectionRange"`
}
// Command represents a reference to a command. Provides a title which will be used to represent a command in the UI.
//
// Commands are identified by a string identifier.
// The recommended way to handle commands is to implement their execution on the server side if the client and
// server provides the corresponding capabilities.
//
// Alternatively the tool extension code could handle the command. The protocol currently doesn't specify
// a set of well-known commands.
type Command struct {
// Title of the command, like `save`.
Title string `json:"title"`
// Command is the identifier of the actual command handler.
Command string `json:"command"`
// Arguments that the command handler should be invoked with.
Arguments []any `json:"arguments,omitempty"`
}
// TextEdit is a textual edit applicable to a text document.
type TextEdit struct {
// Range is the range of the text document to be manipulated.
//
// To insert text into a document create a range where start == end.
Range Range `json:"range"`
// NewText is the string to be inserted. For delete operations use an
// empty string.
NewText string `json:"newText"`
}
// ChangeAnnotation is the additional information that describes document changes.
//
// @since 3.16.0.
type ChangeAnnotation struct {
// Label a human-readable string describing the actual change.
// The string is rendered prominent in the user interface.
Label string `json:"label"`
// NeedsConfirmation is a flag which indicates that user confirmation is needed
// before applying the change.
NeedsConfirmation bool `json:"needsConfirmation,omitempty"`
// Description is a human-readable string which is rendered less prominent in
// the user interface.
Description string `json:"description,omitempty"`
}
// ChangeAnnotationIdentifier an identifier referring to a change annotation managed by a workspace
// edit.
//
// @since 3.16.0.
type ChangeAnnotationIdentifier string
// AnnotatedTextEdit is a special text edit with an additional change annotation.
//
// @since 3.16.0.
type AnnotatedTextEdit struct {
TextEdit
// AnnotationID is the actual annotation identifier.
AnnotationID ChangeAnnotationIdentifier `json:"annotationId"`
}
// TextDocumentEdit describes textual changes on a single text document.
//
// The TextDocument is referred to as a OptionalVersionedTextDocumentIdentifier to allow clients to check the
// text document version before an edit is applied.
//
// TextDocumentEdit describes all changes on a version "Si" and after they are applied move the document to
// version "Si+1".
// So the creator of a TextDocumentEdit doesn't need to sort the array or do any kind of ordering. However the
// edits must be non overlapping.
type TextDocumentEdit struct {
// TextDocument is the text document to change.
TextDocument OptionalVersionedTextDocumentIdentifier `json:"textDocument"`
// Edits is the edits to be applied.
//
// @since 3.16.0 - support for AnnotatedTextEdit.
// This is guarded by the client capability Workspace.WorkspaceEdit.ChangeAnnotationSupport.
Edits []TextEdit `json:"edits"` // []TextEdit | []AnnotatedTextEdit
}
// ResourceOperationKind is the file event type.
type ResourceOperationKind string
const (
// CreateResourceOperation supports creating new files and folders.
CreateResourceOperation ResourceOperationKind = "create"
// RenameResourceOperation supports renaming existing files and folders.
RenameResourceOperation ResourceOperationKind = "rename"
// DeleteResourceOperation supports deleting existing files and folders.
DeleteResourceOperation ResourceOperationKind = "delete"
)
// CreateFileOptions represents an options to create a file.
type CreateFileOptions struct {
// Overwrite existing file. Overwrite wins over `ignoreIfExists`.
Overwrite bool `json:"overwrite,omitempty"`
// IgnoreIfExists ignore if exists.
IgnoreIfExists bool `json:"ignoreIfExists,omitempty"`
}
// CreateFile represents a create file operation.
type CreateFile struct {
// Kind a create.
Kind ResourceOperationKind `json:"kind"` // should be `create`
// URI is the resource to create.
URI DocumentURI `json:"uri"`
// Options additional options.
Options *CreateFileOptions `json:"options,omitempty"`
// AnnotationID an optional annotation identifier describing the operation.
//
// @since 3.16.0.
AnnotationID ChangeAnnotationIdentifier `json:"annotationId,omitempty"`
}
// RenameFileOptions represents a rename file options.
type RenameFileOptions struct {
// Overwrite target if existing. Overwrite wins over `ignoreIfExists`.
Overwrite bool `json:"overwrite,omitempty"`
// IgnoreIfExists ignores if target exists.
IgnoreIfExists bool `json:"ignoreIfExists,omitempty"`
}
// RenameFile represents a rename file operation.
type RenameFile struct {
// Kind a rename.
Kind ResourceOperationKind `json:"kind"` // should be `rename`
// OldURI is the old (existing) location.
OldURI DocumentURI `json:"oldUri"`
// NewURI is the new location.
NewURI DocumentURI `json:"newUri"`
// Options rename options.
Options *RenameFileOptions `json:"options,omitempty"`
// AnnotationID an optional annotation identifier describing the operation.
//
// @since 3.16.0.
AnnotationID ChangeAnnotationIdentifier `json:"annotationId,omitempty"`
}
// DeleteFileOptions represents a delete file options.
type DeleteFileOptions struct {
// Recursive delete the content recursively if a folder is denoted.
Recursive bool `json:"recursive,omitempty"`
// IgnoreIfNotExists ignore the operation if the file doesn't exist.
IgnoreIfNotExists bool `json:"ignoreIfNotExists,omitempty"`
}
// DeleteFile represents a delete file operation.
type DeleteFile struct {
// Kind is a delete.
Kind ResourceOperationKind `json:"kind"` // should be `delete`
// URI is the file to delete.
URI DocumentURI `json:"uri"`
// Options delete options.
Options *DeleteFileOptions `json:"options,omitempty"`
// AnnotationID an optional annotation identifier describing the operation.
//
// @since 3.16.0.
AnnotationID ChangeAnnotationIdentifier `json:"annotationId,omitempty"`
}
// WorkspaceEdit represent a changes to many resources managed in the workspace.
//
// The edit should either provide changes or documentChanges.
// If the client can handle versioned document edits and if documentChanges are present, the latter are preferred over
// changes.
type WorkspaceEdit struct {
// Changes holds changes to existing resources.
Changes map[DocumentURI][]TextEdit `json:"changes,omitempty"`
// DocumentChanges depending on the client capability `workspace.workspaceEdit.resourceOperations` document changes
// are either an array of `TextDocumentEdit`s to express changes to n different text documents
// where each text document edit addresses a specific version of a text document. Or it can contain
// above `TextDocumentEdit`s mixed with create, rename and delete file / folder operations.
//
// Whether a client supports versioned document edits is expressed via
// `workspace.workspaceEdit.documentChanges` client capability.
//
// If a client neither supports `documentChanges` nor `workspace.workspaceEdit.resourceOperations` then
// only plain `TextEdit`s using the `changes` property are supported.
DocumentChanges []TextDocumentEdit `json:"documentChanges,omitempty"`
// ChangeAnnotations is a map of change annotations that can be referenced in
// "AnnotatedTextEdit"s or create, rename and delete file / folder
// operations.
//
// Whether clients honor this property depends on the client capability
// "workspace.changeAnnotationSupport".
//
// @since 3.16.0.
ChangeAnnotations map[ChangeAnnotationIdentifier]ChangeAnnotation `json:"changeAnnotations,omitempty"`
}
// TextDocumentIdentifier indicates the using a URI. On the protocol level, URIs are passed as strings.
type TextDocumentIdentifier struct {
// URI is the text document's URI.
URI DocumentURI `json:"uri"`
}
// TextDocumentItem represent an item to transfer a text document from the client to the server.
type TextDocumentItem struct {
// URI is the text document's URI.
URI DocumentURI `json:"uri"`
// LanguageID is the text document's language identifier.
LanguageID LanguageIdentifier `json:"languageId"`
// Version is the version number of this document (it will increase after each
// change, including undo/redo).
Version int32 `json:"version"`
// Text is the content of the opened text document.
Text string `json:"text"`
}
// LanguageIdentifier represent a text document's language identifier.
type LanguageIdentifier string
const (
// ABAPLanguage ABAP Language.
ABAPLanguage LanguageIdentifier = "abap"
// BatLanguage Windows Bat Language.
BatLanguage LanguageIdentifier = "bat"
// BibtexLanguage BibTeX Language.
BibtexLanguage LanguageIdentifier = "bibtex"
// ClojureLanguage Clojure Language.
ClojureLanguage LanguageIdentifier = "clojure"
// CoffeescriptLanguage CoffeeScript Language.
CoffeeScriptLanguage LanguageIdentifier = "coffeescript"
// CLanguage C Language.
CLanguage LanguageIdentifier = "c"
// CppLanguage C++ Language.
CppLanguage LanguageIdentifier = "cpp"
// CsharpLanguage C# Language.
CsharpLanguage LanguageIdentifier = "csharp"
// CSSLanguage CSS Language.
CSSLanguage LanguageIdentifier = "css"
// DiffLanguage Diff Language.
DiffLanguage LanguageIdentifier = "diff"
// DartLanguage Dart Language.
DartLanguage LanguageIdentifier = "dart"
// DockerfileLanguage Dockerfile Language.
DockerfileLanguage LanguageIdentifier = "dockerfile"
// ElixirLanguage Elixir Language.
ElixirLanguage LanguageIdentifier = "elixir"
// ErlangLanguage Erlang Language.
ErlangLanguage LanguageIdentifier = "erlang"
// FsharpLanguage F# Language.
FsharpLanguage LanguageIdentifier = "fsharp"
// GitCommitLanguage Git Language.
GitCommitLanguage LanguageIdentifier = "git-commit"
// GitRebaseLanguage Git Language.
GitRebaseLanguage LanguageIdentifier = "git-rebase"
// GoLanguage Go Language.
GoLanguage LanguageIdentifier = "go"
// GroovyLanguage Groovy Language.
GroovyLanguage LanguageIdentifier = "groovy"
// HandlebarsLanguage Handlebars Language.
HandlebarsLanguage LanguageIdentifier = "handlebars"
// HTMLLanguage HTML Language.
HTMLLanguage LanguageIdentifier = "html"
// IniLanguage Ini Language.
IniLanguage LanguageIdentifier = "ini"
// JavaLanguage Java Language.
JavaLanguage LanguageIdentifier = "java"
// JavaScriptLanguage JavaScript Language.
JavaScriptLanguage LanguageIdentifier = "javascript"
// JavaScriptReactLanguage JavaScript React Language.
JavaScriptReactLanguage LanguageIdentifier = "javascriptreact"
// JSONLanguage JSON Language.
JSONLanguage LanguageIdentifier = "json"
// LatexLanguage LaTeX Language.
LatexLanguage LanguageIdentifier = "latex"
// LessLanguage Less Language.
LessLanguage LanguageIdentifier = "less"
// LuaLanguage Lua Language.
LuaLanguage LanguageIdentifier = "lua"
// MakefileLanguage Makefile Language.
MakefileLanguage LanguageIdentifier = "makefile"
// MarkdownLanguage Markdown Language.
MarkdownLanguage LanguageIdentifier = "markdown"
// ObjectiveCLanguage Objective-C Language.
ObjectiveCLanguage LanguageIdentifier = "objective-c"
// ObjectiveCppLanguage Objective-C++ Language.
ObjectiveCppLanguage LanguageIdentifier = "objective-cpp"
// PerlLanguage Perl Language.
PerlLanguage LanguageIdentifier = "perl"
// Perl6Language Perl Language.
Perl6Language LanguageIdentifier = "perl6"
// PHPLanguage PHP Language.
PHPLanguage LanguageIdentifier = "php"
// PowershellLanguage Powershell Language.
PowershellLanguage LanguageIdentifier = "powershell"
// JadeLanguage Pug Language.
JadeLanguage LanguageIdentifier = "jade"
// PythonLanguage Python Language.
PythonLanguage LanguageIdentifier = "python"
// RLanguage R Language.
RLanguage LanguageIdentifier = "r"
// RazorLanguage Razor(cshtml) Language.
RazorLanguage LanguageIdentifier = "razor"
// RubyLanguage Ruby Language.
RubyLanguage LanguageIdentifier = "ruby"
// RustLanguage Rust Language.
RustLanguage LanguageIdentifier = "rust"
// SCSSLanguage SCSS Languages syntax using curly brackets.
SCSSLanguage LanguageIdentifier = "scss"
// SASSLanguage SCSS Languages indented syntax.
SASSLanguage LanguageIdentifier = "sass"
// ScalaLanguage Scala Language.
ScalaLanguage LanguageIdentifier = "scala"
// ShaderlabLanguage ShaderLab Language.
ShaderlabLanguage LanguageIdentifier = "shaderlab"
// ShellscriptLanguage Shell Script (Bash) Language.
ShellscriptLanguage LanguageIdentifier = "shellscript"
// SQLLanguage SQL Language.
SQLLanguage LanguageIdentifier = "sql"
// SwiftLanguage Swift Language.
SwiftLanguage LanguageIdentifier = "swift"
// TypeScriptLanguage TypeScript Language.
TypeScriptLanguage LanguageIdentifier = "typescript"
// TypeScriptReactLanguage TypeScript React Language.
TypeScriptReactLanguage LanguageIdentifier = "typescriptreact"
// TeXLanguage TeX Language.
TeXLanguage LanguageIdentifier = "tex"
// VBLanguage Visual Basic Language.
VBLanguage LanguageIdentifier = "vb"
// XMLLanguage XML Language.
XMLLanguage LanguageIdentifier = "xml"
// XslLanguage XSL Language.
XslLanguage LanguageIdentifier = "xsl"
// YamlLanguage YAML Language.
YamlLanguage LanguageIdentifier = "yaml"
)
// languageIdentifierMap map of LanguageIdentifiers.
var languageIdentifierMap = map[string]LanguageIdentifier{
"abap": ABAPLanguage,
"bat": BatLanguage,
"bibtex": BibtexLanguage,
"clojure": ClojureLanguage,
"coffeescript": CoffeeScriptLanguage,
"c": CLanguage,
"cpp": CppLanguage,
"csharp": CsharpLanguage,
"css": CSSLanguage,
"diff": DiffLanguage,
"dart": DartLanguage,
"dockerfile": DockerfileLanguage,
"elixir": ElixirLanguage,
"erlang": ErlangLanguage,
"fsharp": FsharpLanguage,
"git-commit": GitCommitLanguage,
"git-rebase": GitRebaseLanguage,
"go": GoLanguage,
"groovy": GroovyLanguage,
"handlebars": HandlebarsLanguage,
"html": HTMLLanguage,
"ini": IniLanguage,
"java": JavaLanguage,
"javascript": JavaScriptLanguage,
"javascriptreact": JavaScriptReactLanguage,
"json": JSONLanguage,
"latex": LatexLanguage,
"less": LessLanguage,
"lua": LuaLanguage,
"makefile": MakefileLanguage,
"markdown": MarkdownLanguage,
"objective-c": ObjectiveCLanguage,
"objective-cpp": ObjectiveCppLanguage,
"perl": PerlLanguage,
"perl6": Perl6Language,
"php": PHPLanguage,
"powershell": PowershellLanguage,
"jade": JadeLanguage,
"python": PythonLanguage,
"r": RLanguage,
"razor": RazorLanguage,
"ruby": RubyLanguage,
"rust": RustLanguage,
"scss": SCSSLanguage,
"sass": SASSLanguage,
"scala": ScalaLanguage,
"shaderlab": ShaderlabLanguage,
"shellscript": ShellscriptLanguage,
"sql": SQLLanguage,
"swift": SwiftLanguage,
"typescript": TypeScriptLanguage,
"typescriptreact": TypeScriptReactLanguage,
"tex": TeXLanguage,
"vb": VBLanguage,
"xml": XMLLanguage,
"xsl": XslLanguage,
"yaml": YamlLanguage,
}
// ToLanguageIdentifier converts ft to LanguageIdentifier.
func ToLanguageIdentifier(ft string) LanguageIdentifier {
langID, ok := languageIdentifierMap[ft]
if ok {
return langID
}
return LanguageIdentifier(ft)
}
// VersionedTextDocumentIdentifier represents an identifier to denote a specific version of a text document.
//
// This information usually flows from the client to the server.
type VersionedTextDocumentIdentifier struct {
TextDocumentIdentifier
// Version is the version number of this document.
//
// The version number of a document will increase after each change, including
// undo/redo. The number doesn't need to be consecutive.
Version int32 `json:"version"`
}
// OptionalVersionedTextDocumentIdentifier represents an identifier which optionally denotes a specific version of
// a text document.
//
// This information usually flows from the server to the client.
//
// @since 3.16.0.
type OptionalVersionedTextDocumentIdentifier struct {
TextDocumentIdentifier
// Version is the version number of this document. If an optional versioned text document
// identifier is sent from the server to the client and the file is not
// open in the editor (the server has not received an open notification
// before) the server can send `null` to indicate that the version is
// known and the content on disk is the master (as specified with document
// content ownership).
//
// The version number of a document will increase after each change,
// including undo/redo. The number doesn't need to be consecutive.
Version *int32 `json:"version"` // int32 | null
}
// TextDocumentPositionParams is a parameter literal used in requests to pass a text document and a position
// inside that document.
//
// It is up to the client to decide how a selection is converted into a position when issuing a request for a text
// document.
//
// The client can for example honor or ignore the selection direction to make LSP request consistent with features
// implemented internally.
type TextDocumentPositionParams struct {
// TextDocument is the text document.
TextDocument TextDocumentIdentifier `json:"textDocument"`
// Position is the position inside the text document.
Position Position `json:"position"`
}
// DocumentFilter is a document filter denotes a document through properties like language, scheme or pattern.
//
// An example is a filter that applies to TypeScript files on disk.
type DocumentFilter struct {
// Language a language id, like `typescript`.
Language string `json:"language,omitempty"`
// Scheme a URI scheme, like `file` or `untitled`.
Scheme string `json:"scheme,omitempty"`
// Pattern a glob pattern, like `*.{ts,js}`.
//
// Glob patterns can have the following syntax:
// "*"
// "*" to match one or more characters in a path segment
// "?"
// "?" to match on one character in a path segment
// "**"
// "**" to match any number of path segments, including none
// "{}"
// "{}" to group conditions (e.g. `**/*.{ts,js}` matches all TypeScript and JavaScript files)
// "[]"
// "[]" to declare a range of characters to match in a path segment (e.g., `example.[0-9]` to match on `example.0`, `example.1`, …)
// "[!...]"
// "[!...]" to negate a range of characters to match in a path segment (e.g., `example.[!0-9]` to match on `example.a`, `example.b`, but not `example.0`)
Pattern string `json:"pattern,omitempty"`
}
// DocumentSelector is a document selector is the combination of one or more document filters.
type DocumentSelector []*DocumentFilter
// MarkupKind describes the content type that a client supports in various
// result literals like `Hover`, `ParameterInfo` or `CompletionItem`.
//
// Please note that `MarkupKinds` must not start with a `$`. This kinds
// are reserved for internal usage.
type MarkupKind string
const (
// PlainText is supported as a content format.
PlainText MarkupKind = "plaintext"
// Markdown is supported as a content format.
Markdown MarkupKind = "markdown"
)
// MarkupContent a `MarkupContent` literal represents a string value which content is interpreted base on its
// kind flag.
//
// Currently the protocol supports `plaintext` and `markdown` as markup kinds.
//
// If the kind is `markdown` then the value can contain fenced code blocks like in GitHub issues.
// See https://help.github.com/articles/creating-and-highlighting-code-blocks/#syntax-highlighting
//
// Here is an example how such a string can be constructed using JavaScript / TypeScript:
//
// let markdown: MarkdownContent = {
// kind: MarkupKind.Markdown,
// value: [
// '# Header',
// 'Some text',
// '```typescript',
// 'someCode();',
// '```'
// ].join('\n')
// };
//
// NOTE: clients might sanitize the return markdown. A client could decide to
// remove HTML from the markdown to avoid script execution.
type MarkupContent struct {
// Kind is the type of the Markup
Kind MarkupKind `json:"kind"`
// Value is the content itself
Value string `json:"value"`
}
// SPDX-FileCopyrightText: 2021 The Go Language Server Authors
// SPDX-License-Identifier: BSD-3-Clause
package protocol
import "strconv"
// ClientCapabilities now define capabilities for dynamic registration, workspace and text document features
// the client supports.
//
// The experimental can be used to pass experimental capabilities under development.
//
// For future compatibility a ClientCapabilities object literal can have more properties set than currently defined.
// Servers receiving a ClientCapabilities object literal with unknown properties should ignore these properties.
//
// A missing property should be interpreted as an absence of the capability.
// If a missing property normally defines sub properties, all missing sub properties should be interpreted
// as an absence of the corresponding capability.
type ClientCapabilities struct {
// Workspace specific client capabilities.
Workspace *WorkspaceClientCapabilities `json:"workspace,omitempty"`
// TextDocument specific client capabilities.
TextDocument *TextDocumentClientCapabilities `json:"textDocument,omitempty"`
// Window specific client capabilities.
Window *WindowClientCapabilities `json:"window,omitempty"`
// General client capabilities.
//
// @since 3.16.0.
General *GeneralClientCapabilities `json:"general,omitempty"`
// Experimental client capabilities.
Experimental any `json:"experimental,omitempty"`
}
// WorkspaceClientCapabilities Workspace specific client capabilities.
type WorkspaceClientCapabilities struct {
// The client supports applying batch edits to the workspace by supporting
// the request "workspace/applyEdit".
ApplyEdit bool `json:"applyEdit,omitempty"`
// WorkspaceEdit capabilities specific to `WorkspaceEdit`s.
WorkspaceEdit *WorkspaceClientCapabilitiesWorkspaceEdit `json:"workspaceEdit,omitempty"`
// DidChangeConfiguration capabilities specific to the `workspace/didChangeConfiguration` notification.
DidChangeConfiguration *DidChangeConfigurationWorkspaceClientCapabilities `json:"didChangeConfiguration,omitempty"`
// DidChangeWatchedFiles capabilities specific to the `workspace/didChangeWatchedFiles` notification.
DidChangeWatchedFiles *DidChangeWatchedFilesWorkspaceClientCapabilities `json:"didChangeWatchedFiles,omitempty"`
// Symbol capabilities specific to the "workspace/symbol" request.
Symbol *WorkspaceSymbolClientCapabilities `json:"symbol,omitempty"`
// ExecuteCommand capabilities specific to the "workspace/executeCommand" request.
ExecuteCommand *ExecuteCommandClientCapabilities `json:"executeCommand,omitempty"`
// WorkspaceFolders is the client has support for workspace folders.
//
// @since 3.6.0.
WorkspaceFolders bool `json:"workspaceFolders,omitempty"`
// Configuration is the client supports "workspace/configuration" requests.
//
// @since 3.6.0.
Configuration bool `json:"configuration,omitempty"`
// SemanticTokens is the capabilities specific to the semantic token requests scoped to the
// workspace.
//
// @since 3.16.0.
SemanticTokens *SemanticTokensWorkspaceClientCapabilities `json:"semanticTokens,omitempty"`
// CodeLens is the Capabilities specific to the code lens requests scoped to the
// workspace.
//
// @since 3.16.0.
CodeLens *CodeLensWorkspaceClientCapabilities `json:"codeLens,omitempty"`
// FileOperations is the client has support for file requests/notifications.
//
// @since 3.16.0.
FileOperations *WorkspaceClientCapabilitiesFileOperations `json:"fileOperations,omitempty"`
}
// WorkspaceClientCapabilitiesWorkspaceEdit capabilities specific to "WorkspaceEdit"s.
type WorkspaceClientCapabilitiesWorkspaceEdit struct {
// DocumentChanges is the client supports versioned document changes in `WorkspaceEdit`s
DocumentChanges bool `json:"documentChanges,omitempty"`
// FailureHandling is the failure handling strategy of a client if applying the workspace edit
// fails.
//
// Mostly FailureHandlingKind.
FailureHandling string `json:"failureHandling,omitempty"`
// ResourceOperations is the resource operations the client supports. Clients should at least
// support "create", "rename" and "delete" files and folders.
ResourceOperations []string `json:"resourceOperations,omitempty"`
// NormalizesLineEndings whether the client normalizes line endings to the client specific
// setting.
// If set to `true` the client will normalize line ending characters
// in a workspace edit to the client specific new line character(s).
//
// @since 3.16.0.
NormalizesLineEndings bool `json:"normalizesLineEndings,omitempty"`
// ChangeAnnotationSupport whether the client in general supports change annotations on text edits,
// create file, rename file and delete file changes.
//
// @since 3.16.0.
ChangeAnnotationSupport *WorkspaceClientCapabilitiesWorkspaceEditChangeAnnotationSupport `json:"changeAnnotationSupport,omitempty"`
}
// FailureHandlingKind is the kind of failure handling .
type FailureHandlingKind string
const (
// FailureHandlingKindAbort applying the workspace change is simply aborted if one of the changes provided
// fails. All operations executed before the failing operation stay executed.
FailureHandlingKindAbort FailureHandlingKind = "abort"
// FailureHandlingKindTransactional all operations are executed transactional. That means they either all
// succeed or no changes at all are applied to the workspace.
FailureHandlingKindTransactional FailureHandlingKind = "transactional"
// FailureHandlingKindTextOnlyTransactional if the workspace edit contains only textual file changes they are executed transactional.
// If resource changes (create, rename or delete file) are part of the change the failure
// handling strategy is abort.
FailureHandlingKindTextOnlyTransactional FailureHandlingKind = "textOnlyTransactional"
// FailureHandlingKindUndo the client tries to undo the operations already executed. But there is no
// guarantee that this is succeeding.
FailureHandlingKindUndo FailureHandlingKind = "undo"
)
// WorkspaceClientCapabilitiesWorkspaceEditChangeAnnotationSupport is the ChangeAnnotationSupport of WorkspaceClientCapabilitiesWorkspaceEdit.
//
// @since 3.16.0.
type WorkspaceClientCapabilitiesWorkspaceEditChangeAnnotationSupport struct {
// GroupsOnLabel whether the client groups edits with equal labels into tree nodes,
// for instance all edits labeled with "Changes in Strings" would
// be a tree node.
GroupsOnLabel bool `json:"groupsOnLabel,omitempty"`
}
// DidChangeConfigurationWorkspaceClientCapabilities capabilities specific to the "workspace/didChangeConfiguration" notification.
//
// @since 3.16.0.
type DidChangeConfigurationWorkspaceClientCapabilities struct {
// DynamicRegistration whether the did change configuration notification supports dynamic registration.
DynamicRegistration bool `json:"dynamicRegistration,omitempty"`
}
// DidChangeWatchedFilesWorkspaceClientCapabilities capabilities specific to the "workspace/didChangeWatchedFiles" notification.
//
// @since 3.16.0.
type DidChangeWatchedFilesWorkspaceClientCapabilities struct {
// Did change watched files notification supports dynamic registration.
DynamicRegistration bool `json:"dynamicRegistration,omitempty"`
}
// WorkspaceSymbolClientCapabilities capabilities specific to the `workspace/symbol` request.
//
// WorkspaceSymbolClientCapabilities is the workspace symbol request is sent from the client to the server to
// list project-wide symbols matching the query string.
type WorkspaceSymbolClientCapabilities struct {
// DynamicRegistration is the Symbol request supports dynamic registration.
DynamicRegistration bool `json:"dynamicRegistration,omitempty"`
// SymbolKindCapabilities is the specific capabilities for the SymbolKindCapabilities in the "workspace/symbol" request.
SymbolKind *SymbolKindCapabilities `json:"symbolKind,omitempty"`
// TagSupport is the client supports tags on `SymbolInformation`.
// Clients supporting tags have to handle unknown tags gracefully.
//
// @since 3.16.0
TagSupport *TagSupportCapabilities `json:"tagSupport,omitempty"`
}
type SymbolKindCapabilities struct {
// ValueSet is the symbol kind values the client supports. When this
// property exists the client also guarantees that it will
// handle values outside its set gracefully and falls back
// to a default value when unknown.
//
// If this property is not present the client only supports
// the symbol kinds from `File` to `Array` as defined in
// the initial version of the protocol.
ValueSet []SymbolKind `json:"valueSet,omitempty"`
}
type TagSupportCapabilities struct {
// ValueSet is the tags supported by the client.
ValueSet []SymbolTag `json:"valueSet,omitempty"`
}
// ExecuteCommandClientCapabilities capabilities specific to the "workspace/executeCommand" request.
type ExecuteCommandClientCapabilities struct {
// DynamicRegistration Execute command supports dynamic registration.
DynamicRegistration bool `json:"dynamicRegistration,omitempty"`
}
// SemanticTokensWorkspaceClientCapabilities capabilities specific to the "workspace/semanticToken" request.
//
// @since 3.16.0.
type SemanticTokensWorkspaceClientCapabilities struct {
// RefreshSupport whether the client implementation supports a refresh request sent from
// the server to the client.
//
// Note that this event is global and will force the client to refresh all
// semantic tokens currently shown. It should be used with absolute care
// and is useful for situation where a server for example detect a project
// wide change that requires such a calculation.
RefreshSupport bool `json:"refreshSupport,omitempty"`
}
// CodeLensWorkspaceClientCapabilities capabilities specific to the "workspace/codeLens" request.
//
// @since 3.16.0.
type CodeLensWorkspaceClientCapabilities struct {
// RefreshSupport whether the client implementation supports a refresh request sent from the
// server to the client.
//
// Note that this event is global and will force the client to refresh all
// code lenses currently shown. It should be used with absolute care and is
// useful for situation where a server for example detect a project wide
// change that requires such a calculation.
RefreshSupport bool `json:"refreshSupport,omitempty"`
}
// WorkspaceClientCapabilitiesFileOperations capabilities specific to the fileOperations.
//
// @since 3.16.0.
type WorkspaceClientCapabilitiesFileOperations struct {
// DynamicRegistration whether the client supports dynamic registration for file
// requests/notifications.
DynamicRegistration bool `json:"dynamicRegistration,omitempty"`
// DidCreate is the client has support for sending didCreateFiles notifications.
DidCreate bool `json:"didCreate,omitempty"`
// WillCreate is the client has support for sending willCreateFiles requests.
WillCreate bool `json:"willCreate,omitempty"`
// DidRename is the client has support for sending didRenameFiles notifications.
DidRename bool `json:"didRename,omitempty"`
// WillRename is the client has support for sending willRenameFiles requests.
WillRename bool `json:"willRename,omitempty"`
// DidDelete is the client has support for sending didDeleteFiles notifications.
DidDelete bool `json:"didDelete,omitempty"`
// WillDelete is the client has support for sending willDeleteFiles requests.
WillDelete bool `json:"willDelete,omitempty"`
}
// TextDocumentClientCapabilities Text document specific client capabilities.
type TextDocumentClientCapabilities struct {
// Synchronization defines which synchronization capabilities the client supports.
Synchronization *TextDocumentSyncClientCapabilities `json:"synchronization,omitempty"`
// Completion Capabilities specific to the "textDocument/completion".
Completion *CompletionTextDocumentClientCapabilities `json:"completion,omitempty"`
// Hover capabilities specific to the "textDocument/hover".
Hover *HoverTextDocumentClientCapabilities `json:"hover,omitempty"`
// SignatureHelp capabilities specific to the "textDocument/signatureHelp".
SignatureHelp *SignatureHelpTextDocumentClientCapabilities `json:"signatureHelp,omitempty"`
// Declaration capabilities specific to the "textDocument/declaration".
Declaration *DeclarationTextDocumentClientCapabilities `json:"declaration,omitempty"`
// Definition capabilities specific to the "textDocument/definition".
//
// @since 3.14.0.
Definition *DefinitionTextDocumentClientCapabilities `json:"definition,omitempty"`
// TypeDefinition capabilities specific to the "textDocument/typeDefinition".
//
// @since 3.6.0.
TypeDefinition *TypeDefinitionTextDocumentClientCapabilities `json:"typeDefinition,omitempty"`
// Implementation capabilities specific to the "textDocument/implementation".
//
// @since 3.6.0.
Implementation *ImplementationTextDocumentClientCapabilities `json:"implementation,omitempty"`
// References capabilities specific to the "textDocument/references".
References *ReferencesTextDocumentClientCapabilities `json:"references,omitempty"`
// DocumentHighlight capabilities specific to the "textDocument/documentHighlight".
DocumentHighlight *DocumentHighlightClientCapabilities `json:"documentHighlight,omitempty"`
// DocumentSymbol capabilities specific to the "textDocument/documentSymbol".
DocumentSymbol *DocumentSymbolClientCapabilities `json:"documentSymbol,omitempty"`
// CodeAction capabilities specific to the "textDocument/codeAction".
CodeAction *CodeActionClientCapabilities `json:"codeAction,omitempty"`
// CodeLens capabilities specific to the "textDocument/codeLens".
CodeLens *CodeLensClientCapabilities `json:"codeLens,omitempty"`
// DocumentLink capabilities specific to the "textDocument/documentLink".
DocumentLink *DocumentLinkClientCapabilities `json:"documentLink,omitempty"`
// ColorProvider capabilities specific to the "textDocument/documentColor" and the
// "textDocument/colorPresentation" request.
//
// @since 3.6.0.
ColorProvider *DocumentColorClientCapabilities `json:"colorProvider,omitempty"`
// Formatting Capabilities specific to the "textDocument/formatting" request.
Formatting *DocumentFormattingClientCapabilities `json:"formatting,omitempty"`
// RangeFormatting Capabilities specific to the "textDocument/rangeFormatting" request.
RangeFormatting *DocumentRangeFormattingClientCapabilities `json:"rangeFormatting,omitempty"`
// OnTypeFormatting Capabilities specific to the "textDocument/onTypeFormatting" request.
OnTypeFormatting *DocumentOnTypeFormattingClientCapabilities `json:"onTypeFormatting,omitempty"`
// PublishDiagnostics capabilities specific to "textDocument/publishDiagnostics".
PublishDiagnostics *PublishDiagnosticsClientCapabilities `json:"publishDiagnostics,omitempty"`
// Rename capabilities specific to the "textDocument/rename".
Rename *RenameClientCapabilities `json:"rename,omitempty"`
// FoldingRange capabilities specific to "textDocument/foldingRange" requests.
//
// @since 3.10.0.
FoldingRange *FoldingRangeClientCapabilities `json:"foldingRange,omitempty"`
// SelectionRange capabilities specific to "textDocument/selectionRange" requests.
//
// @since 3.15.0.
SelectionRange *SelectionRangeClientCapabilities `json:"selectionRange,omitempty"`
// CallHierarchy capabilities specific to the various call hierarchy requests.
//
// @since 3.16.0.
CallHierarchy *CallHierarchyClientCapabilities `json:"callHierarchy,omitempty"`
// SemanticTokens capabilities specific to the various semantic token requests.
//
// @since 3.16.0.
SemanticTokens *SemanticTokensClientCapabilities `json:"semanticTokens,omitempty"`
// LinkedEditingRange capabilities specific to the "textDocument/linkedEditingRange" request.
//
// @since 3.16.0.
LinkedEditingRange *LinkedEditingRangeClientCapabilities `json:"linkedEditingRange,omitempty"`
// Moniker capabilities specific to the "textDocument/moniker" request.
//
// @since 3.16.0.
Moniker *MonikerClientCapabilities `json:"moniker,omitempty"`
}
// TextDocumentSyncClientCapabilities defines which synchronization capabilities the client supports.
type TextDocumentSyncClientCapabilities struct {
// DynamicRegistration whether text document synchronization supports dynamic registration.
DynamicRegistration bool `json:"dynamicRegistration,omitempty"`
// WillSave is the client supports sending will save notifications.
WillSave bool `json:"willSave,omitempty"`
// WillSaveWaitUntil is the client supports sending a will save request and
// waits for a response providing text edits which will
// be applied to the document before it is saved.
WillSaveWaitUntil bool `json:"willSaveWaitUntil,omitempty"`
// DidSave is the client supports did save notifications.
DidSave bool `json:"didSave,omitempty"`
}
// CompletionTextDocumentClientCapabilities Capabilities specific to the "textDocument/completion".
type CompletionTextDocumentClientCapabilities struct {
// Whether completion supports dynamic registration.
DynamicRegistration bool `json:"dynamicRegistration,omitempty"`
// The client supports the following `CompletionItem` specific
// capabilities.
CompletionItem *CompletionTextDocumentClientCapabilitiesItem `json:"completionItem,omitempty"`
CompletionItemKind *CompletionTextDocumentClientCapabilitiesItemKind `json:"completionItemKind,omitempty"`
// ContextSupport is the client supports to send additional context information for a
// `textDocument/completion` request.
ContextSupport bool `json:"contextSupport,omitempty"`
}
// CompletionTextDocumentClientCapabilitiesItem is the client supports the following "CompletionItem" specific
// capabilities.
type CompletionTextDocumentClientCapabilitiesItem struct {
// SnippetSupport client supports snippets as insert text.
//
// A snippet can define tab stops and placeholders with `$1`, `$2`
// and `${3:foo}`. `$0` defines the final tab stop, it defaults to
// the end of the snippet. Placeholders with equal identifiers are linked,
// that is typing in one will update others too.
SnippetSupport bool `json:"snippetSupport,omitempty"`
// CommitCharactersSupport client supports commit characters on a completion item.
CommitCharactersSupport bool `json:"commitCharactersSupport,omitempty"`
// DocumentationFormat client supports the follow content formats for the documentation
// property. The order describes the preferred format of the client.
DocumentationFormat []MarkupKind `json:"documentationFormat,omitempty"`
// DeprecatedSupport client supports the deprecated property on a completion item.
DeprecatedSupport bool `json:"deprecatedSupport,omitempty"`
// PreselectSupport client supports the preselect property on a completion item.
PreselectSupport bool `json:"preselectSupport,omitempty"`
// TagSupport is the client supports the tag property on a completion item.
//
// Clients supporting tags have to handle unknown tags gracefully.
// Clients especially need to preserve unknown tags when sending
// a completion item back to the server in a resolve call.
//
// @since 3.15.0.
TagSupport *CompletionTextDocumentClientCapabilitiesItemTagSupport `json:"tagSupport,omitempty"`
// InsertReplaceSupport client supports insert replace edit to control different behavior if
// a completion item is inserted in the text or should replace text.
//
// @since 3.16.0.
InsertReplaceSupport bool `json:"insertReplaceSupport,omitempty"`
// ResolveSupport indicates which properties a client can resolve lazily on a
// completion item. Before version 3.16.0 only the predefined properties
// `documentation` and `details` could be resolved lazily.
//
// @since 3.16.0.
ResolveSupport *CompletionTextDocumentClientCapabilitiesItemResolveSupport `json:"resolveSupport,omitempty"`
// InsertTextModeSupport is the client supports the `insertTextMode` property on
// a completion item to override the whitespace handling mode
// as defined by the client (see `insertTextMode`).
//
// @since 3.16.0.
InsertTextModeSupport *CompletionTextDocumentClientCapabilitiesItemInsertTextModeSupport `json:"insertTextModeSupport,omitempty"`
}
// CompletionTextDocumentClientCapabilitiesItemTagSupport specific capabilities for the "TagSupport" in the "textDocument/completion" request.
//
// @since 3.15.0.
type CompletionTextDocumentClientCapabilitiesItemTagSupport struct {
// ValueSet is the tags supported by the client.
//
// @since 3.15.0.
ValueSet []CompletionItemTag `json:"valueSet,omitempty"`
}
// CompletionTextDocumentClientCapabilitiesItemResolveSupport specific capabilities for the ResolveSupport in the CompletionTextDocumentClientCapabilitiesItem.
//
// @since 3.16.0.
type CompletionTextDocumentClientCapabilitiesItemResolveSupport struct {
// Properties is the properties that a client can resolve lazily.
Properties []string `json:"properties"`
}
// CompletionTextDocumentClientCapabilitiesItemInsertTextModeSupport specific capabilities for the InsertTextModeSupport in the CompletionTextDocumentClientCapabilitiesItem.
//
// @since 3.16.0.
type CompletionTextDocumentClientCapabilitiesItemInsertTextModeSupport struct {
// ValueSet is the tags supported by the client.
//
// @since 3.16.0.
ValueSet []InsertTextMode `json:"valueSet,omitempty"`
}
// CompletionTextDocumentClientCapabilitiesItemKind specific capabilities for the "CompletionItemKind" in the "textDocument/completion" request.
type CompletionTextDocumentClientCapabilitiesItemKind struct {
// The completion item kind values the client supports. When this
// property exists the client also guarantees that it will
// handle values outside its set gracefully and falls back
// to a default value when unknown.
//
// If this property is not present the client only supports
// the completion items kinds from `Text` to `Reference` as defined in
// the initial version of the protocol.
//
ValueSet []CompletionItemKind `json:"valueSet,omitempty"`
}
// HoverTextDocumentClientCapabilities capabilities specific to the "textDocument/hover".
type HoverTextDocumentClientCapabilities struct {
// DynamicRegistration whether hover supports dynamic registration.
DynamicRegistration bool `json:"dynamicRegistration,omitempty"`
// ContentFormat is the client supports the follow content formats for the content
// property. The order describes the preferred format of the client.
ContentFormat []MarkupKind `json:"contentFormat,omitempty"`
}
// SignatureHelpTextDocumentClientCapabilities capabilities specific to the "textDocument/signatureHelp".
type SignatureHelpTextDocumentClientCapabilities struct {
// DynamicRegistration whether signature help supports dynamic registration.
DynamicRegistration bool `json:"dynamicRegistration,omitempty"`
// SignatureInformation is the client supports the following "SignatureInformation"
// specific properties.
SignatureInformation *TextDocumentClientCapabilitiesSignatureInformation `json:"signatureInformation,omitempty"`
// ContextSupport is the client supports to send additional context information for a "textDocument/signatureHelp" request.
//
// A client that opts into contextSupport will also support the "retriggerCharacters" on "SignatureHelpOptions".
//
// @since 3.15.0.
ContextSupport bool `json:"contextSupport,omitempty"`
}
// TextDocumentClientCapabilitiesSignatureInformation is the client supports the following "SignatureInformation"
// specific properties.
type TextDocumentClientCapabilitiesSignatureInformation struct {
// DocumentationFormat is the client supports the follow content formats for the documentation
// property. The order describes the preferred format of the client.
DocumentationFormat []MarkupKind `json:"documentationFormat,omitempty"`
// ParameterInformation is the Client capabilities specific to parameter information.
ParameterInformation *TextDocumentClientCapabilitiesParameterInformation `json:"parameterInformation,omitempty"`
// ActiveParameterSupport is the client supports the `activeParameter` property on
// `SignatureInformation` literal.
//
// @since 3.16.0.
ActiveParameterSupport bool `json:"activeParameterSupport,omitempty"`
}
// TextDocumentClientCapabilitiesParameterInformation is the client capabilities specific to parameter information.
type TextDocumentClientCapabilitiesParameterInformation struct {
// LabelOffsetSupport is the client supports processing label offsets instead of a
// simple label string.
//
// @since 3.14.0.
LabelOffsetSupport bool `json:"labelOffsetSupport,omitempty"`
}
// DeclarationTextDocumentClientCapabilities capabilities specific to the "textDocument/declaration".
type DeclarationTextDocumentClientCapabilities struct {
// DynamicRegistration whether declaration supports dynamic registration. If this is set to `true`
// the client supports the new `(TextDocumentRegistrationOptions & StaticRegistrationOptions)`
// return value for the corresponding server capability as well.
DynamicRegistration bool `json:"dynamicRegistration,omitempty"`
// LinkSupport is the client supports additional metadata in the form of declaration links.
//
// @since 3.14.0.
LinkSupport bool `json:"linkSupport,omitempty"`
}
// DefinitionTextDocumentClientCapabilities capabilities specific to the "textDocument/definition".
//
// @since 3.14.0.
type DefinitionTextDocumentClientCapabilities struct {
// DynamicRegistration whether definition supports dynamic registration.
DynamicRegistration bool `json:"dynamicRegistration,omitempty"`
// LinkSupport is the client supports additional metadata in the form of definition links.
LinkSupport bool `json:"linkSupport,omitempty"`
}
// TypeDefinitionTextDocumentClientCapabilities capabilities specific to the "textDocument/typeDefinition".
//
// @since 3.6.0.
type TypeDefinitionTextDocumentClientCapabilities struct {
// DynamicRegistration whether typeDefinition supports dynamic registration. If this is set to `true`
// the client supports the new "(TextDocumentRegistrationOptions & StaticRegistrationOptions)"
// return value for the corresponding server capability as well.
DynamicRegistration bool `json:"dynamicRegistration,omitempty"`
// LinkSupport is the client supports additional metadata in the form of definition links.
//
// @since 3.14.0
LinkSupport bool `json:"linkSupport,omitempty"`
}
// ImplementationTextDocumentClientCapabilities capabilities specific to the "textDocument/implementation".
//
// @since 3.6.0.
type ImplementationTextDocumentClientCapabilities struct {
// DynamicRegistration whether implementation supports dynamic registration. If this is set to `true`
// the client supports the new "(TextDocumentRegistrationOptions & StaticRegistrationOptions)"
// return value for the corresponding server capability as well.
DynamicRegistration bool `json:"dynamicRegistration,omitempty"`
// LinkSupport is the client supports additional metadata in the form of definition links.
//
// @since 3.14.0
LinkSupport bool `json:"linkSupport,omitempty"`
}
// ReferencesTextDocumentClientCapabilities capabilities specific to the "textDocument/references".
type ReferencesTextDocumentClientCapabilities struct {
// DynamicRegistration whether references supports dynamic registration.
DynamicRegistration bool `json:"dynamicRegistration,omitempty"`
}
// DocumentHighlightClientCapabilities capabilities specific to the "textDocument/documentHighlight".
type DocumentHighlightClientCapabilities struct {
// DynamicRegistration Whether document highlight supports dynamic registration.
DynamicRegistration bool `json:"dynamicRegistration,omitempty"`
}
// DocumentSymbolClientCapabilities capabilities specific to the "textDocument/documentSymbol".
type DocumentSymbolClientCapabilities struct {
// DynamicRegistration whether document symbol supports dynamic registration.
DynamicRegistration bool `json:"dynamicRegistration,omitempty"`
// SymbolKind specific capabilities for the "SymbolKindCapabilities".
SymbolKind *SymbolKindCapabilities `json:"symbolKind,omitempty"`
// HierarchicalDocumentSymbolSupport is the client support hierarchical document symbols.
HierarchicalDocumentSymbolSupport bool `json:"hierarchicalDocumentSymbolSupport,omitempty"`
// TagSupport is the client supports tags on "SymbolInformation". Tags are supported on
// "DocumentSymbol" if "HierarchicalDocumentSymbolSupport" is set to true.
// Clients supporting tags have to handle unknown tags gracefully.
//
// @since 3.16.0.
TagSupport *DocumentSymbolClientCapabilitiesTagSupport `json:"tagSupport,omitempty"`
// LabelSupport is the client supports an additional label presented in the UI when
// registering a document symbol provider.
//
// @since 3.16.0.
LabelSupport bool `json:"labelSupport,omitempty"`
}
// DocumentSymbolClientCapabilitiesTagSupport TagSupport in the DocumentSymbolClientCapabilities.
//
// @since 3.16.0.
type DocumentSymbolClientCapabilitiesTagSupport struct {
// ValueSet is the tags supported by the client.
ValueSet []SymbolTag `json:"valueSet"`
}
// CodeActionClientCapabilities capabilities specific to the "textDocument/codeAction".
type CodeActionClientCapabilities struct {
// DynamicRegistration whether code action supports dynamic registration.
DynamicRegistration bool `json:"dynamicRegistration,omitempty"`
// CodeActionLiteralSupport is the client support code action literals as a valid
// response of the "textDocument/codeAction" request.
//
// @since 3.8.0
CodeActionLiteralSupport *CodeActionClientCapabilitiesLiteralSupport `json:"codeActionLiteralSupport,omitempty"`
// IsPreferredSupport whether code action supports the "isPreferred" property.
//
// @since 3.15.0.
IsPreferredSupport bool `json:"isPreferredSupport,omitempty"`
// DisabledSupport whether code action supports the `disabled` property.
//
// @since 3.16.0.
DisabledSupport bool `json:"disabledSupport,omitempty"`
// DataSupport whether code action supports the `data` property which is
// preserved between a `textDocument/codeAction` and a
// `codeAction/resolve` request.
//
// @since 3.16.0.
DataSupport bool `json:"dataSupport,omitempty"`
// ResolveSupport whether the client supports resolving additional code action
// properties via a separate `codeAction/resolve` request.
//
// @since 3.16.0.
ResolveSupport *CodeActionClientCapabilitiesResolveSupport `json:"resolveSupport,omitempty"`
// HonorsChangeAnnotations whether the client honors the change annotations in
// text edits and resource operations returned via the
// `CodeAction#edit` property by for example presenting
// the workspace edit in the user interface and asking
// for confirmation.
//
// @since 3.16.0.
HonorsChangeAnnotations bool `json:"honorsChangeAnnotations,omitempty"`
}
// CodeActionClientCapabilitiesLiteralSupport is the client support code action literals as a valid response of the "textDocument/codeAction" request.
type CodeActionClientCapabilitiesLiteralSupport struct {
// CodeActionKind is the code action kind is support with the following value
// set.
CodeActionKind *CodeActionClientCapabilitiesKind `json:"codeActionKind"`
}
// CodeActionClientCapabilitiesKind is the code action kind is support with the following value set.
type CodeActionClientCapabilitiesKind struct {
// ValueSet is the code action kind values the client supports. When this
// property exists the client also guarantees that it will
// handle values outside its set gracefully and falls back
// to a default value when unknown.
ValueSet []CodeActionKind `json:"valueSet"`
}
// CodeActionClientCapabilitiesResolveSupport ResolveSupport in the CodeActionClientCapabilities.
//
// @since 3.16.0.
type CodeActionClientCapabilitiesResolveSupport struct {
// Properties is the properties that a client can resolve lazily.
Properties []string `json:"properties"`
}
// CodeLensClientCapabilities capabilities specific to the "textDocument/codeLens".
type CodeLensClientCapabilities struct {
// DynamicRegistration Whether code lens supports dynamic registration.
DynamicRegistration bool `json:"dynamicRegistration,omitempty"`
}
// DocumentLinkClientCapabilities capabilities specific to the "textDocument/documentLink".
type DocumentLinkClientCapabilities struct {
// DynamicRegistration whether document link supports dynamic registration.
DynamicRegistration bool `json:"dynamicRegistration,omitempty"`
// TooltipSupport whether the client supports the "tooltip" property on "DocumentLink".
//
// @since 3.15.0.
TooltipSupport bool `json:"tooltipSupport,omitempty"`
}
// DocumentColorClientCapabilities capabilities specific to the "textDocument/documentColor" and the
// "textDocument/colorPresentation" request.
//
// @since 3.6.0.
type DocumentColorClientCapabilities struct {
// DynamicRegistration whether colorProvider supports dynamic registration. If this is set to `true`
// the client supports the new "(ColorProviderOptions & TextDocumentRegistrationOptions & StaticRegistrationOptions)"
// return value for the corresponding server capability as well.
DynamicRegistration bool `json:"dynamicRegistration,omitempty"`
}
// DocumentFormattingClientCapabilities capabilities specific to the "textDocument/formatting".
type DocumentFormattingClientCapabilities struct {
// DynamicRegistration whether code lens supports dynamic registration.
DynamicRegistration bool `json:"dynamicRegistration,omitempty"`
}
// DocumentRangeFormattingClientCapabilities capabilities specific to the "textDocument/rangeFormatting".
type DocumentRangeFormattingClientCapabilities struct {
// DynamicRegistration whether code lens supports dynamic registration.
DynamicRegistration bool `json:"dynamicRegistration,omitempty"`
}
// DocumentOnTypeFormattingClientCapabilities capabilities specific to the "textDocument/onTypeFormatting".
type DocumentOnTypeFormattingClientCapabilities struct {
// DynamicRegistration whether code lens supports dynamic registration.
DynamicRegistration bool `json:"dynamicRegistration,omitempty"`
}
// PublishDiagnosticsClientCapabilities capabilities specific to "textDocument/publishDiagnostics".
type PublishDiagnosticsClientCapabilities struct {
// RelatedInformation whether the clients accepts diagnostics with related information.
RelatedInformation bool `json:"relatedInformation,omitempty"`
// TagSupport clients supporting tags have to handle unknown tags gracefully.
//
// @since 3.15.0.
TagSupport *PublishDiagnosticsClientCapabilitiesTagSupport `json:"tagSupport,omitempty"`
// VersionSupport whether the client interprets the version property of the
// "textDocument/publishDiagnostics" notification`s parameter.
//
// @since 3.15.0.
VersionSupport bool `json:"versionSupport,omitempty"`
// CodeDescriptionSupport client supports a codeDescription property
//
// @since 3.16.0.
CodeDescriptionSupport bool `json:"codeDescriptionSupport,omitempty"`
// DataSupport whether code action supports the `data` property which is
// preserved between a `textDocument/publishDiagnostics` and
// `textDocument/codeAction` request.
//
// @since 3.16.0.
DataSupport bool `json:"dataSupport,omitempty"`
}
// PublishDiagnosticsClientCapabilitiesTagSupport is the client capacity of TagSupport.
//
// @since 3.15.0.
type PublishDiagnosticsClientCapabilitiesTagSupport struct {
// ValueSet is the tags supported by the client.
ValueSet []DiagnosticTag `json:"valueSet"`
}
// RenameClientCapabilities capabilities specific to the "textDocument/rename".
type RenameClientCapabilities struct {
// DynamicRegistration whether rename supports dynamic registration.
DynamicRegistration bool `json:"dynamicRegistration,omitempty"`
// PrepareSupport is the client supports testing for validity of rename operations
// before execution.
PrepareSupport bool `json:"prepareSupport,omitempty"`
// PrepareSupportDefaultBehavior client supports the default behavior result
// (`{ defaultBehavior: boolean }`).
//
// The value indicates the default behavior used by the
// client.
//
// @since 3.16.0.
PrepareSupportDefaultBehavior PrepareSupportDefaultBehavior `json:"prepareSupportDefaultBehavior,omitempty"`
// HonorsChangeAnnotations whether th client honors the change annotations in
// text edits and resource operations returned via the
// rename request's workspace edit by for example presenting
// the workspace edit in the user interface and asking
// for confirmation.
//
// @since 3.16.0.
HonorsChangeAnnotations bool `json:"honorsChangeAnnotations,omitempty"`
}
// PrepareSupportDefaultBehavior default behavior of PrepareSupport.
//
// @since 3.16.0.
type PrepareSupportDefaultBehavior float64
// list of PrepareSupportDefaultBehavior.
const (
// PrepareSupportDefaultBehaviorIdentifier is the client's default behavior is to select the identifier
// according the to language's syntax rule.
PrepareSupportDefaultBehaviorIdentifier PrepareSupportDefaultBehavior = 1
)
// String returns a string representation of the PrepareSupportDefaultBehavior.
func (k PrepareSupportDefaultBehavior) String() string {
switch k {
case PrepareSupportDefaultBehaviorIdentifier:
return "Identifier"
default:
return strconv.FormatFloat(float64(k), 'f', -10, 64)
}
}
// FoldingRangeClientCapabilities capabilities specific to "textDocument/foldingRange" requests.
//
// @since 3.10.0.
type FoldingRangeClientCapabilities struct {
// DynamicRegistration whether implementation supports dynamic registration for folding range providers. If this is set to `true`
// the client supports the new "(FoldingRangeProviderOptions & TextDocumentRegistrationOptions & StaticRegistrationOptions)"
// return value for the corresponding server capability as well.
DynamicRegistration bool `json:"dynamicRegistration,omitempty"`
// RangeLimit is the maximum number of folding ranges that the client prefers to receive per document. The value serves as a
// hint, servers are free to follow the limit.
RangeLimit uint32 `json:"rangeLimit,omitempty"`
// LineFoldingOnly if set, the client signals that it only supports folding complete lines. If set, client will
// ignore specified "startCharacter" and "endCharacter" properties in a FoldingRange.
LineFoldingOnly bool `json:"lineFoldingOnly,omitempty"`
}
// SelectionRangeClientCapabilities capabilities specific to "textDocument/selectionRange" requests.
//
// @since 3.16.0.
type SelectionRangeClientCapabilities struct {
// DynamicRegistration whether implementation supports dynamic registration for selection range providers. If this is set to `true`
// the client supports the new "(SelectionRangeProviderOptions & TextDocumentRegistrationOptions & StaticRegistrationOptions)"
// return value for the corresponding server capability as well.
DynamicRegistration bool `json:"dynamicRegistration,omitempty"`
}
// CallHierarchyClientCapabilities capabilities specific to "textDocument/callHierarchy" requests.
//
// @since 3.16.0.
type CallHierarchyClientCapabilities struct {
// DynamicRegistration whether implementation supports dynamic registration. If this is set to
// `true` the client supports the new `(TextDocumentRegistrationOptions &
// StaticRegistrationOptions)` return value for the corresponding server
// capability as well.}
DynamicRegistration bool `json:"dynamicRegistration,omitempty"`
}
// SemanticTokensClientCapabilities capabilities specific to the "textDocument.semanticTokens" request.
//
// @since 3.16.0.
type SemanticTokensClientCapabilities struct {
// DynamicRegistration whether implementation supports dynamic registration. If this is set to
// `true` the client supports the new `(TextDocumentRegistrationOptions &
// StaticRegistrationOptions)` return value for the corresponding server
// capability as well.
DynamicRegistration bool `json:"dynamicRegistration,omitempty"`
// Requests which requests the client supports and might send to the server
// depending on the server's capability. Please note that clients might not
// show semantic tokens or degrade some of the user experience if a range
// or full request is advertised by the client but not provided by the
// server. If for example the client capability `requests.full` and
// `request.range` are both set to true but the server only provides a
// range provider the client might not render a minimap correctly or might
// even decide to not show any semantic tokens at all.
Requests SemanticTokensWorkspaceClientCapabilitiesRequests `json:"requests"`
// TokenTypes is the token types that the client supports.
TokenTypes []string `json:"tokenTypes"`
// TokenModifiers is the token modifiers that the client supports.
TokenModifiers []string `json:"tokenModifiers"`
// Formats is the formats the clients supports.
Formats []TokenFormat `json:"formats"`
// OverlappingTokenSupport whether the client supports tokens that can overlap each other.
OverlappingTokenSupport bool `json:"overlappingTokenSupport,omitempty"`
// MultilineTokenSupport whether the client supports tokens that can span multiple lines.
MultilineTokenSupport bool `json:"multilineTokenSupport,omitempty"`
}
// SemanticTokensWorkspaceClientCapabilitiesRequests capabilities specific to the "textDocument/semanticTokens/xxx" request.
//
// @since 3.16.0.
type SemanticTokensWorkspaceClientCapabilitiesRequests struct {
// Range is the client will send the "textDocument/semanticTokens/range" request
// if the server provides a corresponding handler.
Range bool `json:"range,omitempty"`
// Full is the client will send the "textDocument/semanticTokens/full" request
// if the server provides a corresponding handler. The client will send the
// `textDocument/semanticTokens/full/delta` request if the server provides a
// corresponding handler.
Full any `json:"full,omitempty"`
}
// LinkedEditingRangeClientCapabilities capabilities specific to "textDocument/linkedEditingRange" requests.
//
// @since 3.16.0.
type LinkedEditingRangeClientCapabilities struct {
// DynamicRegistration whether implementation supports dynamic registration.
// If this is set to `true` the client supports the new
// `(TextDocumentRegistrationOptions & StaticRegistrationOptions)`
// return value for the corresponding server capability as well.
DynamicRegistration bool `json:"dynamicRegistration,omitempty"`
}
// MonikerClientCapabilities capabilities specific to the "textDocument/moniker" request.
//
// @since 3.16.0.
type MonikerClientCapabilities struct {
// DynamicRegistration whether implementation supports dynamic registration. If this is set to
// `true` the client supports the new `(TextDocumentRegistrationOptions &
// StaticRegistrationOptions)` return value for the corresponding server
// capability as well.// DynamicRegistration whether implementation supports dynamic registration. If this is set to
DynamicRegistration bool `json:"dynamicRegistration,omitempty"`
}
// WindowClientCapabilities represents a WindowClientCapabilities specific client capabilities.
//
// @since 3.15.0.
type WindowClientCapabilities struct {
// WorkDoneProgress whether client supports handling progress notifications. If set servers are allowed to
// report in "workDoneProgress" property in the request specific server capabilities.
//
// @since 3.15.0.
WorkDoneProgress bool `json:"workDoneProgress,omitempty"`
// ShowMessage capabilities specific to the showMessage request.
//
// @since 3.16.0.
ShowMessage *ShowMessageRequestClientCapabilities `json:"showMessage,omitempty"`
// ShowDocument client capabilities for the show document request.
//
// @since 3.16.0.
ShowDocument *ShowDocumentClientCapabilities `json:"showDocument,omitempty"`
}
// ShowMessageRequestClientCapabilities show message request client capabilities.
//
// @since 3.16.0.
type ShowMessageRequestClientCapabilities struct {
// MessageActionItem capabilities specific to the "MessageActionItem" type.
MessageActionItem *ShowMessageRequestClientCapabilitiesMessageActionItem `json:"messageActionItem,omitempty"`
}
// ShowMessageRequestClientCapabilitiesMessageActionItem capabilities specific to the "MessageActionItem" type.
//
// @since 3.16.0.
type ShowMessageRequestClientCapabilitiesMessageActionItem struct {
// AdditionalPropertiesSupport whether the client supports additional attributes which
// are preserved and sent back to the server in the
// request's response.
AdditionalPropertiesSupport bool `json:"additionalPropertiesSupport,omitempty"`
}
// ShowDocumentClientCapabilities client capabilities for the show document request.
//
// @since 3.16.0.
type ShowDocumentClientCapabilities struct {
// Support is the client has support for the show document
// request.
Support bool `json:"support"`
}
// GeneralClientCapabilities represents a General specific client capabilities.
//
// @since 3.16.0.
type GeneralClientCapabilities struct {
// RegularExpressions is the client capabilities specific to regular expressions.
//
// @since 3.16.0.
RegularExpressions *RegularExpressionsClientCapabilities `json:"regularExpressions,omitempty"`
// Markdown client capabilities specific to the client's markdown parser.
//
// @since 3.16.0.
Markdown *MarkdownClientCapabilities `json:"markdown,omitempty"`
}
// RegularExpressionsClientCapabilities represents a client capabilities specific to regular expressions.
//
// The following features from the ECMAScript 2020 regular expression specification are NOT mandatory for a client:
//
// Assertions
//
// Lookahead assertion, Negative lookahead assertion, lookbehind assertion, negative lookbehind assertion.
//
// Character classes
//
// Matching control characters using caret notation (e.g. "\cX") and matching UTF-16 code units (e.g. "\uhhhh").
//
// Group and ranges
//
// Named capturing groups.
//
// Unicode property escapes
//
// None of the features needs to be supported.
//
// The only regular expression flag that a client needs to support is "i" to specify a case insensitive search.
//
// @since 3.16.0.
type RegularExpressionsClientCapabilities struct {
// Engine is the engine's name.
//
// Well known engine name is "ECMAScript".
// https://tc39.es/ecma262/#sec-regexp-regular-expression-objects
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
Engine string `json:"engine"`
// Version is the engine's version.
//
// Well known engine version is "ES2020".
// https://tc39.es/ecma262/#sec-regexp-regular-expression-objects
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
Version string `json:"version,omitempty"`
}
// MarkdownClientCapabilities represents a client capabilities specific to the used markdown parser.
//
// @since 3.16.0.
type MarkdownClientCapabilities struct {
// Parser is the name of the parser.
Parser string `json:"parser"`
// version is the version of the parser.
Version string `json:"version,omitempty"`
}
// SPDX-FileCopyrightText: 2021 The Go Language Server Authors
// SPDX-License-Identifier: BSD-3-Clause
package protocol
import (
"strconv"
)
// ServerCapabilities efines the capabilities provided by a language server.
type ServerCapabilities struct {
// TextDocumentSync defines how text documents are synced. Is either a detailed structure defining each notification
// or for backwards compatibility the TextDocumentSyncKind number.
//
// If omitted it defaults to TextDocumentSyncKind.None`
TextDocumentSync any `json:"textDocumentSync,omitempty"` // *TextDocumentSyncOptions | TextDocumentSyncKind
// CompletionProvider is The server provides completion support.
CompletionProvider *CompletionOptions `json:"completionProvider,omitempty"`
// HoverProvider is the server provides hover support.
HoverProvider any `json:"hoverProvider,omitempty"` // TODO(zchee): bool | *HoverOptions
// SignatureHelpProvider is the server provides signature help support.
SignatureHelpProvider *SignatureHelpOptions `json:"signatureHelpProvider,omitempty"`
// DeclarationProvider is the server provides Goto Declaration support.
//
// @since 3.14.0.
DeclarationProvider any `json:"declarationProvider,omitempty"` // TODO(zchee): bool | *DeclarationOptions | *DeclarationRegistrationOptions
// DefinitionProvider is the server provides Goto definition support.
DefinitionProvider any `json:"definitionProvider,omitempty"` // TODO(zchee): bool | *DefinitionOptions
// TypeDefinitionProvider is the provides Goto Type Definition support.
//
// @since 3.6.0.
TypeDefinitionProvider any `json:"typeDefinitionProvider,omitempty"` // TODO(zchee): bool | *TypeDefinitionOptions | *TypeDefinitionRegistrationOptions
// ImplementationProvider is the provides Goto Implementation support.
//
// @since 3.6.0.
ImplementationProvider any `json:"implementationProvider,omitempty"` // TODO(zchee): bool | *ImplementationOptions | *ImplementationRegistrationOptions
// ReferencesProvider is the server provides find references support.
ReferencesProvider any `json:"referencesProvider,omitempty"` // TODO(zchee): bool | *ReferenceOptions
// DocumentHighlightProvider is the server provides document highlight support.
DocumentHighlightProvider any `json:"documentHighlightProvider,omitempty"` // TODO(zchee): bool | *DocumentHighlightOptions
// DocumentSymbolProvider is the server provides document symbol support.
DocumentSymbolProvider any `json:"documentSymbolProvider,omitempty"` // TODO(zchee): bool | *DocumentSymbolOptions
// CodeActionProvider is the server provides code actions.
//
// CodeActionOptions may only be specified if the client states that it supports CodeActionLiteralSupport in its
// initial Initialize request.
CodeActionProvider any `json:"codeActionProvider,omitempty"` // TODO(zchee): bool | *CodeActionOptions
// CodeLensProvider is the server provides code lens.
CodeLensProvider *CodeLensOptions `json:"codeLensProvider,omitempty"`
// The server provides document link support.
DocumentLinkProvider *DocumentLinkOptions `json:"documentLinkProvider,omitempty"`
// ColorProvider is the server provides color provider support.
//
// @since 3.6.0.
ColorProvider any `json:"colorProvider,omitempty"` // TODO(zchee): bool | *DocumentColorOptions | *DocumentColorRegistrationOptions
// WorkspaceSymbolProvider is the server provides workspace symbol support.
WorkspaceSymbolProvider any `json:"workspaceSymbolProvider,omitempty"` // TODO(zchee): bool | *WorkspaceSymbolOptions
// DocumentFormattingProvider is the server provides document formatting.
DocumentFormattingProvider any `json:"documentFormattingProvider,omitempty"` // TODO(zchee): bool | *DocumentFormattingOptions
// DocumentRangeFormattingProvider is the server provides document range formatting.
DocumentRangeFormattingProvider any `json:"documentRangeFormattingProvider,omitempty"` // TODO(zchee): bool | *DocumentRangeFormattingOptions
// DocumentOnTypeFormattingProvider is the server provides document formatting on typing.
DocumentOnTypeFormattingProvider *DocumentOnTypeFormattingOptions `json:"documentOnTypeFormattingProvider,omitempty"`
// RenameProvider is the server provides rename support.
//
// RenameOptions may only be specified if the client states that it supports PrepareSupport in its
// initial Initialize request.
RenameProvider any `json:"renameProvider,omitempty"` // TODO(zchee): bool | *RenameOptions
// FoldingRangeProvider is the server provides folding provider support.
//
// @since 3.10.0.
FoldingRangeProvider any `json:"foldingRangeProvider,omitempty"` // TODO(zchee): bool | *FoldingRangeOptions | *FoldingRangeRegistrationOptions
// SelectionRangeProvider is the server provides selection range support.
//
// @since 3.15.0.
SelectionRangeProvider any `json:"selectionRangeProvider,omitempty"` // TODO(zchee): bool | *SelectionRangeOptions | *SelectionRangeRegistrationOptions
// ExecuteCommandProvider is the server provides execute command support.
ExecuteCommandProvider *ExecuteCommandOptions `json:"executeCommandProvider,omitempty"`
// CallHierarchyProvider is the server provides call hierarchy support.
//
// @since 3.16.0.
CallHierarchyProvider any `json:"callHierarchyProvider,omitempty"` // TODO(zchee): bool | *CallHierarchyOptions | *CallHierarchyRegistrationOptions
// LinkedEditingRangeProvider is the server provides linked editing range support.
//
// @since 3.16.0.
LinkedEditingRangeProvider any `json:"linkedEditingRangeProvider,omitempty"` // TODO(zchee): bool | *LinkedEditingRangeOptions | *LinkedEditingRangeRegistrationOptions
// SemanticTokensProvider is the server provides semantic tokens support.
//
// @since 3.16.0.
SemanticTokensProvider any `json:"semanticTokensProvider,omitempty"` // TODO(zchee): *SemanticTokensOptions | *SemanticTokensRegistrationOptions
// Workspace is the window specific server capabilities.
Workspace *ServerCapabilitiesWorkspace `json:"workspace,omitempty"`
// MonikerProvider is the server provides moniker support.
//
// @since 3.16.0.
MonikerProvider any `json:"monikerProvider,omitempty"` // TODO(zchee): bool | *MonikerOptions | *MonikerRegistrationOptions
// Experimental server capabilities.
Experimental any `json:"experimental,omitempty"`
}
// TextDocumentSyncOptions TextDocumentSync options.
type TextDocumentSyncOptions struct {
// OpenClose open and close notifications are sent to the server.
OpenClose bool `json:"openClose,omitempty"`
// Change notifications are sent to the server. See TextDocumentSyncKind.None, TextDocumentSyncKind.Full
// and TextDocumentSyncKind.Incremental. If omitted it defaults to TextDocumentSyncKind.None.
Change TextDocumentSyncKind `json:"change,omitempty"`
// WillSave notifications are sent to the server.
WillSave bool `json:"willSave,omitempty"`
// WillSaveWaitUntil will save wait until requests are sent to the server.
WillSaveWaitUntil bool `json:"willSaveWaitUntil,omitempty"`
// Save notifications are sent to the server.
Save *SaveOptions `json:"save,omitempty"`
}
// SaveOptions save options.
type SaveOptions struct {
// IncludeText is the client is supposed to include the content on save.
IncludeText bool `json:"includeText,omitempty"`
}
// TextDocumentSyncKind defines how the host (editor) should sync document changes to the language server.
type TextDocumentSyncKind float64
const (
// TextDocumentSyncKindNone documents should not be synced at all.
TextDocumentSyncKindNone TextDocumentSyncKind = 0
// TextDocumentSyncKindFull documents are synced by always sending the full content
// of the document.
TextDocumentSyncKindFull TextDocumentSyncKind = 1
// TextDocumentSyncKindIncremental documents are synced by sending the full content on open.
// After that only incremental updates to the document are
// send.
TextDocumentSyncKindIncremental TextDocumentSyncKind = 2
)
// String implements fmt.Stringer.
func (k TextDocumentSyncKind) String() string {
switch k {
case TextDocumentSyncKindNone:
return "None"
case TextDocumentSyncKindFull:
return "Full"
case TextDocumentSyncKindIncremental:
return "Incremental"
default:
return strconv.FormatFloat(float64(k), 'f', -10, 64)
}
}
// CompletionOptions Completion options.
type CompletionOptions struct {
// The server provides support to resolve additional
// information for a completion item.
ResolveProvider bool `json:"resolveProvider,omitempty"`
// The characters that trigger completion automatically.
TriggerCharacters []string `json:"triggerCharacters,omitempty"`
}
// HoverOptions option of hover provider server capabilities.
type HoverOptions struct {
WorkDoneProgressOptions
}
// SignatureHelpOptions SignatureHelp options.
type SignatureHelpOptions struct {
// The characters that trigger signature help
// automatically.
TriggerCharacters []string `json:"triggerCharacters,omitempty"`
// RetriggerCharacters is the slist of characters that re-trigger signature help.
//
// These trigger characters are only active when signature help is already
// showing.
// All trigger characters are also counted as re-trigger characters.
//
// @since 3.15.0.
RetriggerCharacters []string `json:"retriggerCharacters,omitempty"`
}
// DeclarationOptions registration option of Declaration server capability.
//
// @since 3.15.0.
type DeclarationOptions struct {
WorkDoneProgressOptions
}
// DeclarationRegistrationOptions registration option of Declaration server capability.
//
// @since 3.15.0.
type DeclarationRegistrationOptions struct {
DeclarationOptions
TextDocumentRegistrationOptions
StaticRegistrationOptions
}
// DefinitionOptions registration option of Definition server capability.
//
// @since 3.15.0.
type DefinitionOptions struct {
WorkDoneProgressOptions
}
// TypeDefinitionOptions registration option of TypeDefinition server capability.
//
// @since 3.15.0.
type TypeDefinitionOptions struct {
WorkDoneProgressOptions
}
// TypeDefinitionRegistrationOptions registration option of TypeDefinition server capability.
//
// @since 3.15.0.
type TypeDefinitionRegistrationOptions struct {
TextDocumentRegistrationOptions
TypeDefinitionOptions
StaticRegistrationOptions
}
// ImplementationOptions registration option of Implementation server capability.
//
// @since 3.15.0.
type ImplementationOptions struct {
WorkDoneProgressOptions
}
// ImplementationRegistrationOptions registration option of Implementation server capability.
//
// @since 3.15.0.
type ImplementationRegistrationOptions struct {
TextDocumentRegistrationOptions
ImplementationOptions
StaticRegistrationOptions
}
// ReferenceOptions registration option of Reference server capability.
type ReferenceOptions struct {
WorkDoneProgressOptions
}
// DocumentHighlightOptions registration option of DocumentHighlight server capability.
//
// @since 3.15.0.
type DocumentHighlightOptions struct {
WorkDoneProgressOptions
}
// DocumentSymbolOptions registration option of DocumentSymbol server capability.
//
// @since 3.15.0.
type DocumentSymbolOptions struct {
WorkDoneProgressOptions
// Label a human-readable string that is shown when multiple outlines trees
// are shown for the same document.
//
// @since 3.16.0.
Label string `json:"label,omitempty"`
}
// CodeActionOptions CodeAction options.
type CodeActionOptions struct {
// CodeActionKinds that this server may return.
//
// The list of kinds may be generic, such as "CodeActionKind.Refactor", or the server
// may list out every specific kind they provide.
CodeActionKinds []CodeActionKind `json:"codeActionKinds,omitempty"`
// ResolveProvider is the server provides support to resolve additional
// information for a code action.
//
// @since 3.16.0.
ResolveProvider bool `json:"resolveProvider,omitempty"`
}
// CodeLensOptions CodeLens options.
type CodeLensOptions struct {
// Code lens has a resolve provider as well.
ResolveProvider bool `json:"resolveProvider,omitempty"`
}
// DocumentLinkOptions document link options.
type DocumentLinkOptions struct {
// ResolveProvider document links have a resolve provider as well.
ResolveProvider bool `json:"resolveProvider,omitempty"`
}
// DocumentColorOptions registration option of DocumentColor server capability.
//
// @since 3.15.0.
type DocumentColorOptions struct {
WorkDoneProgressOptions
}
// DocumentColorRegistrationOptions registration option of DocumentColor server capability.
//
// @since 3.15.0.
type DocumentColorRegistrationOptions struct {
TextDocumentRegistrationOptions
StaticRegistrationOptions
DocumentColorOptions
}
// WorkspaceSymbolOptions registration option of WorkspaceSymbol server capability.
//
// @since 3.15.0.
type WorkspaceSymbolOptions struct {
WorkDoneProgressOptions
}
// DocumentFormattingOptions registration option of DocumentFormatting server capability.
//
// @since 3.15.0.
type DocumentFormattingOptions struct {
WorkDoneProgressOptions
}
// DocumentRangeFormattingOptions registration option of DocumentRangeFormatting server capability.
//
// @since 3.15.0.
type DocumentRangeFormattingOptions struct {
WorkDoneProgressOptions
}
// DocumentOnTypeFormattingOptions format document on type options.
type DocumentOnTypeFormattingOptions struct {
// FirstTriggerCharacter a character on which formatting should be triggered, like "}".
FirstTriggerCharacter string `json:"firstTriggerCharacter"`
// MoreTriggerCharacter more trigger characters.
MoreTriggerCharacter []string `json:"moreTriggerCharacter,omitempty"`
}
// RenameOptions rename options.
type RenameOptions struct {
// PrepareProvider renames should be checked and tested before being executed.
PrepareProvider bool `json:"prepareProvider,omitempty"`
}
// FoldingRangeOptions registration option of FoldingRange server capability.
//
// @since 3.15.0.
type FoldingRangeOptions struct {
WorkDoneProgressOptions
}
// FoldingRangeRegistrationOptions registration option of FoldingRange server capability.
//
// @since 3.15.0.
type FoldingRangeRegistrationOptions struct {
TextDocumentRegistrationOptions
FoldingRangeOptions
StaticRegistrationOptions
}
// ExecuteCommandOptions execute command options.
type ExecuteCommandOptions struct {
// Commands is the commands to be executed on the server
Commands []string `json:"commands"`
}
// CallHierarchyOptions option of CallHierarchy.
//
// @since 3.16.0.
type CallHierarchyOptions struct {
WorkDoneProgressOptions
}
// CallHierarchyRegistrationOptions registration options of CallHierarchy.
//
// @since 3.16.0.
type CallHierarchyRegistrationOptions struct {
TextDocumentRegistrationOptions
CallHierarchyOptions
StaticRegistrationOptions
}
// LinkedEditingRangeOptions option of linked editing range provider server capabilities.
//
// @since 3.16.0.
type LinkedEditingRangeOptions struct {
WorkDoneProgressOptions
}
// LinkedEditingRangeRegistrationOptions registration option of linked editing range provider server capabilities.
//
// @since 3.16.0.
type LinkedEditingRangeRegistrationOptions struct {
TextDocumentRegistrationOptions
LinkedEditingRangeOptions
StaticRegistrationOptions
}
// SemanticTokensOptions option of semantic tokens provider server capabilities.
//
// @since 3.16.0.
type SemanticTokensOptions struct {
WorkDoneProgressOptions
}
// SemanticTokensRegistrationOptions registration option of semantic tokens provider server capabilities.
//
// @since 3.16.0.
type SemanticTokensRegistrationOptions struct {
TextDocumentRegistrationOptions
SemanticTokensOptions
StaticRegistrationOptions
}
// ServerCapabilitiesWorkspace specific server capabilities.
type ServerCapabilitiesWorkspace struct {
// WorkspaceFolders is the server supports workspace folder.
//
// @since 3.6.0.
WorkspaceFolders *ServerCapabilitiesWorkspaceFolders `json:"workspaceFolders,omitempty"`
// FileOperations is the server is interested in file notifications/requests.
//
// @since 3.16.0.
FileOperations *ServerCapabilitiesWorkspaceFileOperations `json:"fileOperations,omitempty"`
}
// ServerCapabilitiesWorkspaceFolders is the server supports workspace folder.
//
// @since 3.6.0.
type ServerCapabilitiesWorkspaceFolders struct {
// Supported is the server has support for workspace folders
Supported bool `json:"supported,omitempty"`
// ChangeNotifications whether the server wants to receive workspace folder
// change notifications.
//
// If a strings is provided the string is treated as a ID
// under which the notification is registered on the client
// side. The ID can be used to unregister for these events
// using the `client/unregisterCapability` request.
ChangeNotifications any `json:"changeNotifications,omitempty"` // string | boolean
}
// ServerCapabilitiesWorkspaceFileOperations is the server is interested in file notifications/requests.
//
// @since 3.16.0.
type ServerCapabilitiesWorkspaceFileOperations struct {
// DidCreate is the server is interested in receiving didCreateFiles
// notifications.
DidCreate *FileOperationRegistrationOptions `json:"didCreate,omitempty"`
// WillCreate is the server is interested in receiving willCreateFiles requests.
WillCreate *FileOperationRegistrationOptions `json:"willCreate,omitempty"`
// DidRename is the server is interested in receiving didRenameFiles
// notifications.
DidRename *FileOperationRegistrationOptions `json:"didRename,omitempty"`
// WillRename is the server is interested in receiving willRenameFiles requests.
WillRename *FileOperationRegistrationOptions `json:"willRename,omitempty"`
// DidDelete is the server is interested in receiving didDeleteFiles file
// notifications.
DidDelete *FileOperationRegistrationOptions `json:"didDelete,omitempty"`
// WillDelete is the server is interested in receiving willDeleteFiles file
// requests.
WillDelete *FileOperationRegistrationOptions `json:"willDelete,omitempty"`
}
// FileOperationRegistrationOptions is the options to register for file operations.
//
// @since 3.16.0.
type FileOperationRegistrationOptions struct {
// filters is the actual filters.
Filters []FileOperationFilter `json:"filters"`
}
// MonikerOptions option of moniker provider server capabilities.
//
// @since 3.16.0.
type MonikerOptions struct {
WorkDoneProgressOptions
}
// MonikerRegistrationOptions registration option of moniker provider server capabilities.
//
// @since 3.16.0.
type MonikerRegistrationOptions struct {
TextDocumentRegistrationOptions
MonikerOptions
}
// SPDX-FileCopyrightText: 2019 The Go Language Server Authors
// SPDX-License-Identifier: BSD-3-Clause
package protocol
import (
"bytes"
"context"
"fmt"
"log/slog"
"encoding/json"
"github.com/a-h/templ/lsp/jsonrpc2"
"github.com/a-h/templ/lsp/xcontext"
)
// ClientDispatcher returns a Client that dispatches LSP requests across the
// given jsonrpc2 connection.
func ClientDispatcher(conn jsonrpc2.Conn, logger *slog.Logger) Client {
return &client{
Conn: conn,
logger: logger,
}
}
// ClientHandler handler of LSP client.
func ClientHandler(log *slog.Logger, client Client, handler jsonrpc2.Handler) jsonrpc2.Handler {
h := func(ctx context.Context, reply jsonrpc2.Replier, req jsonrpc2.Request) error {
if ctx.Err() != nil {
xctx := xcontext.Detach(ctx)
return reply(xctx, nil, ErrRequestCancelled)
}
handled, err := clientDispatch(ctx, log, client, reply, req)
if handled || err != nil {
return err
}
return handler(ctx, reply, req)
}
return h
}
// clientDispatch implements jsonrpc2.Handler.
//
//nolint:funlen,cyclop
func clientDispatch(ctx context.Context, log *slog.Logger, client Client, reply jsonrpc2.Replier, req jsonrpc2.Request) (handled bool, err error) {
if ctx.Err() != nil {
return true, reply(ctx, nil, ErrRequestCancelled)
}
dec := json.NewDecoder(bytes.NewReader(req.Params()))
switch req.Method() {
case MethodProgress: // notification
defer log.Debug(MethodProgress, slog.Any("error", err))
var params ProgressParams
if err := dec.Decode(¶ms); err != nil {
return true, replyParseError(ctx, reply, err)
}
err := client.Progress(ctx, ¶ms)
return true, reply(ctx, nil, err)
case MethodWorkDoneProgressCreate: // request
defer log.Debug(MethodWorkDoneProgressCreate, slog.Any("error", err))
var params WorkDoneProgressCreateParams
if err := dec.Decode(¶ms); err != nil {
return true, replyParseError(ctx, reply, err)
}
err := client.WorkDoneProgressCreate(ctx, ¶ms)
return true, reply(ctx, nil, err)
case MethodWindowLogMessage: // notification
defer log.Debug(MethodWindowLogMessage, slog.Any("error", err))
var params LogMessageParams
if err := dec.Decode(¶ms); err != nil {
return true, replyParseError(ctx, reply, err)
}
err := client.LogMessage(ctx, ¶ms)
return true, reply(ctx, nil, err)
case MethodTextDocumentPublishDiagnostics: // notification
defer log.Debug(MethodTextDocumentPublishDiagnostics, slog.Any("error", err))
var params PublishDiagnosticsParams
if err := dec.Decode(¶ms); err != nil {
return true, replyParseError(ctx, reply, err)
}
err := client.PublishDiagnostics(ctx, ¶ms)
return true, reply(ctx, nil, err)
case MethodWindowShowMessage: // notification
defer log.Debug(MethodWindowShowMessage, slog.Any("error", err))
var params ShowMessageParams
if err := dec.Decode(¶ms); err != nil {
return true, replyParseError(ctx, reply, err)
}
err := client.ShowMessage(ctx, ¶ms)
return true, reply(ctx, nil, err)
case MethodWindowShowMessageRequest: // request
defer log.Debug(MethodWindowShowMessageRequest, slog.Any("error", err))
var params ShowMessageRequestParams
if err := dec.Decode(¶ms); err != nil {
return true, replyParseError(ctx, reply, err)
}
resp, err := client.ShowMessageRequest(ctx, ¶ms)
return true, reply(ctx, resp, err)
case MethodTelemetryEvent: // notification
defer log.Debug(MethodTelemetryEvent, slog.Any("error", err))
var params any
if err := dec.Decode(¶ms); err != nil {
return true, replyParseError(ctx, reply, err)
}
err := client.Telemetry(ctx, ¶ms)
return true, reply(ctx, nil, err)
case MethodClientRegisterCapability: // request
defer log.Debug(MethodClientRegisterCapability, slog.Any("error", err))
var params RegistrationParams
if err := dec.Decode(¶ms); err != nil {
return true, replyParseError(ctx, reply, err)
}
err := client.RegisterCapability(ctx, ¶ms)
return true, reply(ctx, nil, err)
case MethodClientUnregisterCapability: // request
defer log.Debug(MethodClientUnregisterCapability, slog.Any("error", err))
var params UnregistrationParams
if err := dec.Decode(¶ms); err != nil {
return true, replyParseError(ctx, reply, err)
}
err := client.UnregisterCapability(ctx, ¶ms)
return true, reply(ctx, nil, err)
case MethodWorkspaceApplyEdit: // request
defer log.Debug(MethodWorkspaceApplyEdit, slog.Any("error", err))
var params ApplyWorkspaceEditParams
if err := dec.Decode(¶ms); err != nil {
return true, replyParseError(ctx, reply, err)
}
resp, err := client.ApplyEdit(ctx, ¶ms)
return true, reply(ctx, resp, err)
case MethodWorkspaceConfiguration: // request
defer log.Debug(MethodWorkspaceConfiguration, slog.Any("error", err))
var params ConfigurationParams
if err := dec.Decode(¶ms); err != nil {
return true, replyParseError(ctx, reply, err)
}
resp, err := client.Configuration(ctx, ¶ms)
return true, reply(ctx, resp, err)
case MethodWorkspaceWorkspaceFolders: // request
defer log.Debug(MethodWorkspaceWorkspaceFolders, slog.Any("error", err))
if len(req.Params()) > 0 {
return true, reply(ctx, nil, fmt.Errorf("expected no params: %w", jsonrpc2.ErrInvalidParams))
}
resp, err := client.WorkspaceFolders(ctx)
return true, reply(ctx, resp, err)
default:
return false, nil
}
}
// Client represents a Language Server Protocol client.
type Client interface {
Progress(ctx context.Context, params *ProgressParams) (err error)
WorkDoneProgressCreate(ctx context.Context, params *WorkDoneProgressCreateParams) (err error)
LogMessage(ctx context.Context, params *LogMessageParams) (err error)
PublishDiagnostics(ctx context.Context, params *PublishDiagnosticsParams) (err error)
ShowMessage(ctx context.Context, params *ShowMessageParams) (err error)
ShowMessageRequest(ctx context.Context, params *ShowMessageRequestParams) (result *MessageActionItem, err error)
Telemetry(ctx context.Context, params any) (err error)
RegisterCapability(ctx context.Context, params *RegistrationParams) (err error)
UnregisterCapability(ctx context.Context, params *UnregistrationParams) (err error)
ApplyEdit(ctx context.Context, params *ApplyWorkspaceEditParams) (result *ApplyWorkspaceEditResponse, err error)
Configuration(ctx context.Context, params *ConfigurationParams) (result []any, err error)
WorkspaceFolders(ctx context.Context) (result []WorkspaceFolder, err error)
}
// list of client methods.
const (
// MethodProgress method name of "$/progress".
MethodProgress = "$/progress"
// MethodWorkDoneProgressCreate method name of "window/workDoneProgress/create".
MethodWorkDoneProgressCreate = "window/workDoneProgress/create"
// MethodWindowShowMessage method name of "window/showMessage".
MethodWindowShowMessage = "window/showMessage"
// MethodWindowShowMessageRequest method name of "window/showMessageRequest.
MethodWindowShowMessageRequest = "window/showMessageRequest"
// MethodWindowLogMessage method name of "window/logMessage.
MethodWindowLogMessage = "window/logMessage"
// MethodTelemetryEvent method name of "telemetry/event.
MethodTelemetryEvent = "telemetry/event"
// MethodClientRegisterCapability method name of "client/registerCapability.
MethodClientRegisterCapability = "client/registerCapability"
// MethodClientUnregisterCapability method name of "client/unregisterCapability.
MethodClientUnregisterCapability = "client/unregisterCapability"
// MethodTextDocumentPublishDiagnostics method name of "textDocument/publishDiagnostics.
MethodTextDocumentPublishDiagnostics = "textDocument/publishDiagnostics"
// MethodWorkspaceApplyEdit method name of "workspace/applyEdit.
MethodWorkspaceApplyEdit = "workspace/applyEdit"
// MethodWorkspaceConfiguration method name of "workspace/configuration.
MethodWorkspaceConfiguration = "workspace/configuration"
// MethodWorkspaceWorkspaceFolders method name of "workspace/workspaceFolders".
MethodWorkspaceWorkspaceFolders = "workspace/workspaceFolders"
)
// client implements a Language Server Protocol client.
type client struct {
jsonrpc2.Conn
logger *slog.Logger
}
// compiler time check whether the Client implements ClientInterface interface.
var _ Client = (*client)(nil)
// Progress is the base protocol offers also support to report progress in a generic fashion.
//
// This mechanism can be used to report any kind of progress including work done progress (usually used to report progress in the user interface using a progress bar) and
// partial result progress to support streaming of results.
//
// @since 3.16.0.
func (c *client) Progress(ctx context.Context, params *ProgressParams) (err error) {
c.logger.Debug("call " + MethodProgress)
defer c.logger.Debug("end "+MethodProgress, slog.Any("error", err))
return c.Conn.Notify(ctx, MethodProgress, params)
}
// WorkDoneProgressCreate sends the request is sent from the server to the client to ask the client to create a work done progress.
//
// @since 3.16.0.
func (c *client) WorkDoneProgressCreate(ctx context.Context, params *WorkDoneProgressCreateParams) (err error) {
c.logger.Debug("call " + MethodWorkDoneProgressCreate)
defer c.logger.Debug("end "+MethodWorkDoneProgressCreate, slog.Any("error", err))
return Call(ctx, c.Conn, MethodWorkDoneProgressCreate, params, nil)
}
// LogMessage sends the notification from the server to the client to ask the client to log a particular message.
func (c *client) LogMessage(ctx context.Context, params *LogMessageParams) (err error) {
c.logger.Debug("call " + MethodWindowLogMessage)
defer c.logger.Debug("end "+MethodWindowLogMessage, slog.Any("error", err))
return c.Conn.Notify(ctx, MethodWindowLogMessage, params)
}
// PublishDiagnostics sends the notification from the server to the client to signal results of validation runs.
//
// Diagnostics are “owned” by the server so it is the server’s responsibility to clear them if necessary. The following rule is used for VS Code servers that generate diagnostics:
//
// - if a language is single file only (for example HTML) then diagnostics are cleared by the server when the file is closed.
// - if a language has a project system (for example C#) diagnostics are not cleared when a file closes. When a project is opened all diagnostics for all files are recomputed (or read from a cache).
//
// When a file changes it is the server’s responsibility to re-compute diagnostics and push them to the client.
// If the computed set is empty it has to push the empty array to clear former diagnostics.
// Newly pushed diagnostics always replace previously pushed diagnostics. There is no merging that happens on the client side.
func (c *client) PublishDiagnostics(ctx context.Context, params *PublishDiagnosticsParams) (err error) {
c.logger.Debug("call " + MethodTextDocumentPublishDiagnostics)
defer c.logger.Debug("end "+MethodTextDocumentPublishDiagnostics, slog.Any("error", err))
return c.Conn.Notify(ctx, MethodTextDocumentPublishDiagnostics, params)
}
// ShowMessage sends the notification from a server to a client to ask the
// client to display a particular message in the user interface.
func (c *client) ShowMessage(ctx context.Context, params *ShowMessageParams) (err error) {
return c.Conn.Notify(ctx, MethodWindowShowMessage, params)
}
// ShowMessageRequest sends the request from a server to a client to ask the client to display a particular message in the user interface.
//
// In addition to the show message notification the request allows to pass actions and to wait for an answer from the client.
func (c *client) ShowMessageRequest(ctx context.Context, params *ShowMessageRequestParams) (_ *MessageActionItem, err error) {
c.logger.Debug("call " + MethodWindowShowMessageRequest)
defer c.logger.Debug("end "+MethodWindowShowMessageRequest, slog.Any("error", err))
var result *MessageActionItem
if err := Call(ctx, c.Conn, MethodWindowShowMessageRequest, params, &result); err != nil {
return nil, err
}
return result, nil
}
// Telemetry sends the notification from the server to the client to ask the client to log a telemetry event.
func (c *client) Telemetry(ctx context.Context, params any) (err error) {
c.logger.Debug("call " + MethodTelemetryEvent)
defer c.logger.Debug("end "+MethodTelemetryEvent, slog.Any("error", err))
return c.Conn.Notify(ctx, MethodTelemetryEvent, params)
}
// RegisterCapability sends the request from the server to the client to register for a new capability on the client side.
//
// Not all clients need to support dynamic capability registration.
//
// A client opts in via the dynamicRegistration property on the specific client capabilities.
// A client can even provide dynamic registration for capability A but not for capability B (see TextDocumentClientCapabilities as an example).
func (c *client) RegisterCapability(ctx context.Context, params *RegistrationParams) (err error) {
c.logger.Debug("call " + MethodClientRegisterCapability)
defer c.logger.Debug("end "+MethodClientRegisterCapability, slog.Any("error", err))
return Call(ctx, c.Conn, MethodClientRegisterCapability, params, nil)
}
// UnregisterCapability sends the request from the server to the client to unregister a previously registered capability.
func (c *client) UnregisterCapability(ctx context.Context, params *UnregistrationParams) (err error) {
c.logger.Debug("call " + MethodClientUnregisterCapability)
defer c.logger.Debug("end "+MethodClientUnregisterCapability, slog.Any("error", err))
return Call(ctx, c.Conn, MethodClientUnregisterCapability, params, nil)
}
// ApplyEdit sends the request from the server to the client to modify resource on the client side.
func (c *client) ApplyEdit(ctx context.Context, params *ApplyWorkspaceEditParams) (result *ApplyWorkspaceEditResponse, err error) {
c.logger.Debug("call " + MethodWorkspaceApplyEdit)
defer c.logger.Debug("end "+MethodWorkspaceApplyEdit, slog.Any("error", err))
if err := Call(ctx, c.Conn, MethodWorkspaceApplyEdit, params, &result); err != nil {
return nil, err
}
return result, nil
}
// Configuration sends the request from the server to the client to fetch configuration settings from the client.
//
// The request can fetch several configuration settings in one roundtrip.
// The order of the returned configuration settings correspond to the order of the
// passed ConfigurationItems (e.g. the first item in the response is the result for the first configuration item in the params).
func (c *client) Configuration(ctx context.Context, params *ConfigurationParams) (_ []any, err error) {
c.logger.Debug("call " + MethodWorkspaceConfiguration)
defer c.logger.Debug("end "+MethodWorkspaceConfiguration, slog.Any("error", err))
var result []any
if err := Call(ctx, c.Conn, MethodWorkspaceConfiguration, params, &result); err != nil {
return nil, err
}
return result, nil
}
// WorkspaceFolders sends the request from the server to the client to fetch the current open list of workspace folders.
//
// Returns null in the response if only a single file is open in the tool. Returns an empty array if a workspace is open but no folders are configured.
//
// @since 3.6.0.
func (c *client) WorkspaceFolders(ctx context.Context) (result []WorkspaceFolder, err error) {
c.logger.Debug("call " + MethodWorkspaceWorkspaceFolders)
defer c.logger.Debug("end "+MethodWorkspaceWorkspaceFolders, slog.Any("error", err))
if err := Call(ctx, c.Conn, MethodWorkspaceWorkspaceFolders, nil, &result); err != nil {
return nil, err
}
return result, nil
}
// SPDX-FileCopyrightText: 2020 The Go Language Server Authors
// SPDX-License-Identifier: BSD-3-Clause
package protocol
import (
"context"
)
type ctxClientKey int
var ctxClient ctxClientKey = 0
// WithClient returns the context with Client value.
func WithClient(ctx context.Context, client Client) context.Context {
return context.WithValue(ctx, ctxClient, client)
}
// ClientFromContext extracts Client from context.
func ClientFromContext(ctx context.Context) Client {
client, ok := ctx.Value(ctxClient).(Client)
if !ok {
return nil
}
return client
}
// SPDX-FileCopyrightText: 2019 The Go Language Server Authors
// SPDX-License-Identifier: BSD-3-Clause
package protocol
import (
"strconv"
)
// Diagnostic represents a diagnostic, such as a compiler error or warning.
//
// Diagnostic objects are only valid in the scope of a resource.
type Diagnostic struct {
// Range is the range at which the message applies.
Range Range `json:"range"`
// Severity is the diagnostic's severity. Can be omitted. If omitted it is up to the
// client to interpret diagnostics as error, warning, info or hint.
Severity DiagnosticSeverity `json:"severity,omitempty"`
// Code is the diagnostic's code, which might appear in the user interface.
Code any `json:"code,omitempty"` // int32 | string;
// CodeDescription an optional property to describe the error code.
//
// @since 3.16.0.
CodeDescription *CodeDescription `json:"codeDescription,omitempty"`
// Source a human-readable string describing the source of this
// diagnostic, e.g. 'typescript' or 'super lint'.
Source string `json:"source,omitempty"`
// Message is the diagnostic's message.
Message string `json:"message"`
// Tags is the additional metadata about the diagnostic.
//
// @since 3.15.0.
Tags []DiagnosticTag `json:"tags,omitempty"`
// RelatedInformation an array of related diagnostic information, e.g. when symbol-names within
// a scope collide all definitions can be marked via this property.
RelatedInformation []DiagnosticRelatedInformation `json:"relatedInformation,omitempty"`
// Data is a data entry field that is preserved between a
// "textDocument/publishDiagnostics" notification and
// "textDocument/codeAction" request.
//
// @since 3.16.0.
Data any `json:"data,omitempty"`
}
// DiagnosticSeverity indicates the severity of a Diagnostic message.
type DiagnosticSeverity float64
const (
// DiagnosticSeverityError reports an error.
DiagnosticSeverityError DiagnosticSeverity = 1
// DiagnosticSeverityWarning reports a warning.
DiagnosticSeverityWarning DiagnosticSeverity = 2
// DiagnosticSeverityInformation reports an information.
DiagnosticSeverityInformation DiagnosticSeverity = 3
// DiagnosticSeverityHint reports a hint.
DiagnosticSeverityHint DiagnosticSeverity = 4
)
// String implements fmt.Stringer.
func (d DiagnosticSeverity) String() string {
switch d {
case DiagnosticSeverityError:
return "Error"
case DiagnosticSeverityWarning:
return "Warning"
case DiagnosticSeverityInformation:
return "Information"
case DiagnosticSeverityHint:
return "Hint"
default:
return strconv.FormatFloat(float64(d), 'f', -10, 64)
}
}
// CodeDescription is the structure to capture a description for an error code.
//
// @since 3.16.0.
type CodeDescription struct {
// Href an URI to open with more information about the diagnostic error.
Href URI `json:"href"`
}
// DiagnosticTag is the diagnostic tags.
//
// @since 3.15.0.
type DiagnosticTag float64
// list of DiagnosticTag.
const (
// DiagnosticTagUnnecessary unused or unnecessary code.
//
// Clients are allowed to render diagnostics with this tag faded out instead of having
// an error squiggle.
DiagnosticTagUnnecessary DiagnosticTag = 1
// DiagnosticTagDeprecated deprecated or obsolete code.
//
// Clients are allowed to rendered diagnostics with this tag strike through.
DiagnosticTagDeprecated DiagnosticTag = 2
)
// String implements fmt.Stringer.
func (d DiagnosticTag) String() string {
switch d {
case DiagnosticTagUnnecessary:
return "Unnecessary"
case DiagnosticTagDeprecated:
return "Deprecated"
default:
return strconv.FormatFloat(float64(d), 'f', -10, 64)
}
}
// DiagnosticRelatedInformation represents a related message and source code location for a diagnostic.
//
// This should be used to point to code locations that cause or related to a diagnostics, e.g when duplicating
// a symbol in a scope.
type DiagnosticRelatedInformation struct {
// Location is the location of this related diagnostic information.
Location Location `json:"location"`
// Message is the message of this related diagnostic information.
Message string `json:"message"`
}
// PublishDiagnosticsParams represents a params of PublishDiagnostics notification.
type PublishDiagnosticsParams struct {
// URI is the URI for which diagnostic information is reported.
URI DocumentURI `json:"uri"`
// Version optional the version number of the document the diagnostics are published for.
//
// @since 3.15
Version uint32 `json:"version,omitempty"`
// Diagnostics an array of diagnostic information items.
Diagnostics []Diagnostic `json:"diagnostics"`
}
// SPDX-FileCopyrightText: 2021 The Go Language Server Authors
// SPDX-License-Identifier: BSD-3-Clause
package protocol
import (
"context"
"fmt"
"encoding/json"
"github.com/a-h/templ/lsp/jsonrpc2"
"github.com/a-h/templ/lsp/xcontext"
)
// CancelHandler handler of cancelling.
func CancelHandler(handler jsonrpc2.Handler) jsonrpc2.Handler {
handler, canceller := jsonrpc2.CancelHandler(handler)
h := func(ctx context.Context, reply jsonrpc2.Replier, req jsonrpc2.Request) error {
if req.Method() != MethodCancelRequest {
// TODO(iancottrell): See if we can generate a reply for the request to be cancelled
// at the point of cancellation rather than waiting for gopls to naturally reply.
// To do that, we need to keep track of whether a reply has been sent already and
// be careful about racing between the two paths.
// TODO(iancottrell): Add a test that watches the stream and verifies the response
// for the cancelled request flows.
reply := func(ctx context.Context, resp any, err error) error {
// https://microsoft.github.io/language-server-protocol/specifications/specification-current/#cancelRequest
if ctx.Err() != nil && err == nil {
err = ErrRequestCancelled
}
ctx = xcontext.Detach(ctx)
return reply(ctx, resp, err)
}
return handler(ctx, reply, req)
}
var params CancelParams
if err := json.Unmarshal(req.Params(), ¶ms); err != nil {
return replyParseError(ctx, reply, err)
}
switch id := params.ID.(type) {
case int32:
canceller(jsonrpc2.NewNumberID(id))
case string:
canceller(jsonrpc2.NewStringID(id))
default:
return replyParseError(ctx, reply, fmt.Errorf("request ID %v malformed", id))
}
return reply(ctx, nil, nil)
}
return h
}
// Handlers default jsonrpc2.Handler.
func Handlers(handler jsonrpc2.Handler) jsonrpc2.Handler {
return CancelHandler(
jsonrpc2.AsyncHandler(
jsonrpc2.ReplyHandler(handler),
),
)
}
// Call calls method to params and result.
func Call(ctx context.Context, conn jsonrpc2.Conn, method string, params, result any) error {
id, err := conn.Call(ctx, method, params, result)
if ctx.Err() != nil {
notifyCancel(ctx, conn, id)
}
return err
}
func notifyCancel(ctx context.Context, conn jsonrpc2.Conn, id jsonrpc2.ID) {
ctx = xcontext.Detach(ctx)
// Note that only *jsonrpc2.ID implements json.Marshaler.
_ = conn.Notify(ctx, MethodCancelRequest, &CancelParams{ID: &id})
}
func replyParseError(ctx context.Context, reply jsonrpc2.Replier, err error) error {
return reply(ctx, nil, fmt.Errorf("%s: %w", jsonrpc2.ErrParse, err))
}
// SPDX-FileCopyrightText: 2019 The Go Language Server Authors
// SPDX-License-Identifier: BSD-3-Clause
package protocol
import (
"strconv"
"encoding/json"
)
// CompletionParams params of Completion request.
type CompletionParams struct {
TextDocumentPositionParams
WorkDoneProgressParams
PartialResultParams
// Context is the completion context. This is only available if the client specifies
// to send this using `ClientCapabilities.textDocument.completion.contextSupport === true`
Context *CompletionContext `json:"context,omitempty"`
}
// CompletionTriggerKind how a completion was triggered.
type CompletionTriggerKind float64
const (
// CompletionTriggerKindInvoked completion was triggered by typing an identifier (24x7 code
// complete), manual invocation (e.g Ctrl+Space) or via API.
CompletionTriggerKindInvoked CompletionTriggerKind = 1
// CompletionTriggerKindTriggerCharacter completion was triggered by a trigger character specified by
// the `triggerCharacters` properties of the `CompletionRegistrationOptions`.
CompletionTriggerKindTriggerCharacter CompletionTriggerKind = 2
// CompletionTriggerKindTriggerForIncompleteCompletions completion was re-triggered as the current completion list is incomplete.
CompletionTriggerKindTriggerForIncompleteCompletions CompletionTriggerKind = 3
)
// String implements fmt.Stringer.
func (k CompletionTriggerKind) String() string {
switch k {
case CompletionTriggerKindInvoked:
return "Invoked"
case CompletionTriggerKindTriggerCharacter:
return "TriggerCharacter"
case CompletionTriggerKindTriggerForIncompleteCompletions:
return "TriggerForIncompleteCompletions"
default:
return strconv.FormatFloat(float64(k), 'f', -10, 64)
}
}
// CompletionContext contains additional information about the context in which a completion request is triggered.
type CompletionContext struct {
// TriggerCharacter is the trigger character (a single character) that has trigger code complete.
// Is undefined if `triggerKind !== CompletionTriggerKind.TriggerCharacter`
TriggerCharacter string `json:"triggerCharacter,omitempty"`
// TriggerKind how the completion was triggered.
TriggerKind CompletionTriggerKind `json:"triggerKind"`
}
// CompletionList represents a collection of [completion items](#CompletionItem) to be presented
// in the editor.
type CompletionList struct {
// IsIncomplete this list it not complete. Further typing should result in recomputing
// this list.
IsIncomplete bool `json:"isIncomplete"`
// Items is the completion items.
Items []CompletionItem `json:"items"`
}
// InsertTextFormat defines whether the insert text in a completion item should be interpreted as
// plain text or a snippet.
type InsertTextFormat float64
const (
// InsertTextFormatPlainText is the primary text to be inserted is treated as a plain string.
InsertTextFormatPlainText InsertTextFormat = 1
// InsertTextFormatSnippet is the primary text to be inserted is treated as a snippet.
//
// A snippet can define tab stops and placeholders with `$1`, `$2`
// and `${3:foo}`. `$0` defines the final tab stop, it defaults to
// the end of the snippet. Placeholders with equal identifiers are linked,
// that is typing in one will update others too.
InsertTextFormatSnippet InsertTextFormat = 2
)
// String implements fmt.Stringer.
func (tf InsertTextFormat) String() string {
switch tf {
case InsertTextFormatPlainText:
return "PlainText"
case InsertTextFormatSnippet:
return "Snippet"
default:
return strconv.FormatFloat(float64(tf), 'f', -10, 64)
}
}
// InsertReplaceEdit is a special text edit to provide an insert and a replace operation.
//
// @since 3.16.0.
type InsertReplaceEdit struct {
// NewText is the string to be inserted.
NewText string `json:"newText"`
// Insert is the range if the insert is requested.
Insert Range `json:"insert"`
// Replace is the range if the replace is requested.
Replace Range `json:"replace"`
}
// InsertTextMode how whitespace and indentation is handled during completion
// item insertion.
//
// @since 3.16.0.
type InsertTextMode float64
const (
// AsIs is the insertion or replace strings is taken as it is. If the
// value is multi line the lines below the cursor will be
// inserted using the indentation defined in the string value.
// The client will not apply any kind of adjustments to the
// string.
InsertTextModeAsIs InsertTextMode = 1
// AdjustIndentation is the editor adjusts leading whitespace of new lines so that
// they match the indentation up to the cursor of the line for
// which the item is accepted.
//
// Consider a line like this: <2tabs><cursor><3tabs>foo. Accepting a
// multi line completion item is indented using 2 tabs and all
// following lines inserted will be indented using 2 tabs as well.
InsertTextModeAdjustIndentation InsertTextMode = 2
)
// String returns a string representation of the InsertTextMode.
func (k InsertTextMode) String() string {
switch k {
case InsertTextModeAsIs:
return "AsIs"
case InsertTextModeAdjustIndentation:
return "AdjustIndentation"
default:
return strconv.FormatFloat(float64(k), 'f', -10, 64)
}
}
// CompletionItem item of CompletionList.
type CompletionItem struct {
// AdditionalTextEdits an optional array of additional text edits that are applied when
// selecting this completion. Edits must not overlap (including the same insert position)
// with the main edit nor with themselves.
//
// Additional text edits should be used to change text unrelated to the current cursor position
// (for example adding an import statement at the top of the file if the completion item will
// insert an unqualified type).
AdditionalTextEdits []TextEdit `json:"additionalTextEdits,omitempty"`
// Command an optional command that is executed *after* inserting this completion. *Note* that
// additional modifications to the current document should be described with the
// additionalTextEdits-property.
Command *Command `json:"command,omitempty"`
// CommitCharacters an optional set of characters that when pressed while this completion is active will accept it first and
// then type that character. *Note* that all commit characters should have `length=1` and that superfluous
// characters will be ignored.
CommitCharacters []string `json:"commitCharacters,omitempty"`
// Tags is the tag for this completion item.
//
// @since 3.15.0.
Tags []CompletionItemTag `json:"tags,omitempty"`
// Data an data entry field that is preserved on a completion item between
// a completion and a completion resolve request.
Data any `json:"data,omitempty"`
// Deprecated indicates if this item is deprecated.
Deprecated bool `json:"deprecated,omitempty"`
// Detail a human-readable string with additional information
// about this item, like type or symbol information.
Detail string `json:"detail,omitempty"`
// Documentation a human-readable string that represents a doc-comment.
Documentation any `json:"documentation,omitempty"`
// FilterText a string that should be used when filtering a set of
// completion items. When `falsy` the label is used.
FilterText string `json:"filterText,omitempty"`
// InsertText a string that should be inserted into a document when selecting
// this completion. When `falsy` the label is used.
//
// The `insertText` is subject to interpretation by the client side.
// Some tools might not take the string literally. For example
// VS Code when code complete is requested in this example `con<cursor position>`
// and a completion item with an `insertText` of `console` is provided it
// will only insert `sole`. Therefore it is recommended to use `textEdit` instead
// since it avoids additional client side interpretation.
InsertText string `json:"insertText,omitempty"`
// InsertTextFormat is the format of the insert text. The format applies to both the `insertText` property
// and the `newText` property of a provided `textEdit`.
InsertTextFormat InsertTextFormat `json:"insertTextFormat,omitempty"`
// InsertTextMode how whitespace and indentation is handled during completion
// item insertion. If not provided the client's default value depends on
// the `textDocument.completion.insertTextMode` client capability.
//
// @since 3.16.0.
InsertTextMode InsertTextMode `json:"insertTextMode,omitempty"`
// Kind is the kind of this completion item. Based of the kind
// an icon is chosen by the editor.
Kind CompletionItemKind `json:"kind,omitempty"`
// Label is the label of this completion item. By default
// also the text that is inserted when selecting
// this completion.
Label string `json:"label"`
// Preselect select this item when showing.
//
// *Note* that only one completion item can be selected and that the
// tool / client decides which item that is. The rule is that the *first*
// item of those that match best is selected.
Preselect bool `json:"preselect,omitempty"`
// SortText a string that should be used when comparing this item
// with other items. When `falsy` the label is used.
SortText string `json:"sortText,omitempty"`
// TextEdit an edit which is applied to a document when selecting this completion. When an edit is provided the value of
// `insertText` is ignored.
//
// NOTE: The range of the edit must be a single line range and it must contain the position at which completion
// has been requested.
//
// Most editors support two different operations when accepting a completion
// item. One is to insert a completion text and the other is to replace an
// existing text with a completion text. Since this can usually not be
// predetermined by a server it can report both ranges. Clients need to
// signal support for `InsertReplaceEdits` via the
// "textDocument.completion.insertReplaceSupport" client capability
// property.
//
// NOTE 1: The text edit's range as well as both ranges from an insert
// replace edit must be a [single line] and they must contain the position
// at which completion has been requested.
//
// NOTE 2: If an "InsertReplaceEdit" is returned the edit's insert range
// must be a prefix of the edit's replace range, that means it must be
// contained and starting at the same position.
//
// @since 3.16.0 additional type "InsertReplaceEdit".
TextEdit *TextEditOrInsertReplaceEdit `json:"textEdit,omitempty"` // *TextEdit | *InsertReplaceEdit
}
type TextEditOrInsertReplaceEdit struct {
TextEdit *TextEdit
InsertReplaceEdit *InsertReplaceEdit
}
func (t *TextEditOrInsertReplaceEdit) MarshalJSON() ([]byte, error) {
if t.TextEdit != nil {
return json.Marshal(t.TextEdit)
}
return json.Marshal(t.InsertReplaceEdit)
}
type textEditAndInsertReplaceEdit struct {
// NewText is in both types.
NewText string `json:"newText"`
// Range is only present in TextEdit.
Range *Range `json:"range"`
// Insert is only present in InsertReplaceEdit.
Insert Range `json:"insert"`
// Replace is only present in InsertReplaceEdit.
Replace Range `json:"replace"`
}
func (t *TextEditOrInsertReplaceEdit) UnmarshalJSON(data []byte) error {
var teaire textEditAndInsertReplaceEdit
if err := json.Unmarshal(data, &teaire); err != nil {
return err
}
if teaire.Range != nil {
t.TextEdit = &TextEdit{
NewText: teaire.NewText,
Range: *teaire.Range,
}
return nil
}
t.InsertReplaceEdit = &InsertReplaceEdit{
NewText: teaire.NewText,
Insert: teaire.Insert,
Replace: teaire.Replace,
}
return nil
}
// CompletionItemKind is the completion item kind values the client supports. When this
// property exists the client also guarantees that it will
// handle values outside its set gracefully and falls back
// to a default value when unknown.
//
// If this property is not present the client only supports
// the completion items kinds from `Text` to `Reference` as defined in
// the initial version of the protocol.
type CompletionItemKind float64
const (
// CompletionItemKindText text completion kind.
CompletionItemKindText CompletionItemKind = 1
// CompletionItemKindMethod method completion kind.
CompletionItemKindMethod CompletionItemKind = 2
// CompletionItemKindFunction function completion kind.
CompletionItemKindFunction CompletionItemKind = 3
// CompletionItemKindConstructor constructor completion kind.
CompletionItemKindConstructor CompletionItemKind = 4
// CompletionItemKindField field completion kind.
CompletionItemKindField CompletionItemKind = 5
// CompletionItemKindVariable variable completion kind.
CompletionItemKindVariable CompletionItemKind = 6
// CompletionItemKindClass class completion kind.
CompletionItemKindClass CompletionItemKind = 7
// CompletionItemKindInterface interface completion kind.
CompletionItemKindInterface CompletionItemKind = 8
// CompletionItemKindModule module completion kind.
CompletionItemKindModule CompletionItemKind = 9
// CompletionItemKindProperty property completion kind.
CompletionItemKindProperty CompletionItemKind = 10
// CompletionItemKindUnit unit completion kind.
CompletionItemKindUnit CompletionItemKind = 11
// CompletionItemKindValue value completion kind.
CompletionItemKindValue CompletionItemKind = 12
// CompletionItemKindEnum enum completion kind.
CompletionItemKindEnum CompletionItemKind = 13
// CompletionItemKindKeyword keyword completion kind.
CompletionItemKindKeyword CompletionItemKind = 14
// CompletionItemKindSnippet snippet completion kind.
CompletionItemKindSnippet CompletionItemKind = 15
// CompletionItemKindColor color completion kind.
CompletionItemKindColor CompletionItemKind = 16
// CompletionItemKindFile file completion kind.
CompletionItemKindFile CompletionItemKind = 17
// CompletionItemKindReference reference completion kind.
CompletionItemKindReference CompletionItemKind = 18
// CompletionItemKindFolder folder completion kind.
CompletionItemKindFolder CompletionItemKind = 19
// CompletionItemKindEnumMember enum member completion kind.
CompletionItemKindEnumMember CompletionItemKind = 20
// CompletionItemKindConstant constant completion kind.
CompletionItemKindConstant CompletionItemKind = 21
// CompletionItemKindStruct struct completion kind.
CompletionItemKindStruct CompletionItemKind = 22
// CompletionItemKindEvent event completion kind.
CompletionItemKindEvent CompletionItemKind = 23
// CompletionItemKindOperator operator completion kind.
CompletionItemKindOperator CompletionItemKind = 24
// CompletionItemKindTypeParameter type parameter completion kind.
CompletionItemKindTypeParameter CompletionItemKind = 25
)
// String implements fmt.Stringer.
//
//nolint:cyclop
func (k CompletionItemKind) String() string {
switch k {
case CompletionItemKindText:
return "Text"
case CompletionItemKindMethod:
return "Method"
case CompletionItemKindFunction:
return "Function"
case CompletionItemKindConstructor:
return "Constructor"
case CompletionItemKindField:
return "Field"
case CompletionItemKindVariable:
return "Variable"
case CompletionItemKindClass:
return "Class"
case CompletionItemKindInterface:
return "Interface"
case CompletionItemKindModule:
return "Module"
case CompletionItemKindProperty:
return "Property"
case CompletionItemKindUnit:
return "Unit"
case CompletionItemKindValue:
return "Value"
case CompletionItemKindEnum:
return "Enum"
case CompletionItemKindKeyword:
return "Keyword"
case CompletionItemKindSnippet:
return "Snippet"
case CompletionItemKindColor:
return "Color"
case CompletionItemKindFile:
return "File"
case CompletionItemKindReference:
return "Reference"
case CompletionItemKindFolder:
return "Folder"
case CompletionItemKindEnumMember:
return "EnumMember"
case CompletionItemKindConstant:
return "Constant"
case CompletionItemKindStruct:
return "Struct"
case CompletionItemKindEvent:
return "Event"
case CompletionItemKindOperator:
return "Operator"
case CompletionItemKindTypeParameter:
return "TypeParameter"
default:
return strconv.FormatFloat(float64(k), 'f', -10, 64)
}
}
// CompletionItemTag completion item tags are extra annotations that tweak the rendering of a completion
// item.
//
// @since 3.15.0.
type CompletionItemTag float64
// list of CompletionItemTag.
const (
// CompletionItemTagDeprecated is the render a completion as obsolete, usually using a strike-out.
CompletionItemTagDeprecated CompletionItemTag = 1
)
// String returns a string representation of the type.
func (c CompletionItemTag) String() string {
switch c {
case CompletionItemTagDeprecated:
return "Deprecated"
default:
return strconv.FormatFloat(float64(c), 'f', -10, 64)
}
}
// CompletionRegistrationOptions CompletionRegistration options.
type CompletionRegistrationOptions struct {
TextDocumentRegistrationOptions
// TriggerCharacters most tools trigger completion request automatically without explicitly requesting
// it using a keyboard shortcut (e.g. Ctrl+Space). Typically they do so when the user
// starts to type an identifier. For example if the user types `c` in a JavaScript file
// code complete will automatically pop up present `console` besides others as a
// completion item. Characters that make up identifiers don't need to be listed here.
//
// If code complete should automatically be trigger on characters not being valid inside
// an identifier (for example `.` in JavaScript) list them in `triggerCharacters`.
TriggerCharacters []string `json:"triggerCharacters,omitempty"`
// ResolveProvider is the server provides support to resolve additional
// information for a completion item.
ResolveProvider bool `json:"resolveProvider,omitempty"`
}
// HoverParams params of Hover request.
//
// @since 3.15.0.
type HoverParams struct {
TextDocumentPositionParams
WorkDoneProgressParams
}
// Hover is the result of a hover request.
type Hover struct {
// Contents is the hover's content
Contents MarkupContent `json:"contents"`
// Range an optional range is a range inside a text document
// that is used to visualize a hover, e.g. by changing the background color.
Range *Range `json:"range,omitempty"`
}
// SignatureHelpParams params of SignatureHelp request.
//
// @since 3.15.0.
type SignatureHelpParams struct {
TextDocumentPositionParams
WorkDoneProgressParams
// context is the signature help context.
//
// This is only available if the client specifies to send this using the
// client capability `textDocument.signatureHelp.contextSupport === true`.
//
// @since 3.15.0.
Context *SignatureHelpContext `json:"context,omitempty"`
}
// SignatureHelpTriggerKind is the how a signature help was triggered.
//
// @since 3.15.0.
type SignatureHelpTriggerKind float64
// list of SignatureHelpTriggerKind.
const (
// SignatureHelpTriggerKindInvoked is the signature help was invoked manually by the user or by a command.
SignatureHelpTriggerKindInvoked SignatureHelpTriggerKind = 1
// SignatureHelpTriggerKindTriggerCharacter is the signature help was triggered by a trigger character.
SignatureHelpTriggerKindTriggerCharacter SignatureHelpTriggerKind = 2
// SignatureHelpTriggerKindContentChange is the signature help was triggered by the cursor moving or
// by the document content changing.
SignatureHelpTriggerKindContentChange SignatureHelpTriggerKind = 3
)
// String returns a string representation of the type.
func (s SignatureHelpTriggerKind) String() string {
switch s {
case SignatureHelpTriggerKindInvoked:
return "Invoked"
case SignatureHelpTriggerKindTriggerCharacter:
return "TriggerCharacter"
case SignatureHelpTriggerKindContentChange:
return "ContentChange"
default:
return strconv.FormatFloat(float64(s), 'f', -10, 64)
}
}
// SignatureHelpContext is the additional information about the context in which a
// signature help request was triggered.
//
// @since 3.15.0.
type SignatureHelpContext struct {
// TriggerKind is the action that caused signature help to be triggered.
TriggerKind SignatureHelpTriggerKind `json:"triggerKind"`
// Character that caused signature help to be triggered.
//
// This is undefined when
// TriggerKind != SignatureHelpTriggerKindTriggerCharacter
TriggerCharacter string `json:"triggerCharacter,omitempty"`
// IsRetrigger is the `true` if signature help was already showing when it was triggered.
//
// Retriggers occur when the signature help is already active and can be
// caused by actions such as typing a trigger character, a cursor move,
// or document content changes.
IsRetrigger bool `json:"isRetrigger"`
// ActiveSignatureHelp is the currently active SignatureHelp.
//
// The `activeSignatureHelp` has its `SignatureHelp.activeSignature` field
// updated based on the user navigating through available signatures.
ActiveSignatureHelp *SignatureHelp `json:"activeSignatureHelp,omitempty"`
}
// SignatureHelp signature help represents the signature of something
// callable. There can be multiple signature but only one
// active and only one active parameter.
type SignatureHelp struct {
// Signatures one or more signatures.
Signatures []SignatureInformation `json:"signatures"`
// ActiveParameter is the active parameter of the active signature. If omitted or the value
// lies outside the range of `signatures[activeSignature].parameters`
// defaults to 0 if the active signature has parameters. If
// the active signature has no parameters it is ignored.
// In future version of the protocol this property might become
// mandatory to better express the active parameter if the
// active signature does have any.
ActiveParameter uint32 `json:"activeParameter,omitempty"`
// ActiveSignature is the active signature. If omitted or the value lies outside the
// range of `signatures` the value defaults to zero or is ignored if
// `signatures.length === 0`. Whenever possible implementors should
// make an active decision about the active signature and shouldn't
// rely on a default value.
// In future version of the protocol this property might become
// mandatory to better express this.
ActiveSignature uint32 `json:"activeSignature,omitempty"`
}
// SignatureInformation is the client supports the following `SignatureInformation`
// specific properties.
type SignatureInformation struct {
// Label is the label of this signature. Will be shown in
// the UI.
//
// @since 3.16.0.
Label string `json:"label"`
// Documentation is the human-readable doc-comment of this signature. Will be shown
// in the UI but can be omitted.
//
// @since 3.16.0.
Documentation any `json:"documentation,omitempty"` // string | *MarkupContent
// Parameters is the parameters of this signature.
//
// @since 3.16.0.
Parameters []ParameterInformation `json:"parameters,omitempty"`
// ActiveParameterSupport is the client supports the `activeParameter` property on
// `SignatureInformation` literal.
//
// @since 3.16.0.
ActiveParameter uint32 `json:"activeParameter,omitempty"`
}
// ParameterInformation represents a parameter of a callable-signature. A parameter can
// have a label and a doc-comment.
type ParameterInformation struct {
// Label is the label of this parameter information.
//
// Either a string or an inclusive start and exclusive end offsets within its containing
// signature label. (see SignatureInformation.label). The offsets are based on a UTF-16
// string representation as "Position" and "Range" does.
//
// *Note*: a label of type string should be a substring of its containing signature label.
// Its intended use case is to highlight the parameter label part in the "SignatureInformation.label".
Label string `json:"label"` // string | [uint32, uint32]
// Documentation is the human-readable doc-comment of this parameter. Will be shown
// in the UI but can be omitted.
Documentation any `json:"documentation,omitempty"` // string | MarkupContent
}
// SignatureHelpRegistrationOptions SignatureHelp Registration options.
type SignatureHelpRegistrationOptions struct {
TextDocumentRegistrationOptions
// TriggerCharacters is the characters that trigger signature help
// automatically.
TriggerCharacters []string `json:"triggerCharacters,omitempty"`
}
// ReferenceParams params of References request.
//
// @since 3.15.0.
type ReferenceParams struct {
TextDocumentPositionParams
WorkDoneProgressParams
PartialResultParams
// Context is the ReferenceParams context.
Context ReferenceContext `json:"context"`
}
// ReferenceContext context of ReferenceParams.
type ReferenceContext struct {
// IncludeDeclaration include the declaration of the current symbol.
IncludeDeclaration bool `json:"includeDeclaration"`
}
// DocumentHighlight a document highlight is a range inside a text document which deserves
// special attention. Usually a document highlight is visualized by changing
// the background color of its range.
type DocumentHighlight struct {
// Range is the range this highlight applies to.
Range Range `json:"range"`
// Kind is the highlight kind, default is DocumentHighlightKind.Text.
Kind DocumentHighlightKind `json:"kind,omitempty"`
}
// DocumentHighlightKind a document highlight kind.
type DocumentHighlightKind float64
const (
// DocumentHighlightKindText a textual occurrence.
DocumentHighlightKindText DocumentHighlightKind = 1
// DocumentHighlightKindRead read-access of a symbol, like reading a variable.
DocumentHighlightKindRead DocumentHighlightKind = 2
// DocumentHighlightKindWrite write-access of a symbol, like writing to a variable.
DocumentHighlightKindWrite DocumentHighlightKind = 3
)
// String implements fmt.Stringer.
func (k DocumentHighlightKind) String() string {
switch k {
case DocumentHighlightKindText:
return "Text"
case DocumentHighlightKindRead:
return "Read"
case DocumentHighlightKindWrite:
return "Write"
default:
return strconv.FormatFloat(float64(k), 'f', -10, 64)
}
}
// DocumentSymbolParams params of Document Symbols request.
type DocumentSymbolParams struct {
WorkDoneProgressParams
PartialResultParams
// TextDocument is the text document.
TextDocument TextDocumentIdentifier `json:"textDocument"`
}
// SymbolKind specific capabilities for the `SymbolKind`.
// The symbol kind values the client supports. When this
// property exists the client also guarantees that it will
// handle values outside its set gracefully and falls back
// to a default value when unknown.
//
// If this property is not present the client only supports
// the symbol kinds from `File` to `Array` as defined in
// the initial version of the protocol.
type SymbolKind float64
const (
// SymbolKindFile symbol of file.
SymbolKindFile SymbolKind = 1
// SymbolKindModule symbol of module.
SymbolKindModule SymbolKind = 2
// SymbolKindNamespace symbol of namespace.
SymbolKindNamespace SymbolKind = 3
// SymbolKindPackage symbol of package.
SymbolKindPackage SymbolKind = 4
// SymbolKindClass symbol of class.
SymbolKindClass SymbolKind = 5
// SymbolKindMethod symbol of method.
SymbolKindMethod SymbolKind = 6
// SymbolKindProperty symbol of property.
SymbolKindProperty SymbolKind = 7
// SymbolKindField symbol of field.
SymbolKindField SymbolKind = 8
// SymbolKindConstructor symbol of constructor.
SymbolKindConstructor SymbolKind = 9
// SymbolKindEnum symbol of enum.
SymbolKindEnum SymbolKind = 10
// SymbolKindInterface symbol of interface.
SymbolKindInterface SymbolKind = 11
// SymbolKindFunction symbol of function.
SymbolKindFunction SymbolKind = 12
// SymbolKindVariable symbol of variable.
SymbolKindVariable SymbolKind = 13
// SymbolKindConstant symbol of constant.
SymbolKindConstant SymbolKind = 14
// SymbolKindString symbol of string.
SymbolKindString SymbolKind = 15
// SymbolKindNumber symbol of number.
SymbolKindNumber SymbolKind = 16
// SymbolKindBoolean symbol of boolean.
SymbolKindBoolean SymbolKind = 17
// SymbolKindArray symbol of array.
SymbolKindArray SymbolKind = 18
// SymbolKindObject symbol of object.
SymbolKindObject SymbolKind = 19
// SymbolKindKey symbol of key.
SymbolKindKey SymbolKind = 20
// SymbolKindNull symbol of null.
SymbolKindNull SymbolKind = 21
// SymbolKindEnumMember symbol of enum member.
SymbolKindEnumMember SymbolKind = 22
// SymbolKindStruct symbol of struct.
SymbolKindStruct SymbolKind = 23
// SymbolKindEvent symbol of event.
SymbolKindEvent SymbolKind = 24
// SymbolKindOperator symbol of operator.
SymbolKindOperator SymbolKind = 25
// SymbolKindTypeParameter symbol of type parameter.
SymbolKindTypeParameter SymbolKind = 26
)
// String implements fmt.Stringer.
//
//nolint:cyclop
func (k SymbolKind) String() string {
switch k {
case SymbolKindFile:
return "File"
case SymbolKindModule:
return "Module"
case SymbolKindNamespace:
return "Namespace"
case SymbolKindPackage:
return "Package"
case SymbolKindClass:
return "Class"
case SymbolKindMethod:
return "Method"
case SymbolKindProperty:
return "Property"
case SymbolKindField:
return "Field"
case SymbolKindConstructor:
return "Constructor"
case SymbolKindEnum:
return "Enum"
case SymbolKindInterface:
return "Interface"
case SymbolKindFunction:
return "Function"
case SymbolKindVariable:
return "Variable"
case SymbolKindConstant:
return "Constant"
case SymbolKindString:
return "String"
case SymbolKindNumber:
return "Number"
case SymbolKindBoolean:
return "Boolean"
case SymbolKindArray:
return "Array"
case SymbolKindObject:
return "Object"
case SymbolKindKey:
return "Key"
case SymbolKindNull:
return "Null"
case SymbolKindEnumMember:
return "EnumMember"
case SymbolKindStruct:
return "Struct"
case SymbolKindEvent:
return "Event"
case SymbolKindOperator:
return "Operator"
case SymbolKindTypeParameter:
return "TypeParameter"
default:
return strconv.FormatFloat(float64(k), 'f', -10, 64)
}
}
// SymbolTag symbol tags are extra annotations that tweak the rendering of a symbol.
//
// @since 3.16.0.
type SymbolTag float64
// list of SymbolTag.
const (
// SymbolTagDeprecated render a symbol as obsolete, usually using a strike-out.
SymbolTagDeprecated SymbolTag = 1
)
// String returns a string representation of the SymbolTag.
func (k SymbolTag) String() string {
switch k {
case SymbolTagDeprecated:
return "Deprecated"
default:
return strconv.FormatFloat(float64(k), 'f', -10, 64)
}
}
// DocumentSymbol represents programming constructs like variables, classes, interfaces etc. that appear in a document. Document symbols can be
// hierarchical and they have two ranges: one that encloses its definition and one that points to its most interesting range,
// e.g. the range of an identifier.
type DocumentSymbol struct {
// Name is the name of this symbol. Will be displayed in the user interface and therefore must not be
// an empty string or a string only consisting of white spaces.
Name string `json:"name"`
// Detail is the more detail for this symbol, e.g the signature of a function.
Detail string `json:"detail,omitempty"`
// Kind is the kind of this symbol.
Kind SymbolKind `json:"kind"`
// Tags for this document symbol.
//
// @since 3.16.0.
Tags []SymbolTag `json:"tags,omitempty"`
// Deprecated indicates if this symbol is deprecated.
Deprecated bool `json:"deprecated,omitempty"`
// Range is the range enclosing this symbol not including leading/trailing whitespace but everything else
// like comments. This information is typically used to determine if the clients cursor is
// inside the symbol to reveal in the symbol in the UI.
Range Range `json:"range"`
// SelectionRange is the range that should be selected and revealed when this symbol is being picked, e.g the name of a function.
// Must be contained by the `range`.
SelectionRange Range `json:"selectionRange"`
// Children children of this symbol, e.g. properties of a class.
Children []DocumentSymbol `json:"children,omitempty"`
}
// SymbolInformation represents information about programming constructs like variables, classes,
// interfaces etc.
type SymbolInformation struct {
// Name is the name of this symbol.
Name string `json:"name"`
// Kind is the kind of this symbol.
Kind SymbolKind `json:"kind"`
// Tags for this completion item.
//
// @since 3.16.0.
Tags []SymbolTag `json:"tags,omitempty"`
// Deprecated indicates if this symbol is deprecated.
Deprecated bool `json:"deprecated,omitempty"`
// Location is the location of this symbol. The location's range is used by a tool
// to reveal the location in the editor. If the symbol is selected in the
// tool the range's start information is used to position the cursor. So
// the range usually spans more then the actual symbol's name and does
// normally include things like visibility modifiers.
//
// The range doesn't have to denote a node range in the sense of a abstract
// syntax tree. It can therefore not be used to re-construct a hierarchy of
// the symbols.
Location Location `json:"location"`
// ContainerName is the name of the symbol containing this symbol. This information is for
// user interface purposes (e.g. to render a qualifier in the user interface
// if necessary). It can't be used to re-infer a hierarchy for the document
// symbols.
ContainerName string `json:"containerName,omitempty"`
}
type SymbolInformationOrDocumentSymbol struct {
SymbolInformation *SymbolInformation
DocumentSymbol *DocumentSymbol
}
func (s *SymbolInformationOrDocumentSymbol) MarshalJSON() ([]byte, error) {
if s.SymbolInformation != nil {
return json.Marshal(s.SymbolInformation)
}
return json.Marshal(s.DocumentSymbol)
}
func (s *SymbolInformationOrDocumentSymbol) UnmarshalJSON(data []byte) error {
var si SymbolInformation
if err := json.Unmarshal(data, &si); err != nil {
return err
}
s.SymbolInformation = &si
var ds DocumentSymbol
err := json.Unmarshal(data, &ds)
if err != nil {
return err
}
s.DocumentSymbol = &ds
// Only SymbolInformation has a location URI.
if s.SymbolInformation.Location.URI == "" {
s.SymbolInformation = nil
} else {
s.DocumentSymbol = nil
}
return nil
}
// CodeActionParams params for the CodeActionRequest.
type CodeActionParams struct {
WorkDoneProgressParams
PartialResultParams
// TextDocument is the document in which the command was invoked.
TextDocument TextDocumentIdentifier `json:"textDocument"`
// Context carrying additional information.
Context CodeActionContext `json:"context"`
// Range is the range for which the command was invoked.
Range Range `json:"range"`
}
// CodeActionKind is the code action kind values the client supports. When this
// property exists the client also guarantees that it will
// handle values outside its set gracefully and falls back
// to a default value when unknown.
type CodeActionKind string
// A set of predefined code action kinds.
const (
// QuickFix base kind for quickfix actions: 'quickfix'.
QuickFix CodeActionKind = "quickfix"
// Refactor base kind for refactoring actions: 'refactor'.
Refactor CodeActionKind = "refactor"
// RefactorExtract base kind for refactoring extraction actions: 'refactor.extract'
//
// Example extract actions:
//
// - Extract method
// - Extract function
// - Extract variable
// - Extract interface from class
// - ...
RefactorExtract CodeActionKind = "refactor.extract"
// RefactorInline base kind for refactoring inline actions: 'refactor.inline'
//
// Example inline actions:
//
// - Inline function
// - Inline variable
// - Inline constant
// - ...
RefactorInline CodeActionKind = "refactor.inline"
// RefactorRewrite base kind for refactoring rewrite actions: 'refactor.rewrite'
//
// Example rewrite actions:
//
// - Convert JavaScript function to class
// - Add or remove parameter
// - Encapsulate field
// - Make method static
// - Move method to base class
// - ...
RefactorRewrite CodeActionKind = "refactor.rewrite"
// Source base kind for source actions: `source`
//
// Source code actions apply to the entire file.
Source CodeActionKind = "source"
// SourceOrganizeImports base kind for an organize imports source action: `source.organizeImports`.
SourceOrganizeImports CodeActionKind = "source.organizeImports"
)
// CodeActionContext contains additional diagnostic information about the context in which
// a code action is run.
type CodeActionContext struct {
// Diagnostics is an array of diagnostics.
Diagnostics []Diagnostic `json:"diagnostics"`
// Only requested kind of actions to return.
//
// Actions not of this kind are filtered out by the client before being shown. So servers
// can omit computing them.
Only []CodeActionKind `json:"only,omitempty"`
}
// CodeAction capabilities specific to the `textDocument/codeAction`.
type CodeAction struct {
// Title is a short, human-readable, title for this code action.
Title string `json:"title"`
// Kind is the kind of the code action.
//
// Used to filter code actions.
Kind CodeActionKind `json:"kind,omitempty"`
// Diagnostics is the diagnostics that this code action resolves.
Diagnostics []Diagnostic `json:"diagnostics,omitempty"`
// IsPreferred marks this as a preferred action. Preferred actions are used by the `auto fix` command and can be targeted
// by keybindings.
//
// A quick fix should be marked preferred if it properly addresses the underlying error.
// A refactoring should be marked preferred if it is the most reasonable choice of actions to take.
//
// @since 3.15.0.
IsPreferred bool `json:"isPreferred,omitempty"`
// Disabled marks that the code action cannot currently be applied.
//
// Clients should follow the following guidelines regarding disabled code
// actions:
//
// - Disabled code actions are not shown in automatic lightbulbs code
// action menus.
//
// - Disabled actions are shown as faded out in the code action menu when
// the user request a more specific type of code action, such as
// refactorings.
//
// - If the user has a keybinding that auto applies a code action and only
// a disabled code actions are returned, the client should show the user
// an error message with `reason` in the editor.
//
// @since 3.16.0.
Disabled *CodeActionDisable `json:"disabled,omitempty"`
// Edit is the workspace edit this code action performs.
Edit *WorkspaceEdit `json:"edit,omitempty"`
// Command is a command this code action executes. If a code action
// provides an edit and a command, first the edit is
// executed and then the command.
Command *Command `json:"command,omitempty"`
// Data is a data entry field that is preserved on a code action between
// a "textDocument/codeAction" and a "codeAction/resolve" request.
//
// @since 3.16.0.
Data any `json:"data,omitempty"`
}
// CodeActionDisable Disable in CodeAction.
//
// @since 3.16.0.
type CodeActionDisable struct {
// Reason human readable description of why the code action is currently
// disabled.
//
// This is displayed in the code actions UI.
Reason string `json:"reason"`
}
// CodeActionRegistrationOptions CodeAction Registrationi options.
type CodeActionRegistrationOptions struct {
TextDocumentRegistrationOptions
CodeActionOptions
}
// CodeLensParams params of Code Lens request.
type CodeLensParams struct {
WorkDoneProgressParams
PartialResultParams
// TextDocument is the document to request code lens for.
TextDocument TextDocumentIdentifier `json:"textDocument"`
}
// CodeLens is a code lens represents a command that should be shown along with
// source text, like the number of references, a way to run tests, etc.
//
// A code lens is _unresolved_ when no command is associated to it. For performance
// reasons the creation of a code lens and resolving should be done in two stages.
type CodeLens struct {
// Range is the range in which this code lens is valid. Should only span a single line.
Range Range `json:"range"`
// Command is the command this code lens represents.
Command *Command `json:"command,omitempty"`
// Data is a data entry field that is preserved on a code lens item between
// a code lens and a code lens resolve request.
Data any `json:"data,omitempty"`
}
// CodeLensRegistrationOptions CodeLens Registration options.
type CodeLensRegistrationOptions struct {
TextDocumentRegistrationOptions
// ResolveProvider code lens has a resolve provider as well.
ResolveProvider bool `json:"resolveProvider,omitempty"`
}
// DocumentLinkParams params of Document Link request.
type DocumentLinkParams struct {
WorkDoneProgressParams
PartialResultParams
// TextDocument is the document to provide document links for.
TextDocument TextDocumentIdentifier `json:"textDocument"`
}
// DocumentLink is a document link is a range in a text document that links to an internal or external resource, like another
// text document or a web site.
type DocumentLink struct {
// Range is the range this link applies to.
Range Range `json:"range"`
// Target is the uri this link points to. If missing a resolve request is sent later.
Target DocumentURI `json:"target,omitempty"`
// Tooltip is the tooltip text when you hover over this link.
//
// If a tooltip is provided, is will be displayed in a string that includes instructions on how to
// trigger the link, such as `{0} (ctrl + click)`. The specific instructions vary depending on OS,
// user settings, and localization.
//
// @since 3.15.0.
Tooltip string `json:"tooltip,omitempty"`
// Data is a data entry field that is preserved on a document link between a
// DocumentLinkRequest and a DocumentLinkResolveRequest.
Data any `json:"data,omitempty"`
}
// DocumentColorParams params of Document Color request.
type DocumentColorParams struct {
WorkDoneProgressParams
PartialResultParams
// TextDocument is the document to format.
TextDocument TextDocumentIdentifier `json:"textDocument"`
}
// ColorInformation response of Document Color request.
type ColorInformation struct {
// Range is the range in the document where this color appears.
Range Range `json:"range"`
// Color is the actual color value for this color range.
Color Color `json:"color"`
}
// Color represents a color in RGBA space.
type Color struct {
// Alpha is the alpha component of this color in the range [0-1].
Alpha float64 `json:"alpha"`
// Blue is the blue component of this color in the range [0-1].
Blue float64 `json:"blue"`
// Green is the green component of this color in the range [0-1].
Green float64 `json:"green"`
// Red is the red component of this color in the range [0-1].
Red float64 `json:"red"`
}
// ColorPresentationParams params of Color Presentation request.
type ColorPresentationParams struct {
WorkDoneProgressParams
PartialResultParams
// TextDocument is the text document.
TextDocument TextDocumentIdentifier `json:"textDocument"`
// Color is the color information to request presentations for.
Color Color `json:"color"`
// Range is the range where the color would be inserted. Serves as a context.
Range Range `json:"range"`
}
// ColorPresentation response of Color Presentation request.
type ColorPresentation struct {
// Label is the label of this color presentation. It will be shown on the color
// picker header. By default this is also the text that is inserted when selecting
// this color presentation.
Label string `json:"label"`
// TextEdit an edit which is applied to a document when selecting
// this presentation for the color. When `falsy` the label is used.
TextEdit *TextEdit `json:"textEdit,omitempty"`
// AdditionalTextEdits an optional array of additional [text edits](#TextEdit) that are applied when
// selecting this color presentation. Edits must not overlap with the main [edit](#ColorPresentation.textEdit) nor with themselves.
AdditionalTextEdits []TextEdit `json:"additionalTextEdits,omitempty"`
}
// DocumentFormattingParams params of Document Formatting request.
type DocumentFormattingParams struct {
WorkDoneProgressParams
// Options is the format options.
Options FormattingOptions `json:"options"`
// TextDocument is the document to format.
TextDocument TextDocumentIdentifier `json:"textDocument"`
}
// FormattingOptions value-object describing what options formatting should use.
type FormattingOptions struct {
// InsertSpaces prefer spaces over tabs.
InsertSpaces bool `json:"insertSpaces"`
// TabSize size of a tab in spaces.
TabSize uint32 `json:"tabSize"`
// TrimTrailingWhitespace trim trailing whitespaces on a line.
//
// @since 3.15.0.
TrimTrailingWhitespace bool `json:"trimTrailingWhitespace,omitempty"`
// InsertFinalNewlines insert a newline character at the end of the file if one does not exist.
//
// @since 3.15.0.
InsertFinalNewline bool `json:"insertFinalNewline,omitempty"`
// TrimFinalNewlines trim all newlines after the final newline at the end of the file.
//
// @since 3.15.0.
TrimFinalNewlines bool `json:"trimFinalNewlines,omitempty"`
// Key is the signature for further properties.
Key map[string]any `json:"key,omitempty"` // bool | int32 | string
}
// DocumentRangeFormattingParams params of Document Range Formatting request.
type DocumentRangeFormattingParams struct {
WorkDoneProgressParams
// TextDocument is the document to format.
TextDocument TextDocumentIdentifier `json:"textDocument"`
// Range is the range to format
Range Range `json:"range"`
// Options is the format options.
Options FormattingOptions `json:"options"`
}
// DocumentOnTypeFormattingParams params of Document on Type Formatting request.
type DocumentOnTypeFormattingParams struct {
// TextDocument is the document to format.
TextDocument TextDocumentIdentifier `json:"textDocument"`
// Position is the position at which this request was sent.
Position Position `json:"position"`
// Ch is the character that has been typed.
Ch string `json:"ch"`
// Options is the format options.
Options FormattingOptions `json:"options"`
}
// DocumentOnTypeFormattingRegistrationOptions DocumentOnTypeFormatting Registration options.
type DocumentOnTypeFormattingRegistrationOptions struct {
TextDocumentRegistrationOptions
// FirstTriggerCharacter a character on which formatting should be triggered, like `}`.
FirstTriggerCharacter string `json:"firstTriggerCharacter"`
// MoreTriggerCharacter a More trigger characters.
MoreTriggerCharacter []string `json:"moreTriggerCharacter"`
}
// RenameParams params of Rename request.
type RenameParams struct {
TextDocumentPositionParams
PartialResultParams
// NewName is the new name of the symbol. If the given name is not valid the
// request must return a [ResponseError](#ResponseError) with an
// appropriate message set.
NewName string `json:"newName"`
}
// RenameRegistrationOptions Rename Registration options.
type RenameRegistrationOptions struct {
TextDocumentRegistrationOptions
// PrepareProvider is the renames should be checked and tested for validity before being executed.
PrepareProvider bool `json:"prepareProvider,omitempty"`
}
// PrepareRenameParams params of PrepareRenameParams request.
//
// @since 3.15.0.
type PrepareRenameParams struct {
TextDocumentPositionParams
}
// FoldingRangeParams params of Folding Range request.
type FoldingRangeParams struct {
TextDocumentPositionParams
PartialResultParams
}
// FoldingRangeKind is the enum of known range kinds.
type FoldingRangeKind string
const (
// CommentFoldingRange is the folding range for a comment.
CommentFoldingRange FoldingRangeKind = "comment"
// ImportsFoldingRange is the folding range for a imports or includes.
ImportsFoldingRange FoldingRangeKind = "imports"
// RegionFoldingRange is the folding range for a region (e.g. `#region`).
RegionFoldingRange FoldingRangeKind = "region"
)
// FoldingRange capabilities specific to `textDocument/foldingRange` requests.
//
// @since 3.10.0.
type FoldingRange struct {
// StartLine is the zero-based line number from where the folded range starts.
StartLine uint32 `json:"startLine"`
// StartCharacter is the zero-based character offset from where the folded range starts. If not defined, defaults to the length of the start line.
StartCharacter uint32 `json:"startCharacter,omitempty"`
// EndLine is the zero-based line number where the folded range ends.
EndLine uint32 `json:"endLine"`
// EndCharacter is the zero-based character offset before the folded range ends. If not defined, defaults to the length of the end line.
EndCharacter uint32 `json:"endCharacter,omitempty"`
// Kind describes the kind of the folding range such as `comment' or 'region'. The kind
// is used to categorize folding ranges and used by commands like 'Fold all comments'.
// See FoldingRangeKind for an enumeration of standardized kinds.
Kind FoldingRangeKind `json:"kind,omitempty"`
}
// SPDX-FileCopyrightText: 2019 The Go Language Server Authors
// SPDX-License-Identifier: BSD-3-Clause
package protocol
import (
"context"
"log/slog"
"github.com/a-h/templ/lsp/jsonrpc2"
)
// NewServer returns the context in which client is embedded, jsonrpc2.Conn, and the Client.
func NewServer(ctx context.Context, server Server, stream jsonrpc2.Stream, logger *slog.Logger) (context.Context, jsonrpc2.Conn, Client) {
conn := jsonrpc2.NewConn(stream)
cliint := ClientDispatcher(conn, logger.With(slog.String("name", "client")))
ctx = WithClient(ctx, cliint)
conn.Go(ctx,
Handlers(
ServerHandler(logger, server, jsonrpc2.MethodNotFoundHandler),
),
)
return ctx, conn, cliint
}
// NewClient returns the context in which Client is embedded, jsonrpc2.Conn, and the Server.
func NewClient(ctx context.Context, client Client, stream jsonrpc2.Stream, logger *slog.Logger) (context.Context, jsonrpc2.Conn, Server) {
ctx = WithClient(ctx, client)
conn := jsonrpc2.NewConn(stream)
conn.Go(ctx,
Handlers(
ClientHandler(logger, client, jsonrpc2.MethodNotFoundHandler),
),
)
server := ServerDispatcher(conn, logger.With(slog.String("name", "server")))
return ctx, conn, server
}
// SPDX-FileCopyrightText: 2021 The Go Language Server Authors
// SPDX-License-Identifier: BSD-3-Clause
package protocol
// SelectionRangeProviderOptions selection range provider options interface.
type SelectionRangeProviderOptions any
// SelectionRange represents a selection range represents a part of a selection hierarchy.
//
// A selection range may have a parent selection range that contains it.
//
// @since 3.15.0.
type SelectionRange struct {
// Range is the Range of this selection range.
Range Range `json:"range"`
// Parent is the parent selection range containing this range. Therefore `parent.range` must contain this Range.
Parent *SelectionRange `json:"parent,omitempty"`
}
// EnableSelectionRange is the whether the selection range.
type EnableSelectionRange bool
// compile time check whether the EnableSelectionRange implements a SelectionRangeProviderOptions interface.
var _ SelectionRangeProviderOptions = (*EnableSelectionRange)(nil)
// Value implements SelectionRangeProviderOptions interface.
func (v EnableSelectionRange) Value() any {
return bool(v)
}
// NewEnableSelectionRange returns the new EnableSelectionRange underlying types SelectionRangeProviderOptions.
func NewEnableSelectionRange(enable bool) SelectionRangeProviderOptions {
v := EnableSelectionRange(enable)
return &v
}
// SelectionRangeOptions is the server capability of selection range.
type SelectionRangeOptions struct {
WorkDoneProgressOptions
}
// compile time check whether the EnableSelectionRange implements a SelectionRangeProviderOptions interface.
var _ SelectionRangeProviderOptions = (*EnableSelectionRange)(nil)
// Value implements SelectionRangeProviderOptions interface.
func (v *SelectionRangeOptions) Value() any {
return v
}
// NewSelectionRangeOptions returns the new SelectionRangeOptions underlying types SelectionRangeProviderOptions.
func NewSelectionRangeOptions(enableWorkDoneProgress bool) SelectionRangeProviderOptions {
v := SelectionRangeOptions{
WorkDoneProgressOptions: WorkDoneProgressOptions{
WorkDoneProgress: enableWorkDoneProgress,
},
}
return &v
}
// SelectionRangeRegistrationOptions is the server capability of selection range registration.
type SelectionRangeRegistrationOptions struct {
SelectionRangeOptions
TextDocumentRegistrationOptions
StaticRegistrationOptions
}
// compile time check whether the SelectionRangeRegistrationOptions implements a SelectionRangeProviderOptions interface.
var _ SelectionRangeProviderOptions = (*SelectionRangeRegistrationOptions)(nil)
// Value implements SelectionRangeProviderOptions interface.
func (v *SelectionRangeRegistrationOptions) Value() any {
return v
}
// NewSelectionRangeRegistrationOptions returns the new SelectionRangeRegistrationOptions underlying types SelectionRangeProviderOptions.
func NewSelectionRangeRegistrationOptions(enableWorkDoneProgress bool, selector DocumentSelector, id string) SelectionRangeProviderOptions {
v := SelectionRangeRegistrationOptions{
SelectionRangeOptions: SelectionRangeOptions{
WorkDoneProgressOptions: WorkDoneProgressOptions{
WorkDoneProgress: enableWorkDoneProgress,
},
},
TextDocumentRegistrationOptions: TextDocumentRegistrationOptions{
DocumentSelector: selector,
},
StaticRegistrationOptions: StaticRegistrationOptions{
ID: id,
},
}
return &v
}
// SelectionRangeParams represents a parameter literal used in selection range requests.
//
// @since 3.15.0.
type SelectionRangeParams struct {
WorkDoneProgressParams
PartialResultParams
// TextDocument is the text document.
TextDocument TextDocumentIdentifier `json:"textDocument"`
// Positions is the positions inside the text document.
Positions []Position `json:"positions"`
}
// SPDX-FileCopyrightText: 2019 The Go Language Server Authors
// SPDX-License-Identifier: BSD-3-Clause
package protocol
import (
"bytes"
"context"
"fmt"
"log/slog"
"encoding/json"
"github.com/a-h/templ/lsp/jsonrpc2"
"github.com/a-h/templ/lsp/xcontext"
)
// ServerDispatcher returns a Server that dispatches LSP requests across the
// given jsonrpc2 connection.
func ServerDispatcher(conn jsonrpc2.Conn, logger *slog.Logger) Server {
return &server{
Conn: conn,
logger: logger,
}
}
// ServerHandler jsonrpc2.Handler of Language Server Prococol Server.
//
//nolint:unparam
func ServerHandler(log *slog.Logger, server Server, handler jsonrpc2.Handler) jsonrpc2.Handler {
h := func(ctx context.Context, reply jsonrpc2.Replier, req jsonrpc2.Request) error {
if ctx.Err() != nil {
xctx := xcontext.Detach(ctx)
return reply(xctx, nil, ErrRequestCancelled)
}
handled, err := serverDispatch(ctx, log, server, reply, req)
if handled || err != nil {
return err
}
// TODO: This code is wrong, it ignores handler and assumes non standard
// request handles everything
// non standard request should just be a layered handler.
var params any
if err := json.Unmarshal(req.Params(), ¶ms); err != nil {
return replyParseError(ctx, reply, err)
}
resp, err := server.Request(ctx, req.Method(), params)
return reply(ctx, resp, err)
}
return h
}
// serverDispatch implements jsonrpc2.Handler.
//
//nolint:gocognit,funlen,gocyclo,cyclop
func serverDispatch(ctx context.Context, logger *slog.Logger, server Server, reply jsonrpc2.Replier, req jsonrpc2.Request) (handled bool, err error) {
if ctx.Err() != nil {
return true, reply(ctx, nil, ErrRequestCancelled)
}
dec := json.NewDecoder(bytes.NewReader(req.Params()))
switch req.Method() {
case MethodInitialize: // request
defer logger.Debug(MethodInitialize, slog.Any("error", err))
var params InitializeParams
if err := dec.Decode(¶ms); err != nil {
return true, replyParseError(ctx, reply, err)
}
resp, err := server.Initialize(ctx, ¶ms)
return true, reply(ctx, resp, err)
case MethodInitialized: // notification
defer logger.Debug(MethodInitialized, slog.Any("error", err))
var params InitializedParams
if err := dec.Decode(¶ms); err != nil {
return true, replyParseError(ctx, reply, err)
}
err := server.Initialized(ctx, ¶ms)
return true, reply(ctx, nil, err)
case MethodShutdown: // request
defer logger.Debug(MethodShutdown, slog.Any("error", err))
if len(req.Params()) > 0 {
return true, reply(ctx, nil, fmt.Errorf("expected no params: %w", jsonrpc2.ErrInvalidParams))
}
err := server.Shutdown(ctx)
return true, reply(ctx, nil, err)
case MethodExit: // notification
defer logger.Debug(MethodExit, slog.Any("error", err))
if len(req.Params()) > 0 {
return true, reply(ctx, nil, fmt.Errorf("expected no params: %w", jsonrpc2.ErrInvalidParams))
}
err := server.Exit(ctx)
return true, reply(ctx, nil, err)
case MethodWorkDoneProgressCancel: // notification
defer logger.Debug(MethodWorkDoneProgressCancel, slog.Any("error", err))
var params WorkDoneProgressCancelParams
if err := dec.Decode(¶ms); err != nil {
return true, replyParseError(ctx, reply, err)
}
err := server.WorkDoneProgressCancel(ctx, ¶ms)
return true, reply(ctx, nil, err)
case MethodLogTrace: // notification
defer logger.Debug(MethodLogTrace, slog.Any("error", err))
var params LogTraceParams
if err := dec.Decode(¶ms); err != nil {
return true, replyParseError(ctx, reply, err)
}
err := server.LogTrace(ctx, ¶ms)
return true, reply(ctx, nil, err)
case MethodSetTrace: // notification
defer logger.Debug(MethodSetTrace, slog.Any("error", err))
var params SetTraceParams
if err := dec.Decode(¶ms); err != nil {
return true, replyParseError(ctx, reply, err)
}
err := server.SetTrace(ctx, ¶ms)
return true, reply(ctx, nil, err)
case MethodTextDocumentCodeAction: // request
defer logger.Debug(MethodTextDocumentCodeAction, slog.Any("error", err))
var params CodeActionParams
if err := dec.Decode(¶ms); err != nil {
return true, replyParseError(ctx, reply, err)
}
resp, err := server.CodeAction(ctx, ¶ms)
return true, reply(ctx, resp, err)
case MethodTextDocumentCodeLens: // request
defer logger.Debug(MethodTextDocumentCodeLens, slog.Any("error", err))
var params CodeLensParams
if err := dec.Decode(¶ms); err != nil {
return true, replyParseError(ctx, reply, err)
}
resp, err := server.CodeLens(ctx, ¶ms)
return true, reply(ctx, resp, err)
case MethodCodeLensResolve: // request
defer logger.Debug(MethodCodeLensResolve, slog.Any("error", err))
var params CodeLens
if err := dec.Decode(¶ms); err != nil {
return true, replyParseError(ctx, reply, err)
}
resp, err := server.CodeLensResolve(ctx, ¶ms)
return true, reply(ctx, resp, err)
case MethodTextDocumentColorPresentation: // request
defer logger.Debug(MethodTextDocumentColorPresentation, slog.Any("error", err))
var params ColorPresentationParams
if err := dec.Decode(¶ms); err != nil {
return true, replyParseError(ctx, reply, err)
}
resp, err := server.ColorPresentation(ctx, ¶ms)
return true, reply(ctx, resp, err)
case MethodTextDocumentCompletion: // request
defer logger.Debug(MethodTextDocumentCompletion, slog.Any("error", err))
var params CompletionParams
if err := dec.Decode(¶ms); err != nil {
return true, replyParseError(ctx, reply, err)
}
resp, err := server.Completion(ctx, ¶ms)
return true, reply(ctx, resp, err)
case MethodCompletionItemResolve: // request
defer logger.Debug(MethodCompletionItemResolve, slog.Any("error", err))
var params CompletionItem
if err := dec.Decode(¶ms); err != nil {
return true, replyParseError(ctx, reply, err)
}
resp, err := server.CompletionResolve(ctx, ¶ms)
return true, reply(ctx, resp, err)
case MethodTextDocumentDeclaration: // request
defer logger.Debug(MethodTextDocumentDeclaration, slog.Any("error", err))
var params DeclarationParams
if err := dec.Decode(¶ms); err != nil {
return true, replyParseError(ctx, reply, err)
}
resp, err := server.Declaration(ctx, ¶ms)
return true, reply(ctx, resp, err)
case MethodTextDocumentDefinition: // request
defer logger.Debug(MethodTextDocumentDefinition, slog.Any("error", err))
var params DefinitionParams
if err := dec.Decode(¶ms); err != nil {
return true, replyParseError(ctx, reply, err)
}
resp, err := server.Definition(ctx, ¶ms)
return true, reply(ctx, resp, err)
case MethodTextDocumentDidChange: // notification
defer logger.Debug(MethodTextDocumentDidChange, slog.Any("error", err))
var params DidChangeTextDocumentParams
if err := dec.Decode(¶ms); err != nil {
return true, replyParseError(ctx, reply, err)
}
err := server.DidChange(ctx, ¶ms)
return true, reply(ctx, nil, err)
case MethodWorkspaceDidChangeConfiguration: // notification
defer logger.Debug(MethodWorkspaceDidChangeConfiguration, slog.Any("error", err))
var params DidChangeConfigurationParams
if err := dec.Decode(¶ms); err != nil {
return true, replyParseError(ctx, reply, err)
}
err := server.DidChangeConfiguration(ctx, ¶ms)
return true, reply(ctx, nil, err)
case MethodWorkspaceDidChangeWatchedFiles: // notification
defer logger.Debug(MethodWorkspaceDidChangeWatchedFiles, slog.Any("error", err))
var params DidChangeWatchedFilesParams
if err := dec.Decode(¶ms); err != nil {
return true, replyParseError(ctx, reply, err)
}
err := server.DidChangeWatchedFiles(ctx, ¶ms)
return true, reply(ctx, nil, err)
case MethodWorkspaceDidChangeWorkspaceFolders: // notification
defer logger.Debug(MethodWorkspaceDidChangeWorkspaceFolders, slog.Any("error", err))
var params DidChangeWorkspaceFoldersParams
if err := dec.Decode(¶ms); err != nil {
return true, replyParseError(ctx, reply, err)
}
err := server.DidChangeWorkspaceFolders(ctx, ¶ms)
return true, reply(ctx, nil, err)
case MethodTextDocumentDidClose: // notification
defer logger.Debug(MethodTextDocumentDidClose, slog.Any("error", err))
var params DidCloseTextDocumentParams
if err := dec.Decode(¶ms); err != nil {
return true, replyParseError(ctx, reply, err)
}
err := server.DidClose(ctx, ¶ms)
return true, reply(ctx, nil, err)
case MethodTextDocumentDidOpen: // notification
defer logger.Debug(MethodTextDocumentDidOpen, slog.Any("error", err))
var params DidOpenTextDocumentParams
if err := dec.Decode(¶ms); err != nil {
return true, replyParseError(ctx, reply, err)
}
err := server.DidOpen(ctx, ¶ms)
return true, reply(ctx, nil, err)
case MethodTextDocumentDidSave: // notification
defer logger.Debug(MethodTextDocumentDidSave, slog.Any("error", err))
var params DidSaveTextDocumentParams
if err := dec.Decode(¶ms); err != nil {
return true, replyParseError(ctx, reply, err)
}
err := server.DidSave(ctx, ¶ms)
return true, reply(ctx, nil, err)
case MethodTextDocumentDocumentColor: // request
defer logger.Debug(MethodTextDocumentDocumentColor, slog.Any("error", err))
var params DocumentColorParams
if err := dec.Decode(¶ms); err != nil {
return true, replyParseError(ctx, reply, err)
}
resp, err := server.DocumentColor(ctx, ¶ms)
return true, reply(ctx, resp, err)
case MethodTextDocumentDocumentHighlight: // request
defer logger.Debug(MethodTextDocumentDocumentHighlight, slog.Any("error", err))
var params DocumentHighlightParams
if err := dec.Decode(¶ms); err != nil {
return true, replyParseError(ctx, reply, err)
}
resp, err := server.DocumentHighlight(ctx, ¶ms)
return true, reply(ctx, resp, err)
case MethodTextDocumentDocumentLink: // request
defer logger.Debug(MethodTextDocumentDocumentLink, slog.Any("error", err))
var params DocumentLinkParams
if err := dec.Decode(¶ms); err != nil {
return true, replyParseError(ctx, reply, err)
}
resp, err := server.DocumentLink(ctx, ¶ms)
return true, reply(ctx, resp, err)
case MethodDocumentLinkResolve: // request
defer logger.Debug(MethodDocumentLinkResolve, slog.Any("error", err))
var params DocumentLink
if err := dec.Decode(¶ms); err != nil {
return true, replyParseError(ctx, reply, err)
}
resp, err := server.DocumentLinkResolve(ctx, ¶ms)
return true, reply(ctx, resp, err)
case MethodTextDocumentDocumentSymbol: // request
defer logger.Debug(MethodTextDocumentDocumentSymbol, slog.Any("error", err))
var params DocumentSymbolParams
if err := dec.Decode(¶ms); err != nil {
return true, replyParseError(ctx, reply, err)
}
resp, err := server.DocumentSymbol(ctx, ¶ms)
return true, reply(ctx, resp, err)
case MethodWorkspaceExecuteCommand: // request
defer logger.Debug(MethodWorkspaceExecuteCommand, slog.Any("error", err))
var params ExecuteCommandParams
if err := dec.Decode(¶ms); err != nil {
return true, replyParseError(ctx, reply, err)
}
resp, err := server.ExecuteCommand(ctx, ¶ms)
return true, reply(ctx, resp, err)
case MethodTextDocumentFoldingRange: // request
defer logger.Debug(MethodTextDocumentFoldingRange, slog.Any("error", err))
var params FoldingRangeParams
if err := dec.Decode(¶ms); err != nil {
return true, replyParseError(ctx, reply, err)
}
resp, err := server.FoldingRanges(ctx, ¶ms)
return true, reply(ctx, resp, err)
case MethodTextDocumentFormatting: // request
defer logger.Debug(MethodTextDocumentFormatting, slog.Any("error", err))
var params DocumentFormattingParams
if err := dec.Decode(¶ms); err != nil {
return true, replyParseError(ctx, reply, err)
}
resp, err := server.Formatting(ctx, ¶ms)
return true, reply(ctx, resp, err)
case MethodTextDocumentHover: // request
defer logger.Debug(MethodTextDocumentHover, slog.Any("error", err))
var params HoverParams
if err := dec.Decode(¶ms); err != nil {
return true, replyParseError(ctx, reply, err)
}
resp, err := server.Hover(ctx, ¶ms)
return true, reply(ctx, resp, err)
case MethodTextDocumentImplementation: // request
defer logger.Debug(MethodTextDocumentImplementation, slog.Any("error", err))
var params ImplementationParams
if err := dec.Decode(¶ms); err != nil {
return true, replyParseError(ctx, reply, err)
}
resp, err := server.Implementation(ctx, ¶ms)
return true, reply(ctx, resp, err)
case MethodTextDocumentOnTypeFormatting: // request
defer logger.Debug(MethodTextDocumentOnTypeFormatting, slog.Any("error", err))
var params DocumentOnTypeFormattingParams
if err := dec.Decode(¶ms); err != nil {
return true, replyParseError(ctx, reply, err)
}
resp, err := server.OnTypeFormatting(ctx, ¶ms)
return true, reply(ctx, resp, err)
case MethodTextDocumentPrepareRename: // request
defer logger.Debug(MethodTextDocumentPrepareRename, slog.Any("error", err))
var params PrepareRenameParams
if err := dec.Decode(¶ms); err != nil {
return true, replyParseError(ctx, reply, err)
}
resp, err := server.PrepareRename(ctx, ¶ms)
return true, reply(ctx, resp, err)
case MethodTextDocumentRangeFormatting: // request
defer logger.Debug(MethodTextDocumentRangeFormatting, slog.Any("error", err))
var params DocumentRangeFormattingParams
if err := dec.Decode(¶ms); err != nil {
return true, replyParseError(ctx, reply, err)
}
resp, err := server.RangeFormatting(ctx, ¶ms)
return true, reply(ctx, resp, err)
case MethodTextDocumentReferences: // request
defer logger.Debug(MethodTextDocumentReferences, slog.Any("error", err))
var params ReferenceParams
if err := dec.Decode(¶ms); err != nil {
return true, replyParseError(ctx, reply, err)
}
resp, err := server.References(ctx, ¶ms)
return true, reply(ctx, resp, err)
case MethodTextDocumentRename: // request
defer logger.Debug(MethodTextDocumentRename, slog.Any("error", err))
var params RenameParams
if err := dec.Decode(¶ms); err != nil {
return true, replyParseError(ctx, reply, err)
}
resp, err := server.Rename(ctx, ¶ms)
return true, reply(ctx, resp, err)
case MethodTextDocumentSignatureHelp: // request
defer logger.Debug(MethodTextDocumentSignatureHelp, slog.Any("error", err))
var params SignatureHelpParams
if err := dec.Decode(¶ms); err != nil {
return true, replyParseError(ctx, reply, err)
}
resp, err := server.SignatureHelp(ctx, ¶ms)
return true, reply(ctx, resp, err)
case MethodWorkspaceSymbol: // request
defer logger.Debug(MethodWorkspaceSymbol, slog.Any("error", err))
var params WorkspaceSymbolParams
if err := dec.Decode(¶ms); err != nil {
return true, replyParseError(ctx, reply, err)
}
resp, err := server.Symbols(ctx, ¶ms)
return true, reply(ctx, resp, err)
case MethodTextDocumentTypeDefinition: // request
defer logger.Debug(MethodTextDocumentTypeDefinition, slog.Any("error", err))
var params TypeDefinitionParams
if err := dec.Decode(¶ms); err != nil {
return true, replyParseError(ctx, reply, err)
}
resp, err := server.TypeDefinition(ctx, ¶ms)
return true, reply(ctx, resp, err)
case MethodTextDocumentWillSave: // notification
defer logger.Debug(MethodTextDocumentWillSave, slog.Any("error", err))
var params WillSaveTextDocumentParams
if err := dec.Decode(¶ms); err != nil {
return true, replyParseError(ctx, reply, err)
}
err := server.WillSave(ctx, ¶ms)
return true, reply(ctx, nil, err)
case MethodTextDocumentWillSaveWaitUntil: // request
defer logger.Debug(MethodTextDocumentWillSaveWaitUntil, slog.Any("error", err))
var params WillSaveTextDocumentParams
if err := dec.Decode(¶ms); err != nil {
return true, replyParseError(ctx, reply, err)
}
resp, err := server.WillSaveWaitUntil(ctx, ¶ms)
return true, reply(ctx, resp, err)
case MethodShowDocument: // request
defer logger.Debug(MethodShowDocument, slog.Any("error", err))
var params ShowDocumentParams
if err := dec.Decode(¶ms); err != nil {
return true, replyParseError(ctx, reply, err)
}
resp, err := server.ShowDocument(ctx, ¶ms)
return true, reply(ctx, resp, err)
case MethodWillCreateFiles: // request
defer logger.Debug(MethodWillCreateFiles, slog.Any("error", err))
var params CreateFilesParams
if err := dec.Decode(¶ms); err != nil {
return true, replyParseError(ctx, reply, err)
}
resp, err := server.WillCreateFiles(ctx, ¶ms)
return true, reply(ctx, resp, err)
case MethodDidCreateFiles: // notification
defer logger.Debug(MethodDidCreateFiles, slog.Any("error", err))
var params CreateFilesParams
if err := dec.Decode(¶ms); err != nil {
return true, replyParseError(ctx, reply, err)
}
err := server.DidCreateFiles(ctx, ¶ms)
return true, reply(ctx, nil, err)
case MethodWillRenameFiles: // request
defer logger.Debug(MethodWillRenameFiles, slog.Any("error", err))
var params RenameFilesParams
if err := dec.Decode(¶ms); err != nil {
return true, replyParseError(ctx, reply, err)
}
resp, err := server.WillRenameFiles(ctx, ¶ms)
return true, reply(ctx, resp, err)
case MethodDidRenameFiles: // notification
defer logger.Debug(MethodDidRenameFiles, slog.Any("error", err))
var params RenameFilesParams
if err := dec.Decode(¶ms); err != nil {
return true, replyParseError(ctx, reply, err)
}
err := server.DidRenameFiles(ctx, ¶ms)
return true, reply(ctx, nil, err)
case MethodWillDeleteFiles: // request
defer logger.Debug(MethodWillDeleteFiles, slog.Any("error", err))
var params DeleteFilesParams
if err := dec.Decode(¶ms); err != nil {
return true, replyParseError(ctx, reply, err)
}
resp, err := server.WillDeleteFiles(ctx, ¶ms)
return true, reply(ctx, resp, err)
case MethodDidDeleteFiles: // notification
defer logger.Debug(MethodDidDeleteFiles, slog.Any("error", err))
var params DeleteFilesParams
if err := dec.Decode(¶ms); err != nil {
return true, replyParseError(ctx, reply, err)
}
err := server.DidDeleteFiles(ctx, ¶ms)
return true, reply(ctx, nil, err)
case MethodCodeLensRefresh: // request
defer logger.Debug(MethodCodeLensRefresh, slog.Any("error", err))
if len(req.Params()) > 0 {
return true, reply(ctx, nil, fmt.Errorf("expected no params: %w", jsonrpc2.ErrInvalidParams))
}
err := server.CodeLensRefresh(ctx)
return true, reply(ctx, nil, err)
case MethodTextDocumentPrepareCallHierarchy: // request
defer logger.Debug(MethodTextDocumentPrepareCallHierarchy, slog.Any("error", err))
var params CallHierarchyPrepareParams
if err := dec.Decode(¶ms); err != nil {
return true, replyParseError(ctx, reply, err)
}
resp, err := server.PrepareCallHierarchy(ctx, ¶ms)
return true, reply(ctx, resp, err)
case MethodCallHierarchyIncomingCalls: // request
defer logger.Debug(MethodCallHierarchyIncomingCalls, slog.Any("error", err))
var params CallHierarchyIncomingCallsParams
if err := dec.Decode(¶ms); err != nil {
return true, replyParseError(ctx, reply, err)
}
resp, err := server.IncomingCalls(ctx, ¶ms)
return true, reply(ctx, resp, err)
case MethodCallHierarchyOutgoingCalls: // request
defer logger.Debug(MethodCallHierarchyOutgoingCalls, slog.Any("error", err))
var params CallHierarchyOutgoingCallsParams
if err := dec.Decode(¶ms); err != nil {
return true, replyParseError(ctx, reply, err)
}
resp, err := server.OutgoingCalls(ctx, ¶ms)
return true, reply(ctx, resp, err)
case MethodSemanticTokensFull: // request
defer logger.Debug(MethodSemanticTokensFull, slog.Any("error", err))
var params SemanticTokensParams
if err := dec.Decode(¶ms); err != nil {
return true, replyParseError(ctx, reply, err)
}
resp, err := server.SemanticTokensFull(ctx, ¶ms)
return true, reply(ctx, resp, err)
case MethodSemanticTokensFullDelta: // request
defer logger.Debug(MethodSemanticTokensFullDelta, slog.Any("error", err))
var params SemanticTokensDeltaParams
if err := dec.Decode(¶ms); err != nil {
return true, replyParseError(ctx, reply, err)
}
resp, err := server.SemanticTokensFullDelta(ctx, ¶ms)
return true, reply(ctx, resp, err)
case MethodSemanticTokensRange: // request
defer logger.Debug(MethodSemanticTokensRange, slog.Any("error", err))
var params SemanticTokensRangeParams
if err := dec.Decode(¶ms); err != nil {
return true, replyParseError(ctx, reply, err)
}
resp, err := server.SemanticTokensRange(ctx, ¶ms)
return true, reply(ctx, resp, err)
case MethodSemanticTokensRefresh: // request
defer logger.Debug(MethodSemanticTokensRefresh, slog.Any("error", err))
if len(req.Params()) > 0 {
return true, reply(ctx, nil, fmt.Errorf("expected no params: %w", jsonrpc2.ErrInvalidParams))
}
err := server.SemanticTokensRefresh(ctx)
return true, reply(ctx, nil, err)
case MethodLinkedEditingRange: // request
defer logger.Debug(MethodLinkedEditingRange, slog.Any("error", err))
var params LinkedEditingRangeParams
if err := dec.Decode(¶ms); err != nil {
return true, replyParseError(ctx, reply, err)
}
resp, err := server.LinkedEditingRange(ctx, ¶ms)
return true, reply(ctx, resp, err)
case MethodMoniker: // request
defer logger.Debug(MethodMoniker, slog.Any("error", err))
var params MonikerParams
if err := dec.Decode(¶ms); err != nil {
return true, replyParseError(ctx, reply, err)
}
resp, err := server.Moniker(ctx, ¶ms)
return true, reply(ctx, resp, err)
default:
return false, nil
}
}
// Server represents a Language Server Protocol server.
type Server interface {
Initialize(ctx context.Context, params *InitializeParams) (result *InitializeResult, err error)
Initialized(ctx context.Context, params *InitializedParams) (err error)
Shutdown(ctx context.Context) (err error)
Exit(ctx context.Context) (err error)
WorkDoneProgressCancel(ctx context.Context, params *WorkDoneProgressCancelParams) (err error)
LogTrace(ctx context.Context, params *LogTraceParams) (err error)
SetTrace(ctx context.Context, params *SetTraceParams) (err error)
CodeAction(ctx context.Context, params *CodeActionParams) (result []CodeAction, err error)
CodeLens(ctx context.Context, params *CodeLensParams) (result []CodeLens, err error)
CodeLensResolve(ctx context.Context, params *CodeLens) (result *CodeLens, err error)
ColorPresentation(ctx context.Context, params *ColorPresentationParams) (result []ColorPresentation, err error)
Completion(ctx context.Context, params *CompletionParams) (result *CompletionList, err error)
CompletionResolve(ctx context.Context, params *CompletionItem) (result *CompletionItem, err error)
Declaration(ctx context.Context, params *DeclarationParams) (result []Location /* Declaration | DeclarationLink[] | null */, err error)
Definition(ctx context.Context, params *DefinitionParams) (result []Location /* Definition | DefinitionLink[] | null */, err error)
DidChange(ctx context.Context, params *DidChangeTextDocumentParams) (err error)
DidChangeConfiguration(ctx context.Context, params *DidChangeConfigurationParams) (err error)
DidChangeWatchedFiles(ctx context.Context, params *DidChangeWatchedFilesParams) (err error)
DidChangeWorkspaceFolders(ctx context.Context, params *DidChangeWorkspaceFoldersParams) (err error)
DidClose(ctx context.Context, params *DidCloseTextDocumentParams) (err error)
DidOpen(ctx context.Context, params *DidOpenTextDocumentParams) (err error)
DidSave(ctx context.Context, params *DidSaveTextDocumentParams) (err error)
DocumentColor(ctx context.Context, params *DocumentColorParams) (result []ColorInformation, err error)
DocumentHighlight(ctx context.Context, params *DocumentHighlightParams) (result []DocumentHighlight, err error)
DocumentLink(ctx context.Context, params *DocumentLinkParams) (result []DocumentLink, err error)
DocumentLinkResolve(ctx context.Context, params *DocumentLink) (result *DocumentLink, err error)
DocumentSymbol(ctx context.Context, params *DocumentSymbolParams) (result []SymbolInformationOrDocumentSymbol, err error)
ExecuteCommand(ctx context.Context, params *ExecuteCommandParams) (result any, err error)
FoldingRanges(ctx context.Context, params *FoldingRangeParams) (result []FoldingRange, err error)
Formatting(ctx context.Context, params *DocumentFormattingParams) (result []TextEdit, err error)
Hover(ctx context.Context, params *HoverParams) (result *Hover, err error)
Implementation(ctx context.Context, params *ImplementationParams) (result []Location, err error)
OnTypeFormatting(ctx context.Context, params *DocumentOnTypeFormattingParams) (result []TextEdit, err error)
PrepareRename(ctx context.Context, params *PrepareRenameParams) (result *Range, err error)
RangeFormatting(ctx context.Context, params *DocumentRangeFormattingParams) (result []TextEdit, err error)
References(ctx context.Context, params *ReferenceParams) (result []Location, err error)
Rename(ctx context.Context, params *RenameParams) (result *WorkspaceEdit, err error)
SignatureHelp(ctx context.Context, params *SignatureHelpParams) (result *SignatureHelp, err error)
Symbols(ctx context.Context, params *WorkspaceSymbolParams) (result []SymbolInformation, err error)
TypeDefinition(ctx context.Context, params *TypeDefinitionParams) (result []Location, err error)
WillSave(ctx context.Context, params *WillSaveTextDocumentParams) (err error)
WillSaveWaitUntil(ctx context.Context, params *WillSaveTextDocumentParams) (result []TextEdit, err error)
ShowDocument(ctx context.Context, params *ShowDocumentParams) (result *ShowDocumentResult, err error)
WillCreateFiles(ctx context.Context, params *CreateFilesParams) (result *WorkspaceEdit, err error)
DidCreateFiles(ctx context.Context, params *CreateFilesParams) (err error)
WillRenameFiles(ctx context.Context, params *RenameFilesParams) (result *WorkspaceEdit, err error)
DidRenameFiles(ctx context.Context, params *RenameFilesParams) (err error)
WillDeleteFiles(ctx context.Context, params *DeleteFilesParams) (result *WorkspaceEdit, err error)
DidDeleteFiles(ctx context.Context, params *DeleteFilesParams) (err error)
CodeLensRefresh(ctx context.Context) (err error)
PrepareCallHierarchy(ctx context.Context, params *CallHierarchyPrepareParams) (result []CallHierarchyItem, err error)
IncomingCalls(ctx context.Context, params *CallHierarchyIncomingCallsParams) (result []CallHierarchyIncomingCall, err error)
OutgoingCalls(ctx context.Context, params *CallHierarchyOutgoingCallsParams) (result []CallHierarchyOutgoingCall, err error)
SemanticTokensFull(ctx context.Context, params *SemanticTokensParams) (result *SemanticTokens, err error)
SemanticTokensFullDelta(ctx context.Context, params *SemanticTokensDeltaParams) (result any /* SemanticTokens | SemanticTokensDelta */, err error)
SemanticTokensRange(ctx context.Context, params *SemanticTokensRangeParams) (result *SemanticTokens, err error)
SemanticTokensRefresh(ctx context.Context) (err error)
LinkedEditingRange(ctx context.Context, params *LinkedEditingRangeParams) (result *LinkedEditingRanges, err error)
Moniker(ctx context.Context, params *MonikerParams) (result []Moniker, err error)
Request(ctx context.Context, method string, params any) (result any, err error)
}
// list of server methods.
const (
// MethodCancelRequest method name of "$/cancelRequest".
MethodCancelRequest = "$/cancelRequest"
// MethodInitialize method name of "initialize".
MethodInitialize = "initialize"
// MethodInitialized method name of "initialized".
MethodInitialized = "initialized"
// MethodShutdown method name of "shutdown".
MethodShutdown = "shutdown"
// MethodExit method name of "exit".
MethodExit = "exit"
// MethodWorkDoneProgressCancel method name of "window/workDoneProgress/cancel".
MethodWorkDoneProgressCancel = "window/workDoneProgress/cancel"
// MethodLogTrace method name of "$/logTrace".
MethodLogTrace = "$/logTrace"
// MethodSetTrace method name of "$/setTrace".
MethodSetTrace = "$/setTrace"
// MethodTextDocumentCodeAction method name of "textDocument/codeAction".
MethodTextDocumentCodeAction = "textDocument/codeAction"
// MethodTextDocumentCodeLens method name of "textDocument/codeLens".
MethodTextDocumentCodeLens = "textDocument/codeLens"
// MethodCodeLensResolve method name of "codeLens/resolve".
MethodCodeLensResolve = "codeLens/resolve"
// MethodTextDocumentColorPresentation method name of "textDocument/colorPresentation".
MethodTextDocumentColorPresentation = "textDocument/colorPresentation"
// MethodTextDocumentCompletion method name of "textDocument/completion".
MethodTextDocumentCompletion = "textDocument/completion"
// MethodCompletionItemResolve method name of "completionItem/resolve".
MethodCompletionItemResolve = "completionItem/resolve"
// MethodTextDocumentDeclaration method name of "textDocument/declaration".
MethodTextDocumentDeclaration = "textDocument/declaration"
// MethodTextDocumentDefinition method name of "textDocument/definition".
MethodTextDocumentDefinition = "textDocument/definition"
// MethodTextDocumentDidChange method name of "textDocument/didChange".
MethodTextDocumentDidChange = "textDocument/didChange"
// MethodWorkspaceDidChangeConfiguration method name of "workspace/didChangeConfiguration".
MethodWorkspaceDidChangeConfiguration = "workspace/didChangeConfiguration"
// MethodWorkspaceDidChangeWatchedFiles method name of "workspace/didChangeWatchedFiles".
MethodWorkspaceDidChangeWatchedFiles = "workspace/didChangeWatchedFiles"
// MethodWorkspaceDidChangeWorkspaceFolders method name of "workspace/didChangeWorkspaceFolders".
MethodWorkspaceDidChangeWorkspaceFolders = "workspace/didChangeWorkspaceFolders"
// MethodTextDocumentDidClose method name of "textDocument/didClose".
MethodTextDocumentDidClose = "textDocument/didClose"
// MethodTextDocumentDidOpen method name of "textDocument/didOpen".
MethodTextDocumentDidOpen = "textDocument/didOpen"
// MethodTextDocumentDidSave method name of "textDocument/didSave".
MethodTextDocumentDidSave = "textDocument/didSave"
// MethodTextDocumentDocumentColor method name of"textDocument/documentColor".
MethodTextDocumentDocumentColor = "textDocument/documentColor"
// MethodTextDocumentDocumentHighlight method name of "textDocument/documentHighlight".
MethodTextDocumentDocumentHighlight = "textDocument/documentHighlight"
// MethodTextDocumentDocumentLink method name of "textDocument/documentLink".
MethodTextDocumentDocumentLink = "textDocument/documentLink"
// MethodDocumentLinkResolve method name of "documentLink/resolve".
MethodDocumentLinkResolve = "documentLink/resolve"
// MethodTextDocumentDocumentSymbol method name of "textDocument/documentSymbol".
MethodTextDocumentDocumentSymbol = "textDocument/documentSymbol"
// MethodWorkspaceExecuteCommand method name of "workspace/executeCommand".
MethodWorkspaceExecuteCommand = "workspace/executeCommand"
// MethodTextDocumentFoldingRange method name of "textDocument/foldingRange".
MethodTextDocumentFoldingRange = "textDocument/foldingRange"
// MethodTextDocumentFormatting method name of "textDocument/formatting".
MethodTextDocumentFormatting = "textDocument/formatting"
// MethodTextDocumentHover method name of "textDocument/hover".
MethodTextDocumentHover = "textDocument/hover"
// MethodTextDocumentImplementation method name of "textDocument/implementation".
MethodTextDocumentImplementation = "textDocument/implementation"
// MethodTextDocumentOnTypeFormatting method name of "textDocument/onTypeFormatting".
MethodTextDocumentOnTypeFormatting = "textDocument/onTypeFormatting"
// MethodTextDocumentPrepareRename method name of "textDocument/prepareRename".
MethodTextDocumentPrepareRename = "textDocument/prepareRename"
// MethodTextDocumentRangeFormatting method name of "textDocument/rangeFormatting".
MethodTextDocumentRangeFormatting = "textDocument/rangeFormatting"
// MethodTextDocumentReferences method name of "textDocument/references".
MethodTextDocumentReferences = "textDocument/references"
// MethodTextDocumentRename method name of "textDocument/rename".
MethodTextDocumentRename = "textDocument/rename"
// MethodTextDocumentSignatureHelp method name of "textDocument/signatureHelp".
MethodTextDocumentSignatureHelp = "textDocument/signatureHelp"
// MethodWorkspaceSymbol method name of "workspace/symbol".
MethodWorkspaceSymbol = "workspace/symbol"
// MethodTextDocumentTypeDefinition method name of "textDocument/typeDefinition".
MethodTextDocumentTypeDefinition = "textDocument/typeDefinition"
// MethodTextDocumentWillSave method name of "textDocument/willSave".
MethodTextDocumentWillSave = "textDocument/willSave"
// MethodTextDocumentWillSaveWaitUntil method name of "textDocument/willSaveWaitUntil".
MethodTextDocumentWillSaveWaitUntil = "textDocument/willSaveWaitUntil"
// MethodShowDocument method name of "window/showDocument".
MethodShowDocument = "window/showDocument"
// MethodWillCreateFiles method name of "workspace/willCreateFiles".
MethodWillCreateFiles = "workspace/willCreateFiles"
// MethodDidCreateFiles method name of "workspace/didCreateFiles".
MethodDidCreateFiles = "workspace/didCreateFiles"
// MethodWillRenameFiles method name of "workspace/willRenameFiles".
MethodWillRenameFiles = "workspace/willRenameFiles"
// MethodDidRenameFiles method name of "workspace/didRenameFiles".
MethodDidRenameFiles = "workspace/didRenameFiles"
// MethodWillDeleteFiles method name of "workspace/willDeleteFiles".
MethodWillDeleteFiles = "workspace/willDeleteFiles"
// MethodDidDeleteFiles method name of "workspace/didDeleteFiles".
MethodDidDeleteFiles = "workspace/didDeleteFiles"
// MethodCodeLensRefresh method name of "workspace/codeLens/refresh".
MethodCodeLensRefresh = "workspace/codeLens/refresh"
// MethodTextDocumentPrepareCallHierarchy method name of "textDocument/prepareCallHierarchy".
MethodTextDocumentPrepareCallHierarchy = "textDocument/prepareCallHierarchy"
// MethodCallHierarchyIncomingCalls method name of "callHierarchy/incomingCalls".
MethodCallHierarchyIncomingCalls = "callHierarchy/incomingCalls"
// MethodCallHierarchyOutgoingCalls method name of "callHierarchy/outgoingCalls".
MethodCallHierarchyOutgoingCalls = "callHierarchy/outgoingCalls"
// MethodSemanticTokensFull method name of "textDocument/semanticTokens/full".
MethodSemanticTokensFull = "textDocument/semanticTokens/full"
// MethodSemanticTokensFullDelta method name of "textDocument/semanticTokens/full/delta".
MethodSemanticTokensFullDelta = "textDocument/semanticTokens/full/delta"
// MethodSemanticTokensRange method name of "textDocument/semanticTokens/range".
MethodSemanticTokensRange = "textDocument/semanticTokens/range"
// MethodSemanticTokensRefresh method name of "workspace/semanticTokens/refresh".
MethodSemanticTokensRefresh = "workspace/semanticTokens/refresh"
// MethodLinkedEditingRange method name of "textDocument/linkedEditingRange".
MethodLinkedEditingRange = "textDocument/linkedEditingRange"
// MethodMoniker method name of "textDocument/moniker".
MethodMoniker = "textDocument/moniker"
)
// server implements a Language Server Protocol server.
type server struct {
jsonrpc2.Conn
logger *slog.Logger
}
var _ Server = (*server)(nil)
// Initialize sents the request as the first request from the client to the server.
//
// If the server receives a request or notification before the initialize request it should act as follows:
//
// - For a request the response should be an error with code: -32002. The message can be picked by the server.
// - Notifications should be dropped, except for the exit notification. This will allow the exit of a server without an initialize request.
//
// Until the server has responded to the initialize request with an InitializeResult, the client
// must not send any additional requests or notifications to the server.
// In addition the server is not allowed to send any requests or notifications to the client until
// it has responded with an InitializeResult, with the exception that during the initialize request
// the server is allowed to send the notifications window/showMessage, window/logMessage and telemetry/event
// as well as the window/showMessageRequest request to the client.
func (s *server) Initialize(ctx context.Context, params *InitializeParams) (_ *InitializeResult, err error) {
s.logger.Debug("call " + MethodInitialize)
defer s.logger.Debug("end "+MethodInitialize, slog.Any("error", err))
var result *InitializeResult
if err := Call(ctx, s.Conn, MethodInitialize, params, &result); err != nil {
return nil, err
}
return result, nil
}
// Initialized sends the notification from the client to the server after the client received the result of the
// initialize request but before the client is sending any other request or notification to the server.
//
// The server can use the initialized notification for example to dynamically register capabilities.
// The initialized notification may only be sent once.
func (s *server) Initialized(ctx context.Context, params *InitializedParams) (err error) {
s.logger.Debug("notify " + MethodInitialized)
defer s.logger.Debug("end "+MethodInitialized, slog.Any("error", err))
return s.Conn.Notify(ctx, MethodInitialized, params)
}
// Shutdown sents the request from the client to the server.
//
// It asks the server to shut down, but to not exit (otherwise the response might not be delivered correctly to the client).
// There is a separate exit notification that asks the server to exit.
//
// Clients must not sent any notifications other than `exit` or requests to a server to which they have sent a shutdown requests.
// If a server receives requests after a shutdown request those requests should be errored with `InvalidRequest`.
func (s *server) Shutdown(ctx context.Context) (err error) {
s.logger.Debug("call " + MethodShutdown)
defer s.logger.Debug("end "+MethodShutdown, slog.Any("error", err))
return Call(ctx, s.Conn, MethodShutdown, nil, nil)
}
// Exit a notification to ask the server to exit its process.
//
// The server should exit with success code 0 if the shutdown request has been received before; otherwise with error code 1.
func (s *server) Exit(ctx context.Context) (err error) {
s.logger.Debug("notify " + MethodExit)
defer s.logger.Debug("end "+MethodExit, slog.Any("error", err))
return s.Conn.Notify(ctx, MethodExit, nil)
}
// LogTrace a notification to log the trace of the server’s execution.
//
// The amount and content of these notifications depends on the current trace configuration.
//
// If trace is "off", the server should not send any logTrace notification. If trace is "message",
// the server should not add the "verbose" field in the LogTraceParams.
//
// @since 3.16.0.
func (s *server) LogTrace(ctx context.Context, params *LogTraceParams) (err error) {
s.logger.Debug("notify " + MethodLogTrace)
defer s.logger.Debug("end "+MethodLogTrace, slog.Any("error", err))
return s.Conn.Notify(ctx, MethodLogTrace, params)
}
// SetTrace a notification that should be used by the client to modify the trace setting of the server.
//
// @since 3.16.0.
func (s *server) SetTrace(ctx context.Context, params *SetTraceParams) (err error) {
s.logger.Debug("notify " + MethodSetTrace)
defer s.logger.Debug("end "+MethodSetTrace, slog.Any("error", err))
return s.Conn.Notify(ctx, MethodSetTrace, params)
}
// WorkDoneProgressCancel is the sends notification from the client to the server to cancel a progress initiated on the
// server side using the "window/workDoneProgress/create".
func (s *server) WorkDoneProgressCancel(ctx context.Context, params *WorkDoneProgressCancelParams) (err error) {
s.logger.Debug("call " + MethodWorkDoneProgressCancel)
defer s.logger.Debug("end "+MethodWorkDoneProgressCancel, slog.Any("error", err))
return s.Conn.Notify(ctx, MethodWorkDoneProgressCancel, params)
}
// CodeAction sends the request is from the client to the server to compute commands for a given text document and range.
//
// These commands are typically code fixes to either fix problems or to beautify/refactor code. The result of a `textDocument/codeAction`
// request is an array of `Command` literals which are typically presented in the user interface.
//
// To ensure that a server is useful in many clients the commands specified in a code actions should be handled by the
// server and not by the client (see `workspace/executeCommand` and `ServerCapabilities.executeCommandProvider`).
// If the client supports providing edits with a code action then the mode should be used.
func (s *server) CodeAction(ctx context.Context, params *CodeActionParams) (result []CodeAction, err error) {
s.logger.Debug("call " + MethodTextDocumentCodeAction)
defer s.logger.Debug("end "+MethodTextDocumentCodeAction, slog.Any("error", err))
if err := Call(ctx, s.Conn, MethodTextDocumentCodeAction, params, &result); err != nil {
return nil, err
}
return result, nil
}
// CodeLens sends the request from the client to the server to compute code lenses for a given text document.
func (s *server) CodeLens(ctx context.Context, params *CodeLensParams) (result []CodeLens, err error) {
s.logger.Debug("call " + MethodTextDocumentCodeLens)
defer s.logger.Debug("end "+MethodTextDocumentCodeLens, slog.Any("error", err))
if err := Call(ctx, s.Conn, MethodTextDocumentCodeLens, params, &result); err != nil {
return nil, err
}
return result, nil
}
// CodeLensResolve sends the request from the client to the server to resolve the command for a given code lens item.
func (s *server) CodeLensResolve(ctx context.Context, params *CodeLens) (_ *CodeLens, err error) {
s.logger.Debug("call " + MethodCodeLensResolve)
defer s.logger.Debug("end "+MethodCodeLensResolve, slog.Any("error", err))
var result *CodeLens
if err := Call(ctx, s.Conn, MethodCodeLensResolve, params, &result); err != nil {
return nil, err
}
return result, nil
}
// ColorPresentation sends the request from the client to the server to obtain a list of presentations for a color value at a given location.
//
// # Clients can use the result to
//
// - modify a color reference.
// - show in a color picker and let users pick one of the presentations.
func (s *server) ColorPresentation(ctx context.Context, params *ColorPresentationParams) (result []ColorPresentation, err error) {
s.logger.Debug("call " + MethodTextDocumentColorPresentation)
defer s.logger.Debug("end "+MethodTextDocumentColorPresentation, slog.Any("error", err))
if err := Call(ctx, s.Conn, MethodTextDocumentColorPresentation, params, &result); err != nil {
return nil, err
}
return result, nil
}
// Completion sends the request from the client to the server to compute completion items at a given cursor position.
//
// Completion items are presented in the IntelliSense user interface.
// If computing full completion items is expensive, servers can additionally provide a handler for the completion item resolve request (‘completionItem/resolve’).
//
// This request is sent when a completion item is selected in the user interface.
// A typical use case is for example: the ‘textDocument/completion’ request doesn’t fill in the documentation property
// for returned completion items since it is expensive to compute. When the item is selected in the user interface then
// a ‘completionItem/resolve’ request is sent with the selected completion item as a parameter.
//
// The returned completion item should have the documentation property filled in. The request can delay the computation of
// the `detail` and `documentation` properties. However, properties that are needed for the initial sorting and filtering,
// like `sortText`, `filterText`, `insertText`, and `textEdit` must be provided in the `textDocument/completion` response and must not be changed during resolve.
func (s *server) Completion(ctx context.Context, params *CompletionParams) (_ *CompletionList, err error) {
s.logger.Debug("call " + MethodTextDocumentCompletion)
defer s.logger.Debug("end "+MethodTextDocumentCompletion, slog.Any("error", err))
var result *CompletionList
if err := Call(ctx, s.Conn, MethodTextDocumentCompletion, params, &result); err != nil {
return nil, err
}
return result, nil
}
// CompletionResolve sends the request from the client to the server to resolve additional information for a given completion item.
func (s *server) CompletionResolve(ctx context.Context, params *CompletionItem) (_ *CompletionItem, err error) {
s.logger.Debug("call " + MethodCompletionItemResolve)
defer s.logger.Debug("end "+MethodCompletionItemResolve, slog.Any("error", err))
var result *CompletionItem
if err := Call(ctx, s.Conn, MethodCompletionItemResolve, params, &result); err != nil {
return nil, err
}
return result, nil
}
// Declaration sends the request from the client to the server to resolve the declaration location of a symbol at a given text document position.
//
// The result type LocationLink[] got introduce with version 3.14.0 and depends in the corresponding client capability `clientCapabilities.textDocument.declaration.linkSupport`.
//
// @since 3.14.0.
func (s *server) Declaration(ctx context.Context, params *DeclarationParams) (result []Location, err error) {
s.logger.Debug("call " + MethodTextDocumentDeclaration)
defer s.logger.Debug("end "+MethodTextDocumentDeclaration, slog.Any("error", err))
if err := Call(ctx, s.Conn, MethodTextDocumentDeclaration, params, &result); err != nil {
return nil, err
}
return result, nil
}
// Definition sends the request from the client to the server to resolve the definition location of a symbol at a given text document position.
//
// The result type `[]LocationLink` got introduce with version 3.14.0 and depends in the corresponding client capability `clientCapabilities.textDocument.definition.linkSupport`.
//
// @since 3.14.0.
func (s *server) Definition(ctx context.Context, params *DefinitionParams) (result []Location, err error) {
s.logger.Debug("call " + MethodTextDocumentDefinition)
defer s.logger.Debug("end "+MethodTextDocumentDefinition, slog.Any("error", err))
if err := Call(ctx, s.Conn, MethodTextDocumentDefinition, params, &result); err != nil {
return nil, err
}
return result, nil
}
// DidChange sends the notification from the client to the server to signal changes to a text document.
//
// In 2.0 the shape of the params has changed to include proper version numbers and language ids.
func (s *server) DidChange(ctx context.Context, params *DidChangeTextDocumentParams) (err error) {
s.logger.Debug("notify " + MethodTextDocumentDidChange)
defer s.logger.Debug("end "+MethodTextDocumentDidChange, slog.Any("error", err))
return s.Conn.Notify(ctx, MethodTextDocumentDidChange, params)
}
// DidChangeConfiguration sends the notification from the client to the server to signal the change of configuration settings.
func (s *server) DidChangeConfiguration(ctx context.Context, params *DidChangeConfigurationParams) (err error) {
s.logger.Debug("call " + MethodWorkspaceDidChangeConfiguration)
defer s.logger.Debug("end "+MethodWorkspaceDidChangeConfiguration, slog.Any("error", err))
return s.Conn.Notify(ctx, MethodWorkspaceDidChangeConfiguration, params)
}
// DidChangeWatchedFiles sends the notification from the client to the server when the client detects changes to files watched by the language client.
//
// It is recommended that servers register for these file events using the registration mechanism.
// In former implementations clients pushed file events without the server actively asking for it.
func (s *server) DidChangeWatchedFiles(ctx context.Context, params *DidChangeWatchedFilesParams) (err error) {
s.logger.Debug("call " + MethodWorkspaceDidChangeWatchedFiles)
defer s.logger.Debug("end "+MethodWorkspaceDidChangeWatchedFiles, slog.Any("error", err))
return s.Conn.Notify(ctx, MethodWorkspaceDidChangeWatchedFiles, params)
}
// DidChangeWorkspaceFolders sents the notification from the client to the server to inform the server about workspace folder configuration changes.
//
// The notification is sent by default if both ServerCapabilities/workspace/workspaceFolders and ClientCapabilities/workspace/workspaceFolders are true;
// or if the server has registered itself to receive this notification.
// To register for the workspace/didChangeWorkspaceFolders send a client/registerCapability request from the server to the client.
//
// The registration parameter must have a registrations item of the following form, where id is a unique id used to unregister the capability (the example uses a UUID).
func (s *server) DidChangeWorkspaceFolders(ctx context.Context, params *DidChangeWorkspaceFoldersParams) (err error) {
s.logger.Debug("call " + MethodWorkspaceDidChangeWorkspaceFolders)
defer s.logger.Debug("end "+MethodWorkspaceDidChangeWorkspaceFolders, slog.Any("error", err))
return s.Conn.Notify(ctx, MethodWorkspaceDidChangeWorkspaceFolders, params)
}
// DidClose sends the notification from the client to the server when the document got closed in the client.
//
// The document’s truth now exists where the document’s Uri points to (e.g. if the document’s Uri is a file Uri the truth now exists on disk).
// As with the open notification the close notification is about managing the document’s content.
// Receiving a close notification doesn’t mean that the document was open in an editor before.
//
// A close notification requires a previous open notification to be sent.
// Note that a server’s ability to fulfill requests is independent of whether a text document is open or closed.
func (s *server) DidClose(ctx context.Context, params *DidCloseTextDocumentParams) (err error) {
s.logger.Debug("call " + MethodTextDocumentDidClose)
defer s.logger.Debug("end "+MethodTextDocumentDidClose, slog.Any("error", err))
return s.Conn.Notify(ctx, MethodTextDocumentDidClose, params)
}
// DidOpen sends the open notification from the client to the server to signal newly opened text documents.
//
// The document’s truth is now managed by the client and the server must not try to read the document’s truth using the document’s Uri.
// Open in this sense means it is managed by the client. It doesn’t necessarily mean that its content is presented in an editor.
//
// An open notification must not be sent more than once without a corresponding close notification send before.
// This means open and close notification must be balanced and the max open count for a particular textDocument is one.
// Note that a server’s ability to fulfill requests is independent of whether a text document is open or closed.
func (s *server) DidOpen(ctx context.Context, params *DidOpenTextDocumentParams) (err error) {
s.logger.Debug("call " + MethodTextDocumentDidOpen)
defer s.logger.Debug("end "+MethodTextDocumentDidOpen, slog.Any("error", err))
return s.Conn.Notify(ctx, MethodTextDocumentDidOpen, params)
}
// DidSave sends the notification from the client to the server when the document was saved in the client.
func (s *server) DidSave(ctx context.Context, params *DidSaveTextDocumentParams) (err error) {
s.logger.Debug("call " + MethodTextDocumentDidSave)
defer s.logger.Debug("end "+MethodTextDocumentDidSave, slog.Any("error", err))
return s.Conn.Notify(ctx, MethodTextDocumentDidSave, params)
}
// DocumentColor sends the request from the client to the server to list all color references found in a given text document.
//
// Along with the range, a color value in RGB is returned.
//
// Clients can use the result to decorate color references in an editor.
// For example:
//
// - Color boxes showing the actual color next to the reference
// - Show a color picker when a color reference is edited.
func (s *server) DocumentColor(ctx context.Context, params *DocumentColorParams) (result []ColorInformation, err error) {
s.logger.Debug("call " + MethodTextDocumentDocumentColor)
defer s.logger.Debug("end "+MethodTextDocumentDocumentColor, slog.Any("error", err))
if err := Call(ctx, s.Conn, MethodTextDocumentDocumentColor, params, &result); err != nil {
return nil, err
}
return result, nil
}
// DocumentHighlight sends the request is from the client to the server to resolve a document highlights for a given text document position.
//
// For programming languages this usually highlights all references to the symbol scoped to this file.
// However we kept ‘textDocument/documentHighlight’ and ‘textDocument/references’ separate requests since the first one is allowed to be more fuzzy.
//
// Symbol matches usually have a `DocumentHighlightKind` of `Read` or `Write` whereas fuzzy or textual matches use `Text` as the kind.
func (s *server) DocumentHighlight(ctx context.Context, params *DocumentHighlightParams) (result []DocumentHighlight, err error) {
s.logger.Debug("call " + MethodTextDocumentDocumentHighlight)
defer s.logger.Debug("end "+MethodTextDocumentDocumentHighlight, slog.Any("error", err))
if err := Call(ctx, s.Conn, MethodTextDocumentDocumentHighlight, params, &result); err != nil {
return nil, err
}
return result, nil
}
// DocumentLink sends the request from the client to the server to request the location of links in a document.
func (s *server) DocumentLink(ctx context.Context, params *DocumentLinkParams) (result []DocumentLink, err error) {
s.logger.Debug("call " + MethodTextDocumentDocumentLink)
defer s.logger.Debug("end "+MethodTextDocumentDocumentLink, slog.Any("error", err))
if err := Call(ctx, s.Conn, MethodTextDocumentDocumentLink, params, &result); err != nil {
return nil, err
}
return result, nil
}
// DocumentLinkResolve sends the request from the client to the server to resolve the target of a given document link.
func (s *server) DocumentLinkResolve(ctx context.Context, params *DocumentLink) (_ *DocumentLink, err error) {
s.logger.Debug("call " + MethodDocumentLinkResolve)
defer s.logger.Debug("end "+MethodDocumentLinkResolve, slog.Any("error", err))
var result *DocumentLink
if err := Call(ctx, s.Conn, MethodDocumentLinkResolve, params, &result); err != nil {
return nil, err
}
return result, nil
}
// DocumentSymbol sends the request from the client to the server to return a flat list of all symbols found in a given text document.
//
// Neither the symbol’s location range nor the symbol’s container name should be used to infer a hierarchy.
func (s *server) DocumentSymbol(ctx context.Context, params *DocumentSymbolParams) (result []SymbolInformationOrDocumentSymbol, err error) {
s.logger.Debug("call " + MethodTextDocumentDocumentSymbol)
defer s.logger.Debug("end "+MethodTextDocumentDocumentSymbol, slog.Any("error", err))
if err := Call(ctx, s.Conn, MethodTextDocumentDocumentSymbol, params, &result); err != nil {
return nil, err
}
return result, nil
}
// ExecuteCommand sends the request from the client to the server to trigger command execution on the server.
//
// In most cases the server creates a `WorkspaceEdit` structure and applies the changes to the workspace using the
// request `workspace/applyEdit` which is sent from the server to the client.
func (s *server) ExecuteCommand(ctx context.Context, params *ExecuteCommandParams) (result any, err error) {
s.logger.Debug("call " + MethodWorkspaceExecuteCommand)
defer s.logger.Debug("end "+MethodWorkspaceExecuteCommand, slog.Any("error", err))
if err := Call(ctx, s.Conn, MethodWorkspaceExecuteCommand, params, &result); err != nil {
return nil, err
}
return result, nil
}
// FoldingRanges sends the request from the client to the server to return all folding ranges found in a given text document.
//
// @since version 3.10.0.
func (s *server) FoldingRanges(ctx context.Context, params *FoldingRangeParams) (result []FoldingRange, err error) {
s.logger.Debug("call " + MethodTextDocumentFoldingRange)
defer s.logger.Debug("end "+MethodTextDocumentFoldingRange, slog.Any("error", err))
if err := Call(ctx, s.Conn, MethodTextDocumentFoldingRange, params, &result); err != nil {
return nil, err
}
return result, nil
}
// Formatting sends the request from the client to the server to format a whole document.
func (s *server) Formatting(ctx context.Context, params *DocumentFormattingParams) (result []TextEdit, err error) {
s.logger.Debug("call " + MethodTextDocumentFormatting)
defer s.logger.Debug("end "+MethodTextDocumentFormatting, slog.Any("error", err))
if err := Call(ctx, s.Conn, MethodTextDocumentFormatting, params, &result); err != nil {
return nil, err
}
return result, nil
}
// Hover sends the request is from the client to the server to request hover information at a given text document position.
func (s *server) Hover(ctx context.Context, params *HoverParams) (_ *Hover, err error) {
s.logger.Debug("call " + MethodTextDocumentHover)
defer s.logger.Debug("end "+MethodTextDocumentHover, slog.Any("error", err))
var result *Hover
if err := Call(ctx, s.Conn, MethodTextDocumentHover, params, &result); err != nil {
return nil, err
}
return result, nil
}
// Implementation sends the request from the client to the server to resolve the implementation location of a symbol at a given text document position.
//
// The result type `[]LocationLink` got introduce with version 3.14.0 and depends in the corresponding client capability `clientCapabilities.implementation.typeDefinition.linkSupport`.
func (s *server) Implementation(ctx context.Context, params *ImplementationParams) (result []Location, err error) {
s.logger.Debug("call " + MethodTextDocumentImplementation)
defer s.logger.Debug("end "+MethodTextDocumentImplementation, slog.Any("error", err))
if err := Call(ctx, s.Conn, MethodTextDocumentImplementation, params, &result); err != nil {
return nil, err
}
return result, nil
}
// OnTypeFormatting sends the request from the client to the server to format parts of the document during typing.
func (s *server) OnTypeFormatting(ctx context.Context, params *DocumentOnTypeFormattingParams) (result []TextEdit, err error) {
s.logger.Debug("call " + MethodTextDocumentOnTypeFormatting)
defer s.logger.Debug("end "+MethodTextDocumentOnTypeFormatting, slog.Any("error", err))
if err := Call(ctx, s.Conn, MethodTextDocumentOnTypeFormatting, params, &result); err != nil {
return nil, err
}
return result, nil
}
// PrepareRename sends the request from the client to the server to setup and test the validity of a rename operation at a given location.
//
// @since version 3.12.0.
func (s *server) PrepareRename(ctx context.Context, params *PrepareRenameParams) (result *Range, err error) {
s.logger.Debug("call " + MethodTextDocumentPrepareRename)
defer s.logger.Debug("end "+MethodTextDocumentPrepareRename, slog.Any("error", err))
if err := Call(ctx, s.Conn, MethodTextDocumentPrepareRename, params, &result); err != nil {
return nil, err
}
return result, nil
}
// RangeFormatting sends the request from the client to the server to format a given range in a document.
func (s *server) RangeFormatting(ctx context.Context, params *DocumentRangeFormattingParams) (result []TextEdit, err error) {
s.logger.Debug("call " + MethodTextDocumentRangeFormatting)
defer s.logger.Debug("end "+MethodTextDocumentRangeFormatting, slog.Any("error", err))
if err := Call(ctx, s.Conn, MethodTextDocumentRangeFormatting, params, &result); err != nil {
return nil, err
}
return result, nil
}
// References sends the request from the client to the server to resolve project-wide references for the symbol denoted by the given text document position.
func (s *server) References(ctx context.Context, params *ReferenceParams) (result []Location, err error) {
s.logger.Debug("call " + MethodTextDocumentReferences)
defer s.logger.Debug("end "+MethodTextDocumentReferences, slog.Any("error", err))
if err := Call(ctx, s.Conn, MethodTextDocumentReferences, params, &result); err != nil {
return nil, err
}
return result, nil
}
// Rename sends the request from the client to the server to perform a workspace-wide rename of a symbol.
func (s *server) Rename(ctx context.Context, params *RenameParams) (result *WorkspaceEdit, err error) {
s.logger.Debug("call " + MethodTextDocumentRename)
defer s.logger.Debug("end "+MethodTextDocumentRename, slog.Any("error", err))
if err := Call(ctx, s.Conn, MethodTextDocumentRename, params, &result); err != nil {
return nil, err
}
return result, nil
}
// SignatureHelp sends the request from the client to the server to request signature information at a given cursor position.
func (s *server) SignatureHelp(ctx context.Context, params *SignatureHelpParams) (_ *SignatureHelp, err error) {
s.logger.Debug("call " + MethodTextDocumentSignatureHelp)
defer s.logger.Debug("end "+MethodTextDocumentSignatureHelp, slog.Any("error", err))
var result *SignatureHelp
if err := Call(ctx, s.Conn, MethodTextDocumentSignatureHelp, params, &result); err != nil {
return nil, err
}
return result, nil
}
// Symbols sends the request from the client to the server to list project-wide symbols matching the query string.
func (s *server) Symbols(ctx context.Context, params *WorkspaceSymbolParams) (result []SymbolInformation, err error) {
s.logger.Debug("call " + MethodWorkspaceSymbol)
defer s.logger.Debug("end "+MethodWorkspaceSymbol, slog.Any("error", err))
if err := Call(ctx, s.Conn, MethodWorkspaceSymbol, params, &result); err != nil {
return nil, err
}
return result, nil
}
// TypeDefinition sends the request from the client to the server to resolve the type definition location of a symbol at a given text document position.
//
// The result type `[]LocationLink` got introduce with version 3.14.0 and depends in the corresponding client capability `clientCapabilities.textDocument.typeDefinition.linkSupport`.
//
// @since version 3.6.0.
func (s *server) TypeDefinition(ctx context.Context, params *TypeDefinitionParams) (result []Location, err error) {
s.logger.Debug("call " + MethodTextDocumentTypeDefinition)
defer s.logger.Debug("end "+MethodTextDocumentTypeDefinition, slog.Any("error", err))
if err := Call(ctx, s.Conn, MethodTextDocumentTypeDefinition, params, &result); err != nil {
return nil, err
}
return result, nil
}
// WillSave sends the notification from the client to the server before the document is actually saved.
func (s *server) WillSave(ctx context.Context, params *WillSaveTextDocumentParams) (err error) {
s.logger.Debug("call " + MethodTextDocumentWillSave)
defer s.logger.Debug("end "+MethodTextDocumentWillSave, slog.Any("error", err))
return s.Conn.Notify(ctx, MethodTextDocumentWillSave, params)
}
// WillSaveWaitUntil sends the request from the client to the server before the document is actually saved.
//
// The request can return an array of TextEdits which will be applied to the text document before it is saved.
// Please note that clients might drop results if computing the text edits took too long or if a server constantly fails on this request.
// This is done to keep the save fast and reliable.
func (s *server) WillSaveWaitUntil(ctx context.Context, params *WillSaveTextDocumentParams) (result []TextEdit, err error) {
s.logger.Debug("call " + MethodTextDocumentWillSaveWaitUntil)
defer s.logger.Debug("end "+MethodTextDocumentWillSaveWaitUntil, slog.Any("error", err))
if err := Call(ctx, s.Conn, MethodTextDocumentWillSaveWaitUntil, params, &result); err != nil {
return nil, err
}
return result, nil
}
// ShowDocument sends the request from a server to a client to ask the client to display a particular document in the user interface.
//
// @since 3.16.0.
func (s *server) ShowDocument(ctx context.Context, params *ShowDocumentParams) (result *ShowDocumentResult, err error) {
s.logger.Debug("call " + MethodShowDocument)
defer s.logger.Debug("end "+MethodShowDocument, slog.Any("error", err))
if err := Call(ctx, s.Conn, MethodShowDocument, params, &result); err != nil {
return nil, err
}
return result, nil
}
// WillCreateFiles sends the will create files request is sent from the client to the server before files are actually created as long as the creation is triggered from within the client.
//
// The request can return a WorkspaceEdit which will be applied to workspace before the files are created.
//
// Please note that clients might drop results if computing the edit took too long or if a server constantly fails on this request. This is done to keep creates fast and reliable.
//
// @since 3.16.0.
func (s *server) WillCreateFiles(ctx context.Context, params *CreateFilesParams) (result *WorkspaceEdit, err error) {
s.logger.Debug("call " + MethodWillCreateFiles)
defer s.logger.Debug("end "+MethodWillCreateFiles, slog.Any("error", err))
if err := Call(ctx, s.Conn, MethodWillCreateFiles, params, &result); err != nil {
return nil, err
}
return result, nil
}
// DidCreateFiles sends the did create files notification is sent from the client to the server when files were created from within the client.
//
// @since 3.16.0.
func (s *server) DidCreateFiles(ctx context.Context, params *CreateFilesParams) (err error) {
s.logger.Debug("call " + MethodDidCreateFiles)
defer s.logger.Debug("end "+MethodDidCreateFiles, slog.Any("error", err))
return s.Conn.Notify(ctx, MethodDidCreateFiles, params)
}
// WillRenameFiles sends the will rename files request is sent from the client to the server before files are actually renamed as long as the rename is triggered from within the client.
//
// The request can return a WorkspaceEdit which will be applied to workspace before the files are renamed.
//
// Please note that clients might drop results if computing the edit took too long or if a server constantly fails on this request. This is done to keep renames fast and reliable.
//
// @since 3.16.0.
func (s *server) WillRenameFiles(ctx context.Context, params *RenameFilesParams) (result *WorkspaceEdit, err error) {
s.logger.Debug("call " + MethodWillRenameFiles)
defer s.logger.Debug("end "+MethodWillRenameFiles, slog.Any("error", err))
if err := Call(ctx, s.Conn, MethodWillRenameFiles, params, &result); err != nil {
return nil, err
}
return result, nil
}
// DidRenameFiles sends the did rename files notification is sent from the client to the server when files were renamed from within the client.
//
// @since 3.16.0.
func (s *server) DidRenameFiles(ctx context.Context, params *RenameFilesParams) (err error) {
s.logger.Debug("call " + MethodDidRenameFiles)
defer s.logger.Debug("end "+MethodDidRenameFiles, slog.Any("error", err))
return s.Conn.Notify(ctx, MethodDidRenameFiles, params)
}
// WillDeleteFiles sends the will delete files request is sent from the client to the server before files are actually deleted as long as the deletion is triggered from within the client.
//
// The request can return a WorkspaceEdit which will be applied to workspace before the files are deleted.
//
// Please note that clients might drop results if computing the edit took too long or if a server constantly fails on this request. This is done to keep deletes fast and reliable.
//
// @since 3.16.0.
func (s *server) WillDeleteFiles(ctx context.Context, params *DeleteFilesParams) (result *WorkspaceEdit, err error) {
s.logger.Debug("call " + MethodWillDeleteFiles)
defer s.logger.Debug("end "+MethodWillDeleteFiles, slog.Any("error", err))
if err := Call(ctx, s.Conn, MethodWillDeleteFiles, params, &result); err != nil {
return nil, err
}
return result, nil
}
// DidDeleteFiles sends the did delete files notification is sent from the client to the server when files were deleted from within the client.
//
// @since 3.16.0.
func (s *server) DidDeleteFiles(ctx context.Context, params *DeleteFilesParams) (err error) {
s.logger.Debug("call " + MethodDidDeleteFiles)
defer s.logger.Debug("end "+MethodDidDeleteFiles, slog.Any("error", err))
return s.Conn.Notify(ctx, MethodDidDeleteFiles, params)
}
// CodeLensRefresh sent from the server to the client.
//
// Servers can use it to ask clients to refresh the code lenses currently shown in editors.
// As a result the client should ask the server to recompute the code lenses for these editors.
// This is useful if a server detects a configuration change which requires a re-calculation of all code lenses.
//
// Note that the client still has the freedom to delay the re-calculation of the code lenses if for example an editor is currently not visible.
//
// @since 3.16.0.
func (s *server) CodeLensRefresh(ctx context.Context) (err error) {
s.logger.Debug("call " + MethodCodeLensRefresh)
defer s.logger.Debug("end "+MethodCodeLensRefresh, slog.Any("error", err))
return Call(ctx, s.Conn, MethodCodeLensRefresh, nil, nil)
}
// PrepareCallHierarchy sent from the client to the server to return a call hierarchy for the language element of given text document positions.
//
// The call hierarchy requests are executed in two steps:
// 1. first a call hierarchy item is resolved for the given text document position
// 2. for a call hierarchy item the incoming or outgoing call hierarchy items are resolved.
//
// @since 3.16.0.
func (s *server) PrepareCallHierarchy(ctx context.Context, params *CallHierarchyPrepareParams) (result []CallHierarchyItem, err error) {
s.logger.Debug("call " + MethodTextDocumentPrepareCallHierarchy)
defer s.logger.Debug("end "+MethodTextDocumentPrepareCallHierarchy, slog.Any("error", err))
if err := Call(ctx, s.Conn, MethodTextDocumentPrepareCallHierarchy, params, &result); err != nil {
return nil, err
}
return result, nil
}
// IncomingCalls is the request is sent from the client to the server to resolve incoming calls for a given call hierarchy item.
//
// The request doesn’t define its own client and server capabilities. It is only issued if a server registers for the "textDocument/prepareCallHierarchy" request.
//
// @since 3.16.0.
func (s *server) IncomingCalls(ctx context.Context, params *CallHierarchyIncomingCallsParams) (result []CallHierarchyIncomingCall, err error) {
s.logger.Debug("call " + MethodCallHierarchyIncomingCalls)
defer s.logger.Debug("end "+MethodCallHierarchyIncomingCalls, slog.Any("error", err))
if err := Call(ctx, s.Conn, MethodCallHierarchyIncomingCalls, params, &result); err != nil {
return nil, err
}
return result, nil
}
// OutgoingCalls is the request is sent from the client to the server to resolve outgoing calls for a given call hierarchy item.
//
// The request doesn’t define its own client and server capabilities. It is only issued if a server registers for the "textDocument/prepareCallHierarchy" request.
//
// @since 3.16.0.
func (s *server) OutgoingCalls(ctx context.Context, params *CallHierarchyOutgoingCallsParams) (result []CallHierarchyOutgoingCall, err error) {
s.logger.Debug("call " + MethodCallHierarchyOutgoingCalls)
defer s.logger.Debug("end "+MethodCallHierarchyOutgoingCalls, slog.Any("error", err))
if err := Call(ctx, s.Conn, MethodCallHierarchyOutgoingCalls, params, &result); err != nil {
return nil, err
}
return result, nil
}
// SemanticTokensFull is the request is sent from the client to the server to resolve semantic tokens for a given file.
//
// Semantic tokens are used to add additional color information to a file that depends on language specific symbol information.
//
// A semantic token request usually produces a large result. The protocol therefore supports encoding tokens with numbers.
//
// @since 3.16.0.
func (s *server) SemanticTokensFull(ctx context.Context, params *SemanticTokensParams) (result *SemanticTokens, err error) {
s.logger.Debug("call " + MethodSemanticTokensFull)
defer s.logger.Debug("end "+MethodSemanticTokensFull, slog.Any("error", err))
if err := Call(ctx, s.Conn, MethodSemanticTokensFull, params, &result); err != nil {
return nil, err
}
return result, nil
}
// SemanticTokensFullDelta is the request is sent from the client to the server to resolve semantic token delta for a given file.
//
// Semantic tokens are used to add additional color information to a file that depends on language specific symbol information.
//
// A semantic token request usually produces a large result. The protocol therefore supports encoding tokens with numbers.
//
// @since 3.16.0.
func (s *server) SemanticTokensFullDelta(ctx context.Context, params *SemanticTokensDeltaParams) (result any, err error) {
s.logger.Debug("call " + MethodSemanticTokensFullDelta)
defer s.logger.Debug("end "+MethodSemanticTokensFullDelta, slog.Any("error", err))
if err := Call(ctx, s.Conn, MethodSemanticTokensFullDelta, params, &result); err != nil {
return nil, err
}
return result, nil
}
// SemanticTokensRange is the request is sent from the client to the server to resolve semantic token delta for a given file.
//
// When a user opens a file it can be beneficial to only compute the semantic tokens for the visible range (faster rendering of the tokens in the user interface).
// If a server can compute these tokens faster than for the whole file it can provide a handler for the "textDocument/semanticTokens/range" request to handle this case special.
//
// Please note that if a client also announces that it will send the "textDocument/semanticTokens/range" server should implement this request as well to allow for flicker free scrolling and semantic coloring of a minimap.
//
// @since 3.16.0.
func (s *server) SemanticTokensRange(ctx context.Context, params *SemanticTokensRangeParams) (result *SemanticTokens, err error) {
s.logger.Debug("call " + MethodSemanticTokensRange)
defer s.logger.Debug("end "+MethodSemanticTokensRange, slog.Any("error", err))
if err := Call(ctx, s.Conn, MethodSemanticTokensRange, params, &result); err != nil {
return nil, err
}
return result, nil
}
// SemanticTokensRefresh is sent from the server to the client. Servers can use it to ask clients to refresh the editors for which this server provides semantic tokens.
//
// As a result the client should ask the server to recompute the semantic tokens for these editors.
// This is useful if a server detects a project wide configuration change which requires a re-calculation of all semantic tokens.
//
// Note that the client still has the freedom to delay the re-calculation of the semantic tokens if for example an editor is currently not visible.
//
// @since 3.16.0.
func (s *server) SemanticTokensRefresh(ctx context.Context) (err error) {
s.logger.Debug("call " + MethodSemanticTokensRefresh)
defer s.logger.Debug("end "+MethodSemanticTokensRefresh, slog.Any("error", err))
return Call(ctx, s.Conn, MethodSemanticTokensRefresh, nil, nil)
}
// LinkedEditingRange is the linked editing request is sent from the client to the server to return for a given position in a document the range of the symbol at the position and all ranges that have the same content.
//
// Optionally a word pattern can be returned to describe valid contents.
//
// A rename to one of the ranges can be applied to all other ranges if the new content is valid. If no result-specific word pattern is provided, the word pattern from the client’s language configuration is used.
//
// @since 3.16.0.
func (s *server) LinkedEditingRange(ctx context.Context, params *LinkedEditingRangeParams) (result *LinkedEditingRanges, err error) {
s.logger.Debug("call " + MethodLinkedEditingRange)
defer s.logger.Debug("end "+MethodLinkedEditingRange, slog.Any("error", err))
if err := Call(ctx, s.Conn, MethodLinkedEditingRange, params, &result); err != nil {
return nil, err
}
return result, nil
}
// Moniker is the request is sent from the client to the server to get the symbol monikers for a given text document position.
//
// An array of Moniker types is returned as response to indicate possible monikers at the given location.
//
// If no monikers can be calculated, an empty array or null should be returned.
//
// @since 3.16.0.
func (s *server) Moniker(ctx context.Context, params *MonikerParams) (result []Moniker, err error) {
s.logger.Debug("call " + MethodMoniker)
defer s.logger.Debug("end "+MethodMoniker, slog.Any("error", err))
if err := Call(ctx, s.Conn, MethodMoniker, params, &result); err != nil {
return nil, err
}
return result, nil
}
// Request sends a request from the client to the server that non-compliant with the Language Server Protocol specifications.
func (s *server) Request(ctx context.Context, method string, params any) (any, error) {
s.logger.Debug("call " + method)
defer s.logger.Debug("end " + method)
var result any
if err := Call(ctx, s.Conn, method, params, &result); err != nil {
return nil, err
}
return result, nil
}
// SPDX-FileCopyrightText: 2019 The Go Language Server Authors
// SPDX-License-Identifier: BSD-3-Clause
package protocol
import (
"strconv"
)
// DidOpenTextDocumentParams params of DidOpenTextDocument notification.
type DidOpenTextDocumentParams struct {
// TextDocument is the document that was opened.
TextDocument TextDocumentItem `json:"textDocument"`
}
// DidChangeTextDocumentParams params of DidChangeTextDocument notification.
type DidChangeTextDocumentParams struct {
// TextDocument is the document that did change. The version number points
// to the version after all provided content changes have
// been applied.
TextDocument VersionedTextDocumentIdentifier `json:"textDocument"`
// ContentChanges is the actual content changes. The content changes describe single state changes
// to the document. So if there are two content changes c1 and c2 for a document
// in state S then c1 move the document to S' and c2 to S''.
ContentChanges []TextDocumentContentChangeEvent `json:"contentChanges"` // []TextDocumentContentChangeEvent | text
}
// TextDocumentSaveReason represents reasons why a text document is saved.
type TextDocumentSaveReason float64
const (
// TextDocumentSaveReasonManual is the manually triggered, e.g. by the user pressing save, by starting debugging,
// or by an API call.
TextDocumentSaveReasonManual TextDocumentSaveReason = 1
// TextDocumentSaveReasonAfterDelay is the automatic after a delay.
TextDocumentSaveReasonAfterDelay TextDocumentSaveReason = 2
// TextDocumentSaveReasonFocusOut when the editor lost focus.
TextDocumentSaveReasonFocusOut TextDocumentSaveReason = 3
)
// String implements fmt.Stringer.
func (t TextDocumentSaveReason) String() string {
switch t {
case TextDocumentSaveReasonManual:
return "Manual"
case TextDocumentSaveReasonAfterDelay:
return "AfterDelay"
case TextDocumentSaveReasonFocusOut:
return "FocusOut"
default:
return strconv.FormatFloat(float64(t), 'f', -10, 64)
}
}
// TextDocumentChangeRegistrationOptions describe options to be used when registering for text document change events.
type TextDocumentChangeRegistrationOptions struct {
TextDocumentRegistrationOptions
// SyncKind how documents are synced to the server. See TextDocumentSyncKind.Full
// and TextDocumentSyncKind.Incremental.
SyncKind TextDocumentSyncKind `json:"syncKind"`
}
// WillSaveTextDocumentParams is the parameters send in a will save text document notification.
type WillSaveTextDocumentParams struct {
// TextDocument is the document that will be saved.
TextDocument TextDocumentIdentifier `json:"textDocument"`
// Reason is the 'TextDocumentSaveReason'.
Reason TextDocumentSaveReason `json:"reason,omitempty"`
}
// DidSaveTextDocumentParams params of DidSaveTextDocument notification.
type DidSaveTextDocumentParams struct {
// Text optional the content when saved. Depends on the includeText value
// when the save notification was requested.
Text string `json:"text,omitempty"`
// TextDocument is the document that was saved.
TextDocument TextDocumentIdentifier `json:"textDocument"`
}
// TextDocumentContentChangeEvent an event describing a change to a text document. If range and rangeLength are omitted
// the new text is considered to be the full content of the document.
type TextDocumentContentChangeEvent struct {
// Range is the range of the document that changed.
Range *Range `json:"range,omitempty"`
// RangeLength is the length of the range that got replaced.
RangeLength uint32 `json:"rangeLength,omitempty"`
// Text is the new text of the document.
Text string `json:"text"`
}
// TextDocumentSaveRegistrationOptions TextDocumentSave Registration options.
type TextDocumentSaveRegistrationOptions struct {
TextDocumentRegistrationOptions
// IncludeText is the client is supposed to include the content on save.
IncludeText bool `json:"includeText,omitempty"`
}
// DidCloseTextDocumentParams params of DidCloseTextDocument notification.
type DidCloseTextDocumentParams struct {
// TextDocument the document that was closed.
TextDocument TextDocumentIdentifier `json:"textDocument"`
}
// SPDX-FileCopyrightText: 2019 The Go Language Server Authors
// SPDX-License-Identifier: BSD-3-Clause
package protocol
// NewVersion returns the int32 pointer converted i.
func NewVersion(i int32) *int32 {
return &i
}
// SPDX-FileCopyrightText: 2019 The Go Language Server Authors
// SPDX-License-Identifier: BSD-3-Clause
package protocol
import "strconv"
// ShowMessageParams params of ShowMessage notification.
type ShowMessageParams struct {
// Message is the actual message.
Message string `json:"message"`
// Type is the message type.
Type MessageType `json:"type"`
}
// MessageType type of ShowMessageParams type.
type MessageType float64
const (
// MessageTypeError an error message.
MessageTypeError MessageType = 1
// MessageTypeWarning a warning message.
MessageTypeWarning MessageType = 2
// MessageTypeInfo an information message.
MessageTypeInfo MessageType = 3
// MessageTypeLog a log message.
MessageTypeLog MessageType = 4
)
// String implements fmt.Stringer.
func (m MessageType) String() string {
switch m {
case MessageTypeError:
return "error"
case MessageTypeWarning:
return "warning"
case MessageTypeInfo:
return "info"
case MessageTypeLog:
return "log"
default:
return strconv.FormatFloat(float64(m), 'f', -10, 64)
}
}
// Enabled reports whether the level is enabled.
func (m MessageType) Enabled(level MessageType) bool {
return level > 0 && m >= level
}
// messageTypeMap map of MessageTypes.
var messageTypeMap = map[string]MessageType{
"error": MessageTypeError,
"warning": MessageTypeWarning,
"info": MessageTypeInfo,
"log": MessageTypeLog,
}
// ToMessageType converts level to the MessageType.
func ToMessageType(level string) MessageType {
mt, ok := messageTypeMap[level]
if !ok {
return MessageType(0) // unknown
}
return mt
}
// ShowMessageRequestParams params of ShowMessage request.
type ShowMessageRequestParams struct {
// Actions is the message action items to present.
Actions []MessageActionItem `json:"actions"`
// Message is the actual message
Message string `json:"message"`
// Type is the message type. See {@link MessageType}
Type MessageType `json:"type"`
}
// MessageActionItem item of ShowMessageRequestParams action.
type MessageActionItem struct {
// Title a short title like 'Retry', 'Open Log' etc.
Title string `json:"title"`
}
// LogMessageParams params of LogMessage notification.
type LogMessageParams struct {
// Message is the actual message
Message string `json:"message"`
// Type is the message type. See {@link MessageType}
Type MessageType `json:"type"`
}
// WorkDoneProgressCreateParams params of WorkDoneProgressCreate request.
//
// @since 3.15.0.
type WorkDoneProgressCreateParams struct {
// Token is the token to be used to report progress.
Token ProgressToken `json:"token"`
}
// WorkDoneProgressCreateParams params of WorkDoneProgressCancel request.
//
// @since 3.15.0.
type WorkDoneProgressCancelParams struct {
// Token is the token to be used to report progress.
Token ProgressToken `json:"token"`
}
// SPDX-FileCopyrightText: 2019 The Go Language Server Authors
// SPDX-License-Identifier: BSD-3-Clause
package protocol
import (
"strconv"
"github.com/a-h/templ/lsp/uri"
)
// WorkspaceFolder response of Workspace folders request.
type WorkspaceFolder struct {
// URI is the associated URI for this workspace folder.
URI string `json:"uri"`
// Name is the name of the workspace folder. Used to refer to this
// workspace folder in the user interface.
Name string `json:"name"`
}
// DidChangeWorkspaceFoldersParams params of DidChangeWorkspaceFolders notification.
type DidChangeWorkspaceFoldersParams struct {
// Event is the actual workspace folder change event.
Event WorkspaceFoldersChangeEvent `json:"event"`
}
// WorkspaceFoldersChangeEvent is the workspace folder change event.
type WorkspaceFoldersChangeEvent struct {
// Added is the array of added workspace folders
Added []WorkspaceFolder `json:"added"`
// Removed is the array of the removed workspace folders
Removed []WorkspaceFolder `json:"removed"`
}
// DidChangeConfigurationParams params of DidChangeConfiguration notification.
type DidChangeConfigurationParams struct {
// Settings is the actual changed settings
Settings any `json:"settings,omitempty"`
}
// ConfigurationParams params of Configuration request.
type ConfigurationParams struct {
Items []ConfigurationItem `json:"items"`
}
// ConfigurationItem a ConfigurationItem consists of the configuration section to ask for and an additional scope URI.
// The configuration section ask for is defined by the server and doesn’t necessarily need to correspond to the configuration store used be the client.
// So a server might ask for a configuration cpp.formatterOptions but the client stores the configuration in a XML store layout differently.
// It is up to the client to do the necessary conversion. If a scope URI is provided the client should return the setting scoped to the provided resource.
// If the client for example uses EditorConfig to manage its settings the configuration should be returned for the passed resource URI. If the client can’t provide a configuration setting for a given scope then null need to be present in the returned array.
type ConfigurationItem struct {
// ScopeURI is the scope to get the configuration section for.
ScopeURI uri.URI `json:"scopeUri,omitempty"`
// Section is the configuration section asked for.
Section string `json:"section,omitempty"`
}
// DidChangeWatchedFilesParams params of DidChangeWatchedFiles notification.
type DidChangeWatchedFilesParams struct {
// Changes is the actual file events.
Changes []*FileEvent `json:"changes,omitempty"`
}
// FileEvent an event describing a file change.
type FileEvent struct {
// Type is the change type.
Type FileChangeType `json:"type"`
// URI is the file's URI.
URI uri.URI `json:"uri"`
}
// FileChangeType is the file event type.
type FileChangeType float64
const (
// FileChangeTypeCreated is the file got created.
FileChangeTypeCreated FileChangeType = 1
// FileChangeTypeChanged is the file got changed.
FileChangeTypeChanged FileChangeType = 2
// FileChangeTypeDeleted is the file got deleted.
FileChangeTypeDeleted FileChangeType = 3
)
// String implements fmt.Stringer.
func (t FileChangeType) String() string {
switch t {
case FileChangeTypeCreated:
return "Created"
case FileChangeTypeChanged:
return "Changed"
case FileChangeTypeDeleted:
return "Deleted"
default:
return strconv.FormatFloat(float64(t), 'f', -10, 64)
}
}
// DidChangeWatchedFilesRegistrationOptions describe options to be used when registering for file system change events.
type DidChangeWatchedFilesRegistrationOptions struct {
// Watchers is the watchers to register.
Watchers []FileSystemWatcher `json:"watchers"`
}
// FileSystemWatcher watchers of DidChangeWatchedFiles Registration options.
type FileSystemWatcher struct {
// GlobPattern is the glob pattern to watch.
//
// Glob patterns can have the following syntax:
// - `*` to match one or more characters in a path segment
// - `?` to match on one character in a path segment
// - `**` to match any number of path segments, including none
// - `{}` to group conditions (e.g. `**/*.{ts,js}` matches all TypeScript and JavaScript files)
// - `[]` to declare a range of characters to match in a path segment (e.g., `example.[0-9]` to match on `example.0`, `example.1`, …)
// - `[!...]` to negate a range of characters to match in a path segment (e.g., `example.[!0-9]` to match on `example.a`, `example.b`, but not `example.0`)
GlobPattern string `json:"globPattern"`
// Kind is the kind of events of interest. If omitted it defaults
// to WatchKind.Create | WatchKind.Change | WatchKind.Delete
// which is 7.
Kind WatchKind `json:"kind,omitempty"`
}
// WatchKind kind of FileSystemWatcher kind.
type WatchKind float64
const (
// WatchKindCreate interested in create events.
WatchKindCreate WatchKind = 1
// WatchKindChange interested in change events.
WatchKindChange WatchKind = 2
// WatchKindDelete interested in delete events.
WatchKindDelete WatchKind = 4
)
// String implements fmt.Stringer.
func (k WatchKind) String() string {
switch k {
case WatchKindCreate:
return "Create"
case WatchKindChange:
return "Change"
case WatchKindDelete:
return "Delete"
default:
return strconv.FormatFloat(float64(k), 'f', -10, 64)
}
}
// WorkspaceSymbolParams is the parameters of a Workspace Symbol request.
type WorkspaceSymbolParams struct {
WorkDoneProgressParams
PartialResultParams
// Query a query string to filter symbols by.
//
// Clients may send an empty string here to request all symbols.
Query string `json:"query"`
}
// ExecuteCommandParams params of Execute a command.
type ExecuteCommandParams struct {
WorkDoneProgressParams
// Command is the identifier of the actual command handler.
Command string `json:"command"`
// Arguments that the command should be invoked with.
Arguments []any `json:"arguments,omitempty"`
}
// ExecuteCommandRegistrationOptions execute command registration options.
type ExecuteCommandRegistrationOptions struct {
// Commands is the commands to be executed on the server
Commands []string `json:"commands"`
}
// ApplyWorkspaceEditParams params of Applies a WorkspaceEdit.
type ApplyWorkspaceEditParams struct {
// Label an optional label of the workspace edit. This label is
// presented in the user interface for example on an undo
// stack to undo the workspace edit.
Label string `json:"label,omitempty"`
// Edit is the edits to apply.
Edit WorkspaceEdit `json:"edit"`
}
// ApplyWorkspaceEditResponse response of Applies a WorkspaceEdit.
type ApplyWorkspaceEditResponse struct {
// Applied indicates whether the edit was applied or not.
Applied bool `json:"applied"`
// FailureReason an optional textual description for why the edit was not applied.
// This may be used by the server for diagnostic logging or to provide
// a suitable error for a request that triggered the edit.
//
// @since 3.16.0.
FailureReason string `json:"failureReason,omitempty"`
// FailedChange depending on the client's failure handling strategy "failedChange"
// might contain the index of the change that failed. This property is
// only available if the client signals a "failureHandlingStrategy"
// in its client capabilities.
//
// @since 3.16.0.
FailedChange uint32 `json:"failedChange,omitempty"`
}
// Copyright 2019 The Go Language Server Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package uri
import (
"errors"
"fmt"
"net/url"
"path/filepath"
"runtime"
"strings"
"unicode"
)
const (
// FileScheme schema of filesystem path.
FileScheme = "file"
// HTTPScheme schema of http.
HTTPScheme = "http"
// HTTPSScheme schema of https.
HTTPSScheme = "https"
)
const (
hierPart = "://"
)
// URI Uniform Resource Identifier (URI) https://tools.ietf.org/html/rfc3986.
//
// This class is a simple parser which creates the basic component parts
// (http://tools.ietf.org/html/rfc3986#section-3) with minimal validation
// and encoding.
//
// foo://example.com:8042/over/there?name=ferret#nose
// \_/ \______________/\_________/ \_________/ \__/
// | | | | |
// scheme authority path query fragment
// | _____________________|__
// / \ / \
// urn:example:animal:ferret:nose
type URI string
// Filename returns the file path for the given URI.
// It is an error to call this on a URI that is not a valid filename.
func (u URI) Filename() string {
filename, err := filename(u)
if err != nil {
panic(err)
}
return filepath.FromSlash(filename)
}
func filename(uri URI) (string, error) {
u, err := url.ParseRequestURI(string(uri))
if err != nil {
return "", fmt.Errorf("failed to parse request URI: %w", err)
}
if u.Scheme != FileScheme {
return "", fmt.Errorf("only file URIs are supported, got %v", u.Scheme)
}
if isWindowsDriveURI(u.Path) {
u.Path = u.Path[1:]
}
return u.Path, nil
}
// New parses and creates a new URI from s.
func New(s string) URI {
if u, err := url.PathUnescape(s); err == nil {
s = u
}
if strings.HasPrefix(s, FileScheme+hierPart) {
return URI(s)
}
return File(s)
}
// File parses and creates a new filesystem URI from path.
func File(path string) URI {
const goRootPragma = "$GOROOT"
if len(path) >= len(goRootPragma) && strings.EqualFold(goRootPragma, path[:len(goRootPragma)]) {
path = runtime.GOROOT() + path[len(goRootPragma):]
}
if !isWindowsDrivePath(path) {
if abs, err := filepath.Abs(path); err == nil {
path = abs
}
}
if isWindowsDrivePath(path) {
path = "/" + path
}
path = filepath.ToSlash(path)
u := url.URL{
Scheme: FileScheme,
Path: path,
}
return URI(u.String())
}
// Parse parses and creates a new URI from s.
func Parse(s string) (u URI, err error) {
us, err := url.Parse(s)
if err != nil {
return u, fmt.Errorf("url.Parse: %w", err)
}
switch us.Scheme {
case FileScheme:
ut := url.URL{
Scheme: FileScheme,
Path: us.Path,
RawPath: filepath.FromSlash(us.Path),
}
u = URI(ut.String())
case HTTPScheme, HTTPSScheme:
ut := url.URL{
Scheme: us.Scheme,
Host: us.Host,
Path: us.Path,
RawQuery: us.Query().Encode(),
Fragment: us.Fragment,
}
u = URI(ut.String())
default:
return u, errors.New("unknown scheme")
}
return
}
// From returns the new URI from args.
func From(scheme, authority, path, query, fragment string) URI {
switch scheme {
case FileScheme:
u := url.URL{
Scheme: FileScheme,
Path: path,
RawPath: filepath.FromSlash(path),
}
return URI(u.String())
case HTTPScheme, HTTPSScheme:
u := url.URL{
Scheme: scheme,
Host: authority,
Path: path,
RawQuery: url.QueryEscape(query),
Fragment: fragment,
}
return URI(u.String())
default:
panic(fmt.Sprintf("unknown scheme: %s", scheme))
}
}
// isWindowsDrivePath returns true if the file path is of the form used by Windows.
//
// We check if the path begins with a drive letter, followed by a ":".
func isWindowsDrivePath(path string) bool {
if len(path) < 4 {
return false
}
return unicode.IsLetter(rune(path[0])) && path[1] == ':'
}
// isWindowsDriveURI returns true if the file URI is of the format used by
// Windows URIs. The url.Parse package does not specially handle Windows paths
// (see https://golang.org/issue/6027). We check if the URI path has
// a drive prefix (e.g. "/C:"). If so, we trim the leading "/".
func isWindowsDriveURI(uri string) bool {
if len(uri) < 4 {
return false
}
return uri[0] == '/' && unicode.IsLetter(rune(uri[1])) && uri[2] == ':'
}
// Copyright 2020 The Go Language Server Authors
// SPDX-License-Identifier: BSD-3-Clause
// Package xcontext is a package to offer the extra functionality we need
// from contexts that is not available from the standard context package.
package xcontext
import (
"context"
"time"
)
// Detach returns a context that keeps all the values of its parent context
// but detaches from the cancellation and error handling.
func Detach(ctx context.Context) context.Context { return detachedContext{ctx} }
type detachedContext struct{ parent context.Context }
func (v detachedContext) Deadline() (time.Time, bool) { return time.Time{}, false }
func (v detachedContext) Done() <-chan struct{} { return nil }
func (v detachedContext) Err() error { return nil }
func (v detachedContext) Value(key any) any { return v.parent.Value(key) }
package templ
import (
"context"
"io"
"sync/atomic"
)
// onceHandleIndex is used to identify unique once handles in a program run.
var onceHandleIndex int64
type OnceOpt func(*OnceHandle)
// WithOnceComponent sets the component to be rendered once per context.
// This can be used instead of setting the children of the `Once` method,
// for example, if creating a code component outside of a templ HTML template.
func WithComponent(c Component) OnceOpt {
return func(o *OnceHandle) {
o.c = c
}
}
// NewOnceHandle creates a OnceHandle used to ensure that the children of its
// `Once` method are only rendered once per context.
func NewOnceHandle(opts ...OnceOpt) *OnceHandle {
oh := &OnceHandle{
id: atomic.AddInt64(&onceHandleIndex, 1),
}
for _, opt := range opts {
opt(oh)
}
return oh
}
// OnceHandle is used to ensure that the children of its `Once` method are are only
// rendered once per context.
type OnceHandle struct {
// id is used to identify which instance of the OnceHandle is being used.
// The OnceHandle can't be an empty struct, because:
//
// | Two distinct zero-size variables may
// | have the same address in memory
//
// https://go.dev/ref/spec#Size_and_alignment_guarantees
id int64
// c is the component to be rendered once per context.
// if c is nil, the children of the `Once` method are rendered.
c Component
}
// Once returns a component that renders its children once per context.
func (o *OnceHandle) Once() Component {
return ComponentFunc(func(ctx context.Context, w io.Writer) (err error) {
_, v := getContext(ctx)
if v.getHasBeenRendered(o) {
return nil
}
v.setHasBeenRendered(o)
if o.c != nil {
return o.c.Render(ctx, w)
}
return GetChildren(ctx).Render(ctx, w)
})
}
package parser
import (
"github.com/a-h/parse"
"github.com/a-h/templ/parser/v2/goexpression"
)
var callTemplateExpression callTemplateExpressionParser
var callTemplateExpressionStart = parse.Or(parse.String("{! "), parse.String("{!"))
type callTemplateExpressionParser struct{}
func (p callTemplateExpressionParser) Parse(pi *parse.Input) (n Node, ok bool, err error) {
// Check the prefix first.
if _, ok, err = callTemplateExpressionStart.Parse(pi); err != nil || !ok {
return
}
// Once we have a prefix, we must have an expression that returns a template.
var r CallTemplateExpression
if r.Expression, err = parseGo("call template expression", pi, goexpression.Expression); err != nil {
return
}
// Eat the final brace.
if _, ok, err = closeBraceWithOptionalPadding.Parse(pi); err != nil || !ok {
err = parse.Error("call template expression: missing closing brace", pi.Position())
return
}
return r, true, nil
}
package parser
import (
"github.com/a-h/parse"
)
var childrenExpressionParser = parse.StringFrom(
openBraceWithOptionalPadding,
parse.OptionalWhitespace,
parse.String("children..."),
parse.OptionalWhitespace,
closeBraceWithOptionalPadding,
)
var childrenExpression = parse.Func(func(in *parse.Input) (n Node, ok bool, err error) {
_, ok, err = childrenExpressionParser.Parse(in)
if err != nil || !ok {
return
}
return ChildrenExpression{}, true, nil
})
package parser
import (
"github.com/a-h/parse"
"github.com/a-h/templ/parser/v2/goexpression"
)
var conditionalAttribute parse.Parser[ConditionalAttribute] = conditionalAttributeParser{}
type conditionalAttributeParser struct{}
func (conditionalAttributeParser) Parse(pi *parse.Input) (r ConditionalAttribute, ok bool, err error) {
start := pi.Index()
// Strip leading whitespace and look for `if `.
if _, _, err = parse.OptionalWhitespace.Parse(pi); err != nil {
return
}
if !peekPrefix(pi, "if ") {
pi.Seek(start)
return
}
// Parse the Go if expression.
if r.Expression, err = parseGo("if attribute", pi, goexpression.If); err != nil {
return
}
// Eat " {\n".
if _, ok, err = openBraceWithOptionalPadding.Parse(pi); err != nil || !ok {
err = parse.Error("attribute if: unterminated (missing closing '{\n')", pi.PositionAt(start))
return
}
if _, _, err = parse.OptionalWhitespace.Parse(pi); err != nil {
return
}
// Read the 'Then' attributes.
// If there's no match, there's a problem reading the attributes.
if r.Then, ok, err = (attributesParser{}).Parse(pi); err != nil || !ok {
err = parse.Error("attribute if: expected attributes in block, but none were found", pi.Position())
return
}
if len(r.Then) == 0 {
err = parse.Error("attribute if: invalid content or no attributes were found in the if block", pi.Position())
return
}
// Read the optional 'Else' Nodes.
if r.Else, ok, err = attributeElseExpression.Parse(pi); err != nil {
return
}
if ok && len(r.Else) == 0 {
err = parse.Error("attribute if: invalid content or no attributes were found in the else block", pi.Position())
return
}
// Clear any optional whitespace.
_, _, _ = parse.OptionalWhitespace.Parse(pi)
// Read the required closing brace.
if _, ok, err = closeBraceWithOptionalPadding.Parse(pi); err != nil || !ok {
err = parse.Error("attribute if: missing end (expected '}')", pi.Position())
return
}
return r, true, nil
}
var attributeElseExpression parse.Parser[[]Attribute] = attributeElseExpressionParser{}
type attributeElseExpressionParser struct{}
func (attributeElseExpressionParser) Parse(in *parse.Input) (r []Attribute, ok bool, err error) {
start := in.Index()
// Strip any initial whitespace.
_, _, _ = parse.OptionalWhitespace.Parse(in)
// } else {
var endElseParser = parse.All(
parse.Rune('}'),
parse.OptionalWhitespace,
parse.String("else"),
parse.OptionalWhitespace,
parse.Rune('{'))
if _, ok, err = endElseParser.Parse(in); err != nil || !ok {
in.Seek(start)
return
}
// Else contents
if r, ok, err = (attributesParser{}).Parse(in); err != nil || !ok {
err = parse.Error("attribute if: expected attributes in else block, but none were found", in.Position())
return
}
return r, true, nil
}
package parser
import (
"github.com/a-h/parse"
)
// CSS.
// CSS Parser.
var cssParser = parse.Func(func(pi *parse.Input) (r CSSTemplate, ok bool, err error) {
from := pi.Position()
r = CSSTemplate{
Properties: []CSSProperty{},
}
// Parse the name.
var exp cssExpression
if exp, ok, err = cssExpressionParser.Parse(pi); err != nil || !ok {
return
}
r.Name = exp.Name
r.Expression = exp.Expression
for {
var cssProperty CSSProperty
// Try for an expression CSS declaration.
// background-color: { constants.BackgroundColor };
cssProperty, ok, err = expressionCSSPropertyParser.Parse(pi)
if err != nil {
return
}
if ok {
r.Properties = append(r.Properties, cssProperty)
continue
}
// Try for a constant CSS declaration.
// color: #ffffff;
cssProperty, ok, err = constantCSSPropertyParser.Parse(pi)
if err != nil {
return
}
if ok {
r.Properties = append(r.Properties, cssProperty)
continue
}
// Eat any whitespace.
if _, ok, err = parse.OptionalWhitespace.Parse(pi); err != nil || !ok {
return
}
// Try for }
if _, ok, err = closeBraceWithOptionalPadding.Parse(pi); err != nil || !ok {
err = parse.Error("css property expression: missing closing brace", pi.Position())
return
}
r.Range = NewRange(from, pi.Position())
return r, true, nil
}
})
// css Func() {
type cssExpression struct {
Expression Expression
Name string
}
var cssExpressionParser = parse.Func(func(pi *parse.Input) (r cssExpression, ok bool, err error) {
start := pi.Index()
if !peekPrefix(pi, "css ") {
return r, false, nil
}
// Once we have the prefix, everything to the brace is Go.
// e.g.
// css (x []string) Test() {
// becomes:
// func (x []string) Test() templ.CSSComponent {
if r.Name, r.Expression, err = parseCSSFuncDecl(pi); err != nil {
return r, false, err
}
// Eat " {\n".
if _, ok, err = parse.All(openBraceWithOptionalPadding, parse.NewLine).Parse(pi); err != nil || !ok {
err = parse.Error("css expression: parameters missing open bracket", pi.PositionAt(start))
return
}
return r, true, nil
})
// CSS property name parser.
var cssPropertyNameFirst = "abcdefghijklmnopqrstuvwxyz-"
var cssPropertyNameSubsequent = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-"
var cssPropertyNameParser = parse.Func(func(in *parse.Input) (name string, ok bool, err error) {
start := in.Position()
var prefix, suffix string
if prefix, ok, err = parse.RuneIn(cssPropertyNameFirst).Parse(in); err != nil || !ok {
return
}
if suffix, ok, err = parse.StringUntil(parse.RuneNotIn(cssPropertyNameSubsequent)).Parse(in); err != nil || !ok {
in.Seek(start.Index)
return
}
if len(suffix)+1 > 128 {
ok = false
err = parse.Error("css property names must be < 128 characters long", in.Position())
return
}
return prefix + suffix, true, nil
})
// background-color: {%= constants.BackgroundColor %};
var expressionCSSPropertyParser = parse.Func(func(pi *parse.Input) (r ExpressionCSSProperty, ok bool, err error) {
start := pi.Index()
// Optional whitespace.
if _, ok, err = parse.OptionalWhitespace.Parse(pi); err != nil || !ok {
return
}
// Property name.
if r.Name, ok, err = cssPropertyNameParser.Parse(pi); err != nil || !ok {
pi.Seek(start)
return
}
// <space>:<space>
if _, ok, err = parse.All(parse.OptionalWhitespace, parse.Rune(':'), parse.OptionalWhitespace).Parse(pi); err != nil || !ok {
pi.Seek(start)
return
}
// { string }
var se Node
if se, ok, err = stringExpression.Parse(pi); err != nil || !ok {
pi.Seek(start)
return
}
r.Value = se.(StringExpression)
// ;
if _, ok, err = parse.String(";").Parse(pi); err != nil || !ok {
err = parse.Error("missing expected semicolon (;)", pi.Position())
return
}
// \n
if _, ok, err = parse.NewLine.Parse(pi); err != nil || !ok {
err = parse.Error("missing expected linebreak", pi.Position())
return
}
return r, true, nil
})
// background-color: #ffffff;
var constantCSSPropertyParser = parse.Func(func(pi *parse.Input) (r ConstantCSSProperty, ok bool, err error) {
start := pi.Index()
// Optional whitespace.
if _, ok, err = parse.OptionalWhitespace.Parse(pi); err != nil || !ok {
return
}
// Property name.
if r.Name, ok, err = cssPropertyNameParser.Parse(pi); err != nil || !ok {
pi.Seek(start)
return
}
// <space>:<space>
if _, ok, err = parse.All(parse.OptionalWhitespace, parse.Rune(':'), parse.OptionalWhitespace).Parse(pi); err != nil || !ok {
pi.Seek(start)
return
}
// Everything until ';\n'
untilEnd := parse.All(
parse.OptionalWhitespace,
parse.Rune(';'),
parse.NewLine,
)
if r.Value, ok, err = parse.StringUntil(untilEnd).Parse(pi); err != nil || !ok {
err = parse.Error("missing expected semicolon and linebreak (;\\n", pi.Position())
return
}
// Chomp the ;\n
if _, ok, err = untilEnd.Parse(pi); err != nil || !ok {
err = parse.Error("failed to chomp semicolon and linebreak (;\\n)", pi.Position())
return
}
return r, true, nil
})
package parser
import (
"errors"
)
type diagnoser func(Node) ([]Diagnostic, error)
// Diagnostic for template file.
type Diagnostic struct {
Message string
Range Range
}
func walkTemplate(t TemplateFile, f func(Node) bool) {
for _, n := range t.Nodes {
hn, ok := n.(HTMLTemplate)
if !ok {
continue
}
walkNodes(hn.Children, f)
}
}
func walkNodes(t []Node, f func(Node) bool) {
for _, n := range t {
if !f(n) {
continue
}
if h, ok := n.(CompositeNode); ok {
walkNodes(h.ChildNodes(), f)
}
}
}
var diagnosers = []diagnoser{
useOfLegacyCallSyntaxDiagnoser,
}
func Diagnose(t TemplateFile) ([]Diagnostic, error) {
var diags []Diagnostic
var errs error
walkTemplate(t, func(n Node) bool {
for _, d := range diagnosers {
diag, err := d(n)
if err != nil {
errs = errors.Join(errs, err)
return false
}
diags = append(diags, diag...)
}
return true
})
return diags, errs
}
func useOfLegacyCallSyntaxDiagnoser(n Node) ([]Diagnostic, error) {
if c, ok := n.(CallTemplateExpression); ok {
return []Diagnostic{{
Message: "`{! foo }` syntax is deprecated. Use `@foo` syntax instead. Run `templ fmt .` to fix all instances.",
Range: c.Expression.Range,
}}, nil
}
return nil, nil
}
package parser
import (
"github.com/a-h/parse"
)
var doctypeStartParser = parse.StringInsensitive("<!doctype ")
var untilLtOrGt = parse.Or(lt, gt)
var stringUntilLtOrGt = parse.StringUntil(untilLtOrGt)
var docType = parse.Func(func(pi *parse.Input) (n Node, ok bool, err error) {
start := pi.Position()
var r DocType
if _, ok, err = doctypeStartParser.Parse(pi); err != nil || !ok {
return
}
// Once a doctype has started, take everything until the end.
if r.Value, ok, err = stringUntilLtOrGt.Parse(pi); err != nil || !ok {
err = parse.Error("unclosed DOCTYPE", start)
return
}
// Clear the final '>'.
if _, ok, err = gt.Parse(pi); err != nil || !ok {
err = parse.Error("unclosed DOCTYPE", start)
return
}
return r, true, nil
})
package parser
import (
"fmt"
"html"
"strings"
"github.com/a-h/parse"
"github.com/a-h/templ/parser/v2/goexpression"
)
// Element.
// Element open tag.
type elementOpenTag struct {
Name string
Attributes []Attribute
IndentAttrs bool
NameRange Range
Void bool
}
var elementOpenTagParser = parse.Func(func(pi *parse.Input) (e elementOpenTag, ok bool, err error) {
start := pi.Position()
// <
if _, ok, err = lt.Parse(pi); err != nil || !ok {
return
}
// Element name.
l := pi.Position().Line
if e.Name, ok, err = elementNameParser.Parse(pi); err != nil || !ok {
pi.Seek(start.Index)
return
}
e.NameRange = NewRange(pi.PositionAt(pi.Index()-len(e.Name)), pi.Position())
if e.Attributes, ok, err = (attributesParser{}).Parse(pi); err != nil || !ok {
pi.Seek(start.Index)
return
}
// If any attribute is not on the same line as the element name, indent them.
if pi.Position().Line != l {
e.IndentAttrs = true
}
// Optional whitespace.
if _, _, err = parse.OptionalWhitespace.Parse(pi); err != nil {
pi.Seek(start.Index)
return
}
// />
if _, ok, err = parse.String("/>").Parse(pi); err != nil {
return
}
if ok {
e.Void = true
return
}
// >
if _, ok, err = gt.Parse(pi); err != nil {
return
}
// If it's not a self-closing or complete open element, we have an error.
if !ok {
err = parse.Error(fmt.Sprintf("<%s>: malformed open element", e.Name), pi.Position())
return
}
return e, true, nil
})
// Attribute name.
var (
attributeNameFirst = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ:_@"
attributeNameSubsequent = attributeNameFirst + "-.0123456789*"
attributeNameParser = parse.Func(func(in *parse.Input) (name string, ok bool, err error) {
start := in.Index()
var prefix, suffix string
if prefix, ok, err = parse.RuneIn(attributeNameFirst).Parse(in); err != nil || !ok {
return
}
if suffix, ok, err = parse.StringUntil(parse.RuneNotIn(attributeNameSubsequent)).Parse(in); err != nil {
in.Seek(start)
return
}
if len(suffix)+1 > 128 {
ok = false
err = parse.Error("attribute names must be < 128 characters long", in.Position())
return
}
return prefix + suffix, true, nil
})
)
type attributeValueParser struct {
EqualsAndQuote parse.Parser[string]
Suffix parse.Parser[string]
UseSingleQuote bool
}
func (avp attributeValueParser) Parse(pi *parse.Input) (value string, ok bool, err error) {
start := pi.Index()
if _, ok, err = avp.EqualsAndQuote.Parse(pi); err != nil || !ok {
return
}
if value, ok, err = parse.StringUntil(avp.Suffix).Parse(pi); err != nil || !ok {
pi.Seek(start)
return
}
if _, ok, err = avp.Suffix.Parse(pi); err != nil || !ok {
pi.Seek(start)
return
}
return value, true, nil
}
// Constant attribute.
var (
attributeValueParsers = []attributeValueParser{
// Double quoted.
{EqualsAndQuote: parse.String(`="`), Suffix: parse.String(`"`), UseSingleQuote: false},
// Single quoted.
{EqualsAndQuote: parse.String(`='`), Suffix: parse.String(`'`), UseSingleQuote: true},
// Unquoted.
// A valid unquoted attribute value in HTML is any string of text that is not an empty string,
// and that doesn’t contain spaces, tabs, line feeds, form feeds, carriage returns, ", ', `, =, <, or >.
{EqualsAndQuote: parse.String("="), Suffix: parse.Any(parse.RuneIn(" \t\n\r\"'`=<>/"), parse.EOF[string]()), UseSingleQuote: false},
}
constantAttributeParser = parse.Func(func(pi *parse.Input) (attr ConstantAttribute, ok bool, err error) {
start := pi.Index()
// Optional whitespace leader.
if _, ok, err = parse.OptionalWhitespace.Parse(pi); err != nil || !ok {
return
}
// Attribute name.
if attr.Name, ok, err = attributeNameParser.Parse(pi); err != nil || !ok {
pi.Seek(start)
return
}
attr.NameRange = NewRange(pi.PositionAt(pi.Index()-len(attr.Name)), pi.Position())
for _, p := range attributeValueParsers {
attr.Value, ok, err = p.Parse(pi)
if err != nil {
pos := pi.Position()
if pErr, isParseError := err.(parse.ParseError); isParseError {
pos = pErr.Pos
}
return attr, false, parse.Error(fmt.Sprintf("%s: %v", attr.Name, err), pos)
}
if ok {
attr.SingleQuote = p.UseSingleQuote
break
}
}
if !ok {
pi.Seek(start)
return attr, false, nil
}
attr.Value = html.UnescapeString(attr.Value)
// Only use single quotes if actually required, due to double quote in the value (prefer double quotes).
attr.SingleQuote = attr.SingleQuote && strings.Contains(attr.Value, "\"")
return attr, true, nil
})
)
// BoolConstantAttribute.
var boolConstantAttributeParser = parse.Func(func(pi *parse.Input) (attr BoolConstantAttribute, ok bool, err error) {
start := pi.Index()
// Optional whitespace leader.
if _, ok, err = parse.OptionalWhitespace.Parse(pi); err != nil || !ok {
return
}
// Attribute name.
if attr.Name, ok, err = attributeNameParser.Parse(pi); err != nil || !ok {
pi.Seek(start)
return
}
attr.NameRange = NewRange(pi.PositionAt(pi.Index()-len(attr.Name)), pi.Position())
// We have a name, but if we have an equals sign, it's not a constant boolean attribute.
next, ok := pi.Peek(1)
if !ok {
err = parse.Error("boolConstantAttributeParser: unexpected EOF after attribute name", pi.Position())
return
}
if next == "=" || next == "?" {
// It's one of the other attribute types.
pi.Seek(start)
return attr, false, nil
}
if !(next == " " || next == "\t" || next == "\r" || next == "\n" || next == "/" || next == ">") {
err = parse.Error(fmt.Sprintf("boolConstantAttributeParser: expected attribute name to end with space, newline, '/>' or '>', but got %q", next), pi.Position())
return attr, false, err
}
return attr, true, nil
})
// BoolExpressionAttribute.
var boolExpressionStart = parse.Or(parse.String("?={ "), parse.String("?={"))
var boolExpressionAttributeParser = parse.Func(func(pi *parse.Input) (r BoolExpressionAttribute, ok bool, err error) {
start := pi.Index()
// Optional whitespace leader.
if _, ok, err = parse.OptionalWhitespace.Parse(pi); err != nil || !ok {
pi.Seek(start)
return
}
// Attribute name.
if r.Name, ok, err = attributeNameParser.Parse(pi); err != nil || !ok {
pi.Seek(start)
return
}
r.NameRange = NewRange(pi.PositionAt(pi.Index()-len(r.Name)), pi.Position())
// Check whether this is a boolean expression attribute.
if _, ok, err = boolExpressionStart.Parse(pi); err != nil || !ok {
pi.Seek(start)
return
}
// Once we have a prefix, we must have an expression that returns a boolean.
if r.Expression, err = parseGo("boolean attribute", pi, goexpression.Expression); err != nil {
return r, false, err
}
// Eat the Final brace.
if _, ok, err = closeBraceWithOptionalPadding.Parse(pi); err != nil || !ok {
err = parse.Error("boolean expression: missing closing brace", pi.Position())
pi.Seek(start)
return
}
return r, true, nil
})
var expressionAttributeParser = parse.Func(func(pi *parse.Input) (attr ExpressionAttribute, ok bool, err error) {
start := pi.Index()
// Optional whitespace leader.
if _, ok, err = parse.OptionalWhitespace.Parse(pi); err != nil || !ok {
return
}
// Attribute name.
if attr.Name, ok, err = attributeNameParser.Parse(pi); err != nil || !ok {
pi.Seek(start)
return
}
attr.NameRange = NewRange(pi.PositionAt(pi.Index()-len(attr.Name)), pi.Position())
// ={
if _, ok, err = parse.Or(parse.String("={ "), parse.String("={")).Parse(pi); err != nil || !ok {
pi.Seek(start)
return
}
// Expression.
if attr.Expression, err = parseGoSliceArgs(pi); err != nil {
return attr, false, err
}
// Eat whitespace, plus the final brace.
if _, _, err = parse.OptionalWhitespace.Parse(pi); err != nil {
return attr, false, err
}
if _, ok, err = closeBrace.Parse(pi); err != nil || !ok {
err = parse.Error("string expression attribute: missing closing brace", pi.Position())
return
}
return attr, true, nil
})
var spreadAttributesParser = parse.Func(func(pi *parse.Input) (attr SpreadAttributes, ok bool, err error) {
start := pi.Index()
// Optional whitespace leader.
if _, ok, err = parse.OptionalWhitespace.Parse(pi); err != nil || !ok {
return
}
// Eat the first brace.
if _, ok, err = openBraceWithOptionalPadding.Parse(pi); err != nil ||
!ok {
pi.Seek(start)
return
}
// Expression.
if attr.Expression, err = parseGo("spread attributes", pi, goexpression.Expression); err != nil {
return
}
// Check if end of expression has "..." for spread.
if !strings.HasSuffix(attr.Expression.Value, "...") {
pi.Seek(start)
ok = false
return
}
// Remove extra spread characters from expression.
attr.Expression.Value = strings.TrimSuffix(attr.Expression.Value, "...")
attr.Expression.Range.To.Col -= 3
attr.Expression.Range.To.Index -= 3
// Eat the final brace.
if _, ok, err = closeBraceWithOptionalPadding.Parse(pi); err != nil || !ok {
err = parse.Error("attribute spread expression: missing closing brace", pi.Position())
return
}
return attr, true, nil
})
// Attributes.
type attributeParser struct{}
func (attributeParser) Parse(in *parse.Input) (out Attribute, ok bool, err error) {
if out, ok, err = boolExpressionAttributeParser.Parse(in); err != nil || ok {
return
}
if out, ok, err = expressionAttributeParser.Parse(in); err != nil || ok {
return
}
if out, ok, err = conditionalAttribute.Parse(in); err != nil || ok {
return
}
if out, ok, err = boolConstantAttributeParser.Parse(in); err != nil || ok {
return
}
if out, ok, err = spreadAttributesParser.Parse(in); err != nil || ok {
return
}
if out, ok, err = constantAttributeParser.Parse(in); err != nil || ok {
return
}
return
}
var attribute attributeParser
type attributesParser struct{}
func (attributesParser) Parse(in *parse.Input) (attributes []Attribute, ok bool, err error) {
for {
var attr Attribute
attr, ok, err = attribute.Parse(in)
if err != nil {
return
}
if !ok {
break
}
attributes = append(attributes, attr)
}
return attributes, true, nil
}
// Element name.
var (
elementNameFirst = "abcdefghijklmnopqrstuvwxyz"
elementNameSubsequent = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-:"
elementNameParser = parse.Func(func(in *parse.Input) (name string, ok bool, err error) {
start := in.Index()
var prefix, suffix string
if prefix, ok, err = parse.RuneIn(elementNameFirst).Parse(in); err != nil || !ok {
return
}
if suffix, ok, err = parse.StringUntil(parse.RuneNotIn(elementNameSubsequent)).Parse(in); err != nil || !ok {
in.Seek(start)
return
}
if len(suffix)+1 > 128 {
ok = false
err = parse.Error("element names must be < 128 characters long", in.Position())
return
}
return prefix + suffix, true, nil
})
)
// Void element closer.
var voidElementCloser voidElementCloserParser
type voidElementCloserParser struct{}
var voidElementCloseTags = []string{"</area>", "</base>", "</br>", "</col>", "</command>", "</embed>", "</hr>", "</img>", "</input>", "</keygen>", "</link>", "</meta>", "</param>", "</source>", "</track>", "</wbr>"}
func (voidElementCloserParser) Parse(pi *parse.Input) (n Node, ok bool, err error) {
var ve string
for _, ve = range voidElementCloseTags {
s, canPeekLen := pi.Peek(len(ve))
if !canPeekLen {
continue
}
if !strings.EqualFold(s, ve) {
continue
}
// Found a match.
ok = true
break
}
if !ok {
return nil, false, nil
}
pi.Take(len(ve))
return nil, true, nil
}
// Element.
var element elementParser
type elementParser struct{}
func (elementParser) Parse(pi *parse.Input) (n Node, ok bool, err error) {
var r Element
start := pi.Position()
// Check the open tag.
var ot elementOpenTag
if ot, ok, err = elementOpenTagParser.Parse(pi); err != nil || !ok {
return
}
r.Name = ot.Name
r.Attributes = ot.Attributes
r.IndentAttrs = ot.IndentAttrs
r.NameRange = ot.NameRange
// Once we've got an open tag, the rest must be present.
l := pi.Position().Line
// If the element is self-closing, even if it's not really a void element (br, hr etc.), we can return early.
if ot.Void || r.IsVoidElement() {
// Escape early, no need to try to parse children for self-closing elements.
return addTrailingSpaceAndValidate(start, r, pi)
}
// Parse children.
closer := StripType(parse.All(parse.String("</"), parse.String(ot.Name), parse.Rune('>')))
tnp := newTemplateNodeParser(closer, fmt.Sprintf("<%s>: close tag", ot.Name))
nodes, _, err := tnp.Parse(pi)
if err != nil {
notFoundErr, isNotFoundError := err.(UntilNotFoundError)
if isNotFoundError {
err = notFoundErr.ParseError
}
return r, false, err
}
r.Children = nodes.Nodes
// If the children are not all on the same line, indent them.
if l != pi.Position().Line {
r.IndentChildren = true
}
// Close tag.
_, ok, err = closer.Parse(pi)
if err != nil {
return r, false, err
}
if !ok {
err = parse.Error(fmt.Sprintf("<%s>: expected end tag not present or invalid tag contents", r.Name), pi.Position())
return r, false, err
}
return addTrailingSpaceAndValidate(start, r, pi)
}
func addTrailingSpaceAndValidate(start parse.Position, e Element, pi *parse.Input) (n Node, ok bool, err error) {
// Elide any void close tags.
if _, _, err = voidElementCloser.Parse(pi); err != nil {
return e, false, err
}
// Add trailing space.
ws, _, err := parse.Whitespace.Parse(pi)
if err != nil {
return e, false, err
}
e.TrailingSpace, err = NewTrailingSpace(ws)
if err != nil {
return e, false, err
}
// Validate.
var msgs []string
if msgs, ok = e.Validate(); !ok {
err = parse.Error(fmt.Sprintf("<%s>: %s", e.Name, strings.Join(msgs, ", ")), start)
return e, false, err
}
return e, true, nil
}
package parser
import (
"strings"
"github.com/a-h/parse"
)
// StripType takes the parser and throws away the return value.
func StripType[T any](p parse.Parser[T]) parse.Parser[any] {
return parse.Func(func(in *parse.Input) (out any, ok bool, err error) {
return p.Parse(in)
})
}
func ExpressionOf(p parse.Parser[string]) parse.Parser[Expression] {
return parse.Func(func(in *parse.Input) (out Expression, ok bool, err error) {
from := in.Position()
var exp string
if exp, ok, err = p.Parse(in); err != nil || !ok {
return
}
return NewExpression(exp, from, in.Position()), true, nil
})
}
var lt = parse.Rune('<')
var gt = parse.Rune('>')
var openBrace = parse.String("{")
var optionalSpaces = parse.StringFrom(parse.Optional(
parse.AtLeast(1, parse.Rune(' '))))
var openBraceWithPadding = parse.StringFrom(optionalSpaces,
openBrace,
optionalSpaces)
var openBraceWithOptionalPadding = parse.Any(openBraceWithPadding, openBrace)
var closeBrace = parse.String("}")
var closeBraceWithOptionalPadding = parse.StringFrom(optionalSpaces, closeBrace)
var dblCloseBrace = parse.String("}}")
var dblCloseBraceWithOptionalPadding = parse.StringFrom(optionalSpaces, dblCloseBrace)
var openBracket = parse.String("(")
var closeBracket = parse.String(")")
var stringUntilNewLine = parse.StringUntil(parse.NewLine)
var newLineOrEOF = parse.Or(parse.NewLine, parse.EOF[string]())
var stringUntilNewLineOrEOF = parse.StringUntil(newLineOrEOF)
var jsOrGoSingleLineComment = parse.StringFrom(parse.String("//"), parse.StringUntil(parse.Any(parse.NewLine, parse.EOF[string]())))
var jsOrGoMultiLineComment = parse.StringFrom(parse.String("/*"), parse.StringUntil(parse.String("*/")))
var exp = expressionParser{
startBraceCount: 1,
}
type expressionParser struct {
startBraceCount int
}
func (p expressionParser) Parse(pi *parse.Input) (s Expression, ok bool, err error) {
from := pi.Position()
braceCount := p.startBraceCount
sb := new(strings.Builder)
loop:
for {
var result string
// Try to parse a single line comment.
if result, ok, err = jsOrGoSingleLineComment.Parse(pi); err != nil {
return
}
if ok {
sb.WriteString(result)
continue
}
// Try to parse a multi-line comment.
if result, ok, err = jsOrGoMultiLineComment.Parse(pi); err != nil {
return
}
if ok {
sb.WriteString(result)
continue
}
// Try to read a string literal.
if result, ok, err = string_lit.Parse(pi); err != nil {
return
}
if ok {
sb.WriteString(result)
continue
}
// Also try for a rune literal.
if result, ok, err = rune_lit.Parse(pi); err != nil {
return
}
if ok {
sb.WriteString(result)
continue
}
// Try opener.
if result, ok, err = openBrace.Parse(pi); err != nil {
return
}
if ok {
braceCount++
sb.WriteString(result)
continue
}
// Try closer.
startOfCloseBrace := pi.Index()
if result, ok, err = closeBraceWithOptionalPadding.Parse(pi); err != nil {
return
}
if ok {
braceCount--
if braceCount < 0 {
err = parse.Error("expression: too many closing braces", pi.Position())
return
}
if braceCount == 0 {
pi.Seek(startOfCloseBrace)
break loop
}
sb.WriteString(result)
continue
}
// Read anything else.
var c string
c, ok = pi.Take(1)
if !ok {
break loop
}
if rune(c[0]) == 65533 { // Invalid Unicode.
break loop
}
sb.WriteString(c)
}
if braceCount != 0 {
err = parse.Error("expression: unexpected brace count", pi.Position())
return
}
return NewExpression(sb.String(), from, pi.Position()), true, nil
}
// Letters and digits
var octal_digit = parse.RuneIn("01234567")
var hex_digit = parse.RuneIn("0123456789ABCDEFabcdef")
// https://go.dev/ref/spec#Rune_literals
var rune_lit = parse.StringFrom(
parse.Rune('\''),
parse.StringFrom(parse.Until(
parse.Any(unicode_value_rune, byte_value),
parse.Rune('\''),
)),
parse.Rune('\''),
)
var unicode_value_rune = parse.Any(little_u_value, big_u_value, escaped_char, parse.RuneNotIn("'"))
// byte_value = octal_byte_value | hex_byte_value .
var byte_value = parse.Any(octal_byte_value, hex_byte_value)
// octal_byte_value = `\` octal_digit octal_digit octal_digit .
var octal_byte_value = parse.StringFrom(
parse.String(`\`),
octal_digit, octal_digit, octal_digit,
)
// hex_byte_value = `\` "x" hex_digit hex_digit .
var hex_byte_value = parse.StringFrom(
parse.String(`\x`),
hex_digit, hex_digit,
)
// little_u_value = `\` "u" hex_digit hex_digit hex_digit hex_digit .
var little_u_value = parse.StringFrom(
parse.String(`\u`),
hex_digit, hex_digit,
hex_digit, hex_digit,
)
// big_u_value = `\` "U" hex_digit hex_digit hex_digit hex_digit
var big_u_value = parse.StringFrom(
parse.String(`\U`),
hex_digit, hex_digit, hex_digit, hex_digit,
hex_digit, hex_digit, hex_digit, hex_digit,
)
// escaped_char = `\` ( "a" | "b" | "f" | "n" | "r" | "t" | "v" | `\` | "'" | `"` ) .
var escaped_char = parse.StringFrom(
parse.Rune('\\'),
parse.Any(
parse.Rune('a'),
parse.Rune('b'),
parse.Rune('f'),
parse.Rune('n'),
parse.Rune('r'),
parse.Rune('t'),
parse.Rune('v'),
parse.Rune('\\'),
parse.Rune('\''),
parse.Rune('"'),
),
)
// https://go.dev/ref/spec#String_literals
var string_lit = parse.Any(parse.String(`""`), parse.String(`''`), interpreted_string_lit, raw_string_lit)
var interpreted_string_lit = parse.StringFrom(
parse.Rune('"'),
parse.StringFrom(parse.Until(
parse.Any(unicode_value_interpreted, byte_value),
parse.Rune('"'),
)),
parse.Rune('"'),
)
var unicode_value_interpreted = parse.Any(little_u_value, big_u_value, escaped_char, parse.RuneNotIn("\n\""))
var raw_string_lit = parse.StringFrom(
parse.Rune('`'),
parse.StringFrom(parse.Until(
unicode_value_raw,
parse.Rune('`'),
)),
parse.Rune('`'),
)
var unicode_value_raw = parse.Any(little_u_value, big_u_value, escaped_char, parse.RuneNotIn("`"))
package parser
import (
"github.com/a-h/parse"
"github.com/a-h/templ/parser/v2/goexpression"
)
var forExpression parse.Parser[Node] = forExpressionParser{}
type forExpressionParser struct{}
func (forExpressionParser) Parse(pi *parse.Input) (n Node, ok bool, err error) {
var r ForExpression
start := pi.Index()
// Strip leading whitespace and look for `for `.
if _, _, err = parse.OptionalWhitespace.Parse(pi); err != nil {
return r, false, err
}
if !peekPrefix(pi, "for ") {
pi.Seek(start)
return r, false, nil
}
// Parse the Go for expression.
if r.Expression, err = parseGo("for", pi, goexpression.For); err != nil {
return r, false, err
}
// Eat " {\n".
if _, ok, err = parse.All(openBraceWithOptionalPadding, parse.NewLine).Parse(pi); err != nil || !ok {
pi.Seek(start)
return r, false, err
}
// Node contents.
tnp := newTemplateNodeParser(closeBraceWithOptionalPadding, "for expression closing brace")
var nodes Nodes
if nodes, ok, err = tnp.Parse(pi); err != nil || !ok {
err = parse.Error("for: expected nodes, but none were found", pi.Position())
return
}
r.Children = nodes.Nodes
// Read the required closing brace.
if _, ok, err = closeBraceWithOptionalPadding.Parse(pi); err != nil || !ok {
err = parse.Error("for: "+unterminatedMissingEnd, pi.Position())
return
}
return r, true, nil
}
package parser
import (
"github.com/a-h/parse"
"github.com/a-h/templ/parser/v2/goexpression"
)
var goCode = parse.Func(func(pi *parse.Input) (n Node, ok bool, err error) {
// Check the prefix first.
if _, ok, err = parse.Or(parse.String("{{ "), parse.String("{{")).Parse(pi); err != nil || !ok {
return
}
// Once we have a prefix, we must have an expression that returns a string, with optional err.
l := pi.Position().Line
var r GoCode
if r.Expression, err = parseGo("go code", pi, goexpression.Expression); err != nil {
return r, false, err
}
if l != pi.Position().Line {
r.Multiline = true
}
// Clear any optional whitespace.
_, _, _ = parse.OptionalWhitespace.Parse(pi)
// }}
if _, ok, err = dblCloseBraceWithOptionalPadding.Parse(pi); err != nil || !ok {
err = parse.Error("go code: missing close braces", pi.Position())
return
}
// Parse trailing whitespace.
ws, _, err := parse.Whitespace.Parse(pi)
if err != nil {
return r, false, err
}
r.TrailingSpace, err = NewTrailingSpace(ws)
if err != nil {
return r, false, err
}
return r, true, nil
})
package parser
import (
"github.com/a-h/parse"
)
var goSingleLineCommentStart = parse.String("//")
var goSingleLineCommentEnd = parse.Any(parse.NewLine, parse.EOF[string]())
type goSingleLineCommentParser struct {
}
var goSingleLineComment = goSingleLineCommentParser{}
func (p goSingleLineCommentParser) Parse(pi *parse.Input) (n Node, ok bool, err error) {
// Comment start.
var c GoComment
if _, ok, err = goSingleLineCommentStart.Parse(pi); err != nil || !ok {
return
}
// Once we've got the comment start sequence, parse anything until the end
// sequence as the comment contents.
if c.Contents, ok, err = parse.StringUntil(goSingleLineCommentEnd).Parse(pi); err != nil || !ok {
err = parse.Error("expected end comment literal '\n' not found", pi.Position())
return
}
// Move past the end element.
_, _, _ = goSingleLineCommentEnd.Parse(pi)
// Return the comment.
c.Multiline = false
return c, true, nil
}
var goMultiLineCommentStart = parse.String("/*")
var goMultiLineCommentEnd = parse.String("*/")
type goMultiLineCommentParser struct {
}
var goMultiLineComment = goMultiLineCommentParser{}
func (p goMultiLineCommentParser) Parse(pi *parse.Input) (n Node, ok bool, err error) {
// Comment start.
start := pi.Position()
var c GoComment
if _, ok, err = goMultiLineCommentStart.Parse(pi); err != nil || !ok {
return
}
// Once we've got the comment start sequence, parse anything until the end
// sequence as the comment contents.
if c.Contents, ok, err = parse.StringUntil(goMultiLineCommentEnd).Parse(pi); err != nil || !ok {
err = parse.Error("expected end comment literal '*/' not found", start)
return
}
// Move past the end element.
_, _, _ = goMultiLineCommentEnd.Parse(pi)
// Return the comment.
c.Multiline = true
return c, true, nil
}
var goComment = parse.Any(goSingleLineComment, goMultiLineComment)
package goexpression
import (
"errors"
"fmt"
"go/ast"
"go/parser"
"go/scanner"
"go/token"
"regexp"
"strings"
"unicode"
)
var (
ErrContainerFuncNotFound = errors.New("parser error: templ container function not found")
ErrExpectedNodeNotFound = errors.New("parser error: expected node not found")
)
var defaultRegexp = regexp.MustCompile(`^default\s*:`)
func Case(content string) (start, end int, err error) {
if !(strings.HasPrefix(content, "case ") || defaultRegexp.MatchString(content)) {
return 0, 0, ErrExpectedNodeNotFound
}
prefix := "switch {\n"
src := prefix + content
start, end, err = extract(src, func(body []ast.Stmt) (start, end int, err error) {
sw, ok := body[0].(*ast.SwitchStmt)
if !ok {
return 0, 0, ErrExpectedNodeNotFound
}
if sw.Body == nil || len(sw.Body.List) == 0 {
return 0, 0, ErrExpectedNodeNotFound
}
stmt, ok := sw.Body.List[0].(*ast.CaseClause)
if !ok {
return 0, 0, ErrExpectedNodeNotFound
}
start = int(stmt.Case) - 1
end = int(stmt.Colon)
return start, end, nil
})
if err != nil {
return 0, 0, err
}
// Since we added a `switch {` prefix, we need to remove it.
start -= len(prefix)
end -= len(prefix)
return start, end, nil
}
func If(content string) (start, end int, err error) {
if !strings.HasPrefix(content, "if") {
return 0, 0, ErrExpectedNodeNotFound
}
return extract(content, func(body []ast.Stmt) (start, end int, err error) {
stmt, ok := body[0].(*ast.IfStmt)
if !ok {
return 0, 0, ErrExpectedNodeNotFound
}
start = int(stmt.If) + len("if")
end = latestEnd(start, stmt.Init, stmt.Cond)
return start, end, nil
})
}
func For(content string) (start, end int, err error) {
if !strings.HasPrefix(content, "for") {
return 0, 0, ErrExpectedNodeNotFound
}
return extract(content, func(body []ast.Stmt) (start, end int, err error) {
stmt := body[0]
switch stmt := stmt.(type) {
case *ast.ForStmt:
start = int(stmt.For) + len("for")
end = latestEnd(start, stmt.Init, stmt.Cond, stmt.Post)
return start, end, nil
case *ast.RangeStmt:
start = int(stmt.For) + len("for")
end = latestEnd(start, stmt.Key, stmt.Value, stmt.X)
return start, end, nil
}
return 0, 0, ErrExpectedNodeNotFound
})
}
func Switch(content string) (start, end int, err error) {
if !strings.HasPrefix(content, "switch") {
return 0, 0, ErrExpectedNodeNotFound
}
return extract(content, func(body []ast.Stmt) (start, end int, err error) {
stmt := body[0]
switch stmt := stmt.(type) {
case *ast.SwitchStmt:
start = int(stmt.Switch) + len("switch")
end = latestEnd(start, stmt.Init, stmt.Tag)
return start, end, nil
case *ast.TypeSwitchStmt:
start = int(stmt.Switch) + len("switch")
end = latestEnd(start, stmt.Init, stmt.Assign)
return start, end, nil
}
return 0, 0, ErrExpectedNodeNotFound
})
}
func TemplExpression(src string) (start, end int, err error) {
var s scanner.Scanner
fset := token.NewFileSet()
file := fset.AddFile("", fset.Base(), len(src))
errorHandler := func(pos token.Position, msg string) {
err = fmt.Errorf("error parsing expression: %v", msg)
}
s.Init(file, []byte(src), errorHandler, scanner.ScanComments)
// Read chains of identifiers, e.g.:
// components.Variable
// components[0].Variable
// components["name"].Function()
// functionCall(withLots(), func() { return true })
ep := NewExpressionParser()
for {
pos, tok, lit := s.Scan()
stop, err := ep.Insert(pos, tok, lit)
if err != nil {
return 0, 0, err
}
if stop {
break
}
}
return 0, ep.End, nil
}
func Expression(src string) (start, end int, err error) {
var s scanner.Scanner
fset := token.NewFileSet()
file := fset.AddFile("", fset.Base(), len(src))
errorHandler := func(pos token.Position, msg string) {
err = fmt.Errorf("error parsing expression: %v", msg)
}
s.Init(file, []byte(src), errorHandler, scanner.ScanComments)
// Read chains of identifiers and constants up until RBRACE, e.g.:
// true
// 123.45 == true
// components.Variable
// components[0].Variable
// components["name"].Function()
// functionCall(withLots(), func() { return true })
// !true
parenDepth := 0
bracketDepth := 0
braceDepth := 0
loop:
for {
pos, tok, lit := s.Scan()
if tok == token.EOF {
break loop
}
switch tok {
case token.LPAREN: // (
parenDepth++
case token.RPAREN: // )
end = int(pos)
parenDepth--
case token.LBRACK: // [
bracketDepth++
case token.RBRACK: // ]
end = int(pos)
bracketDepth--
case token.LBRACE: // {
braceDepth++
case token.RBRACE: // }
braceDepth--
if braceDepth < 0 {
// We've hit the end of the expression.
break loop
}
end = int(pos)
case token.IDENT, token.INT, token.FLOAT, token.IMAG, token.CHAR, token.STRING:
end = int(pos) + len(lit) - 1
case token.SEMICOLON:
continue
case token.COMMENT:
end = int(pos) + len(lit) - 1
case token.ILLEGAL:
return 0, 0, fmt.Errorf("illegal token: %v", lit)
default:
end = int(pos) + len(tok.String()) - 1
}
}
return start, end, nil
}
func SliceArgs(content string) (expr string, err error) {
prefix := "package main\nvar templ_args = []any{"
src := prefix + content + "}"
node, parseErr := parser.ParseFile(token.NewFileSet(), "", src, parser.AllErrors)
if node == nil {
return expr, parseErr
}
var from, to int
inspectFirstNode(node, func(n ast.Node) bool {
decl, ok := n.(*ast.CompositeLit)
if !ok {
return true
}
from = int(decl.Lbrace)
to = int(decl.Rbrace) - 1
for _, e := range decl.Elts {
to = int(e.End()) - 1
}
if to > int(decl.Rbrace)-1 {
to = int(decl.Rbrace) - 1
}
betweenEndAndBrace := src[to : decl.Rbrace-1]
var hasCodeBetweenEndAndBrace bool
for _, r := range betweenEndAndBrace {
if !unicode.IsSpace(r) {
hasCodeBetweenEndAndBrace = true
break
}
}
if hasCodeBetweenEndAndBrace {
to = int(decl.Rbrace) - 1
}
return false
})
return src[from:to], err
}
// Func returns the Go code up to the opening brace of the function body.
func Func(content string) (name, expr string, err error) {
prefix := "package main\n"
src := prefix + content
node, parseErr := parser.ParseFile(token.NewFileSet(), "", src, parser.AllErrors)
if node == nil {
return name, expr, parseErr
}
inspectFirstNode(node, func(n ast.Node) bool {
// Find the first function declaration.
fn, ok := n.(*ast.FuncDecl)
if !ok {
return true
}
start := int(fn.Pos()) + len("func")
end := fn.Type.Params.End() - 1
if len(src) < int(end) {
err = errors.New("parser error: function identifier")
return false
}
expr = strings.Clone(src[start:end])
name = fn.Name.Name
return false
})
return name, expr, err
}
func latestEnd(start int, nodes ...ast.Node) (end int) {
end = start
for _, n := range nodes {
if n == nil {
continue
}
if int(n.End())-1 > end {
end = int(n.End()) - 1
}
}
return end
}
func inspectFirstNode(node ast.Node, f func(ast.Node) bool) {
var stop bool
ast.Inspect(node, func(n ast.Node) bool {
if stop {
return true
}
if f(n) {
return true
}
stop = true
return false
})
}
// Extract a Go expression from the content.
// The Go expression starts at "start" and ends at "end".
// The reader should skip until "length" to pass over the expression and into the next
// logical block.
type Extractor func(body []ast.Stmt) (start, end int, err error)
func extract(content string, extractor Extractor) (start, end int, err error) {
prefix := "package main\nfunc templ_container() {\n"
src := prefix + content
node, parseErr := parser.ParseFile(token.NewFileSet(), "", src, parser.AllErrors)
if node == nil {
return 0, 0, parseErr
}
var found bool
inspectFirstNode(node, func(n ast.Node) bool {
// Find the "templ_container" function.
fn, ok := n.(*ast.FuncDecl)
if !ok {
return true
}
if fn.Name == nil || fn.Name.Name != "templ_container" {
err = ErrContainerFuncNotFound
return false
}
if fn.Body == nil || len(fn.Body.List) == 0 {
err = ErrExpectedNodeNotFound
return false
}
found = true
start, end, err = extractor(fn.Body.List)
return false
})
if !found {
return 0, 0, ErrExpectedNodeNotFound
}
start -= len(prefix)
end -= len(prefix)
if end > len(content) {
end = len(content)
}
if start > end {
start = end
}
return start, end, err
}
package goexpression
import (
"fmt"
"go/token"
)
type Stack[T any] []T
func (s *Stack[T]) Push(v T) {
*s = append(*s, v)
}
func (s *Stack[T]) Pop() (v T) {
if len(*s) == 0 {
return v
}
v = (*s)[len(*s)-1]
*s = (*s)[:len(*s)-1]
return v
}
func (s *Stack[T]) Peek() (v T) {
if len(*s) == 0 {
return v
}
return (*s)[len(*s)-1]
}
var goTokenOpenToClose = map[token.Token]token.Token{
token.LPAREN: token.RPAREN,
token.LBRACE: token.RBRACE,
token.LBRACK: token.RBRACK,
}
var goTokenCloseToOpen = map[token.Token]token.Token{
token.RPAREN: token.LPAREN,
token.RBRACE: token.LBRACE,
token.RBRACK: token.LBRACK,
}
type ErrUnbalanced struct {
Token token.Token
}
func (e ErrUnbalanced) Error() string {
return fmt.Sprintf("unbalanced '%s'", e.Token)
}
func NewExpressionParser() *ExpressionParser {
return &ExpressionParser{
Stack: make(Stack[token.Token], 0),
Previous: token.PERIOD,
Fns: make(Stack[int], 0),
}
}
type ExpressionParser struct {
Stack Stack[token.Token]
End int
Previous token.Token
Fns Stack[int] // Stack of function depths.
}
func (ep *ExpressionParser) setEnd(pos token.Pos, tok token.Token, lit string) {
ep.End = int(pos) + len(tokenString(tok, lit)) - 1
}
func (ep *ExpressionParser) hasSpaceBeforeCurrentToken(pos token.Pos) bool {
return (int(pos) - 1) > ep.End
}
func (ep *ExpressionParser) isTopLevel() bool {
return len(ep.Fns) == 0 && len(ep.Stack) == 0
}
func (ep *ExpressionParser) Insert(
pos token.Pos,
tok token.Token,
lit string,
) (stop bool, err error) {
defer func() {
ep.Previous = tok
}()
// If we've reach the end of the file, terminate reading.
if tok == token.EOF {
// If the EOF was reached, but we're not at the top level, we must have an unbalanced expression.
if !ep.isTopLevel() {
return true, ErrUnbalanced{ep.Stack.Pop()}
}
return true, nil
}
// Handle function literals e.g. func() { fmt.Println("Hello") }
// By pushing the current depth onto the stack, we prevent stopping
// until we've closed the function.
if tok == token.FUNC {
ep.Fns.Push(len(ep.Stack))
ep.setEnd(pos, tok, lit)
return false, nil
}
// If we're opening a pair, we don't stop until we've closed it.
if _, isOpener := goTokenOpenToClose[tok]; isOpener {
// If we're at an open brace, at the top level, where a space has been used, stop.
if tok == token.LBRACE && ep.isTopLevel() {
// Previous was paren, e.g. () {
if ep.Previous == token.RPAREN {
return true, nil
}
// Previous was ident that isn't a type.
// In `name {`, `name` is considered to be a variable.
// In `name{`, `name` is considered to be a type name.
if ep.Previous == token.IDENT && ep.hasSpaceBeforeCurrentToken(pos) {
return true, nil
}
}
ep.Stack.Push(tok)
ep.setEnd(pos, tok, lit)
return false, nil
}
if opener, isCloser := goTokenCloseToOpen[tok]; isCloser {
if len(ep.Stack) == 0 {
// We've got a close token, but there's nothing to close, so we must be done.
return true, nil
}
actual := ep.Stack.Pop()
if !isCloser {
return false, ErrUnbalanced{tok}
}
if actual != opener {
return false, ErrUnbalanced{tok}
}
if tok == token.RBRACE {
// If we're closing a function, pop the function depth.
if len(ep.Stack) == ep.Fns.Peek() {
ep.Fns.Pop()
}
}
ep.setEnd(pos, tok, lit)
return false, nil
}
// If we're in a function literal slice, or pair, we allow anything until we close it.
if len(ep.Fns) > 0 || len(ep.Stack) > 0 {
ep.setEnd(pos, tok, lit)
return false, nil
}
// We allow an ident to follow a period or a closer.
// e.g. "package.name", "typeName{field: value}.name()".
// or "call().name", "call().name()".
// But not "package .name" or "typeName{field: value} .name()".
if tok == token.IDENT && (ep.Previous == token.PERIOD || isCloser(ep.Previous)) {
if isCloser(ep.Previous) && ep.hasSpaceBeforeCurrentToken(pos) {
// This token starts later than the last ending, which means
// there's a space.
return true, nil
}
ep.setEnd(pos, tok, lit)
return false, nil
}
if tok == token.PERIOD && (ep.Previous == token.IDENT || isCloser(ep.Previous)) {
ep.setEnd(pos, tok, lit)
return false, nil
}
// No match, so stop.
return true, nil
}
func tokenString(tok token.Token, lit string) string {
if tok.IsKeyword() || tok.IsOperator() {
return tok.String()
}
return lit
}
func isCloser(tok token.Token) bool {
_, ok := goTokenCloseToOpen[tok]
return ok
}
package parser
import (
"fmt"
"strings"
"github.com/a-h/parse"
"github.com/a-h/templ/parser/v2/goexpression"
)
func parseGoFuncDecl(prefix string, pi *parse.Input) (name string, expression Expression, err error) {
prefix = prefix + " "
from := pi.Index()
src, _ := pi.Peek(-1)
src = strings.TrimPrefix(src, prefix)
name, expr, err := goexpression.Func("func " + src)
if err != nil {
return name, expression, parse.Error(fmt.Sprintf("invalid %s declaration: %v", prefix, err.Error()), pi.Position())
}
pi.Take(len(prefix) + len(expr))
to := pi.Position()
return name, NewExpression(expr, pi.PositionAt(from+len(prefix)), to), nil
}
func parseTemplFuncDecl(pi *parse.Input) (name string, expression Expression, err error) {
return parseGoFuncDecl("templ", pi)
}
func parseCSSFuncDecl(pi *parse.Input) (name string, expression Expression, err error) {
return parseGoFuncDecl("css", pi)
}
func parseGoSliceArgs(pi *parse.Input) (r Expression, err error) {
from := pi.Position()
src, _ := pi.Peek(-1)
expr, err := goexpression.SliceArgs(src)
if err != nil {
return r, err
}
pi.Take(len(expr))
to := pi.Position()
return NewExpression(expr, from, to), nil
}
func peekPrefix(pi *parse.Input, prefixes ...string) bool {
for _, prefix := range prefixes {
pp, ok := pi.Peek(len(prefix))
if !ok {
continue
}
if prefix == pp {
return true
}
}
return false
}
type extractor func(content string) (start, end int, err error)
func parseGo(name string, pi *parse.Input, e extractor) (r Expression, err error) {
from := pi.Index()
src, _ := pi.Peek(-1)
start, end, err := e(src)
if err != nil {
return r, parse.Error(fmt.Sprintf("%s: invalid go expression: %v", name, err.Error()), pi.Position())
}
expr := src[start:end]
pi.Take(end)
return NewExpression(expr, pi.PositionAt(from+start), pi.PositionAt(from+end)), nil
}
package parser
import (
"github.com/a-h/parse"
)
var htmlCommentStart = parse.String("<!--")
var htmlCommentEnd = parse.String("--")
type htmlCommentParser struct {
}
var htmlComment = htmlCommentParser{}
func (p htmlCommentParser) Parse(pi *parse.Input) (n Node, ok bool, err error) {
// Comment start.
start := pi.Position()
var c HTMLComment
if _, ok, err = htmlCommentStart.Parse(pi); err != nil || !ok {
return
}
// Once we've got the comment start sequence, parse anything until the end
// sequence as the comment contents.
if c.Contents, ok, err = parse.StringUntil(htmlCommentEnd).Parse(pi); err != nil || !ok {
err = parse.Error("expected end comment literal '-->' not found", start)
return
}
// Cut the end element.
_, _, _ = htmlCommentEnd.Parse(pi)
// Cut the gt.
if _, ok, err = gt.Parse(pi); err != nil || !ok {
err = parse.Error("comment contains invalid sequence '--'", pi.Position())
return
}
return c, true, nil
}
package parser
import (
"github.com/a-h/parse"
"github.com/a-h/templ/parser/v2/goexpression"
)
var ifExpression ifExpressionParser
var untilElseIfElseOrEnd = parse.Any(StripType(elseIfExpression), StripType(elseExpression), StripType(closeBraceWithOptionalPadding))
type ifExpressionParser struct{}
func (ifExpressionParser) Parse(pi *parse.Input) (n Node, ok bool, err error) {
var r IfExpression
start := pi.Index()
if !peekPrefix(pi, "if ") {
return r, false, nil
}
// Parse the Go if expression.
if r.Expression, err = parseGo("if", pi, goexpression.If); err != nil {
return r, false, err
}
// Eat " {\n".
if _, ok, err = parse.All(openBraceWithOptionalPadding, parse.NewLine).Parse(pi); err != nil || !ok {
err = parse.Error("if: "+unterminatedMissingCurly, pi.PositionAt(start))
return
}
// Once we've had the start of an if block, we must conclude the block.
// Read the 'Then' nodes.
// If there's no match, there's a problem in the template nodes.
np := newTemplateNodeParser(untilElseIfElseOrEnd, "else expression or closing brace")
var thenNodes Nodes
if thenNodes, ok, err = np.Parse(pi); err != nil || !ok {
err = parse.Error("if: expected nodes, but none were found", pi.Position())
return
}
r.Then = thenNodes.Nodes
// Read the optional 'ElseIf' Nodes.
if r.ElseIfs, _, err = parse.ZeroOrMore(elseIfExpression).Parse(pi); err != nil {
return
}
// Read the optional 'Else' Nodes.
var elseNodes Nodes
if elseNodes, _, err = elseExpression.Parse(pi); err != nil {
return
}
r.Else = elseNodes.Nodes
// Read the required closing brace.
if _, ok, err = closeBraceWithOptionalPadding.Parse(pi); err != nil || !ok {
err = parse.Error("if: "+unterminatedMissingEnd, pi.Position())
return
}
return r, true, nil
}
var elseIfExpression parse.Parser[ElseIfExpression] = elseIfExpressionParser{}
type elseIfExpressionParser struct{}
func (elseIfExpressionParser) Parse(pi *parse.Input) (r ElseIfExpression, ok bool, err error) {
start := pi.Index()
// Check the prefix first.
if _, ok, err = parse.All(parse.OptionalWhitespace, closeBrace, parse.OptionalWhitespace, parse.String("else if")).Parse(pi); err != nil || !ok {
pi.Seek(start)
return
}
// Rewind to the start of the `if` statement.
pi.Seek(pi.Index() - 2)
// Parse the Go if expression.
if r.Expression, err = parseGo("else if", pi, goexpression.If); err != nil {
return r, false, err
}
// Eat " {\n".
if _, ok, err = parse.All(openBraceWithOptionalPadding, parse.NewLine).Parse(pi); err != nil || !ok {
err = parse.Error("else if: "+unterminatedMissingCurly, pi.PositionAt(start))
return
}
// Once we've had the start of an if block, we must conclude the block.
// Read the 'Then' nodes.
// If there's no match, there's a problem in the template nodes.
np := newTemplateNodeParser(untilElseIfElseOrEnd, "else expression or closing brace")
var thenNodes Nodes
if thenNodes, ok, err = np.Parse(pi); err != nil || !ok {
err = parse.Error("if: expected nodes, but none were found", pi.Position())
return
}
r.Then = thenNodes.Nodes
return r, true, nil
}
var endElseParser = parse.All(
parse.Rune('}'),
parse.OptionalWhitespace,
parse.String("else"),
parse.OptionalWhitespace,
parse.Rune('{'),
parse.OptionalWhitespace)
var elseExpression parse.Parser[Nodes] = elseExpressionParser{}
type elseExpressionParser struct{}
func (elseExpressionParser) Parse(in *parse.Input) (r Nodes, ok bool, err error) {
start := in.Index()
// } else {
if _, ok, err = endElseParser.Parse(in); err != nil || !ok {
in.Seek(start)
return
}
// Else contents
if r, ok, err = newTemplateNodeParser(closeBraceWithOptionalPadding, "else expression closing brace").Parse(in); err != nil || !ok {
in.Seek(start)
return
}
return r, true, nil
}
package parser
import (
"github.com/a-h/parse"
)
// Package.
var pkg = parse.Func(func(pi *parse.Input) (pkg Package, ok bool, err error) {
start := pi.Position()
// Package prefix.
if _, ok, err = parse.String("package ").Parse(pi); err != nil || !ok {
return
}
// Once we have the prefix, it's an expression until the end of the line.
var exp string
if exp, ok, err = stringUntilNewLine.Parse(pi); err != nil || !ok {
err = parse.Error("package literal not terminated", pi.Position())
return
}
if len(exp) == 0 {
ok = false
err = parse.Error("package literal not terminated", start)
return
}
// Success!
pkg.Expression = NewExpression("package "+exp, start, pi.Position())
return pkg, true, nil
})
package parser
import (
"github.com/a-h/parse"
)
// ) {
var expressionFuncEnd = parse.All(parse.Rune(')'), openBraceWithOptionalPadding)
// Template
var template = parse.Func(func(pi *parse.Input) (r HTMLTemplate, ok bool, err error) {
start := pi.Position()
// templ FuncName(p Person, other Other) {
var te templateExpression
if te, ok, err = templateExpressionParser.Parse(pi); err != nil || !ok {
return
}
r.Expression = te.Expression
// Once we're in a template, we should expect some template whitespace, if/switch/for,
// or node string expressions etc.
var nodes Nodes
nodes, ok, err = newTemplateNodeParser(closeBraceWithOptionalPadding, "template closing brace").Parse(pi)
if err != nil {
return
}
if !ok {
err = parse.Error("templ: expected nodes in templ body, but found none", pi.Position())
return
}
r.Children = nodes.Nodes
// Eat any whitespace.
_, _, err = parse.OptionalWhitespace.Parse(pi)
if err != nil {
return
}
// Try for }
if _, ok, err = closeBraceWithOptionalPadding.Parse(pi); err != nil || !ok {
err = parse.Error("template: missing closing brace", pi.Position())
return
}
r.Range = NewRange(start, pi.Position())
return r, true, nil
})
package parser
import (
"fmt"
"github.com/a-h/parse"
)
var styleElement = rawElementParser{
name: "style",
}
var scriptElement = rawElementParser{
name: "script",
}
type rawElementParser struct {
name string
}
func (p rawElementParser) Parse(pi *parse.Input) (n Node, ok bool, err error) {
start := pi.Index()
// <
if _, ok, err = lt.Parse(pi); err != nil || !ok {
return
}
// Element name.
var e RawElement
if e.Name, ok, err = elementNameParser.Parse(pi); err != nil || !ok {
pi.Seek(start)
return
}
if e.Name != p.name {
pi.Seek(start)
ok = false
return
}
if e.Attributes, ok, err = (attributesParser{}).Parse(pi); err != nil || !ok {
pi.Seek(start)
return
}
// Optional whitespace.
if _, _, err = parse.OptionalWhitespace.Parse(pi); err != nil {
pi.Seek(start)
return
}
// >
if _, ok, err = gt.Parse(pi); err != nil || !ok {
pi.Seek(start)
return
}
// Once we've got an open tag, parse anything until the end tag as the tag contents.
// It's going to be rendered out raw.
end := parse.All(parse.String("</"), parse.String(p.name), parse.String(">"))
if e.Contents, ok, err = parse.StringUntil(end).Parse(pi); err != nil || !ok {
err = parse.Error(fmt.Sprintf("<%s>: expected end tag not present", e.Name), pi.Position())
return
}
// Cut the end element.
_, _, _ = end.Parse(pi)
return e, true, nil
}
package parser
import (
"github.com/a-h/parse"
)
var scriptTemplateParser = parse.Func(func(pi *parse.Input) (r ScriptTemplate, ok bool, err error) {
start := pi.Position()
// Parse the name.
var se scriptExpression
if se, ok, err = scriptExpressionParser.Parse(pi); err != nil || !ok {
pi.Seek(start.Index)
return
}
r.Name = se.Name
r.Parameters = se.Parameters
// Read code expression.
var e Expression
if e, ok, err = exp.Parse(pi); err != nil || !ok {
pi.Seek(start.Index)
return
}
r.Value = e.Value
// Try for }
if _, ok, err = closeBraceWithOptionalPadding.Parse(pi); err != nil || !ok {
err = parse.Error("script template: missing closing brace", pi.Position())
return
}
r.Range = NewRange(start, pi.Position())
return r, true, nil
})
// script Func() {
type scriptExpression struct {
Name Expression
Parameters Expression
}
var scriptExpressionNameParser = ExpressionOf(parse.StringFrom(
parse.Letter,
parse.StringFrom(parse.AtMost(1000, parse.Any(parse.Letter, parse.ZeroToNine))),
))
var scriptExpressionParser = parse.Func(func(pi *parse.Input) (r scriptExpression, ok bool, err error) {
// Check the prefix first.
if _, ok, err = parse.String("script ").Parse(pi); err != nil || !ok {
return
}
// Once we have the prefix, we must have a name and parameters.
// Read the name of the function.
if r.Name, ok, err = scriptExpressionNameParser.Parse(pi); err != nil || !ok {
err = parse.Error("script expression: invalid name", pi.Position())
return
}
// Eat the open bracket.
if _, ok, err = openBracket.Parse(pi); err != nil || !ok {
err = parse.Error("script expression: parameters missing open bracket", pi.Position())
return
}
// Read the parameters.
// p Person, other Other, t thing.Thing)
if r.Parameters, ok, err = ExpressionOf(parse.StringUntil(closeBracket)).Parse(pi); err != nil || !ok {
err = parse.Error("script expression: parameters missing close bracket", pi.Position())
return
}
// Eat ") {".
if _, ok, err = expressionFuncEnd.Parse(pi); err != nil || !ok {
err = parse.Error("script expression: unterminated (missing ') {')", pi.Position())
return
}
// Expect a newline.
if _, ok, err = parse.NewLine.Parse(pi); err != nil || !ok {
err = parse.Error("script expression: missing terminating newline", pi.Position())
return
}
return r, true, nil
})
package parser
import (
"strings"
"unicode/utf8"
)
// NewSourceMap creates a new lookup to map templ source code to items in the
// parsed template.
func NewSourceMap() *SourceMap {
return &SourceMap{
SourceLinesToTarget: make(map[uint32]map[uint32]Position),
TargetLinesToSource: make(map[uint32]map[uint32]Position),
SourceSymbolRangeToTarget: make(map[uint32]map[uint32]Range),
TargetSymbolRangeToSource: make(map[uint32]map[uint32]Range),
}
}
type SourceMap struct {
Expressions []string
SourceLinesToTarget map[uint32]map[uint32]Position
TargetLinesToSource map[uint32]map[uint32]Position
SourceSymbolRangeToTarget map[uint32]map[uint32]Range
TargetSymbolRangeToSource map[uint32]map[uint32]Range
}
func (sm *SourceMap) AddSymbolRange(src Range, tgt Range) {
sm.SourceSymbolRangeToTarget[src.From.Line] = make(map[uint32]Range)
sm.SourceSymbolRangeToTarget[src.From.Line][src.From.Col] = tgt
sm.TargetSymbolRangeToSource[tgt.From.Line] = make(map[uint32]Range)
sm.TargetSymbolRangeToSource[tgt.From.Line][tgt.From.Col] = src
}
func (sm *SourceMap) SymbolTargetRangeFromSource(line, col uint32) (tgt Range, ok bool) {
lm, ok := sm.SourceSymbolRangeToTarget[line]
if !ok {
return
}
tgt, ok = lm[col]
return
}
func (sm *SourceMap) SymbolSourceRangeFromTarget(line, col uint32) (src Range, ok bool) {
lm, ok := sm.TargetSymbolRangeToSource[line]
if !ok {
return
}
src, ok = lm[col]
return
}
// Add an item to the lookup.
func (sm *SourceMap) Add(src Expression, tgt Range) (updatedFrom Position) {
sm.Expressions = append(sm.Expressions, src.Value)
srcIndex := src.Range.From.Index
tgtIndex := tgt.From.Index
lines := strings.Split(src.Value, "\n")
for lineIndex, line := range lines {
srcLine := src.Range.From.Line + uint32(lineIndex)
tgtLine := tgt.From.Line + uint32(lineIndex)
var srcCol, tgtCol uint32
if lineIndex == 0 {
// First line can have an offset.
srcCol += src.Range.From.Col
tgtCol += tgt.From.Col
}
// Process the cols.
for _, r := range line {
if _, ok := sm.SourceLinesToTarget[srcLine]; !ok {
sm.SourceLinesToTarget[srcLine] = make(map[uint32]Position)
}
sm.SourceLinesToTarget[srcLine][srcCol] = NewPosition(tgtIndex, tgtLine, tgtCol)
if _, ok := sm.TargetLinesToSource[tgtLine]; !ok {
sm.TargetLinesToSource[tgtLine] = make(map[uint32]Position)
}
sm.TargetLinesToSource[tgtLine][tgtCol] = NewPosition(srcIndex, srcLine, srcCol)
// Ignore invalid runes.
rlen := utf8.RuneLen(r)
if rlen < 0 {
rlen = 1
}
srcCol += uint32(rlen)
tgtCol += uint32(rlen)
srcIndex += int64(rlen)
tgtIndex += int64(rlen)
}
// LSPs include the newline char as a col.
if _, ok := sm.SourceLinesToTarget[srcLine]; !ok {
sm.SourceLinesToTarget[srcLine] = make(map[uint32]Position)
}
sm.SourceLinesToTarget[srcLine][srcCol] = NewPosition(tgtIndex, tgtLine, tgtCol)
if _, ok := sm.TargetLinesToSource[tgtLine]; !ok {
sm.TargetLinesToSource[tgtLine] = make(map[uint32]Position)
}
sm.TargetLinesToSource[tgtLine][tgtCol] = NewPosition(srcIndex, srcLine, srcCol)
srcIndex++
tgtIndex++
}
return src.Range.From
}
// TargetPositionFromSource looks up the target position using the source position.
func (sm *SourceMap) TargetPositionFromSource(line, col uint32) (tgt Position, ok bool) {
lm, ok := sm.SourceLinesToTarget[line]
if !ok {
return
}
tgt, ok = lm[col]
return
}
// SourcePositionFromTarget looks the source position using the target position.
// If a source exists on the line but not the col, the function will search backwards.
func (sm *SourceMap) SourcePositionFromTarget(line, col uint32) (src Position, ok bool) {
lm, ok := sm.TargetLinesToSource[line]
if !ok {
return
}
for {
src, ok = lm[col]
if ok || col == 0 {
return
}
col--
}
}
package parser
import (
"github.com/a-h/parse"
)
var stringExpression = parse.Func(func(pi *parse.Input) (n Node, ok bool, err error) {
// Check the prefix first.
if _, ok, err = parse.Or(parse.String("{ "), parse.String("{")).Parse(pi); err != nil || !ok {
return
}
// Once we have a prefix, we must have an expression that returns a string, with optional err.
var r StringExpression
if r.Expression, err = parseGoSliceArgs(pi); err != nil {
return r, false, err
}
// Clear any optional whitespace.
_, _, _ = parse.OptionalWhitespace.Parse(pi)
// }
if _, ok, err = closeBraceWithOptionalPadding.Parse(pi); err != nil || !ok {
err = parse.Error("string expression: missing close brace", pi.Position())
return
}
// Parse trailing whitespace.
ws, _, err := parse.Whitespace.Parse(pi)
if err != nil {
return r, false, err
}
r.TrailingSpace, err = NewTrailingSpace(ws)
if err != nil {
return r, false, err
}
return r, true, nil
})
package parser
import (
"github.com/a-h/parse"
"github.com/a-h/templ/parser/v2/goexpression"
)
var switchExpression parse.Parser[Node] = switchExpressionParser{}
type switchExpressionParser struct{}
func (switchExpressionParser) Parse(pi *parse.Input) (n Node, ok bool, err error) {
var r SwitchExpression
start := pi.Index()
// Check the prefix first.
if !peekPrefix(pi, "switch ") {
pi.Seek(start)
return
}
// Parse the Go switch expression.
if r.Expression, err = parseGo("switch", pi, goexpression.Switch); err != nil {
return r, false, err
}
// Eat " {\n".
if _, ok, err = parse.All(openBraceWithOptionalPadding, parse.NewLine).Parse(pi); err != nil || !ok {
err = parse.Error("switch: "+unterminatedMissingCurly, pi.PositionAt(start))
return
}
// Once we've had the start of a switch block, we must conclude the block.
// Read the optional 'case' nodes.
for {
var ce CaseExpression
ce, ok, err = caseExpressionParser.Parse(pi)
if err != nil {
return
}
if !ok {
break
}
r.Cases = append(r.Cases, ce)
}
// Read the required closing brace.
if _, ok, err = closeBraceWithOptionalPadding.Parse(pi); err != nil || !ok {
err = parse.Error("switch: "+unterminatedMissingEnd, pi.Position())
return
}
return r, true, nil
}
var caseExpressionStartParser = parse.Func(func(pi *parse.Input) (r Expression, ok bool, err error) {
start := pi.Index()
// Optional whitespace.
if _, _, err = parse.OptionalWhitespace.Parse(pi); err != nil {
return
}
// Strip leading whitespace and look for `case ` or `default`.
if !peekPrefix(pi, "case ", "default") {
pi.Seek(start)
return r, false, nil
}
// Parse the Go expression.
if r, err = parseGo("case", pi, goexpression.Case); err != nil {
return r, false, err
}
// Eat terminating newline.
_, _, _ = parse.ZeroOrMore(parse.String(" ")).Parse(pi)
_, _, _ = parse.NewLine.Parse(pi)
return r, true, nil
})
var caseExpressionParser = parse.Func(func(pi *parse.Input) (r CaseExpression, ok bool, err error) {
if r.Expression, ok, err = caseExpressionStartParser.Parse(pi); err != nil || !ok {
return
}
// Read until the next case statement, default, or end of the block.
pr := newTemplateNodeParser(parse.Any(StripType(closeBraceWithOptionalPadding), StripType(caseExpressionStartParser)), "closing brace or case expression")
var nodes Nodes
if nodes, ok, err = pr.Parse(pi); err != nil || !ok {
err = parse.Error("case: expected nodes, but none were found", pi.Position())
return
}
r.Children = nodes.Nodes
// Optional whitespace.
if _, ok, err = parse.OptionalWhitespace.Parse(pi); err != nil || !ok {
return
}
return r, true, nil
})
package parser
import (
"errors"
"os"
"path/filepath"
"strings"
"unicode"
"github.com/a-h/parse"
)
func Parse(fileName string) (TemplateFile, error) {
fc, err := os.ReadFile(fileName)
if err != nil {
return TemplateFile{}, err
}
return ParseString(string(fc))
}
func getDefaultPackageName(fileName string) (pkg string) {
parent := filepath.Base(filepath.Dir(fileName))
if !isGoIdentifier(parent) {
return "main"
}
return parent
}
func isGoIdentifier(s string) bool {
if len(s) == 0 {
return false
}
for i, r := range s {
if unicode.IsLetter(r) || r == '_' {
continue
}
if i > 0 && unicode.IsDigit(r) {
continue
}
return false
}
return true
}
func ParseString(template string) (TemplateFile, error) {
tf, ok, err := NewTemplateFileParser("main").Parse(parse.NewInput(template))
if err != nil {
return tf, err
}
if !ok {
err = ErrTemplateNotFound
}
return tf, err
}
// NewTemplateFileParser creates a new TemplateFileParser.
func NewTemplateFileParser(pkg string) TemplateFileParser {
return TemplateFileParser{
DefaultPackage: pkg,
}
}
var ErrLegacyFileFormat = errors.New("legacy file format - run templ migrate")
var ErrTemplateNotFound = errors.New("template not found")
type TemplateFileParser struct {
DefaultPackage string
}
var legacyPackageParser = parse.String("{% package")
func (p TemplateFileParser) Parse(pi *parse.Input) (tf TemplateFile, ok bool, err error) {
// If we're parsing a legacy file, complain that migration needs to happen.
_, ok, err = legacyPackageParser.Parse(pi)
if err != nil {
return
}
if ok {
return tf, false, ErrLegacyFileFormat
}
// Read until the package.
for {
// Package.
// package name
from := pi.Position()
tf.Package, ok, err = pkg.Parse(pi)
if err != nil {
return
}
if ok {
break
}
var line string
line, ok, err = stringUntilNewLine.Parse(pi)
if err != nil {
return
}
if !ok {
break
}
var newLine string
newLine, _, _ = parse.NewLine.Parse(pi)
tf.Header = append(tf.Header, TemplateFileGoExpression{Expression: NewExpression(line+newLine, from, pi.Position()), BeforePackage: true})
}
// Strip any whitespace between the template declaration and the first template.
_, _, _ = parse.OptionalWhitespace.Parse(pi)
outer:
for {
// Optional templates, CSS, and script templates.
// templ Name(p Parameter)
var tn HTMLTemplate
tn, ok, err = template.Parse(pi)
if err != nil {
return tf, false, err
}
if ok {
tf.Nodes = append(tf.Nodes, tn)
_, _, _ = parse.OptionalWhitespace.Parse(pi)
continue
}
// css Name()
var cn CSSTemplate
cn, ok, err = cssParser.Parse(pi)
if err != nil {
return tf, false, err
}
if ok {
tf.Nodes = append(tf.Nodes, cn)
_, _, _ = parse.OptionalWhitespace.Parse(pi)
continue
}
// script Name()
var sn ScriptTemplate
sn, ok, err = scriptTemplateParser.Parse(pi)
if err != nil {
return tf, false, err
}
if ok {
tf.Nodes = append(tf.Nodes, sn)
_, _, _ = parse.OptionalWhitespace.Parse(pi)
continue
}
// Anything that isn't template content is Go code.
code := new(strings.Builder)
from := pi.Position()
inner:
for {
// Check to see if this line isn't Go code.
last := pi.Index()
var l string
if l, ok, err = stringUntilNewLineOrEOF.Parse(pi); err != nil {
return
}
hasTemplatePrefix := strings.HasPrefix(l, "templ ") || strings.HasPrefix(l, "css ") || strings.HasPrefix(l, "script ")
if hasTemplatePrefix && strings.Contains(l, "(") {
// Unread the line.
pi.Seek(last)
// Take the code so far.
if code.Len() > 0 {
expr := NewExpression(strings.TrimSpace(code.String()), from, pi.Position())
tf.Nodes = append(tf.Nodes, TemplateFileGoExpression{Expression: expr})
}
// Carry on parsing.
break inner
}
code.WriteString(l)
// Eat the newline or EOF that we read until.
var newLine string
if newLine, ok, err = parse.NewLine.Parse(pi); err != nil {
return
}
code.WriteString(newLine)
if _, isEOF, _ := parse.EOF[string]().Parse(pi); isEOF {
if code.Len() > 0 {
expr := NewExpression(strings.TrimSpace(code.String()), from, pi.Position())
tf.Nodes = append(tf.Nodes, TemplateFileGoExpression{Expression: expr})
}
// Stop parsing.
break outer
}
}
}
return tf, true, nil
}
package parser
import (
"fmt"
"github.com/a-h/parse"
)
// TemplateExpression.
// TemplateExpression.
// templ Func(p Parameter) {
// templ (data Data) Func(p Parameter) {
// templ (data []string) Func(p Parameter) {
type templateExpression struct {
Expression Expression
}
var templateExpressionParser = parse.Func(func(pi *parse.Input) (r templateExpression, ok bool, err error) {
start := pi.Index()
if !peekPrefix(pi, "templ ") {
return r, false, nil
}
// Once we have the prefix, everything to the brace is Go.
// e.g.
// templ (x []string) Test() {
// becomes:
// func (x []string) Test() templ.Component {
if _, r.Expression, err = parseTemplFuncDecl(pi); err != nil {
return r, false, err
}
// Eat " {\n".
if _, ok, err = parse.All(openBraceWithOptionalPadding, parse.StringFrom(parse.Optional(parse.NewLine))).Parse(pi); err != nil || !ok {
err = parse.Error("templ: malformed templ expression, expected `templ functionName() {`", pi.PositionAt(start))
return
}
return r, true, nil
})
const (
unterminatedMissingCurly = `unterminated (missing closing '{\n') - https://templ.guide/syntax-and-usage/statements#incomplete-statements`
unterminatedMissingEnd = `missing end (expected '}') - https://templ.guide/syntax-and-usage/statements#incomplete-statements`
)
// Template node (element, call, if, switch, for, whitespace etc.)
func newTemplateNodeParser[TUntil any](until parse.Parser[TUntil], untilName string) templateNodeParser[TUntil] {
return templateNodeParser[TUntil]{
until: until,
untilName: untilName,
}
}
type templateNodeParser[TUntil any] struct {
until parse.Parser[TUntil]
untilName string
}
var rawElements = parse.Any(styleElement, scriptElement)
var templateNodeSkipParsers = []parse.Parser[Node]{
voidElementCloser, // </br>, </img> etc. - should be ignored.
}
var templateNodeParsers = []parse.Parser[Node]{
docType, // <!DOCTYPE html>
htmlComment, // <!--
goComment, // // or /*
rawElements, // <text>, <>, or <style> element (special behaviour - contents are not parsed).
element, // <a>, <br/> etc.
ifExpression, // if {}
forExpression, // for {}
switchExpression, // switch {}
callTemplateExpression, // {! TemplateName(a, b, c) }
templElementExpression, // @TemplateName(a, b, c) { <div>Children</div> }
childrenExpression, // { children... }
goCode, // {{ myval := x.myval }}
stringExpression, // { "abc" }
whitespaceExpression, // { " " }
textParser, // anything & everything accepted...
}
func (p templateNodeParser[T]) Parse(pi *parse.Input) (op Nodes, ok bool, err error) {
outer:
for {
// Check if we've reached the end.
if p.until != nil {
start := pi.Index()
_, ok, err = p.until.Parse(pi)
if err != nil {
return
}
if ok {
pi.Seek(start)
return op, true, nil
}
}
// Skip any nodes that we don't care about.
for _, p := range templateNodeSkipParsers {
_, matched, err := p.Parse(pi)
if err != nil {
return Nodes{}, false, err
}
if matched {
continue outer
}
}
// Attempt to parse a node.
// Loop through the parsers and try to parse a node.
var matched bool
for _, p := range templateNodeParsers {
var node Node
node, matched, err = p.Parse(pi)
if err != nil {
return Nodes{}, false, err
}
if matched {
op.Nodes = append(op.Nodes, node)
break
}
}
if matched {
continue
}
if p.until == nil {
// In this case, we're just reading as many nodes as we can until we can't read any more.
// If we've reached here, we couldn't find a node.
// The element parser checks the final node returned to make sure it's the expected close tag.
break
}
err = UntilNotFoundError{
ParseError: parse.Error(fmt.Sprintf("%v not found", p.untilName), pi.Position()),
}
return
}
return op, true, nil
}
type UntilNotFoundError struct {
parse.ParseError
}
package parser
import (
"github.com/a-h/parse"
"github.com/a-h/templ/parser/v2/goexpression"
)
type templElementExpressionParser struct{}
func (p templElementExpressionParser) Parse(pi *parse.Input) (n Node, ok bool, err error) {
// Check the prefix first.
if _, ok, err = parse.Rune('@').Parse(pi); err != nil || !ok {
return
}
var r TemplElementExpression
// Parse the Go expression.
if r.Expression, err = parseGo("templ element", pi, goexpression.TemplExpression); err != nil {
return r, false, err
}
// Once we've got a start expression, check to see if there's an open brace for children. {\n.
var hasOpenBrace bool
_, hasOpenBrace, err = openBraceWithOptionalPadding.Parse(pi)
if err != nil {
return
}
if !hasOpenBrace {
return r, true, nil
}
// Once we've had the start of an element's children, we must conclude the block.
// Node contents.
np := newTemplateNodeParser(closeBraceWithOptionalPadding, "templ element closing brace")
var nodes Nodes
if nodes, ok, err = np.Parse(pi); err != nil || !ok {
err = parse.Error("@"+r.Expression.Value+": expected nodes, but none were found", pi.Position())
return
}
r.Children = nodes.Nodes
// Read the required closing brace.
if _, ok, err = closeBraceWithOptionalPadding.Parse(pi); err != nil || !ok {
err = parse.Error("@"+r.Expression.Value+": missing end (expected '}')", pi.Position())
return
}
return r, true, nil
}
var templElementExpression templElementExpressionParser
package parser
import (
"unicode"
"github.com/a-h/parse"
)
var tagTemplOrNewLine = parse.Any(parse.Rune('<'), parse.Rune('{'), parse.Rune('}'), parse.String("\r\n"), parse.Rune('\n'))
var textParser = parse.Func(func(pi *parse.Input) (n Node, ok bool, err error) {
from := pi.Position()
// Read until a tag or templ expression opens.
var t Text
if t.Value, ok, err = parse.StringUntil(tagTemplOrNewLine).Parse(pi); err != nil || !ok {
return
}
if isWhitespace(t.Value) {
return t, false, nil
}
if _, ok = pi.Peek(1); !ok {
err = parse.Error("textParser: unterminated text, expected tag open, templ expression open, or newline", from)
return
}
t.Range = NewRange(from, pi.Position())
// Elide any void element closing tags.
if _, _, err = voidElementCloser.Parse(pi); err != nil {
return
}
// Parse trailing whitespace.
ws, _, err := parse.Whitespace.Parse(pi)
if err != nil {
return t, false, err
}
t.TrailingSpace, err = NewTrailingSpace(ws)
if err != nil {
return t, false, err
}
return t, true, nil
})
func isWhitespace(s string) bool {
for _, r := range s {
if !unicode.IsSpace(r) {
return false
}
}
return true
}
package parser
import (
"bytes"
"errors"
"fmt"
"go/format"
"io"
"strings"
"unicode"
"github.com/a-h/parse"
)
// package parser
//
// import "strings"
// import strs "strings"
//
// css AddressLineStyle() {
// background-color: #ff0000;
// color: #ffffff;
// }
//
// templ RenderAddress(addr Address) {
// <div style={ AddressLineStyle() }>{ addr.Address1 }</div>
// <div>{ addr.Address2 }</div>
// <div>{ addr.Address3 }</div>
// <div>{ addr.Address4 }</div>
// }
//
// templ Render(p Person) {
// <div>
// <div>{ p.Name() }</div>
// <a href={ p.URL }>{ strings.ToUpper(p.Name()) }</a>
// <div>
// if p.Type == "test" {
// <span>{ "Test user" }</span>
// } else {
// <span>{ "Not test user" }</span>
// }
// for _, v := range p.Addresses {
// {! call RenderAddress(v) }
// }
// </div>
// </div>
// }
// Source mapping to map from the source code of the template to the
// in-memory representation.
type Position struct {
Index int64
Line uint32
Col uint32
}
func (p Position) String() string {
return fmt.Sprintf("line %d, col %d (index %d)", p.Line, p.Col, p.Index)
}
// NewPosition initialises a position.
func NewPosition(index int64, line, col uint32) Position {
return Position{
Index: index,
Line: line,
Col: col,
}
}
// NewExpression creates a Go expression.
func NewExpression(value string, from, to parse.Position) Expression {
return Expression{
Value: value,
Range: Range{
From: Position{
Index: int64(from.Index),
Line: uint32(from.Line),
Col: uint32(from.Col),
},
To: Position{
Index: int64(to.Index),
Line: uint32(to.Line),
Col: uint32(to.Col),
},
},
}
}
// NewRange creates a Range expression.
func NewRange(from, to parse.Position) Range {
return Range{
From: Position{
Index: int64(from.Index),
Line: uint32(from.Line),
Col: uint32(from.Col),
},
To: Position{
Index: int64(to.Index),
Line: uint32(to.Line),
Col: uint32(to.Col),
},
}
}
// Range of text within a file.
type Range struct {
From Position
To Position
}
// Expression containing Go code.
type Expression struct {
Value string
Range Range
}
type TemplateFile struct {
// Header contains comments or whitespace at the top of the file.
Header []TemplateFileGoExpression
// Package expression.
Package Package
// Filepath is where the file was loaded from. It is not always available.
Filepath string
// Nodes in the file.
Nodes []TemplateFileNode
}
func (tf TemplateFile) Write(w io.Writer) error {
for _, n := range tf.Header {
if err := n.Write(w, 0); err != nil {
return err
}
}
var indent int
if err := tf.Package.Write(w, indent); err != nil {
return err
}
if _, err := io.WriteString(w, "\n\n"); err != nil {
return err
}
for i := 0; i < len(tf.Nodes); i++ {
if err := tf.Nodes[i].Write(w, indent); err != nil {
return err
}
if _, err := io.WriteString(w, getNodeWhitespace(tf.Nodes, i)); err != nil {
return err
}
}
return nil
}
func getNodeWhitespace(nodes []TemplateFileNode, i int) string {
if i == len(nodes)-1 {
return "\n"
}
if _, nextIsTemplate := nodes[i+1].(HTMLTemplate); nextIsTemplate {
if e, isGo := nodes[i].(TemplateFileGoExpression); isGo && endsWithComment(e.Expression.Value) {
return "\n"
}
}
return "\n\n"
}
func endsWithComment(s string) bool {
lineSlice := strings.Split(s, "\n")
return strings.HasPrefix(lineSlice[len(lineSlice)-1], "//")
}
// TemplateFileNode can be a Template, CSS, Script or Go.
type TemplateFileNode interface {
IsTemplateFileNode() bool
Write(w io.Writer, indent int) error
}
// TemplateFileGoExpression within a TemplateFile
type TemplateFileGoExpression struct {
Expression Expression
BeforePackage bool
}
func (exp TemplateFileGoExpression) IsTemplateFileNode() bool { return true }
func (exp TemplateFileGoExpression) Write(w io.Writer, indent int) error {
in := exp.Expression.Value
if exp.BeforePackage {
in += "\\\\formatstring\npackage p\n\\\\formatstring"
}
data, err := format.Source([]byte(in))
if err != nil {
return writeIndent(w, indent, exp.Expression.Value)
}
if exp.BeforePackage {
data = bytes.TrimSuffix(data, []byte("\\\\formatstring\npackage p\n\\\\formatstring"))
}
_, err = w.Write(data)
return err
}
func writeIndent(w io.Writer, level int, s ...string) (err error) {
indent := strings.Repeat("\t", level)
if _, err = io.WriteString(w, indent); err != nil {
return err
}
for _, ss := range s {
_, err = io.WriteString(w, ss)
if err != nil {
return
}
}
return
}
type Package struct {
Expression Expression
}
func (p Package) Write(w io.Writer, indent int) error {
return writeIndent(w, indent, p.Expression.Value)
}
// Whitespace.
type Whitespace struct {
Value string
}
func (ws Whitespace) IsNode() bool { return true }
func (ws Whitespace) Write(w io.Writer, indent int) error {
if ws.Value == "" || !strings.Contains(ws.Value, "\n") {
return nil
}
// https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model/Whitespace
// - All spaces and tabs immediately before and after a line break are ignored.
// - All tab characters are handled as space characters.
// - Line breaks are converted to spaces.
// Any space immediately following another space (even across two separate inline elements) is ignored.
// Sequences of spaces at the beginning and end of an element are removed.
// Notes: Since we only have whitespace in this node, we can strip anything that isn't a line break.
// Since any space following another space is ignored, we can collapse to a single rule.
// So, the rule is... if there's a newline, it becomes a single space, or it's stripped.
// We have to remove the start and end space elsewhere.
_, err := io.WriteString(w, " ")
return err
}
// CSS definition.
//
// css Name() {
// color: #ffffff;
// background-color: { constants.BackgroundColor };
// background-image: url('./somewhere.png');
// }
type CSSTemplate struct {
Range Range
Name string
Expression Expression
Properties []CSSProperty
}
func (css CSSTemplate) IsTemplateFileNode() bool { return true }
func (css CSSTemplate) Write(w io.Writer, indent int) error {
source := formatFunctionArguments(css.Expression.Value)
if err := writeIndent(w, indent, "css ", string(source), " {\n"); err != nil {
return err
}
for _, p := range css.Properties {
if err := p.Write(w, indent+1); err != nil {
return err
}
}
if err := writeIndent(w, indent, "}"); err != nil {
return err
}
return nil
}
// CSSProperty is a CSS property and value pair.
type CSSProperty interface {
IsCSSProperty() bool
Write(w io.Writer, indent int) error
}
// color: #ffffff;
type ConstantCSSProperty struct {
Name string
Value string
}
func (c ConstantCSSProperty) IsCSSProperty() bool { return true }
func (c ConstantCSSProperty) Write(w io.Writer, indent int) error {
if err := writeIndent(w, indent, c.String(false)); err != nil {
return err
}
return nil
}
func (c ConstantCSSProperty) String(minified bool) string {
sb := new(strings.Builder)
sb.WriteString(c.Name)
if minified {
sb.WriteString(":")
} else {
sb.WriteString(": ")
}
sb.WriteString(c.Value)
sb.WriteString(";")
if !minified {
sb.WriteString("\n")
}
return sb.String()
}
// background-color: { constants.BackgroundColor };
type ExpressionCSSProperty struct {
Name string
Value StringExpression
}
func (c ExpressionCSSProperty) IsCSSProperty() bool { return true }
func (c ExpressionCSSProperty) Write(w io.Writer, indent int) error {
if err := writeIndent(w, indent, c.Name, ": "); err != nil {
return err
}
if err := c.Value.Write(w, 0); err != nil {
return err
}
if _, err := w.Write([]byte(";\n")); err != nil {
return err
}
return nil
}
// <!DOCTYPE html>
type DocType struct {
Value string
}
func (dt DocType) IsNode() bool { return true }
func (dt DocType) Write(w io.Writer, indent int) error {
return writeIndent(w, indent, "<!DOCTYPE ", dt.Value, ">")
}
// HTMLTemplate definition.
//
// templ Name(p Parameter) {
// if ... {
// <Element></Element>
// }
// }
type HTMLTemplate struct {
Range Range
Expression Expression
Children []Node
}
func (t HTMLTemplate) IsTemplateFileNode() bool { return true }
func (t HTMLTemplate) Write(w io.Writer, indent int) error {
source := formatFunctionArguments(t.Expression.Value)
if err := writeIndent(w, indent, "templ ", string(source), " {\n"); err != nil {
return err
}
if err := writeNodesIndented(w, indent+1, t.Children); err != nil {
return err
}
if err := writeIndent(w, indent, "}"); err != nil {
return err
}
return nil
}
// TrailingSpace defines the whitespace that may trail behind the close of an element, a
// text node, or string expression.
type TrailingSpace string
const (
SpaceNone TrailingSpace = ""
SpaceHorizontal TrailingSpace = " "
SpaceVertical TrailingSpace = "\n"
)
var ErrNonSpaceCharacter = errors.New("non space character found")
func NewTrailingSpace(s string) (ts TrailingSpace, err error) {
var hasHorizontalSpace bool
for _, r := range s {
if r == '\n' {
return SpaceVertical, nil
}
if unicode.IsSpace(r) {
hasHorizontalSpace = true
continue
}
return ts, ErrNonSpaceCharacter
}
if hasHorizontalSpace {
return SpaceHorizontal, nil
}
return SpaceNone, nil
}
type Nodes struct {
Nodes []Node
}
// A Node appears within a template, e.g. an StringExpression, Element, IfExpression etc.
type Node interface {
IsNode() bool
// Write out the string.
Write(w io.Writer, indent int) error
}
type CompositeNode interface {
Node
ChildNodes() []Node
}
type WhitespaceTrailer interface {
Trailing() TrailingSpace
}
var (
_ WhitespaceTrailer = Element{}
_ WhitespaceTrailer = Text{}
_ WhitespaceTrailer = StringExpression{}
)
// Text node within the document.
type Text struct {
// Range of the text within the templ file.
Range Range
// Value is the raw HTML encoded value.
Value string
// TrailingSpace lists what happens after the text.
TrailingSpace TrailingSpace
}
func (t Text) Trailing() TrailingSpace {
return t.TrailingSpace
}
func (t Text) IsNode() bool { return true }
func (t Text) Write(w io.Writer, indent int) error {
return writeIndent(w, indent, t.Value)
}
// <a .../> or <div ...>...</div>
type Element struct {
Name string
Attributes []Attribute
IndentAttrs bool
Children []Node
IndentChildren bool
TrailingSpace TrailingSpace
NameRange Range
}
func (e Element) Trailing() TrailingSpace {
return e.TrailingSpace
}
var voidElements = map[string]struct{}{
"area": {}, "base": {}, "br": {}, "col": {}, "command": {}, "embed": {}, "hr": {}, "img": {}, "input": {}, "keygen": {}, "link": {}, "meta": {}, "param": {}, "source": {}, "track": {}, "wbr": {},
}
// https://www.w3.org/TR/2011/WD-html-markup-20110113/syntax.html#void-element
func (e Element) IsVoidElement() bool {
_, ok := voidElements[e.Name]
return ok
}
func (e Element) hasNonWhitespaceChildren() bool {
for _, c := range e.Children {
if _, isWhitespace := c.(Whitespace); !isWhitespace {
return true
}
}
return false
}
var blockElements = map[string]struct{}{
"address": {}, "article": {}, "aside": {}, "body": {}, "blockquote": {}, "canvas": {}, "dd": {}, "div": {}, "dl": {}, "dt": {}, "fieldset": {}, "figcaption": {}, "figure": {}, "footer": {}, "form": {}, "h1": {}, "h2": {}, "h3": {}, "h4": {}, "h5": {}, "h6": {}, "head": {}, "header": {}, "hr": {}, "html": {}, "li": {}, "main": {}, "meta": {}, "nav": {}, "noscript": {}, "ol": {}, "p": {}, "pre": {}, "script": {}, "section": {}, "table": {}, "template": {}, "tfoot": {}, "turbo-stream": {}, "ul": {}, "video": {},
// Not strictly block but for the purposes of layout, they are.
"title": {}, "style": {}, "link": {}, "td": {}, "th": {}, "tr": {}, "br": {},
}
func (e Element) IsBlockElement() bool {
_, ok := blockElements[e.Name]
return ok
}
// Validate that no invalid expressions have been used.
func (e Element) Validate() (msgs []string, ok bool) {
// Validate that script and style tags don't contain expressions.
if strings.EqualFold(e.Name, "script") || strings.EqualFold(e.Name, "style") {
if containsNonTextNodes(e.Children) {
msgs = append(msgs, "invalid node contents: script and style attributes must only contain text")
}
}
return msgs, len(msgs) == 0
}
func containsNonTextNodes(nodes []Node) bool {
for i := 0; i < len(nodes); i++ {
n := nodes[i]
switch n.(type) {
case Text:
continue
case Whitespace:
continue
default:
return true
}
}
return false
}
func (e Element) ChildNodes() []Node {
return e.Children
}
func (e Element) IsNode() bool { return true }
func (e Element) Write(w io.Writer, indent int) error {
if err := writeIndent(w, indent, "<", e.Name); err != nil {
return err
}
for i := 0; i < len(e.Attributes); i++ {
a := e.Attributes[i]
// Only the conditional attributes get indented.
var attrIndent int
if e.IndentAttrs {
if _, err := w.Write([]byte("\n")); err != nil {
return err
}
attrIndent = indent + 1
} else {
if _, err := w.Write([]byte(" ")); err != nil {
return err
}
}
if err := a.Write(w, attrIndent); err != nil {
return err
}
}
var closeAngleBracketIndent int
if e.IndentAttrs {
if _, err := w.Write([]byte("\n")); err != nil {
return err
}
closeAngleBracketIndent = indent
}
if e.hasNonWhitespaceChildren() {
if e.IndentChildren {
if err := writeIndent(w, closeAngleBracketIndent, ">\n"); err != nil {
return err
}
if err := writeNodesIndented(w, indent+1, e.Children); err != nil {
return err
}
if err := writeIndent(w, indent, "</", e.Name, ">"); err != nil {
return err
}
return nil
}
if err := writeIndent(w, closeAngleBracketIndent, ">"); err != nil {
return err
}
if err := writeNodesWithoutIndentation(w, e.Children); err != nil {
return err
}
if _, err := w.Write([]byte("</" + e.Name + ">")); err != nil {
return err
}
return nil
}
if e.IsVoidElement() {
if err := writeIndent(w, closeAngleBracketIndent, "/>"); err != nil {
return err
}
return nil
}
if err := writeIndent(w, closeAngleBracketIndent, "></", e.Name, ">"); err != nil {
return err
}
return nil
}
func writeNodesWithoutIndentation(w io.Writer, nodes []Node) error {
return writeNodes(w, 0, nodes, false)
}
func writeNodesIndented(w io.Writer, level int, nodes []Node) error {
return writeNodes(w, level, nodes, true)
}
func writeNodes(w io.Writer, level int, nodes []Node, indent bool) error {
startLevel := level
for i := 0; i < len(nodes); i++ {
_, isWhitespace := nodes[i].(Whitespace)
// Skip whitespace nodes.
if isWhitespace {
continue
}
if err := nodes[i].Write(w, level); err != nil {
return err
}
// Apply trailing whitespace if present.
trailing := SpaceVertical
if wst, isWhitespaceTrailer := nodes[i].(WhitespaceTrailer); isWhitespaceTrailer {
trailing = wst.Trailing()
}
// Put a newline after the last node in indentation mode.
if indent && ((nextNodeIsBlock(nodes, i) || i == len(nodes)-1) || shouldAlwaysBreakAfter(nodes[i])) {
trailing = SpaceVertical
}
switch trailing {
case SpaceNone:
level = 0
case SpaceHorizontal:
level = 0
case SpaceVertical:
level = startLevel
}
if _, err := w.Write([]byte(trailing)); err != nil {
return err
}
}
return nil
}
func shouldAlwaysBreakAfter(node Node) bool {
if el, isElement := node.(Element); isElement {
return strings.EqualFold(el.Name, "br") || strings.EqualFold(el.Name, "hr")
}
return false
}
func nextNodeIsBlock(nodes []Node, i int) bool {
if len(nodes)-1 < i+1 {
return false
}
return isBlockNode(nodes[i+1])
}
func isBlockNode(node Node) bool {
switch n := node.(type) {
case IfExpression:
return true
case SwitchExpression:
return true
case ForExpression:
return true
case Element:
return n.IsBlockElement() || n.IndentChildren
}
return false
}
type RawElement struct {
Name string
Attributes []Attribute
Contents string
}
func (e RawElement) IsNode() bool { return true }
func (e RawElement) Write(w io.Writer, indent int) error {
// Start.
if err := writeIndent(w, indent, "<", e.Name); err != nil {
return err
}
for i := 0; i < len(e.Attributes); i++ {
if _, err := w.Write([]byte(" ")); err != nil {
return err
}
a := e.Attributes[i]
// Don't indent the attributes, only the conditional attributes get indented.
if err := a.Write(w, 0); err != nil {
return err
}
}
if _, err := w.Write([]byte(">")); err != nil {
return err
}
// Contents.
if _, err := w.Write([]byte(e.Contents)); err != nil {
return err
}
// Close.
if _, err := w.Write([]byte("</" + e.Name + ">")); err != nil {
return err
}
return nil
}
type Attribute interface {
// Write out the string.
Write(w io.Writer, indent int) error
}
// <hr noshade/>
type BoolConstantAttribute struct {
Name string
NameRange Range
}
func (bca BoolConstantAttribute) String() string {
return bca.Name
}
func (bca BoolConstantAttribute) Write(w io.Writer, indent int) error {
return writeIndent(w, indent, bca.String())
}
// href=""
type ConstantAttribute struct {
Name string
Value string
SingleQuote bool
NameRange Range
}
func (ca ConstantAttribute) String() string {
quote := `"`
if ca.SingleQuote {
quote = `'`
}
return ca.Name + `=` + quote + ca.Value + quote
}
func (ca ConstantAttribute) Write(w io.Writer, indent int) error {
return writeIndent(w, indent, ca.String())
}
// noshade={ templ.Bool(...) }
type BoolExpressionAttribute struct {
Name string
Expression Expression
NameRange Range
}
func (bea BoolExpressionAttribute) String() string {
return bea.Name + `?={ ` + bea.Expression.Value + ` }`
}
func (bea BoolExpressionAttribute) Write(w io.Writer, indent int) error {
return writeIndent(w, indent, bea.String())
}
// href={ ... }
type ExpressionAttribute struct {
Name string
Expression Expression
NameRange Range
}
func (ea ExpressionAttribute) String() string {
sb := new(strings.Builder)
_ = ea.Write(sb, 0)
return sb.String()
}
func (ea ExpressionAttribute) formatExpression() (exp []string) {
trimmed := strings.TrimSpace(ea.Expression.Value)
if !strings.Contains(trimmed, "\n") {
formatted, err := format.Source([]byte(trimmed))
if err != nil {
return []string{trimmed}
}
return []string{string(formatted)}
}
buf := bytes.NewBufferString("[]any{\n")
buf.WriteString(trimmed)
buf.WriteString("\n}")
formatted, err := format.Source(buf.Bytes())
if err != nil {
return []string{trimmed}
}
// Trim prefix and suffix.
lines := strings.Split(string(formatted), "\n")
if len(lines) < 3 {
return []string{trimmed}
}
// Return.
return lines[1 : len(lines)-1]
}
func (ea ExpressionAttribute) Write(w io.Writer, indent int) (err error) {
lines := ea.formatExpression()
if len(lines) == 1 {
return writeIndent(w, indent, ea.Name, `={ `, lines[0], ` }`)
}
if err = writeIndent(w, indent, ea.Name, "={\n"); err != nil {
return err
}
for _, line := range lines {
if err = writeIndent(w, indent, line, "\n"); err != nil {
return err
}
}
return writeIndent(w, indent, "}")
}
// <a { spread... } />
type SpreadAttributes struct {
Expression Expression
}
func (sa SpreadAttributes) String() string {
return `{ ` + sa.Expression.Value + `... }`
}
func (sa SpreadAttributes) Write(w io.Writer, indent int) error {
return writeIndent(w, indent, sa.String())
}
// <a href="test" \
// if active {
// class="isActive"
// }
type ConditionalAttribute struct {
Expression Expression
Then []Attribute
Else []Attribute
}
func (ca ConditionalAttribute) String() string {
sb := new(strings.Builder)
_ = ca.Write(sb, 0)
return sb.String()
}
func (ca ConditionalAttribute) Write(w io.Writer, indent int) error {
if err := writeIndent(w, indent, "if "); err != nil {
return err
}
if _, err := w.Write([]byte(ca.Expression.Value)); err != nil {
return err
}
if _, err := w.Write([]byte(" {\n")); err != nil {
return err
}
{
indent++
for _, attr := range ca.Then {
if err := attr.Write(w, indent); err != nil {
return err
}
if _, err := w.Write([]byte("\n")); err != nil {
return err
}
}
indent--
}
if err := writeIndent(w, indent, "}"); err != nil {
return err
}
if len(ca.Else) == 0 {
return nil
}
// Write the else blocks.
if _, err := w.Write([]byte(" else {\n")); err != nil {
return err
}
{
indent++
for _, attr := range ca.Else {
if err := attr.Write(w, indent); err != nil {
return err
}
if _, err := w.Write([]byte("\n")); err != nil {
return err
}
}
indent--
}
if err := writeIndent(w, indent, "}"); err != nil {
return err
}
return nil
}
// GoComment.
type GoComment struct {
Contents string
Multiline bool
}
func (c GoComment) IsNode() bool { return true }
func (c GoComment) Write(w io.Writer, indent int) error {
if c.Multiline {
return writeIndent(w, indent, "/*", c.Contents, "*/")
}
return writeIndent(w, indent, "//", c.Contents)
}
// HTMLComment.
type HTMLComment struct {
Contents string
}
func (c HTMLComment) IsNode() bool { return true }
func (c HTMLComment) Write(w io.Writer, indent int) error {
return writeIndent(w, indent, "<!--", c.Contents, "-->")
}
// Nodes.
// CallTemplateExpression can be used to create and render a template using data.
// {! Other(p.First, p.Last) }
// or it can be used to render a template parameter.
// {! v }
type CallTemplateExpression struct {
// Expression returns a template to execute.
Expression Expression
}
func (cte CallTemplateExpression) IsNode() bool { return true }
func (cte CallTemplateExpression) Write(w io.Writer, indent int) error {
// Rewrite to new call syntax
return writeIndent(w, indent, `@`, cte.Expression.Value)
}
// TemplElementExpression can be used to create and render a template using data.
// @Other(p.First, p.Last)
// or it can be used to render a template parameter.
// @v
type TemplElementExpression struct {
// Expression returns a template to execute.
Expression Expression
// Children returns the elements in a block element.
Children []Node
}
func (tee TemplElementExpression) ChildNodes() []Node {
return tee.Children
}
func (tee TemplElementExpression) IsNode() bool { return true }
func (tee TemplElementExpression) Write(w io.Writer, indent int) error {
source, err := format.Source([]byte(tee.Expression.Value))
if err != nil {
source = []byte(tee.Expression.Value)
}
// Indent all lines and re-format, we can then use this to only re-indent lines that gofmt would modify.
reformattedSource, err := format.Source(bytes.ReplaceAll(source, []byte("\n"), []byte("\n\t")))
if err != nil {
reformattedSource = source
}
sourceLines := bytes.Split(source, []byte("\n"))
reformattedSourceLines := bytes.Split(reformattedSource, []byte("\n"))
for i := range sourceLines {
if i == 0 {
if err := writeIndent(w, indent, "@"+string(sourceLines[i])); err != nil {
return err
}
continue
}
if _, err := io.WriteString(w, "\n"); err != nil {
return err
}
if string(sourceLines[i]) != string(reformattedSourceLines[i]) {
if _, err := w.Write(sourceLines[i]); err != nil {
return err
}
continue
}
if err := writeIndent(w, indent, string(sourceLines[i])); err != nil {
return err
}
}
if len(tee.Children) == 0 {
return nil
}
if _, err = io.WriteString(w, " {\n"); err != nil {
return err
}
if err := writeNodesIndented(w, indent+1, tee.Children); err != nil {
return err
}
if err := writeIndent(w, indent, "}"); err != nil {
return err
}
return nil
}
// ChildrenExpression can be used to rended the children of a templ element.
// { children ... }
type ChildrenExpression struct{}
func (ChildrenExpression) IsNode() bool { return true }
func (ChildrenExpression) Write(w io.Writer, indent int) error {
if err := writeIndent(w, indent, "{ children... }"); err != nil {
return err
}
return nil
}
// if p.Type == "test" && p.thing {
// }
type IfExpression struct {
Expression Expression
Then []Node
ElseIfs []ElseIfExpression
Else []Node
}
type ElseIfExpression struct {
Expression Expression
Then []Node
}
func (n IfExpression) ChildNodes() []Node {
var nodes []Node
nodes = append(nodes, n.Then...)
nodes = append(nodes, n.Else...)
for _, elseIf := range n.ElseIfs {
nodes = append(nodes, elseIf.Then...)
}
return nodes
}
func (n IfExpression) IsNode() bool { return true }
func (n IfExpression) Write(w io.Writer, indent int) error {
if err := writeIndent(w, indent, "if ", n.Expression.Value, " {\n"); err != nil {
return err
}
indent++
if err := writeNodesIndented(w, indent, n.Then); err != nil {
return err
}
indent--
for _, elseIf := range n.ElseIfs {
if err := writeIndent(w, indent, "} else if ", elseIf.Expression.Value, " {\n"); err != nil {
return err
}
indent++
if err := writeNodesIndented(w, indent, elseIf.Then); err != nil {
return err
}
indent--
}
if len(n.Else) > 0 {
if err := writeIndent(w, indent, "} else {\n"); err != nil {
return err
}
if err := writeNodesIndented(w, indent+1, n.Else); err != nil {
return err
}
}
if err := writeIndent(w, indent, "}"); err != nil {
return err
}
return nil
}
// switch p.Type {
// case "Something":
// }
type SwitchExpression struct {
Expression Expression
Cases []CaseExpression
}
func (se SwitchExpression) ChildNodes() []Node {
var nodes []Node
for _, c := range se.Cases {
nodes = append(nodes, c.Children...)
}
return nodes
}
func (se SwitchExpression) IsNode() bool { return true }
func (se SwitchExpression) Write(w io.Writer, indent int) error {
if err := writeIndent(w, indent, "switch ", se.Expression.Value, " {\n"); err != nil {
return err
}
indent++
for i := 0; i < len(se.Cases); i++ {
c := se.Cases[i]
if err := writeIndent(w, indent, c.Expression.Value, "\n"); err != nil {
return err
}
if err := writeNodesIndented(w, indent+1, c.Children); err != nil {
return err
}
}
indent--
if err := writeIndent(w, indent, "}"); err != nil {
return err
}
return nil
}
// case "Something":
type CaseExpression struct {
Expression Expression
Children []Node
}
// for i, v := range p.Addresses {
// {! Address(v) }
// }
type ForExpression struct {
Expression Expression
Children []Node
}
func (fe ForExpression) ChildNodes() []Node {
return fe.Children
}
func (fe ForExpression) IsNode() bool { return true }
func (fe ForExpression) Write(w io.Writer, indent int) error {
if err := writeIndent(w, indent, "for ", fe.Expression.Value, " {\n"); err != nil {
return err
}
if err := writeNodesIndented(w, indent+1, fe.Children); err != nil {
return err
}
if err := writeIndent(w, indent, "}"); err != nil {
return err
}
return nil
}
// GoCode is used within HTML elements, and allows arbitrary go code.
// {{ ... }}
type GoCode struct {
Expression Expression
// TrailingSpace lists what happens after the expression.
TrailingSpace TrailingSpace
Multiline bool
}
func (gc GoCode) Trailing() TrailingSpace {
return gc.TrailingSpace
}
func (gc GoCode) IsNode() bool { return true }
func (gc GoCode) Write(w io.Writer, indent int) error {
if isWhitespace(gc.Expression.Value) {
gc.Expression.Value = ""
}
source, err := format.Source([]byte(gc.Expression.Value))
if err != nil {
source = []byte(gc.Expression.Value)
}
if !gc.Multiline {
return writeIndent(w, indent, `{{ `, string(source), ` }}`)
}
if err := writeIndent(w, indent, "{{"+string(source)+"\n"); err != nil {
return err
}
return writeIndent(w, indent, "}}")
}
// StringExpression is used within HTML elements, and for style values.
// { ... }
type StringExpression struct {
Expression Expression
// TrailingSpace lists what happens after the expression.
TrailingSpace TrailingSpace
}
func (se StringExpression) Trailing() TrailingSpace {
return se.TrailingSpace
}
func (se StringExpression) IsNode() bool { return true }
func (se StringExpression) IsStyleDeclarationValue() bool { return true }
func (se StringExpression) Write(w io.Writer, indent int) error {
if isWhitespace(se.Expression.Value) {
se.Expression.Value = ""
}
return writeIndent(w, indent, `{ `, se.Expression.Value, ` }`)
}
// ScriptTemplate is a script block.
type ScriptTemplate struct {
Range Range
Name Expression
Parameters Expression
Value string
}
func (s ScriptTemplate) IsTemplateFileNode() bool { return true }
func (s ScriptTemplate) Write(w io.Writer, indent int) error {
source := formatFunctionArguments(s.Name.Value + "(" + s.Parameters.Value + ")")
if err := writeIndent(w, indent, "script ", string(source), " {\n"); err != nil {
return err
}
if _, err := io.WriteString(w, s.Value); err != nil {
return err
}
if err := writeIndent(w, indent, "}"); err != nil {
return err
}
return nil
}
// formatFunctionArguments formats the function arguments, if possible.
func formatFunctionArguments(expression string) string {
source := []byte(expression)
formatted, err := format.Source([]byte("func " + expression))
if err == nil {
formatted = bytes.TrimPrefix(formatted, []byte("func "))
source = formatted
}
return string(source)
}
package parser
import "github.com/a-h/parse"
// Eat any whitespace.
var whitespaceExpression = parse.Func(func(pi *parse.Input) (n Node, ok bool, err error) {
var r Whitespace
if r.Value, ok, err = parse.OptionalWhitespace.Parse(pi); err != nil || !ok {
return
}
return r, len(r.Value) > 0, nil
})
package templ
import (
"bytes"
"context"
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"
"html"
"html/template"
"io"
"net/http"
"reflect"
"sort"
"strings"
"sync"
"github.com/a-h/templ/safehtml"
)
// Types exposed by all components.
// Component is the interface that all templates implement.
type Component interface {
// Render the template.
Render(ctx context.Context, w io.Writer) error
}
// ComponentFunc converts a function that matches the Component interface's
// Render method into a Component.
type ComponentFunc func(ctx context.Context, w io.Writer) error
// Render the template.
func (cf ComponentFunc) Render(ctx context.Context, w io.Writer) error {
return cf(ctx, w)
}
// WithNonce sets a CSP nonce on the context and returns it.
func WithNonce(ctx context.Context, nonce string) context.Context {
ctx, v := getContext(ctx)
v.nonce = nonce
return ctx
}
// GetNonce returns the CSP nonce value set with WithNonce, or an
// empty string if none has been set.
func GetNonce(ctx context.Context) (nonce string) {
if ctx == nil {
return ""
}
_, v := getContext(ctx)
return v.nonce
}
func WithChildren(ctx context.Context, children Component) context.Context {
ctx, v := getContext(ctx)
v.children = &children
return ctx
}
func ClearChildren(ctx context.Context) context.Context {
_, v := getContext(ctx)
v.children = nil
return ctx
}
// NopComponent is a component that doesn't render anything.
var NopComponent = ComponentFunc(func(ctx context.Context, w io.Writer) error { return nil })
// GetChildren from the context.
func GetChildren(ctx context.Context) Component {
_, v := getContext(ctx)
if v.children == nil {
return NopComponent
}
return *v.children
}
// EscapeString escapes HTML text within templates.
func EscapeString(s string) string {
return html.EscapeString(s)
}
// Bool attribute value.
func Bool(value bool) bool {
return value
}
// Classes for CSS.
// Supported types are string, ConstantCSSClass, ComponentCSSClass, map[string]bool.
func Classes(classes ...any) CSSClasses {
return CSSClasses(classes)
}
// CSSClasses is a slice of CSS classes.
type CSSClasses []any
// String returns the names of all CSS classes.
func (classes CSSClasses) String() string {
if len(classes) == 0 {
return ""
}
cp := newCSSProcessor()
for _, v := range classes {
cp.Add(v)
}
return cp.String()
}
func newCSSProcessor() *cssProcessor {
return &cssProcessor{
classNameToEnabled: make(map[string]bool),
}
}
type cssProcessor struct {
classNameToEnabled map[string]bool
orderedNames []string
}
func (cp *cssProcessor) Add(item any) {
switch c := item.(type) {
case []string:
for _, className := range c {
cp.AddClassName(className, true)
}
case string:
cp.AddClassName(c, true)
case ConstantCSSClass:
cp.AddClassName(c.ClassName(), true)
case ComponentCSSClass:
cp.AddClassName(c.ClassName(), true)
case map[string]bool:
// In Go, map keys are iterated in a randomized order.
// So the keys in the map must be sorted to produce consistent output.
keys := make([]string, len(c))
var i int
for key := range c {
keys[i] = key
i++
}
sort.Strings(keys)
for _, className := range keys {
cp.AddClassName(className, c[className])
}
case []KeyValue[string, bool]:
for _, kv := range c {
cp.AddClassName(kv.Key, kv.Value)
}
case KeyValue[string, bool]:
cp.AddClassName(c.Key, c.Value)
case []KeyValue[CSSClass, bool]:
for _, kv := range c {
cp.AddClassName(kv.Key.ClassName(), kv.Value)
}
case KeyValue[CSSClass, bool]:
cp.AddClassName(c.Key.ClassName(), c.Value)
case CSSClasses:
for _, item := range c {
cp.Add(item)
}
case []CSSClass:
for _, item := range c {
cp.Add(item)
}
case func() CSSClass:
cp.AddClassName(c().ClassName(), true)
default:
cp.AddClassName(unknownTypeClassName, true)
}
}
func (cp *cssProcessor) AddClassName(className string, enabled bool) {
cp.classNameToEnabled[className] = enabled
cp.orderedNames = append(cp.orderedNames, className)
}
func (cp *cssProcessor) String() string {
// Order the outputs according to how they were input, and remove disabled names.
rendered := make(map[string]any, len(cp.classNameToEnabled))
var names []string
for _, name := range cp.orderedNames {
if enabled := cp.classNameToEnabled[name]; !enabled {
continue
}
if _, hasBeenRendered := rendered[name]; hasBeenRendered {
continue
}
names = append(names, name)
rendered[name] = struct{}{}
}
return strings.Join(names, " ")
}
// KeyValue is a key and value pair.
type KeyValue[TKey comparable, TValue any] struct {
Key TKey `json:"name"`
Value TValue `json:"value"`
}
// KV creates a new key/value pair from the input key and value.
func KV[TKey comparable, TValue any](key TKey, value TValue) KeyValue[TKey, TValue] {
return KeyValue[TKey, TValue]{
Key: key,
Value: value,
}
}
const unknownTypeClassName = "--templ-css-class-unknown-type"
// Class returns a CSS class name.
// Deprecated: use a string instead.
func Class(name string) CSSClass {
return SafeClass(name)
}
// SafeClass bypasses CSS class name validation.
// Deprecated: use a string instead.
func SafeClass(name string) CSSClass {
return ConstantCSSClass(name)
}
// CSSClass provides a class name.
type CSSClass interface {
ClassName() string
}
// ConstantCSSClass is a string constant of a CSS class name.
// Deprecated: use a string instead.
type ConstantCSSClass string
// ClassName of the CSS class.
func (css ConstantCSSClass) ClassName() string {
return string(css)
}
// ComponentCSSClass is a templ.CSS
type ComponentCSSClass struct {
// ID of the class, will be autogenerated.
ID string
// Definition of the CSS.
Class SafeCSS
}
// ClassName of the CSS class.
func (css ComponentCSSClass) ClassName() string {
return css.ID
}
// CSSID calculates an ID.
func CSSID(name string, css string) string {
sum := sha256.Sum256([]byte(css))
hs := hex.EncodeToString(sum[:])[0:8] // NOTE: See issue #978. Minimum recommended hs length is 6.
// Benchmarking showed this was fastest, and with fewest allocations (1).
// Using strings.Builder (2 allocs).
// Using fmt.Sprintf (3 allocs).
return name + "_" + hs
}
// NewCSSMiddleware creates HTTP middleware that renders a global stylesheet of ComponentCSSClass
// CSS if the request path matches, or updates the HTTP context to ensure that any handlers that
// use templ.Components skip rendering <style> elements for classes that are included in the global
// stylesheet. By default, the stylesheet path is /styles/templ.css
func NewCSSMiddleware(next http.Handler, classes ...CSSClass) CSSMiddleware {
return CSSMiddleware{
Path: "/styles/templ.css",
CSSHandler: NewCSSHandler(classes...),
Next: next,
}
}
// CSSMiddleware renders a global stylesheet.
type CSSMiddleware struct {
Path string
CSSHandler CSSHandler
Next http.Handler
}
func (cssm CSSMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == cssm.Path {
cssm.CSSHandler.ServeHTTP(w, r)
return
}
// Add registered classes to the context.
ctx, v := getContext(r.Context())
for _, c := range cssm.CSSHandler.Classes {
v.addClass(c.ID)
}
// Serve the request. Templ components will use the updated context
// to know to skip rendering <style> elements for any component CSS
// classes that have been included in the global stylesheet.
cssm.Next.ServeHTTP(w, r.WithContext(ctx))
}
// NewCSSHandler creates a handler that serves a stylesheet containing the CSS of the
// classes passed in. This is used by the CSSMiddleware to provide global stylesheets
// for templ components.
func NewCSSHandler(classes ...CSSClass) CSSHandler {
ccssc := make([]ComponentCSSClass, 0, len(classes))
for _, c := range classes {
ccss, ok := c.(ComponentCSSClass)
if !ok {
continue
}
ccssc = append(ccssc, ccss)
}
return CSSHandler{
Classes: ccssc,
}
}
// CSSHandler is a HTTP handler that serves CSS.
type CSSHandler struct {
Logger func(err error)
Classes []ComponentCSSClass
}
func (cssh CSSHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/css")
for _, c := range cssh.Classes {
_, err := w.Write([]byte(c.Class))
if err != nil && cssh.Logger != nil {
cssh.Logger(err)
}
}
}
// RenderCSSItems renders the CSS to the writer, if the items haven't already been rendered.
func RenderCSSItems(ctx context.Context, w io.Writer, classes ...any) (err error) {
if len(classes) == 0 {
return nil
}
_, v := getContext(ctx)
sb := new(strings.Builder)
renderCSSItemsToBuilder(sb, v, classes...)
if sb.Len() > 0 {
if _, err = io.WriteString(w, `<style type="text/css">`); err != nil {
return err
}
if _, err = io.WriteString(w, sb.String()); err != nil {
return err
}
if _, err = io.WriteString(w, `</style>`); err != nil {
return err
}
}
return nil
}
func renderCSSItemsToBuilder(sb *strings.Builder, v *contextValue, classes ...any) {
for _, c := range classes {
switch ccc := c.(type) {
case ComponentCSSClass:
if !v.hasClassBeenRendered(ccc.ID) {
sb.WriteString(string(ccc.Class))
v.addClass(ccc.ID)
}
case KeyValue[ComponentCSSClass, bool]:
if !ccc.Value {
continue
}
renderCSSItemsToBuilder(sb, v, ccc.Key)
case KeyValue[CSSClass, bool]:
if !ccc.Value {
continue
}
renderCSSItemsToBuilder(sb, v, ccc.Key)
case CSSClasses:
renderCSSItemsToBuilder(sb, v, ccc...)
case []CSSClass:
for _, item := range ccc {
renderCSSItemsToBuilder(sb, v, item)
}
case func() CSSClass:
renderCSSItemsToBuilder(sb, v, ccc())
case []string:
// Skip. These are class names, not CSS classes.
case string:
// Skip. This is a class name, not a CSS class.
case ConstantCSSClass:
// Skip. This is a class name, not a CSS class.
case CSSClass:
// Skip. This is a class name, not a CSS class.
case map[string]bool:
// Skip. These are class names, not CSS classes.
case KeyValue[string, bool]:
// Skip. These are class names, not CSS classes.
case []KeyValue[string, bool]:
// Skip. These are class names, not CSS classes.
case KeyValue[ConstantCSSClass, bool]:
// Skip. These are class names, not CSS classes.
case []KeyValue[ConstantCSSClass, bool]:
// Skip. These are class names, not CSS classes.
}
}
}
// SafeCSS is CSS that has been sanitized.
type SafeCSS string
type SafeCSSProperty string
var safeCSSPropertyType = reflect.TypeOf(SafeCSSProperty(""))
// SanitizeCSS sanitizes CSS properties to ensure that they are safe.
func SanitizeCSS[T ~string](property string, value T) SafeCSS {
if reflect.TypeOf(value) == safeCSSPropertyType {
return SafeCSS(safehtml.SanitizeCSSProperty(property) + ":" + string(value) + ";")
}
p, v := safehtml.SanitizeCSS(property, string(value))
return SafeCSS(p + ":" + v + ";")
}
// Attributes is an alias to map[string]any made for spread attributes.
type Attributes map[string]any
// sortedKeys returns the keys of a map in sorted order.
func sortedKeys(m map[string]any) (keys []string) {
keys = make([]string, len(m))
var i int
for k := range m {
keys[i] = k
i++
}
sort.Strings(keys)
return keys
}
func writeStrings(w io.Writer, ss ...string) (err error) {
for _, s := range ss {
if _, err = io.WriteString(w, s); err != nil {
return err
}
}
return nil
}
func RenderAttributes(ctx context.Context, w io.Writer, attributes Attributes) (err error) {
for _, key := range sortedKeys(attributes) {
value := attributes[key]
switch value := value.(type) {
case string:
if err = writeStrings(w, ` `, EscapeString(key), `="`, EscapeString(value), `"`); err != nil {
return err
}
case *string:
if value != nil {
if err = writeStrings(w, ` `, EscapeString(key), `="`, EscapeString(*value), `"`); err != nil {
return err
}
}
case bool:
if value {
if err = writeStrings(w, ` `, EscapeString(key)); err != nil {
return err
}
}
case *bool:
if value != nil && *value {
if err = writeStrings(w, ` `, EscapeString(key)); err != nil {
return err
}
}
case KeyValue[string, bool]:
if value.Value {
if err = writeStrings(w, ` `, EscapeString(key), `="`, EscapeString(value.Key), `"`); err != nil {
return err
}
}
case KeyValue[bool, bool]:
if value.Value && value.Key {
if err = writeStrings(w, ` `, EscapeString(key)); err != nil {
return err
}
}
case func() bool:
if value() {
if err = writeStrings(w, ` `, EscapeString(key)); err != nil {
return err
}
}
}
}
return nil
}
// Context.
type contextKeyType int
const contextKey = contextKeyType(0)
type contextValue struct {
ss map[string]struct{}
onceHandles map[*OnceHandle]struct{}
children *Component
nonce string
}
func (v *contextValue) setHasBeenRendered(h *OnceHandle) {
if v.onceHandles == nil {
v.onceHandles = map[*OnceHandle]struct{}{}
}
v.onceHandles[h] = struct{}{}
}
func (v *contextValue) getHasBeenRendered(h *OnceHandle) (ok bool) {
if v.onceHandles == nil {
v.onceHandles = map[*OnceHandle]struct{}{}
}
_, ok = v.onceHandles[h]
return
}
func (v *contextValue) addScript(s string) {
if v.ss == nil {
v.ss = map[string]struct{}{}
}
v.ss["script_"+s] = struct{}{}
}
func (v *contextValue) hasScriptBeenRendered(s string) (ok bool) {
if v.ss == nil {
v.ss = map[string]struct{}{}
}
_, ok = v.ss["script_"+s]
return
}
func (v *contextValue) addClass(s string) {
if v.ss == nil {
v.ss = map[string]struct{}{}
}
v.ss["class_"+s] = struct{}{}
}
func (v *contextValue) hasClassBeenRendered(s string) (ok bool) {
if v.ss == nil {
v.ss = map[string]struct{}{}
}
_, ok = v.ss["class_"+s]
return
}
// InitializeContext initializes context used to store internal state used during rendering.
func InitializeContext(ctx context.Context) context.Context {
if _, ok := ctx.Value(contextKey).(*contextValue); ok {
return ctx
}
v := &contextValue{}
ctx = context.WithValue(ctx, contextKey, v)
return ctx
}
func getContext(ctx context.Context) (context.Context, *contextValue) {
v, ok := ctx.Value(contextKey).(*contextValue)
if !ok {
ctx = InitializeContext(ctx)
v = ctx.Value(contextKey).(*contextValue)
}
return ctx, v
}
var bufferPool = sync.Pool{
New: func() any {
return new(bytes.Buffer)
},
}
func GetBuffer() *bytes.Buffer {
return bufferPool.Get().(*bytes.Buffer)
}
func ReleaseBuffer(b *bytes.Buffer) {
b.Reset()
bufferPool.Put(b)
}
// JoinStringErrs joins an optional list of errors.
func JoinStringErrs(s string, errs ...error) (string, error) {
return s, errors.Join(errs...)
}
// Error returned during template rendering.
type Error struct {
Err error
// FileName of the template file.
FileName string
// Line index of the error.
Line int
// Col index of the error.
Col int
}
func (e Error) Error() string {
if e.FileName == "" {
e.FileName = "templ"
}
return fmt.Sprintf("%s: error at line %d, col %d: %v", e.FileName, e.Line, e.Col, e.Err)
}
func (e Error) Unwrap() error {
return e.Err
}
// Raw renders the input HTML to the output without applying HTML escaping.
//
// Use of this component presents a security risk - the HTML should come from
// a trusted source, because it will be included as-is in the output.
func Raw[T ~string](html T, errs ...error) Component {
return ComponentFunc(func(ctx context.Context, w io.Writer) (err error) {
if err = errors.Join(errs...); err != nil {
return err
}
_, err = io.WriteString(w, string(html))
return err
})
}
// FromGoHTML creates a templ Component from a Go html/template template.
func FromGoHTML(t *template.Template, data any) Component {
return ComponentFunc(func(ctx context.Context, w io.Writer) (err error) {
return t.Execute(w, data)
})
}
// ToGoHTML renders the component to a Go html/template template.HTML string.
func ToGoHTML(ctx context.Context, c Component) (s template.HTML, err error) {
b := GetBuffer()
defer ReleaseBuffer(b)
if err = c.Render(ctx, b); err != nil {
return
}
s = template.HTML(b.String())
return
}
package runtime
import (
"bufio"
"io"
"net/http"
)
// DefaultBufferSize is the default size of buffers. It is set to 4KB by default, which is the
// same as the default buffer size of bufio.Writer.
var DefaultBufferSize = 4 * 1024 // 4KB
// Buffer is a wrapper around bufio.Writer that enables flushing and closing of
// the underlying writer.
type Buffer struct {
Underlying io.Writer
b *bufio.Writer
}
// Write the contents of p into the buffer.
func (b *Buffer) Write(p []byte) (n int, err error) {
return b.b.Write(p)
}
// Flush writes any buffered data to the underlying io.Writer and
// calls the Flush method of the underlying http.Flusher if it implements it.
func (b *Buffer) Flush() error {
if err := b.b.Flush(); err != nil {
return err
}
if f, ok := b.Underlying.(http.Flusher); ok {
f.Flush()
}
return nil
}
// Close closes the buffer and the underlying io.Writer if it implements io.Closer.
func (b *Buffer) Close() error {
if c, ok := b.Underlying.(io.Closer); ok {
return c.Close()
}
return nil
}
// Reset sets the underlying io.Writer to w and resets the buffer.
func (b *Buffer) Reset(w io.Writer) {
if b.b == nil {
b.b = bufio.NewWriterSize(b, DefaultBufferSize)
}
b.Underlying = w
b.b.Reset(w)
}
// Size returns the size of the underlying buffer in bytes.
func (b *Buffer) Size() int {
return b.b.Size()
}
// WriteString writes the contents of s into the buffer.
func (b *Buffer) WriteString(s string) (n int, err error) {
return b.b.WriteString(s)
}
package runtime
import (
"io"
"sync"
)
var bufferPool = sync.Pool{
New: func() any {
return new(Buffer)
},
}
// GetBuffer creates and returns a new buffer if the writer is not already a buffer,
// or returns the existing buffer if it is.
func GetBuffer(w io.Writer) (b *Buffer, existing bool) {
if w == nil {
return nil, false
}
b, ok := w.(*Buffer)
if ok {
return b, true
}
b = bufferPool.Get().(*Buffer)
b.Reset(w)
return b, false
}
// ReleaseBuffer flushes the buffer and returns it to the pool.
func ReleaseBuffer(w io.Writer) (err error) {
b, ok := w.(*Buffer)
if !ok {
return nil
}
err = b.Flush()
bufferPool.Put(b)
return err
}
package runtime
import "strings"
// GetBuilder returns a strings.Builder.
func GetBuilder() (sb strings.Builder) {
return sb
}
package runtime
import (
"context"
"io"
"github.com/a-h/templ"
)
// GeneratedComponentInput is used to avoid generated code needing to import the `context` and `io` packages.
type GeneratedComponentInput struct {
Context context.Context
Writer io.Writer
}
// GeneratedTemplate is used to avoid generated code needing to import the `context` and `io` packages.
func GeneratedTemplate(f func(GeneratedComponentInput) error) templ.Component {
return templ.ComponentFunc(func(ctx context.Context, w io.Writer) error {
return f(GeneratedComponentInput{ctx, w})
})
}
package runtime
import (
"errors"
"fmt"
"html"
"maps"
"reflect"
"slices"
"strings"
"github.com/a-h/templ"
"github.com/a-h/templ/safehtml"
)
// SanitizeStyleAttributeValues renders a style attribute value.
// The supported types are:
// - string
// - templ.SafeCSS
// - map[string]string
// - map[string]templ.SafeCSSProperty
// - templ.KeyValue[string, string] - A map of key/values where the key is the CSS property name and the value is the CSS property value.
// - templ.KeyValue[string, templ.SafeCSSProperty] - A map of key/values where the key is the CSS property name and the value is the CSS property value.
// - templ.KeyValue[string, bool] - The bool determines whether the value should be included.
// - templ.KeyValue[templ.SafeCSS, bool] - The bool determines whether the value should be included.
// - func() (anyOfTheAboveTypes)
// - func() (anyOfTheAboveTypes, error)
// - []anyOfTheAboveTypes
//
// In the above, templ.SafeCSS and templ.SafeCSSProperty are types that are used to indicate that the value is safe to render as CSS without sanitization.
// All other types are sanitized before rendering.
//
// If an error is returned by any function, or a non-nil error is included in the input, the error is returned.
func SanitizeStyleAttributeValues(values ...any) (string, error) {
if err := getJoinedErrorsFromValues(values...); err != nil {
return "", err
}
sb := new(strings.Builder)
for _, v := range values {
if v == nil {
continue
}
if err := sanitizeStyleAttributeValue(sb, v); err != nil {
return "", err
}
}
return sb.String(), nil
}
func sanitizeStyleAttributeValue(sb *strings.Builder, v any) error {
// Process concrete types.
switch v := v.(type) {
case string:
return processString(sb, v)
case templ.SafeCSS:
return processSafeCSS(sb, v)
case map[string]string:
return processStringMap(sb, v)
case map[string]templ.SafeCSSProperty:
return processSafeCSSPropertyMap(sb, v)
case templ.KeyValue[string, string]:
return processStringKV(sb, v)
case templ.KeyValue[string, bool]:
if v.Value {
return processString(sb, v.Key)
}
return nil
case templ.KeyValue[templ.SafeCSS, bool]:
if v.Value {
return processSafeCSS(sb, v.Key)
}
return nil
}
// Fall back to reflection.
// Handle functions first using reflection.
if handled, err := handleFuncWithReflection(sb, v); handled {
return err
}
// Handle slices using reflection before concrete types.
if handled, err := handleSliceWithReflection(sb, v); handled {
return err
}
_, err := sb.WriteString(TemplUnsupportedStyleAttributeValue)
return err
}
func processSafeCSS(sb *strings.Builder, v templ.SafeCSS) error {
if v == "" {
return nil
}
sb.WriteString(html.EscapeString(string(v)))
if !strings.HasSuffix(string(v), ";") {
sb.WriteRune(';')
}
return nil
}
func processString(sb *strings.Builder, v string) error {
if v == "" {
return nil
}
sanitized := strings.TrimSpace(safehtml.SanitizeStyleValue(v))
sb.WriteString(html.EscapeString(sanitized))
if !strings.HasSuffix(sanitized, ";") {
sb.WriteRune(';')
}
return nil
}
var ErrInvalidStyleAttributeFunctionSignature = errors.New("invalid function signature, should be in the form func() (string, error)")
// handleFuncWithReflection handles functions using reflection.
func handleFuncWithReflection(sb *strings.Builder, v any) (bool, error) {
rv := reflect.ValueOf(v)
if rv.Kind() != reflect.Func {
return false, nil
}
t := rv.Type()
if t.NumIn() != 0 || (t.NumOut() != 1 && t.NumOut() != 2) {
return false, ErrInvalidStyleAttributeFunctionSignature
}
// Check the types of the return values
if t.NumOut() == 2 {
// Ensure the second return value is of type `error`
secondReturnType := t.Out(1)
if !secondReturnType.Implements(reflect.TypeOf((*error)(nil)).Elem()) {
return false, fmt.Errorf("second return value must be of type error, got %v", secondReturnType)
}
}
results := rv.Call(nil)
if t.NumOut() == 2 {
// Check if the second return value is an error
if errVal := results[1].Interface(); errVal != nil {
if err, ok := errVal.(error); ok && err != nil {
return true, err
}
}
}
return true, sanitizeStyleAttributeValue(sb, results[0].Interface())
}
// handleSliceWithReflection handles slices using reflection.
func handleSliceWithReflection(sb *strings.Builder, v any) (bool, error) {
rv := reflect.ValueOf(v)
if rv.Kind() != reflect.Slice {
return false, nil
}
for i := 0; i < rv.Len(); i++ {
elem := rv.Index(i).Interface()
if err := sanitizeStyleAttributeValue(sb, elem); err != nil {
return true, err
}
}
return true, nil
}
// processStringMap processes a map[string]string.
func processStringMap(sb *strings.Builder, m map[string]string) error {
for _, name := range slices.Sorted(maps.Keys(m)) {
name, value := safehtml.SanitizeCSS(name, m[name])
sb.WriteString(html.EscapeString(name))
sb.WriteRune(':')
sb.WriteString(html.EscapeString(value))
sb.WriteRune(';')
}
return nil
}
// processSafeCSSPropertyMap processes a map[string]templ.SafeCSSProperty.
func processSafeCSSPropertyMap(sb *strings.Builder, m map[string]templ.SafeCSSProperty) error {
for _, name := range slices.Sorted(maps.Keys(m)) {
sb.WriteString(html.EscapeString(safehtml.SanitizeCSSProperty(name)))
sb.WriteRune(':')
sb.WriteString(html.EscapeString(string(m[name])))
sb.WriteRune(';')
}
return nil
}
// processStringKV processes a templ.KeyValue[string, string].
func processStringKV(sb *strings.Builder, kv templ.KeyValue[string, string]) error {
name, value := safehtml.SanitizeCSS(kv.Key, kv.Value)
sb.WriteString(html.EscapeString(name))
sb.WriteRune(':')
sb.WriteString(html.EscapeString(value))
sb.WriteRune(';')
return nil
}
// getJoinedErrorsFromValues collects and joins errors from the input values.
func getJoinedErrorsFromValues(values ...any) error {
var errs []error
for _, v := range values {
if err, ok := v.(error); ok {
errs = append(errs, err)
}
}
return errors.Join(errs...)
}
// TemplUnsupportedStyleAttributeValue is the default value returned for unsupported types.
var TemplUnsupportedStyleAttributeValue = "zTemplUnsupportedStyleAttributeValue:Invalid;"
package runtime
import (
"errors"
"fmt"
"io"
"os"
"runtime"
"strconv"
"strings"
"sync"
"time"
)
var developmentMode = os.Getenv("TEMPL_DEV_MODE") == "true"
// WriteString writes the string to the writer. If development mode is enabled
// s is replaced with the string at the index in the _templ.txt file.
func WriteString(w io.Writer, index int, s string) (err error) {
if developmentMode {
_, path, _, _ := runtime.Caller(1)
if !strings.HasSuffix(path, "_templ.go") {
return errors.New("templ: attempt to use WriteString from a non templ file")
}
txtFilePath := strings.Replace(path, "_templ.go", "_templ.txt", 1)
literals, err := getWatchedStrings(txtFilePath)
if err != nil {
return fmt.Errorf("templ: failed to cache strings: %w", err)
}
if index > len(literals) {
return fmt.Errorf("templ: failed to find line %d in %s", index, txtFilePath)
}
s, err = strconv.Unquote(`"` + literals[index-1] + `"`)
if err != nil {
return err
}
}
_, err = io.WriteString(w, s)
return err
}
var (
watchModeCache = map[string]watchState{}
watchStateMutex sync.Mutex
)
type watchState struct {
modTime time.Time
strings []string
}
func getWatchedStrings(txtFilePath string) ([]string, error) {
watchStateMutex.Lock()
defer watchStateMutex.Unlock()
state, cached := watchModeCache[txtFilePath]
if !cached {
return cacheStrings(txtFilePath)
}
if time.Since(state.modTime) < time.Millisecond*100 {
return state.strings, nil
}
info, err := os.Stat(txtFilePath)
if err != nil {
return nil, fmt.Errorf("templ: failed to stat %s: %w", txtFilePath, err)
}
if !info.ModTime().After(state.modTime) {
return state.strings, nil
}
return cacheStrings(txtFilePath)
}
func cacheStrings(txtFilePath string) ([]string, error) {
txtFile, err := os.Open(txtFilePath)
if err != nil {
return nil, fmt.Errorf("templ: failed to open %s: %w", txtFilePath, err)
}
defer txtFile.Close()
info, err := txtFile.Stat()
if err != nil {
return nil, fmt.Errorf("templ: failed to stat %s: %w", txtFilePath, err)
}
all, err := io.ReadAll(txtFile)
if err != nil {
return nil, fmt.Errorf("templ: failed to read %s: %w", txtFilePath, err)
}
literals := strings.Split(string(all), "\n")
watchModeCache[txtFilePath] = watchState{
modTime: info.ModTime(),
strings: literals,
}
return literals, nil
}
// Adapted from https://raw.githubusercontent.com/google/safehtml/3c4cd5b5d8c9a6c5882fba099979e9f50b65c876/style.go
// Copyright (c) 2017 The Go Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
package safehtml
import (
"bytes"
"fmt"
"net/url"
"regexp"
"strings"
)
// SanitizeCSS attempts to sanitize CSS properties.
func SanitizeCSS(property, value string) (string, string) {
property = SanitizeCSSProperty(property)
if property == InnocuousPropertyName {
return InnocuousPropertyName, InnocuousPropertyValue
}
return property, SanitizeCSSValue(property, value)
}
func SanitizeCSSValue(property, value string) string {
if sanitizer, ok := cssPropertyNameToValueSanitizer[property]; ok {
return sanitizer(value)
}
return sanitizeRegular(value)
}
func SanitizeCSSProperty(property string) string {
if !identifierPattern.MatchString(property) {
return InnocuousPropertyName
}
return strings.ToLower(property)
}
// identifierPattern matches a subset of valid <ident-token> values defined in
// https://www.w3.org/TR/css-syntax-3/#ident-token-diagram. This pattern matches all generic family name
// keywords defined in https://drafts.csswg.org/css-fonts-3/#family-name-value.
var identifierPattern = regexp.MustCompile(`^[-a-zA-Z]+$`)
var cssPropertyNameToValueSanitizer = map[string]func(string) string{
"background-image": sanitizeBackgroundImage,
"font-family": sanitizeFontFamily,
"display": sanitizeEnum,
"background-color": sanitizeRegular,
"background-position": sanitizeRegular,
"background-repeat": sanitizeRegular,
"background-size": sanitizeRegular,
"color": sanitizeRegular,
"height": sanitizeRegular,
"width": sanitizeRegular,
"left": sanitizeRegular,
"right": sanitizeRegular,
"top": sanitizeRegular,
"bottom": sanitizeRegular,
"font-weight": sanitizeRegular,
"padding": sanitizeRegular,
"z-index": sanitizeRegular,
}
var validURLPrefixes = []string{
`url("`,
`url('`,
`url(`,
}
var validURLSuffixes = []string{
`")`,
`')`,
`)`,
}
func sanitizeBackgroundImage(v string) string {
// Check for <> as per https://github.com/google/safehtml/blob/be23134998433fcf0135dda53593fc8f8bf4df7c/style.go#L87C2-L89C3
if strings.ContainsAny(v, "<>") {
return InnocuousPropertyValue
}
for _, u := range strings.Split(v, ",") {
u = strings.TrimSpace(u)
var found bool
for i, prefix := range validURLPrefixes {
if strings.HasPrefix(u, prefix) && strings.HasSuffix(u, validURLSuffixes[i]) {
found = true
u = strings.TrimPrefix(u, validURLPrefixes[i])
u = strings.TrimSuffix(u, validURLSuffixes[i])
break
}
}
if !found || !urlIsSafe(u) {
return InnocuousPropertyValue
}
}
return v
}
func urlIsSafe(s string) bool {
u, err := url.Parse(s)
if err != nil {
return false
}
if u.IsAbs() {
if strings.EqualFold(u.Scheme, "http") || strings.EqualFold(u.Scheme, "https") || strings.EqualFold(u.Scheme, "mailto") {
return true
}
return false
}
return true
}
var genericFontFamilyName = regexp.MustCompile(`^[a-zA-Z][- a-zA-Z]+$`)
func sanitizeFontFamily(s string) string {
for _, f := range strings.Split(s, ",") {
f = strings.TrimSpace(f)
if strings.HasPrefix(f, `"`) {
if !strings.HasSuffix(f, `"`) {
return InnocuousPropertyValue
}
continue
}
if !genericFontFamilyName.MatchString(f) {
return InnocuousPropertyValue
}
}
return s
}
func sanitizeEnum(s string) string {
if !safeEnumPropertyValuePattern.MatchString(s) {
return InnocuousPropertyValue
}
return s
}
func sanitizeRegular(s string) string {
if !safeRegularPropertyValuePattern.MatchString(s) {
return InnocuousPropertyValue
}
return s
}
// InnocuousPropertyName is an innocuous property generated by a sanitizer when its input is unsafe.
const InnocuousPropertyName = "zTemplUnsafeCSSPropertyName"
// InnocuousPropertyValue is an innocuous property generated by a sanitizer when its input is unsafe.
const InnocuousPropertyValue = "zTemplUnsafeCSSPropertyValue"
// safeRegularPropertyValuePattern matches strings that are safe to use as property values.
// Specifically, it matches string where every '*' or '/' is followed by end-of-text or a safe rune
// (i.e. alphanumerics or runes in the set [+-.!#%_ \t]). This regex ensures that the following
// are disallowed:
// - "/*" and "*/", which are CSS comment markers.
// - "//", even though this is not a comment marker in the CSS specification. Disallowing
// this string minimizes the chance that browser peculiarities or parsing bugs will allow
// sanitization to be bypassed.
// - '(' and ')', which can be used to call functions.
// - ',', since it can be used to inject extra values into a property.
// - Runes which could be matched on CSS error recovery of a previously malformed token, such as '@'
// and ':'. See http://www.w3.org/TR/css3-syntax/#error-handling.
var safeRegularPropertyValuePattern = regexp.MustCompile(`^(?:[*/]?(?:[0-9a-zA-Z+-.!#%_ \t]|$))*$`)
// safeEnumPropertyValuePattern matches strings that are safe to use as enumerated property values.
// Specifically, it matches strings that contain only alphabetic and '-' runes.
var safeEnumPropertyValuePattern = regexp.MustCompile(`^[a-zA-Z-]*$`)
// SanitizeStyleValue escapes s so that it is safe to put between "" to form a CSS <string-token>.
// See syntax at https://www.w3.org/TR/css-syntax-3/#string-token-diagram.
//
// On top of the escape sequences required in <string-token>, this function also escapes
// control runes to minimize the risk of these runes triggering browser-specific bugs.
// Taken from cssEscapeString in safehtml package.
func SanitizeStyleValue(s string) string {
var b bytes.Buffer
b.Grow(len(s))
for _, c := range s {
switch {
case c == '\u0000':
// Replace the NULL byte according to https://www.w3.org/TR/css-syntax-3/#input-preprocessing.
// We take this extra precaution in case the user agent fails to handle NULL properly.
b.WriteString("\uFFFD")
case c == '<', // Prevents breaking out of a style element with `</style>`. Escape this in case the Style user forgets to.
c == '"', c == '\\', // Must be CSS-escaped in <string-token>. U+000A line feed is handled in the next case.
c <= '\u001F', c == '\u007F', // C0 control codes
c >= '\u0080' && c <= '\u009F', // C1 control codes
c == '\u2028', c == '\u2029': // Unicode newline characters
// See CSS escape sequence syntax at https://www.w3.org/TR/css-syntax-3/#escape-diagram.
fmt.Fprintf(&b, "\\%06X", c)
default:
b.WriteRune(c)
}
}
return b.String()
}
package templ
import (
"context"
"encoding/json"
"fmt"
"html"
"io"
"regexp"
"strings"
)
// ComponentScript is a templ Script template.
type ComponentScript struct {
// Name of the script, e.g. print.
Name string
// Function to render.
Function string
// Call of the function in JavaScript syntax, including parameters, and
// ensures parameters are HTML escaped; useful for injecting into HTML
// attributes like onclick, onhover, etc.
//
// Given:
// functionName("some string",12345)
// It would render:
// __templ_functionName_sha("some string",12345))
//
// This is can be injected into HTML attributes:
// <button onClick="__templ_functionName_sha("some string",12345))">Click Me</button>
Call string
// Call of the function in JavaScript syntax, including parameters. It
// does not HTML escape parameters; useful for directly calling in script
// elements.
//
// Given:
// functionName("some string",12345)
// It would render:
// __templ_functionName_sha("some string",12345))
//
// This is can be used to call the function inside a script tag:
// <script>__templ_functionName_sha("some string",12345))</script>
CallInline string
}
var _ Component = ComponentScript{}
func writeScriptHeader(ctx context.Context, w io.Writer) (err error) {
var nonceAttr string
if nonce := GetNonce(ctx); nonce != "" {
nonceAttr = " nonce=\"" + EscapeString(nonce) + "\""
}
_, err = fmt.Fprintf(w, `<script%s>`, nonceAttr)
return err
}
func (c ComponentScript) Render(ctx context.Context, w io.Writer) error {
err := RenderScriptItems(ctx, w, c)
if err != nil {
return err
}
if len(c.Call) > 0 {
if err = writeScriptHeader(ctx, w); err != nil {
return err
}
if _, err = io.WriteString(w, c.CallInline); err != nil {
return err
}
if _, err = io.WriteString(w, `</script>`); err != nil {
return err
}
}
return nil
}
// RenderScriptItems renders a <script> element, if the script has not already been rendered.
func RenderScriptItems(ctx context.Context, w io.Writer, scripts ...ComponentScript) (err error) {
if len(scripts) == 0 {
return nil
}
_, v := getContext(ctx)
sb := new(strings.Builder)
for _, s := range scripts {
if !v.hasScriptBeenRendered(s.Name) {
sb.WriteString(s.Function)
v.addScript(s.Name)
}
}
if sb.Len() > 0 {
if err = writeScriptHeader(ctx, w); err != nil {
return err
}
if _, err = io.WriteString(w, sb.String()); err != nil {
return err
}
if _, err = io.WriteString(w, `</script>`); err != nil {
return err
}
}
return nil
}
// JSExpression represents a JavaScript expression intended for use as an argument for script templates.
// The string value of JSExpression will be inserted directly as JavaScript code in function call arguments.
type JSExpression string
// SafeScript encodes unknown parameters for safety for inside HTML attributes.
func SafeScript(functionName string, params ...any) string {
if !jsFunctionName.MatchString(functionName) {
functionName = "__templ_invalid_js_function_name"
}
sb := new(strings.Builder)
sb.WriteString(html.EscapeString(functionName))
sb.WriteRune('(')
for i, p := range params {
sb.WriteString(EscapeString(jsonEncodeParam(p)))
if i < len(params)-1 {
sb.WriteRune(',')
}
}
sb.WriteRune(')')
return sb.String()
}
// SafeScript encodes unknown parameters for safety for inline scripts.
func SafeScriptInline(functionName string, params ...any) string {
if !jsFunctionName.MatchString(functionName) {
functionName = "__templ_invalid_js_function_name"
}
sb := new(strings.Builder)
sb.WriteString(functionName)
sb.WriteRune('(')
for i, p := range params {
sb.WriteString(jsonEncodeParam(p))
if i < len(params)-1 {
sb.WriteRune(',')
}
}
sb.WriteRune(')')
return sb.String()
}
func jsonEncodeParam(param any) string {
if val, ok := param.(JSExpression); ok {
return string(val)
}
enc, _ := json.Marshal(param)
return string(enc)
}
// isValidJSFunctionName returns true if the given string is a valid JavaScript function name, e.g. console.log, alert, etc.
var jsFunctionName = regexp.MustCompile(`^([$_a-zA-Z][$_a-zA-Z0-9]+\.?)+$`)
package turbo
import (
"context"
"net/http"
"strings"
"github.com/a-h/templ"
)
// Append adds an append action to the output stream.
func Append(w http.ResponseWriter, target string, template templ.Component) error {
return AppendWithContext(context.Background(), w, target, template)
}
// AppendWithContext adds an append action to the output stream.
func AppendWithContext(ctx context.Context, w http.ResponseWriter, target string, template templ.Component) error {
w.Header().Set("Content-Type", "text/vnd.turbo-stream.html")
return actionTemplate("append", target).Render(templ.WithChildren(ctx, template), w)
}
// Prepend adds a prepend action to the output stream.
func Prepend(w http.ResponseWriter, target string, template templ.Component) error {
return PrependWithContext(context.Background(), w, target, template)
}
// PrependWithContext adds a prepend action to the output stream.
func PrependWithContext(ctx context.Context, w http.ResponseWriter, target string, template templ.Component) error {
w.Header().Set("Content-Type", "text/vnd.turbo-stream.html")
return actionTemplate("prepend", target).Render(templ.WithChildren(ctx, template), w)
}
// Replace adds a replace action to the output stream.
func Replace(w http.ResponseWriter, target string, template templ.Component) error {
return ReplaceWithContext(context.Background(), w, target, template)
}
// ReplaceWithContext adds a replace action to the output stream.
func ReplaceWithContext(ctx context.Context, w http.ResponseWriter, target string, template templ.Component) error {
w.Header().Set("Content-Type", "text/vnd.turbo-stream.html")
return actionTemplate("replace", target).Render(templ.WithChildren(ctx, template), w)
}
// Update adds an update action to the output stream.
func Update(w http.ResponseWriter, target string, template templ.Component) error {
return UpdateWithContext(context.Background(), w, target, template)
}
// UpdateWithContext adds an update action to the output stream.
func UpdateWithContext(ctx context.Context, w http.ResponseWriter, target string, template templ.Component) error {
w.Header().Set("Content-Type", "text/vnd.turbo-stream.html")
return actionTemplate("update", target).Render(templ.WithChildren(ctx, template), w)
}
// Remove adds a remove action to the output stream.
func Remove(w http.ResponseWriter, target string) error {
return RemoveWithContext(context.Background(), w, target)
}
// RemoveWithContext adds a remove action to the output stream.
func RemoveWithContext(ctx context.Context, w http.ResponseWriter, target string) error {
w.Header().Set("Content-Type", "text/vnd.turbo-stream.html")
return removeTemplate("remove", target).Render(ctx, w)
}
// IsTurboRequest returns true if the incoming request is able to receive a Turbo stream.
// This is determined by checking the request header for "text/vnd.turbo-stream.html"
func IsTurboRequest(r *http.Request) bool {
return strings.Contains(r.Header.Get("accept"), "text/vnd.turbo-stream.html")
}
// Code generated by templ - DO NOT EDIT.
package turbo
//lint:file-ignore SA4006 This context is only used if a nested component is present.
import "github.com/a-h/templ"
import templruntime "github.com/a-h/templ/runtime"
func actionTemplate(action string, target string) templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
return templ_7745c5c3_CtxErr
}
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var1 := templ.GetChildren(ctx)
if templ_7745c5c3_Var1 == nil {
templ_7745c5c3_Var1 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 1, "<turbo-stream action=\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var2 string
templ_7745c5c3_Var2, templ_7745c5c3_Err = templ.JoinStringErrs(action)
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `turbo/stream.templ`, Line: 4, Col: 30}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var2))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 2, "\" target=\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var3 string
templ_7745c5c3_Var3, templ_7745c5c3_Err = templ.JoinStringErrs(target)
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `turbo/stream.templ`, Line: 4, Col: 48}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var3))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 3, "\"><template>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templ_7745c5c3_Var1.Render(ctx, templ_7745c5c3_Buffer)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 4, "</template></turbo-stream>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return nil
})
}
func removeTemplate(action string, target string) templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
return templ_7745c5c3_CtxErr
}
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var4 := templ.GetChildren(ctx)
if templ_7745c5c3_Var4 == nil {
templ_7745c5c3_Var4 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 5, "<turbo-stream action=\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var5 string
templ_7745c5c3_Var5, templ_7745c5c3_Err = templ.JoinStringErrs(action)
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `turbo/stream.templ`, Line: 12, Col: 30}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var5))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 6, "\" target=\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var6 string
templ_7745c5c3_Var6, templ_7745c5c3_Err = templ.JoinStringErrs(target)
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `turbo/stream.templ`, Line: 12, Col: 48}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var6))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 7, "\"></turbo-stream>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return nil
})
}
var _ = templruntime.GeneratedTemplate
package templ
import "strings"
// FailedSanitizationURL is returned if a URL fails sanitization checks.
const FailedSanitizationURL = SafeURL("about:invalid#TemplFailedSanitizationURL")
// URL sanitizes the input string s and returns a SafeURL.
func URL(s string) SafeURL {
if i := strings.IndexRune(s, ':'); i >= 0 && !strings.ContainsRune(s[:i], '/') {
protocol := s[:i]
if !strings.EqualFold(protocol, "http") && !strings.EqualFold(protocol, "https") && !strings.EqualFold(protocol, "mailto") && !strings.EqualFold(protocol, "tel") && !strings.EqualFold(protocol, "ftp") && !strings.EqualFold(protocol, "ftps") {
return FailedSanitizationURL
}
}
return SafeURL(s)
}
// SafeURL is a URL that has been sanitized.
type SafeURL string
package templ
import _ "embed"
//go:embed .version
var version string
func Version() string {
return "v" + version
}
package templ
import (
"errors"
"fmt"
"io"
"os"
"runtime"
"strconv"
"strings"
"sync"
"time"
)
// WriteWatchModeString is used when rendering templates in development mode.
// the generator would have written non-go code to the _templ.txt file, which
// is then read by this function and written to the output.
//
// Deprecated: since templ v0.3.x generated code uses WriteString.
func WriteWatchModeString(w io.Writer, lineNum int) error {
_, path, _, _ := runtime.Caller(1)
if !strings.HasSuffix(path, "_templ.go") {
return errors.New("templ: WriteWatchModeString can only be called from _templ.go")
}
txtFilePath := strings.Replace(path, "_templ.go", "_templ.txt", 1)
literals, err := getWatchedStrings(txtFilePath)
if err != nil {
return fmt.Errorf("templ: failed to cache strings: %w", err)
}
if lineNum > len(literals) {
return fmt.Errorf("templ: failed to find line %d in %s", lineNum, txtFilePath)
}
s, err := strconv.Unquote(`"` + literals[lineNum-1] + `"`)
if err != nil {
return err
}
_, err = io.WriteString(w, s)
return err
}
var (
watchModeCache = map[string]watchState{}
watchStateMutex sync.Mutex
)
type watchState struct {
modTime time.Time
strings []string
}
func getWatchedStrings(txtFilePath string) ([]string, error) {
watchStateMutex.Lock()
defer watchStateMutex.Unlock()
state, cached := watchModeCache[txtFilePath]
if !cached {
return cacheStrings(txtFilePath)
}
if time.Since(state.modTime) < time.Millisecond*100 {
return state.strings, nil
}
info, err := os.Stat(txtFilePath)
if err != nil {
return nil, fmt.Errorf("templ: failed to stat %s: %w", txtFilePath, err)
}
if !info.ModTime().After(state.modTime) {
return state.strings, nil
}
return cacheStrings(txtFilePath)
}
func cacheStrings(txtFilePath string) ([]string, error) {
txtFile, err := os.Open(txtFilePath)
if err != nil {
return nil, fmt.Errorf("templ: failed to open %s: %w", txtFilePath, err)
}
defer txtFile.Close()
info, err := txtFile.Stat()
if err != nil {
return nil, fmt.Errorf("templ: failed to stat %s: %w", txtFilePath, err)
}
all, err := io.ReadAll(txtFile)
if err != nil {
return nil, fmt.Errorf("templ: failed to read %s: %w", txtFilePath, err)
}
literals := strings.Split(string(all), "\n")
watchModeCache[txtFilePath] = watchState{
modTime: info.ModTime(),
strings: literals,
}
return literals, nil
}