diff options
-rw-r--r-- | lib/Format/TokenAnnotator.cpp | 3 | ||||
-rw-r--r-- | lib/Format/UnwrappedLineParser.cpp | 4 | ||||
-rw-r--r-- | unittests/Format/FormatTestJava.cpp | 11 |
3 files changed, 17 insertions, 1 deletions
diff --git a/lib/Format/TokenAnnotator.cpp b/lib/Format/TokenAnnotator.cpp index d0d888fa4d..4ba3f91969 100644 --- a/lib/Format/TokenAnnotator.cpp +++ b/lib/Format/TokenAnnotator.cpp @@ -1701,7 +1701,8 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line, (Style.SpaceBeforeParens != FormatStyle::SBPO_Never && (Left.isOneOf(tok::kw_if, tok::kw_for, tok::kw_while, tok::kw_switch, tok::kw_case) || - (Left.isOneOf(tok::kw_catch, tok::kw_new, tok::kw_delete) && + (Left.isOneOf(tok::kw_try, tok::kw_catch, tok::kw_new, + tok::kw_delete) && (!Left.Previous || Left.Previous->isNot(tok::period))) || Left.IsForEachMacro)) || (Style.SpaceBeforeParens == FormatStyle::SBPO_Always && diff --git a/lib/Format/UnwrappedLineParser.cpp b/lib/Format/UnwrappedLineParser.cpp index 01c8acf0e6..ec04af5231 100644 --- a/lib/Format/UnwrappedLineParser.cpp +++ b/lib/Format/UnwrappedLineParser.cpp @@ -1164,6 +1164,10 @@ void UnwrappedLineParser::parseTryCatch() { nextToken(); } } + // Parse try with resource. + if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_paren)) { + parseParens(); + } if (FormatTok->is(tok::l_brace)) { CompoundStatementIndenter Indenter(this, Style, Line->Level); parseBlock(/*MustBeDeclaration=*/false); diff --git a/unittests/Format/FormatTestJava.cpp b/unittests/Format/FormatTestJava.cpp index af4199b46f..8d6daa62a5 100644 --- a/unittests/Format/FormatTestJava.cpp +++ b/unittests/Format/FormatTestJava.cpp @@ -361,6 +361,17 @@ TEST_F(FormatTestJava, TryCatchFinally) { "}"); } +TEST_F(FormatTestJava, TryWithResources) { + verifyFormat("try (SomeResource rs = someFunction()) {\n" + " Something();\n" + "}"); + verifyFormat("try (SomeResource rs = someFunction()) {\n" + " Something();\n" + "} catch (SomeException e) {\n" + " HandleException(e);\n" + "}"); +} + TEST_F(FormatTestJava, SynchronizedKeyword) { verifyFormat("synchronized (mData) {\n" " // ...\n" |