Problem
The formatter tracks indentation by counting { and } on every line,
including braces that appear inside string literals and comments. A line such as
title "a { b" or // note about } therefore changes the indentation depth and
mis-indents the rest of the file. The parser already has a string/comment-aware
braceScanner that handles this correctly, but the formatter does not reuse it.
This is a real correctness fragility in the current line-oriented formatter,
separate from the larger AST-backed formatter rewrite deferred to #250.
Relevant code:
internal/lang/format.go: depth counting via strings.Count(line, "{") - strings.Count(line, "}")
internal/parser/braces.go: braceScanner tracks string/comment/template state
internal/lang/format_test.go: idempotence and golden tests
Scope
Make formatter depth tracking ignore braces inside string literals, template
literals, and comments by reusing or sharing the parser's brace scanner rather
than naive strings.Count. Add tests for the previously-broken cases.
Acceptance Criteria
- Braces inside string literals, template literals, and comments do not change
indentation depth.
- The formatter remains idempotent on supported shapes.
- Standalone comment lines are still preserved.
- Tests cover brace-in-string, brace-in-comment, and brace-in-template-literal.
Verification
Problem
The formatter tracks indentation by counting
{and}on every line,including braces that appear inside string literals and comments. A line such as
title "a { b"or// note about }therefore changes the indentation depth andmis-indents the rest of the file. The parser already has a string/comment-aware
braceScannerthat handles this correctly, but the formatter does not reuse it.This is a real correctness fragility in the current line-oriented formatter,
separate from the larger AST-backed formatter rewrite deferred to #250.
Relevant code:
internal/lang/format.go: depth counting viastrings.Count(line, "{") - strings.Count(line, "}")internal/parser/braces.go:braceScannertracks string/comment/template stateinternal/lang/format_test.go: idempotence and golden testsScope
Make formatter depth tracking ignore braces inside string literals, template
literals, and comments by reusing or sharing the parser's brace scanner rather
than naive
strings.Count. Add tests for the previously-broken cases.Acceptance Criteria
indentation depth.
Verification
go test ./internal/lang