diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst --- a/clang/docs/ClangFormatStyleOptions.rst +++ b/clang/docs/ClangFormatStyleOptions.rst @@ -2021,6 +2021,20 @@ false: import {VeryLongImportsAreAnnoying, VeryLongImportsAreAnnoying, VeryLongImportsAreAnnoying,} from "some/module.js" +**JavaStaticImportAfterImport** (``bool``) + If true, clang-format will put Java static imports after all non-static imports. + + .. code-block:: java + true: + import static org.example.function1; + + import org.example.ClassA; + + false: + import org.example.ClassA; + + import static org.example.function1; + **KeepEmptyLinesAtTheStartOfBlocks** (``bool``) If true, the empty line at the start of blocks is kept. diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h --- a/clang/include/clang/Format/Format.h +++ b/clang/include/clang/Format/Format.h @@ -1671,6 +1671,10 @@ bool JavaScriptWrapImports; // clang-format on + /// If true, clang-format will put Java Static imports after all non-static + /// imports. + bool JavaStaticImportAfterImport; + /// If true, the empty line at the start of blocks is kept. /// \code /// true: false: @@ -2391,6 +2395,7 @@ JavaImportGroups == R.JavaImportGroups && JavaScriptQuotes == R.JavaScriptQuotes && JavaScriptWrapImports == R.JavaScriptWrapImports && + JavaStaticImportAfterImport == R.JavaStaticImportAfterImport && KeepEmptyLinesAtTheStartOfBlocks == R.KeepEmptyLinesAtTheStartOfBlocks && MacroBlockBegin == R.MacroBlockBegin && diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -543,6 +543,8 @@ IO.mapOptional("JavaImportGroups", Style.JavaImportGroups); IO.mapOptional("JavaScriptQuotes", Style.JavaScriptQuotes); IO.mapOptional("JavaScriptWrapImports", Style.JavaScriptWrapImports); + IO.mapOptional("JavaStaticImportAfterImport", + Style.JavaStaticImportAfterImport); IO.mapOptional("KeepEmptyLinesAtTheStartOfBlocks", Style.KeepEmptyLinesAtTheStartOfBlocks); IO.mapOptional("MacroBlockBegin", Style.MacroBlockBegin); @@ -899,6 +901,7 @@ LLVMStyle.InsertTrailingCommas = FormatStyle::TCS_None; LLVMStyle.JavaScriptQuotes = FormatStyle::JSQS_Leave; LLVMStyle.JavaScriptWrapImports = true; + LLVMStyle.JavaStaticImportAfterImport = false; LLVMStyle.TabWidth = 8; LLVMStyle.MaxEmptyLinesToKeep = 1; LLVMStyle.KeepEmptyLinesAtTheStartOfBlocks = true; @@ -2310,12 +2313,15 @@ JavaImportGroups.push_back( findJavaImportGroup(Style, Imports[i].Identifier)); } + bool StaticImportAfterNormalImport = Style.JavaStaticImportAfterImport; llvm::sort(Indices, [&](unsigned LHSI, unsigned RHSI) { // Negating IsStatic to push static imports above non-static imports. - return std::make_tuple(!Imports[LHSI].IsStatic, JavaImportGroups[LHSI], - Imports[LHSI].Identifier) < - std::make_tuple(!Imports[RHSI].IsStatic, JavaImportGroups[RHSI], - Imports[RHSI].Identifier); + return std::make_tuple(!Imports[LHSI].IsStatic ^ + StaticImportAfterNormalImport, + JavaImportGroups[LHSI], Imports[LHSI].Identifier) < + std::make_tuple(!Imports[RHSI].IsStatic ^ + StaticImportAfterNormalImport, + JavaImportGroups[RHSI], Imports[RHSI].Identifier); }); // Deduplicate imports. diff --git a/clang/unittests/Format/SortImportsTestJava.cpp b/clang/unittests/Format/SortImportsTestJava.cpp --- a/clang/unittests/Format/SortImportsTestJava.cpp +++ b/clang/unittests/Format/SortImportsTestJava.cpp @@ -250,6 +250,30 @@ "import org.c;\n")); } +TEST_F(SortImportsTestJava, FormatJavaStaticImportAfterImport) { + FmtStyle.JavaStaticImportAfterImport = true; + + EXPECT_EQ("import com.test.b;\n" + "import com.test.c;\n" + "\n" + "import org.b;\n" + "\n" + "import com.b;\n" + "\n" + "import static com.test.a;\n" + "\n" + "import static org.a;\n" + "\n" + "import static com.a;\n", + sort("import static com.test.a;\n" + "import static org.a;\n" + "import static com.a;\n" + "import com.test.b;\n" + "import org.b;\n" + "import com.b;\n" + "import com.test.c;\n")); +} + TEST_F(SortImportsTestJava, DeduplicateImports) { EXPECT_EQ("import org.a;\n", sort("import org.a;\n" "import org.a;\n"));