Skip to content

Commit

Permalink
printer: support void elements
Browse files Browse the repository at this point in the history
  • Loading branch information
mateusz834 committed Oct 31, 2024
1 parent 5fb4b3d commit 3fe510e
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 23 deletions.
25 changes: 18 additions & 7 deletions printer/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -1200,10 +1200,12 @@ func (p *printer) stmtList(list []ast.Stmt, nindent int, nextIsRBrace bool) {

var (
forceNextNewline = false
tagOneLine = make([]bool, 1, 16)
tagOneLine = make([]bool, 1, 32)
)

for j, s := range list {
openTagIndent, endTagUnindent, oneline := p.tagIndent(list)

for _, s := range list {
// ignore empty statements (was issue 3466)
if _, isEmpty := s.(*ast.EmptyStmt); !isEmpty {
// nindent == 0 only for lists of switch/select case clauses;
Expand All @@ -1214,21 +1216,30 @@ func (p *printer) stmtList(list []ast.Stmt, nindent int, nextIsRBrace bool) {
p.linebreak(p.lineFor(s.Pos()), 1, ignore, i == 0 || nindent == 0 || p.linesFrom(line) > 0)
}

if _, ok := s.(*ast.EndTagStmt); ok {
if v, ok := s.(*ast.EndTagStmt); ok {
if _, ok := endTagUnindent[v]; ok {
p.print(unindent)
}
forceNextNewline = !tagOneLine[len(tagOneLine)-1]
tagOneLine = tagOneLine[:len(tagOneLine)-1]
p.print(unindent)
if forceNextNewline && p.commentBefore(p.posFor(s.Pos())) {
p.linebreak(p.lineFor(s.Pos()), 0, ignore, false)
}
} else if _, ok := s.(*ast.OpenTagStmt); ok {
} else if v, ok := s.(*ast.OpenTagStmt); ok {
forceNextNewline = false
// TODO(mateusz834): void elements
tagOneLine = append(tagOneLine, p.oneLineTag(list[j:]))
_, ok := oneline[v]
tagOneLine = append(tagOneLine, ok)
}

p.recordLine(&line)
p.stmt(s, nextIsRBrace && i == len(list)-1)

if v, ok := s.(*ast.OpenTagStmt); ok {
if _, ok := openTagIndent[v]; ok {
p.print(indent)
}
}

// labeled statements put labels on a separate line, but here
// we only care about the start line of the actual statement
// without label - correct line for each label
Expand Down
63 changes: 63 additions & 0 deletions printer/testdata/tgo/void_elements.formatted
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package templates

func test() {
<div>
<br>
_ = 3
</div>
}

func test() {
<div>
<br>
<br>
_ = 3
</div>
}

func test() {
<div>
<span>
_ = 3
</div>
}

func test() {
<div>
<span>
_ = 3
</span>
</div>
}

func test() {
<div>
<div>
_ = 3
</div>
}

func test() {
<div>
_ = 3
</div>
</div>
}

func test() {
<div>
<div>
<div>
_ = 3
</div>
</div>
}

func test() {
<div>
<div>
_ = 3
</div>
</div>
</div>
}
63 changes: 63 additions & 0 deletions printer/testdata/tgo/void_elements.tgo
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package templates

func test() {
<div>
<br>
_ = 3
</div>
}

func test() {
<div>
<br>
<br>
_ = 3
</div>
}

func test() {
<div>
<span>
_ = 3
</div>
}

func test() {
<div>
<span>
_ = 3
</span>
</div>
}

func test() {
<div>
<div>
_ = 3
</div>
}

func test() {
<div>
_ = 3
</div>
</div>
}

func test() {
<div>
<div>
<div>
_ = 3
</div>
</div>
}

func test() {
<div>
<div>
_ = 3
</div>
</div>
</div>
}
39 changes: 23 additions & 16 deletions printer/tgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,6 @@ func (p *printer) opentag(b *ast.OpenTagStmt) {
p.tagEndLine = p.lineFor(b.ClosePos)
p.print(token.GTR)
p.inStartTag = false

// TODO(nmateusz834): void elements
p.print(indent)
}

func (p *printer) endtag(b *ast.EndTagStmt) {
Expand Down Expand Up @@ -142,24 +139,34 @@ func (p *printer) templateLiteralExpr(x *ast.TemplateLiteralExpr) {
}
}

func (p *printer) oneLineTag(list []ast.Stmt) bool {
deep := 0
startPos := token.NoPos
func (p *printer) tagIndent(list []ast.Stmt) (indent map[*ast.OpenTagStmt]struct{}, unindent map[*ast.EndTagStmt]struct{}, oneline map[*ast.OpenTagStmt]struct{}) {
indent = make(map[*ast.OpenTagStmt]struct{})
unindent = make(map[*ast.EndTagStmt]struct{})
oneline = make(map[*ast.OpenTagStmt]struct{})

deep := make([]int, 0, 32)
for i, v := range list {
if _, ok := v.(*ast.OpenTagStmt); ok {
// TODO(mateusz834): void elements
deep++
startPos = v.Pos()
}
if _, ok := v.(*ast.EndTagStmt); ok {
if deep--; deep == 0 {
return p.lineFor(startPos) == p.lineFor(v.End()) &&
!p.willHaveNewLine(list[0].(*ast.OpenTagStmt), list[1:i])
switch v := v.(type) {
case *ast.OpenTagStmt:
deep = append(deep, i)
case *ast.EndTagStmt:
for len(deep) != 0 {
openTagIndex := deep[len(deep)-1]
openTag := list[openTagIndex].(*ast.OpenTagStmt)
deep = deep[:len(deep)-1]
if openTag.Name.Name == v.Name.Name {
indent[openTag] = struct{}{}
unindent[v] = struct{}{}
if p.lineFor(openTag.Pos()) == p.lineFor(v.End()) && !p.willHaveNewLine(openTag, list[openTagIndex+1:i]) {
oneline[openTag] = struct{}{}
}
break
}
}
}
}

panic("unreachable")
return
}

func (p *printer) willHaveNewLine(o *ast.OpenTagStmt, list []ast.Stmt) bool {
Expand Down

0 comments on commit 3fe510e

Please sign in to comment.