gitstack

grindlemire/go-tui code browser

4.5 KB Go 189 lines 2026-02-28 · 2075837 raw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package formatter

import (
	"fmt"
	"strings"

	"github.com/grindlemire/go-tui/internal/tuigen"
)

// printElement outputs an element with its attributes and children.
func (p *printer) printElement(elem *tuigen.Element) {
	// Leading comments
	p.printLeadingComments(elem.LeadingComments)

	p.writeIndent()
	p.write("<")
	p.write(elem.Tag)

	// Determine multi-line: either user had attrs on separate lines, or > was on its own line
	multiLine := elem.MultiLineAttrs || elem.ClosingBracketNewLine

	if multiLine {
		// Multi-line: each attr on its own line, indented one tab deeper than element
		p.depth++
		// Emit ref={expr} first if present (extracted from attributes during parsing)
		if elem.RefExpr != nil {
			p.newline()
			p.writeIndent()
			p.write("ref={")
			p.write(elem.RefExpr.Code)
			p.write("}")
		}
		// Emit key={expr} if present (extracted from attributes during parsing)
		if elem.RefKey != nil {
			p.newline()
			p.writeIndent()
			p.write("key={")
			p.write(elem.RefKey.Code)
			p.write("}")
		}
		for _, attr := range elem.Attributes {
			p.newline()
			p.writeIndent()
			p.printAttribute(attr)
		}
		p.depth--
	} else {
		// Single-line: all attrs on same line
		// Emit ref={expr} first if present (extracted from attributes during parsing)
		if elem.RefExpr != nil {
			p.write(" ref={")
			p.write(elem.RefExpr.Code)
			p.write("}")
		}
		// Emit key={expr} if present (extracted from attributes during parsing)
		if elem.RefKey != nil {
			p.write(" key={")
			p.write(elem.RefKey.Code)
			p.write("}")
		}
		for _, attr := range elem.Attributes {
			p.write(" ")
			p.printAttribute(attr)
		}
	}

	if elem.SelfClose {
		if elem.ClosingBracketNewLine {
			p.newline()
			p.writeIndent()
			p.write("/>")
		} else {
			p.write(" />")
		}
		p.printTrailingComment(elem.TrailingComments)
		p.newline()
		return
	}

	if elem.ClosingBracketNewLine {
		p.newline()
		p.writeIndent()
		p.write(">")
	} else {
		p.write(">")
	}

	// Check if children should be rendered inline (preserving user's source layout)
	// Don't inline if there are orphan comments that need to be printed inside the element
	if elem.InlineChildren && len(elem.OrphanComments) == 0 && p.canStructurallyInline(elem.Children) {
		p.printChildrenInline(elem.Children)
		p.write("</")
		p.write(elem.Tag)
		p.write(">")
		p.printTrailingComment(elem.TrailingComments)
		p.newline()
		return
	}

	// Trailing comment after opening tag
	p.printTrailingComment(elem.TrailingComments)

	// Multi-line children
	p.newline()
	p.depth++
	p.printBody(elem.Children)
	p.printOrphanComments(elem.OrphanComments)
	p.depth--
	p.writeIndent()
	p.write("</")
	p.write(elem.Tag)
	p.write(">")
	p.newline()
}

// printAttribute outputs a single attribute.
func (p *printer) printAttribute(attr *tuigen.Attribute) {
	p.write(attr.Name)

	// Check for boolean shorthand (attr with true value and no explicit =true)
	if bl, ok := attr.Value.(*tuigen.BoolLit); ok && bl.Value {
		// Could be shorthand boolean - check if it was originally shorthand
		// For now, always use explicit form for consistency
		p.write("={true}")
		return
	}

	p.write("=")
	p.printAttrValue(attr.Value)
}

// printAttrValue outputs an attribute value.
func (p *printer) printAttrValue(val tuigen.Node) {
	switch v := val.(type) {
	case *tuigen.StringLit:
		p.write(`"`)
		p.write(escapeString(v.Value))
		p.write(`"`)
	case *tuigen.IntLit:
		p.write(fmt.Sprintf("{%d}", v.Value))
	case *tuigen.FloatLit:
		p.write(fmt.Sprintf("{%g}", v.Value))
	case *tuigen.BoolLit:
		p.write(fmt.Sprintf("{%t}", v.Value))
	case *tuigen.GoExpr:
		p.write("{")
		p.write(formatInlineBlockComments(v.Code))
		p.write("}")
	}
}

// canStructurallyInline returns true if the children types support inline rendering.
// This checks structural compatibility (types and content), not user layout preference.
func (p *printer) canStructurallyInline(children []tuigen.Node) bool {
	if len(children) == 0 {
		return true
	}

	for _, child := range children {
		switch c := child.(type) {
		case *tuigen.GoExpr:
			if strings.Contains(c.Code, "\n") {
				return false
			}
		case *tuigen.TextContent:
			if strings.Contains(c.Text, "\n") {
				return false
			}
		default:
			return false
		}
	}

	return true
}

// printChildrenInline prints children on the same line (no newline).
func (p *printer) printChildrenInline(children []tuigen.Node) {
	for _, child := range children {
		switch c := child.(type) {
		case *tuigen.GoExpr:
			p.write("{")
			p.write(formatInlineBlockComments(c.Code))
			p.write("}")
		case *tuigen.TextContent:
			p.write(c.Text)
		}
	}
}