From ad06391ca98085a5245ac2fec6cccae23d3a1089 Mon Sep 17 00:00:00 2001 From: Martin Probst Date: Wed, 26 Apr 2017 12:36:49 +0000 Subject: [PATCH] clang-format: [JS/Java] ignore Objective-C constructs in JS & Java. 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 llvm-svn: 301399 --- clang/lib/Format/TokenAnnotator.cpp | 6 +++++- clang/unittests/Format/FormatTestJS.cpp | 3 +++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index 679b73f..c274d7b 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -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: diff --git a/clang/unittests/Format/FormatTestJS.cpp b/clang/unittests/Format/FormatTestJS.cpp index f7e605f..7886c4f 100644 --- a/clang/unittests/Format/FormatTestJS.cpp +++ b/clang/unittests/Format/FormatTestJS.cpp @@ -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) { -- 2.7.4