Skip to content

[Compiler] Make formatter brace tracking ignore braces in strings and comments #296

@cssbruno

Description

@cssbruno

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

go test ./internal/lang

Metadata

Metadata

Assignees

No one assigned

    Labels

    parser.gwdk parser and syntax handling

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions