]> granicus.if.org Git - clang/commitdiff
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.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@235582 91177308-0d34-0410-b5e6-96231b3b80d8

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

index 7733b8a6e951a7c1f4b754b954db01e96b403a52..3275ac1abd59f87819fa8658d42ba3653e18ae51 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 d55fe8cf0934262f77b35e7c288398709906cf0b..ac8fcbdda4fcf3a36d5f8ca44b4ab981fa75f9ba 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"