Skip to content

Commit

Permalink
Small tweak to printer to handle indent if there is //~ on the first …
Browse files Browse the repository at this point in the history
…line

Lines with "//~ ..." should be considered a "first line" like non-"//~" lines, to avoid overly indenting when in raw strings.

PiperOrigin-RevId: 704689179
  • Loading branch information
protobuf-github-bot authored and copybara-github committed Dec 10, 2024
1 parent e9b9b5f commit 7519359
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/google/protobuf/descriptor.pb.cc

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/google/protobuf/io/printer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,9 @@ Printer::Format Printer::TokenizeFormat(absl::string_view format_string,
if (comment_index != absl::string_view::npos) {
line_text = line_text.substr(0, comment_index);
if (absl::StripLeadingAsciiWhitespace(line_text).empty()) {
// If the first line is part of an ignored comment, consider that a
// first line as well.
is_first = false;
continue;
}
}
Expand Down
18 changes: 18 additions & 0 deletions src/google/protobuf/io/printer_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,24 @@ TEST_F(PrinterTest, EmitWithIndent) {
" };\n");
}

TEST_F(PrinterTest, EmitWithIndentAndIgnoredCommentOnFirstLine) {
{
Printer printer(output());
auto v = printer.WithIndent();
printer.Emit({{"f1", "x"}, {"f2", "y"}, {"f3", "z"}}, R"cc(
//~ First line comment.
class Foo {
int $f1$, $f2$, $f3$;
};
)cc");
}

EXPECT_EQ(written(),
" class Foo {\n"
" int x, y, z;\n"
" };\n");
}

TEST_F(PrinterTest, EmitWithPreprocessor) {
{
Printer printer(output());
Expand Down

0 comments on commit 7519359

Please sign in to comment.