Small tweak to printer to handle indent if there is //~ on the first line

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

PiperOrigin-RevId: 704689179
pull/19549/head
Protobuf Team Bot 3 months ago committed by Copybara-Service
parent e9b9b5f08a
commit 7519359b4b
  1. 2
      src/google/protobuf/descriptor.pb.cc
  2. 3
      src/google/protobuf/io/printer.cc
  3. 18
      src/google/protobuf/io/printer_unittest.cc

@ -12791,7 +12791,7 @@ void UninterpretedOption_NamePart::InternalSwap(UninterpretedOption_NamePart* PR
_internal_metadata_.InternalSwap(&other->_internal_metadata_);
swap(_impl_._has_bits_[0], other->_impl_._has_bits_[0]);
::_pbi::ArenaStringPtr::InternalSwap(&_impl_.name_part_, &other->_impl_.name_part_, arena);
swap(_impl_.is_extension_, other->_impl_.is_extension_);
swap(_impl_.is_extension_, other->_impl_.is_extension_);
}
::google::protobuf::Metadata UninterpretedOption_NamePart::GetMetadata() const {

@ -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;
}
}

@ -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());

Loading…
Cancel
Save