clang-format: [Proto] Don't linewrap top-level options.
authorDaniel Jasper <djasper@google.com>
Thu, 23 Apr 2015 09:54:10 +0000 (09:54 +0000)
committerDaniel Jasper <djasper@google.com>
Thu, 23 Apr 2015 09:54:10 +0000 (09:54 +0000)
They are very similar to import statements.

llvm-svn: 235582

clang/lib/Format/TokenAnnotator.cpp
clang/unittests/Format/FormatTestProto.cpp

index 7733b8a..3275ac1 100644 (file)
@@ -637,9 +637,8 @@ private:
 
 public:
   LineType parseLine() {
-    if (CurrentToken->is(tok::hash)) {
+    if (CurrentToken->is(tok::hash))
       return parsePreprocessorDirective();
-    }
 
     // Directly allow to 'import <string-literal>' 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) {
index d55fe8c..ac8fcbd 100644 (file)
@@ -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"