Update Carbon::Format to produce semi-reasonable output. (#7687)

Makes following changes to Carbon::Format()

- TokenKind::Period (i.e. `.`) should never have a space before or after
it.
- TokenKind::CloseSquareParen (i.e. `]`) should be treated as packed
content (no space preceeding it)
  - Only exception I can think of is `impl forall [...]`
- Remove preceeding space from `[` and `(` if previous token was an
identifier (or identifier-ish token)
- Remove seperator following `++` / `--` unary operators.
- Explicit gaps in source code should be retained, up to 2 new lines.

Multiple test files were added to test formatting. 

I imagine eventually this will need to be updated to read parse tree to
gather more context but this atleast lets us get a decent-ish format for
many of our current sample files (e.g. sieve.carbon)

Assisted-With: Gemini / Antigravity

---------

Co-authored-by: David Blaikie <dblaikie@gmail.com>
This commit is contained in:
DavidLoftus
2026-09-08 16:20:47 +00:00
committed by GitHub
co-authored by David Blaikie
parent 812cc1e032
commit 301172f589
9 changed files with 238 additions and 50 deletions
+4
View File
@@ -32,6 +32,10 @@ auto TokenizedBuffer::GetLineNumber(TokenIndex token) const -> int {
return GetLine(token).index + 1;
}
auto TokenizedBuffer::GetLine(CommentIndex comment) const -> LineIndex {
return FindLineIndex(comments_.Get(comment).start);
}
auto TokenizedBuffer::GetColumnNumber(TokenIndex token) const -> int {
const auto& token_info = token_infos_.Get(token);
const auto& line_info =