From ed3f395773957d204b7653624a10312e0d6a22dd Mon Sep 17 00:00:00 2001 From: Daniel Jasper Date: Thu, 18 Jun 2015 12:32:59 +0000 Subject: [PATCH] clang-format: [JS] Add a special case for indenting function literals. 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. llvm-svn: 240014 --- clang/lib/Format/ContinuationIndenter.cpp | 16 +++++++++++++++- clang/unittests/Format/FormatTestJS.cpp | 4 ++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp index 7e751d4..c41da01 100644 --- a/clang/lib/Format/ContinuationIndenter.cpp +++ b/clang/lib/Format/ContinuationIndenter.cpp @@ -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; diff --git a/clang/unittests/Format/FormatTestJS.cpp b/clang/unittests/Format/FormatTestJS.cpp index 6479805..15d62eb 100644 --- a/clang/unittests/Format/FormatTestJS.cpp +++ b/clang/unittests/Format/FormatTestJS.cpp @@ -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" -- 2.7.4