From: Daniel Jasper Date: Thu, 23 Apr 2015 09:54:10 +0000 (+0000) Subject: clang-format: [Proto] Don't linewrap top-level options. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=adda160629b926c9a3cb8874cfa274b43df86cce;p=clang clang-format: [Proto] Don't linewrap top-level options. They are very similar to import statements. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@235582 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Format/TokenAnnotator.cpp b/lib/Format/TokenAnnotator.cpp index 7733b8a6e9..3275ac1abd 100644 --- a/lib/Format/TokenAnnotator.cpp +++ b/lib/Format/TokenAnnotator.cpp @@ -637,9 +637,8 @@ private: public: LineType parseLine() { - if (CurrentToken->is(tok::hash)) { + if (CurrentToken->is(tok::hash)) return parsePreprocessorDirective(); - } // Directly allow to 'import ' to support protocol buffer // definitions (code.google.com/p/protobuf) or missing "#" (either way we @@ -663,6 +662,15 @@ public: return LT_ImportStatement; } + // In .proto files, top-level options are very similar to import statements + // and should not be line-wrapped. + if (Style.Language == FormatStyle::LK_Proto && Line.Level == 0 && + CurrentToken->is(Keywords.kw_option)) { + next(); + if (CurrentToken && CurrentToken->is(tok::identifier)) + return LT_ImportStatement; + } + bool KeywordVirtualFound = false; bool ImportStatement = false; while (CurrentToken) { diff --git a/unittests/Format/FormatTestProto.cpp b/unittests/Format/FormatTestProto.cpp index d55fe8cf09..ac8fcbdda4 100644 --- a/unittests/Format/FormatTestProto.cpp +++ b/unittests/Format/FormatTestProto.cpp @@ -105,6 +105,14 @@ TEST_F(FormatTestProto, MessageFieldAttributes) { "}];"); } +TEST_F(FormatTestProto, DoesntWrapFileOptions) { + EXPECT_EQ( + "option java_package = " + "\"some.really.long.package.that.exceeds.the.column.limit\";", + format("option java_package = " + "\"some.really.long.package.that.exceeds.the.column.limit\";")); +} + TEST_F(FormatTestProto, FormatsOptions) { verifyFormat("option (MyProto.options) = {\n" " field_a: OK\n"