]> granicus.if.org Git - clang/commitdiff
clang-format: [JS] Add a special case for indenting function literals.
authorDaniel Jasper <djasper@google.com>
Thu, 18 Jun 2015 12:32:59 +0000 (12:32 +0000)
committerDaniel Jasper <djasper@google.com>
Thu, 18 Jun 2015 12:32:59 +0000 (12:32 +0000)
Before:
  var func =
      function() {
        doSomething();
      };

After:
  var func =
      function() {
    doSomething();
  };

This is a very narrow special case which fixes most of the discrepency
with what our users do. In the long run, we should try to come up with
a more generic fix for indenting these.

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

lib/Format/ContinuationIndenter.cpp
unittests/Format/FormatTestJS.cpp

index 7e751d46fdd09b211b583f3aeeaf580c354a8a3f..c41da01256af4b7661f27bab736ac08ba26ab77f 100644 (file)
@@ -418,7 +418,21 @@ unsigned ContinuationIndenter::addTokenOnNewLine(LineState &State,
     Penalty += Style.PenaltyBreakFirstLessLess;
 
   State.Column = getNewLineColumn(State);
-  State.Stack.back().NestedBlockIndent = State.Column;
+
+  // Indent nested blocks relative to this column, unless in a very specific
+  // JavaScript special case where:
+  //
+  //   var loooooong_name =
+  //       function() {
+  //     // code
+  //   }
+  //
+  // is common and should be formatted like a free-standing function.
+  if (Style.Language != FormatStyle::LK_JavaScript ||
+      Current.NestingLevel != 0 || !PreviousNonComment->is(tok::equal) ||
+      !Current.is(Keywords.kw_function))
+    State.Stack.back().NestedBlockIndent = State.Column;
+
   if (NextNonComment->isMemberAccess()) {
     if (State.Stack.back().CallContinuation == 0)
       State.Stack.back().CallContinuation = State.Column;
index 6479805b16fc367617ebdedb58dc375780b0923d..15d62eb66c339a9a7d5082f4ebd967d468514698 100644 (file)
@@ -294,6 +294,10 @@ TEST_F(FormatTestJS, FunctionLiterals) {
   verifyFormat("var func = function() {\n"
                "  return 1;\n"
                "};");
+  verifyFormat("var func =  //\n"
+               "    function() {\n"
+               "  return 1;\n"
+               "};");
   verifyFormat("return {\n"
                "  body: {\n"
                "    setAttribute: function(key, val) { this[key] = val; },\n"