Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use buffered IO to enable streaming responses, and implement flush component #802

Merged
merged 15 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.2.726
0.2.728
14 changes: 9 additions & 5 deletions benchmarks/templ/template_templ.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion cmd/templ/fmtcmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"sync"
"time"

imports "github.com/a-h/templ/cmd/templ/import"
"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"
Expand Down
14 changes: 9 additions & 5 deletions cmd/templ/generatecmd/testwatch/testdata/templates_templ.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 33 additions & 28 deletions cmd/templ/import/process.go → cmd/templ/imports/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ import (
"go/ast"
"go/format"
"go/token"
"log"
"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"
Expand Down Expand Up @@ -88,60 +89,64 @@ func Process(t parser.TemplateFile) (parser.TemplateFile, error) {
return nil
})

var gofile *ast.File
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) {
gofile, err = goparser.ParseFile(fset, fileName, t.Package.Expression.Value+"\n"+importsNode.Expression.Value, goparser.AllErrors)
firstGoNodeInTemplate, err = goparser.ParseFile(fset, fileName, t.Package.Expression.Value+"\n"+importsNode.Expression.Value, goparser.AllErrors|goparser.ParseComments)
if err != nil {
log.Printf("failed to parse go code: %v", importsNode.Expression.Value)
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
}
slices.SortFunc(updatedImports, func(a, b *ast.ImportSpec) int {
return strings.Compare(a.Path.Value, b.Path.Value)
})
newImportDecl := &ast.GenDecl{
Tok: token.IMPORT,
Specs: convertSlice(updatedImports),
}

// Delete all the existing imports.
var indicesToDelete []int
for i, decl := range gofile.Decls {
if decl, ok := decl.(*ast.GenDecl); ok && decl.Tok == token.IMPORT {
indicesToDelete = append(indicesToDelete, i)
for _, imp := range firstGoNodeInTemplate.Imports {
name, path, err := getImportDetails(imp)
if err != nil {
return t, err
}
astutil.DeleteNamedImport(fset, firstGoNodeInTemplate, name, path)
}
for i := len(indicesToDelete) - 1; i >= 0; i-- {
gofile.Decls = append(gofile.Decls[:indicesToDelete[i]], gofile.Decls[indicesToDelete[i]+1:]...)
}
if len(updatedImports) > 0 {
gofile.Imports = updatedImports
gofile.Decls = append([]ast.Decl{newImportDecl}, gofile.Decls...)
// Add imports, if there are any to add.
for _, imp := range updatedImports {
name, path, err := getImportDetails(imp)
if err != nil {
return t, err
}
astutil.AddNamedImport(fset, firstGoNodeInTemplate, name, path)
}
// Write out the Go code with the imports.
updatedGoCode := new(strings.Builder)
err := format.Node(updatedGoCode, fset, gofile)
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 == "" {
if len(updatedImports) == 0 {
t.Nodes = t.Nodes[1:]
return t, nil
}
t.Nodes[0] = importsNode
return t, nil
}

func convertSlice(slice []*ast.ImportSpec) []ast.Spec {
result := make([]ast.Spec, len(slice))
for i, v := range slice {
result[i] = ast.Spec(v)
func getImportDetails(imp *ast.ImportSpec) (name, path string, err error) {
if imp.Name != nil {
name = imp.Name.Name
}
if imp.Path != nil {
path, 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 result
return name, path, nil
}
File renamed without changes.
12 changes: 12 additions & 0 deletions cmd/templ/imports/testdata/comments.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
-- fmt_templ.templ --
package test

// Comment on variable or function.
var x = fmt.Sprintf("Hello")
-- fmt_templ.templ --
package test

import "fmt"

// Comment on variable or function.
var x = fmt.Sprintf("Hello")
19 changes: 19 additions & 0 deletions cmd/templ/imports/testdata/namedimportsadd.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
-- fmt_templ.templ --
package test

import (
sconv "strconv"
)

// Comment on variable or function.
var x = fmt.Sprintf(sconv.Quote("Hello"))
-- fmt_templ.templ --
package test

import (
"fmt"
sconv "strconv"
)

// Comment on variable or function.
var x = fmt.Sprintf(sconv.Quote("Hello"))
16 changes: 16 additions & 0 deletions cmd/templ/imports/testdata/namedimportsremoved.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
-- fmt_templ.templ --
package test

import (
sconv "strconv"
)

// Comment on variable or function.
var x = fmt.Sprintf("Hello")
-- fmt_templ.templ --
package test

import "fmt"

// Comment on variable or function.
var x = fmt.Sprintf("Hello")
14 changes: 9 additions & 5 deletions cmd/templ/lspcmd/httpdebug/list_templ.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion cmd/templ/lspcmd/proxy/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/a-h/parse"
lsp "github.com/a-h/protocol"
"github.com/a-h/templ"
imports "github.com/a-h/templ/cmd/templ/import"
"github.com/a-h/templ/cmd/templ/imports"
"github.com/a-h/templ/generator"
"github.com/a-h/templ/parser/v2"
"go.lsp.dev/uri"
Expand Down
14 changes: 9 additions & 5 deletions cmd/templ/testproject/testdata/templates_templ.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 21 additions & 16 deletions cmd/templ/visualize/sourcemapvisualisation_templ.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading