Skip to content

Commit

Permalink
feat: use buffered IO to enable streaming responses, and implement fl…
Browse files Browse the repository at this point in the history
…ush component (#802)
  • Loading branch information
a-h authored Jun 25, 2024
1 parent 6c50830 commit df80639
Show file tree
Hide file tree
Showing 99 changed files with 2,635 additions and 882 deletions.
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.

59 changes: 32 additions & 27 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,47 +89,45 @@ 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 == "" {
t.Nodes = t.Nodes[1:]
Expand All @@ -138,10 +137,16 @@ func Process(t parser.TemplateFile) (parser.TemplateFile, error) {
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")
15 changes: 15 additions & 0 deletions cmd/templ/imports/testdata/extraspace.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
-- fmt_templ.templ --
package test

const x = 123


var x = fmt.Sprintf("Hello")
-- fmt_templ.templ --
package test

import "fmt"

const x = 123

var x = fmt.Sprintf("Hello")
File renamed without changes.
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")
File renamed without changes.
20 changes: 20 additions & 0 deletions cmd/templ/imports/testdata/noimportscode.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
-- fmt.templ --
package test

func test() {
// Do nothing.
}

templ Hello() {
<div>Hello</div>
}
-- fmt.templ --
package test

func test() {
// Do nothing.
}

templ Hello() {
<div>Hello</div>
}
File renamed without changes.
File renamed without changes.
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.

Loading

0 comments on commit df80639

Please sign in to comment.