gitstack

grindlemire/go-tui code browser

5.5 KB Go 225 lines 2026-06-12 · f0bb13a 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package formatter

import (
	"strings"

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

// escapeString escapes special characters in a string for output.
func escapeString(s string) string {
	var buf strings.Builder
	for _, r := range s {
		switch r {
		case '\n':
			buf.WriteString(`\n`)
		case '\t':
			buf.WriteString(`\t`)
		case '\r':
			buf.WriteString(`\r`)
		case '"':
			buf.WriteString(`\"`)
		case '\\':
			buf.WriteString(`\\`)
		default:
			buf.WriteRune(r)
		}
	}
	return buf.String()
}

// Comment printing helpers

// formatBlockComment formats a block comment with proper spacing.
// Single-line: /* text */ -> /* text */ (ensures spaces around text)
// Multi-line: formats with /* and */ on their own lines
func formatBlockComment(text string) string {
	// Must start with /* and end with */
	if !strings.HasPrefix(text, "/*") || !strings.HasSuffix(text, "*/") {
		return text
	}

	// Extract the content between /* and */
	content := text[2 : len(text)-2]

	// Collect all non-empty content lines
	var contentLines []string
	for line := range strings.SplitSeq(content, "\n") {
		trimmed := strings.TrimSpace(line)
		if trimmed != "" {
			contentLines = append(contentLines, trimmed)
		}
	}

	// Empty comment
	if len(contentLines) == 0 {
		return "/* */"
	}

	// Single line of content: use inline format
	if len(contentLines) == 1 {
		return "/* " + contentLines[0] + " */"
	}

	// Multi-line content: format with /* and */ on their own lines
	var result strings.Builder
	result.WriteString("/*\n")
	for _, line := range contentLines {
		result.WriteString(line)
		result.WriteString("\n")
	}
	result.WriteString("*/")
	return result.String()
}

// formatLineComment formats a line comment with proper spacing.
// Ensures a space after // if not already present.
func formatLineComment(text string) string {
	if !strings.HasPrefix(text, "//") {
		return text
	}

	// Get content after //
	content := text[2:]

	// If empty or already starts with space, return as-is
	if content == "" || content[0] == ' ' || content[0] == '\t' {
		return text
	}

	// Add space after //
	return "// " + content
}

// formatComment formats a comment, handling both line and block comments.
func formatComment(c *tuigen.Comment) string {
	if c.IsBlock {
		return formatBlockComment(c.Text)
	}
	return formatLineComment(c.Text)
}

// formatInlineBlockComments formats any block comments within Go code.
// This handles cases like: fmt.Sprintf("> %s", /* ItemList item */ item)
func formatInlineBlockComments(code string) string {
	var result strings.Builder
	i := 0

	for i < len(code) {
		// Check for block comment start
		if i+1 < len(code) && code[i] == '/' && code[i+1] == '*' {
			// Find the end of the block comment
			start := i
			i += 2
			for i+1 < len(code) && !(code[i] == '*' && code[i+1] == '/') {
				i++
			}
			if i+1 < len(code) {
				i += 2 // skip */
			}

			// Extract and format the block comment
			commentText := code[start:i]
			result.WriteString(formatBlockComment(commentText))
			continue
		}

		// Check for string literal (skip to avoid formatting comments inside strings)
		if code[i] == '"' {
			result.WriteByte(code[i])
			i++
			for i < len(code) && code[i] != '"' {
				if code[i] == '\\' && i+1 < len(code) {
					result.WriteByte(code[i])
					i++
				}
				if i < len(code) {
					result.WriteByte(code[i])
					i++
				}
			}
			if i < len(code) {
				result.WriteByte(code[i])
				i++
			}
			continue
		}

		// Check for raw string literal
		if code[i] == '`' {
			result.WriteByte(code[i])
			i++
			for i < len(code) && code[i] != '`' {
				result.WriteByte(code[i])
				i++
			}
			if i < len(code) {
				result.WriteByte(code[i])
				i++
			}
			continue
		}

		// Regular character
		result.WriteByte(code[i])
		i++
	}

	return result.String()
}

// printCommentGroup outputs a comment group with proper indentation.
// Each comment in the group is printed on its own line.
// Respects BlankLineBefore to preserve blank line separation between comment groups.
func (p *printer) printCommentGroup(cg *tuigen.CommentGroup) {
	if cg == nil || len(cg.List) == 0 {
		return
	}
	for _, c := range cg.List {
		if c.BlankLineBefore {
			p.newline()
		}
		p.writeIndent()
		p.write(formatComment(c))
		p.newline()
	}
}

// printLeadingComments outputs leading comments (before a node).
// Comments are printed with proper indentation, each on its own line.
// Respects BlankLineBefore to preserve blank line separation between comment groups.
func (p *printer) printLeadingComments(cg *tuigen.CommentGroup) {
	if cg == nil || len(cg.List) == 0 {
		return
	}
	for _, c := range cg.List {
		if c.BlankLineBefore {
			p.newline()
		}
		p.writeIndent()
		p.write(formatComment(c))
		p.newline()
	}
}

// printTrailingComment outputs a trailing comment (on same line as node).
// Prints with leading spaces, no newline (caller handles newline).
func (p *printer) printTrailingComment(cg *tuigen.CommentGroup) {
	if cg == nil || len(cg.List) == 0 {
		return
	}
	// Only print the first comment as trailing (others would be on next lines)
	p.write("  ")
	p.write(formatComment(cg.List[0]))
}

// printOrphanComments outputs orphan comments (not attached to any node).
// Each comment group is printed with proper indentation. Blank lines between
// groups come from the first comment's BlankLineBefore flag, which the parser
// always sets because groups are split on blank lines; emitting a separator
// here as well would double the blank line and break format idempotency.
func (p *printer) printOrphanComments(groups []*tuigen.CommentGroup) {
	for _, cg := range groups {
		p.printCommentGroup(cg)
	}
}