diff options
author | Daniel Jasper <djasper@google.com> | 2014-09-29 07:54:54 +0000 |
---|---|---|
committer | Daniel Jasper <djasper@google.com> | 2014-09-29 07:54:54 +0000 |
commit | 1779d438bc389f0cb67ecd3d9fed7d13bca7db39 (patch) | |
tree | 0c9708a4ab56fe5bdc63529a54d7d5612811c6bd | |
parent | e598d7b01dcfec4387156b7b3c0aa3257b049f83 (diff) | |
download | bcm5719-llvm-1779d438bc389f0cb67ecd3d9fed7d13bca7db39.tar.gz bcm5719-llvm-1779d438bc389f0cb67ecd3d9fed7d13bca7db39.zip |
clang-format: [JS] Improve formatting of function literals in chains
Before:
getSomeLongPromise(.....)
.then(
function(value) {
body();
body();
})
.thenCatch(function(error) {
body();
body();
});
After:
getSomeLongPromise(.....)
.then(function(value) {
body();
body();
})
.thenCatch(function(error) {
body();
body();
});
llvm-svn: 218595
-rw-r--r-- | clang/lib/Format/ContinuationIndenter.cpp | 5 | ||||
-rw-r--r-- | clang/unittests/Format/FormatTestJS.cpp | 13 |
2 files changed, 17 insertions, 1 deletions
diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp index ce6ebd84b6a..c48df870416 100644 --- a/clang/lib/Format/ContinuationIndenter.cpp +++ b/clang/lib/Format/ContinuationIndenter.cpp @@ -656,7 +656,10 @@ unsigned ContinuationIndenter::moveStateToNextToken(LineState &State, } if (Current.TokenText == "function") State.Stack.back().JSFunctionInlined = - !Newline && Previous && Previous->Type != TT_DictLiteral; + !Newline && Previous && Previous->Type != TT_DictLiteral && + // If the unnamed function is the only parameter to another function, + // we can likely inline it and come up with a good format. + (Previous->isNot(tok::l_paren) || Previous->ParameterCount > 1); } moveStatePastFakeLParens(State, Newline); diff --git a/clang/unittests/Format/FormatTestJS.cpp b/clang/unittests/Format/FormatTestJS.cpp index 52f45c7ddd1..09d5f1feb2f 100644 --- a/clang/unittests/Format/FormatTestJS.cpp +++ b/clang/unittests/Format/FormatTestJS.cpp @@ -249,6 +249,19 @@ TEST_F(FormatTestJS, MultipleFunctionLiterals) { " doFoo();\n" " doBaz();\n" " });\n"); + + verifyFormat("getSomeLongPromise()\n" + " .then(function(value) { body(); })\n" + " .thenCatch(function(error) { body(); });"); + verifyFormat("getSomeLongPromise()\n" + " .then(function(value) {\n" + " body();\n" + " body();\n" + " })\n" + " .thenCatch(function(error) {\n" + " body();\n" + " body();\n" + " });"); } TEST_F(FormatTestJS, ReturnStatements) { |