]> granicus.if.org Git - clang/commitdiff
clang-format: [JS/Java] ignore Objective-C constructs in JS & Java.
authorMartin Probst <martin@probst.io>
Wed, 26 Apr 2017 12:36:49 +0000 (12:36 +0000)
committerMartin Probst <martin@probst.io>
Wed, 26 Apr 2017 12:36:49 +0000 (12:36 +0000)
Summary:
Java and JavaScript support annotations and decorators, respectively, that use a leading "@" token. clang-format currently detects this as an Objective-C construct and applies special formatting, for example no whitespace around "=" operators. This change disables the distinction for Java and JavaScript, which leads to normal formatting of single line annotated and initialized properties.

Before:
    class X {
      @foo() bar=false;
    }

After:
    class X {
      @foo() bar = false;
    }

Reviewers: djasper, bkramer

Subscribers: klimek, cfe-commits

Differential Revision: https://reviews.llvm.org/D32532

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

lib/Format/TokenAnnotator.cpp
unittests/Format/FormatTestJS.cpp

index 679b73ffa90536afe73c77a796c92fce986f5ef4..c274d7bf07f82a21ea65ec5b6602fe5ce5aa8625 100644 (file)
@@ -1120,7 +1120,11 @@ private:
                 Current.Type = TT_FunctionAnnotationRParen;
           }
         }
-    } else if (Current.is(tok::at) && Current.Next) {
+    } else if (Current.is(tok::at) && Current.Next &&
+               Style.Language != FormatStyle::LK_JavaScript &&
+               Style.Language != FormatStyle::LK_Java) {
+      // In Java & JavaScript, "@..." is a decorator or annotation. In ObjC, it
+      // marks declarations and properties that need special formatting.
       switch (Current.Next->Tok.getObjCKeywordID()) {
       case tok::objc_interface:
       case tok::objc_implementation:
index f7e605f31a4b8a556d2505c7d9c218d9721054b0..7886c4fe27ad8ff3077c51ab8ccc2d9d638abf94 100644 (file)
@@ -1239,6 +1239,9 @@ TEST_F(FormatTestJS, MetadataAnnotations) {
                "}");
   verifyFormat("class X {}\n"
                "class Y {}");
+  verifyFormat("class X {\n"
+               "  @property() private isReply = false;\n"
+               "}\n");
 }
 
 TEST_F(FormatTestJS, TypeAliases) {