MCPcopy Create free account
hub / github.com/ajitpratap0/GoSQLX / FormatAST

Function FormatAST

pkg/formatter/render.go:81–120  ·  view source on GitHub ↗

─── public rendering API ──────────────────────────────────────────────────── FormatAST renders a full *ast.AST with the given options.

(a *ast.AST, opts ast.FormatOptions)

Source from the content-addressed store, hash-verified

79
80// FormatAST renders a full *ast.AST with the given options.
81func FormatAST(a *ast.AST, opts ast.FormatOptions) string {
82 if a == nil {
83 return ""
84 }
85 parts := make([]string, 0, len(a.Statements))
86 for _, stmt := range a.Statements {
87 parts = append(parts, FormatStatement(stmt, opts))
88 }
89 sep := ";\n"
90 if opts.AddSemicolon {
91 // Each statement already ends with ";"
92 sep = "\n"
93 }
94 result := strings.Join(parts, sep)
95
96 // Emit preserved comments
97 if len(a.Comments) > 0 {
98 var leading, trailing []string
99 for _, c := range a.Comments {
100 if c.Inline {
101 trailing = append(trailing, c.Text)
102 } else {
103 leading = append(leading, c.Text)
104 }
105 }
106 var sb strings.Builder
107 for _, lc := range leading {
108 sb.WriteString(lc)
109 sb.WriteString("\n")
110 }
111 sb.WriteString(result)
112 for _, tc := range trailing {
113 sb.WriteString(" ")
114 sb.WriteString(tc)
115 }
116 result = sb.String()
117 }
118
119 return result
120}
121
122// FormatStatement renders a single AST statement with the given options.
123// Returns "" for nil.

Callers 5

FuzzFormatFunction · 0.92
FormatFunction · 0.92
TestFormatAST_NilFunction · 0.92
FormatMethod · 0.85

Calls 2

FormatStatementFunction · 0.85
StringMethod · 0.45

Tested by 3

FuzzFormatFunction · 0.74
TestFormatAST_NilFunction · 0.74