Files
carbon-lang/toolchain/format/testdata/basics/comments.carbon
T
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

87 lines
2.9 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/comments.carbon
// TIP: To dump output, run:
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/format/testdata/basics/comments.carbon
// --- test.carbon
// A comment
fn F() {}
// Another comment
// Block
// comment
class C {
// Internal comment
}
// Another
// Block
//
// Comment
// A trailing comment may follow a variable, a function call, or a closing
// brace, and the formatter keeps it on the line it annotates.
var count: i32 = 0; // a) A local variable,
fn Render(frame: Frame) {
Draw(frame); // b) A function call,
Flush();
} // c) And a closing brace.
// A trailing comment on a block string literal's introducer line is carried
// within the literal token and is preserved, with or without a file type
// indicator. A trailing comment after the literal's closing line stays
// attached to that line.
var query: String = '''sql // TODO: switch to a prepared statement
SELECT * FROM t
''';
var poem: String = ''' // No file type indicator, just a comment.
Roses are red.
'''; // A trailing comment after the closing line.
// --- AUTOUPDATE-SPLIT
// CHECK:STDOUT: // A comment
// CHECK:STDOUT: fn F() {}
// CHECK:STDOUT:
// CHECK:STDOUT: // Another comment
// CHECK:STDOUT: // Block
// CHECK:STDOUT: // comment
// CHECK:STDOUT: class C {
// CHECK:STDOUT: // Internal comment
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: // Another
// CHECK:STDOUT: // Block
// CHECK:STDOUT: //
// CHECK:STDOUT: // Comment
// CHECK:STDOUT: // A trailing comment may follow a variable, a function call, or a closing
// CHECK:STDOUT: // brace, and the formatter keeps it on the line it annotates.
// CHECK:STDOUT: var count: i32 = 0; // a) A local variable,
// CHECK:STDOUT: fn Render(frame: Frame) {
// CHECK:STDOUT: Draw(frame); // b) A function call,
// CHECK:STDOUT: Flush();
// CHECK:STDOUT: } // c) And a closing brace.
// CHECK:STDOUT: // A trailing comment on a block string literal's introducer line is carried
// CHECK:STDOUT: // within the literal token and is preserved, with or without a file type
// CHECK:STDOUT: // indicator. A trailing comment after the literal's closing line stays
// CHECK:STDOUT: // attached to that line.
// CHECK:STDOUT: var query: String = '''sql // TODO: switch to a prepared statement
// CHECK:STDOUT: SELECT * FROM t
// CHECK:STDOUT: ''';
// CHECK:STDOUT:
// CHECK:STDOUT: var poem: String = ''' // No file type indicator, just a comment.
// CHECK:STDOUT: Roses are red.
// CHECK:STDOUT: '''; // A trailing comment after the closing line.