Files
DavidLoftusandDavid Blaikie 301172f589 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>
2026-09-08 16:20:47 +00:00

57 lines
1.2 KiB
Plaintext

// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
// AUTOUPDATE
// TIP: To test this file alone, run:
// TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/format/testdata/basics/braces.carbon
// TIP: To dump output, run:
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/format/testdata/basics/braces.carbon
// --- test.carbon
fn F() {
}
fn G() -> i32 {
return 3;
}
fn H(x: i32, y: i32) -> i32 {
var z: i32 = x + y;
return z;
}
class C {
class D {
class E {}
}
}
// --- AUTOUPDATE-SPLIT
// CHECK:STDOUT: fn F() {}
// CHECK:STDOUT:
// CHECK:STDOUT: fn G() -> i32 {
// CHECK:STDOUT: return 3;
// CHECK:STDOUT:
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: fn H(x: i32, y: i32) -> i32 {
// CHECK:STDOUT: var z: i32 = x + y;
// CHECK:STDOUT: return z;
// CHECK:STDOUT:
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: class C {
// CHECK:STDOUT: class D {
// CHECK:STDOUT: class E {}
// CHECK:STDOUT: }
// CHECK:STDOUT: }