/// Pointer and reference alignment style.
PointerAlignmentStyle PointerAlignment;
+ /// \brief If true, clang-format will sort #includes.
+ bool SortIncludes;
+
/// \brief If \c true, a space may be inserted after C style casts.
bool SpaceAfterCStyleCast;
IO.mapOptional("PenaltyExcessCharacter", Style.PenaltyExcessCharacter);
IO.mapOptional("PenaltyReturnTypeOnItsOwnLine",
Style.PenaltyReturnTypeOnItsOwnLine);
+ IO.mapOptional("SortIncludes", Style.SortIncludes);
IO.mapOptional("PointerAlignment", Style.PointerAlignment);
IO.mapOptional("SpaceAfterCStyleCast", Style.SpaceAfterCStyleCast);
IO.mapOptional("SpaceBeforeAssignmentOperators",
LLVMStyle.PenaltyBreakBeforeFirstCallParameter = 19;
LLVMStyle.DisableFormat = false;
+ LLVMStyle.SortIncludes = true;
return LLVMStyle;
}
FormatStyle getNoStyle() {
FormatStyle NoStyle = getLLVMStyle();
NoStyle.DisableFormat = true;
+ NoStyle.SortIncludes = false;
return NoStyle;
}
ArrayRef<tooling::Range> Ranges,
StringRef FileName) {
tooling::Replacements Replaces;
+ if (!Style.SortIncludes)
+ return Replaces;
+
unsigned Prev = 0;
unsigned SearchFrom = 0;
llvm::Regex IncludeRegex(
--- /dev/null
+// RUN: clang-format %s | FileCheck %s
+// RUN: clang-format %s -sort-includes -style="{SortIncludes: false}" | FileCheck %s
+// RUN: clang-format %s -sort-includes=false | FileCheck %s -check-prefix=NOT-SORTED
+
+#include <b>
+#include <a>
+// CHECK: <a>
+// CHECK-NEXT: <b>
+// NOT-SORTED: <b>
+// NOT-SORTED-NEXT: <a>
"clang-format from an editor integration"),
cl::init(0), cl::cat(ClangFormatCategory));
-static cl::opt<bool> SortIncludes("sort-includes",
- cl::desc("Sort touched include lines"),
- cl::cat(ClangFormatCategory));
+static cl::opt<bool> SortIncludes(
+ "sort-includes",
+ cl::desc("If set, overrides the include sorting behavior determined by the "
+ "SortIncludes style flag"),
+ cl::cat(ClangFormatCategory));
static cl::list<std::string> FileNames(cl::Positional, cl::desc("[<file> ...]"),
cl::cat(ClangFormatCategory));
return true;
StringRef AssumedFileName = (FileName == "-") ? AssumeFileName : FileName;
FormatStyle FormatStyle = getStyle(Style, AssumedFileName, FallbackStyle);
- Replacements Replaces;
- std::string ChangedCode;
- if (SortIncludes) {
- Replaces =
- sortIncludes(FormatStyle, Code->getBuffer(), Ranges, AssumedFileName);
- ChangedCode = tooling::applyAllReplacements(Code->getBuffer(), Replaces);
- for (const auto &R : Replaces)
- Ranges.push_back({R.getOffset(), R.getLength()});
- } else {
- ChangedCode = Code->getBuffer().str();
- }
+ if (SortIncludes.getNumOccurrences() != 0)
+ FormatStyle.SortIncludes = SortIncludes;
+ Replacements Replaces =
+ sortIncludes(FormatStyle, Code->getBuffer(), Ranges, AssumedFileName);
+ std::string ChangedCode =
+ tooling::applyAllReplacements(Code->getBuffer(), Replaces);
+ for (const auto &R : Replaces)
+ Ranges.push_back({R.getOffset(), R.getLength()});
bool IncompleteFormat = false;
Replaces = tooling::mergeReplacements(
if encoding == 'Undefined':
encoding = 'utf-8'
regions = []
- command = [binary, '-sort-includes', '-style', style]
+ command = [binary, '-style', style]
for region in self.view.sel():
regions.append(region)
region_offset = min(region.a, region.b)
nil `(,temp-buffer ,temp-file) nil
"-output-replacements-xml"
- "-sort-includes"
"-assume-filename" (or (buffer-file-name) "")
"-style" style
"-offset" (number-to-string start)
startupinfo.wShowWindow = subprocess.SW_HIDE
# Call formatter.
- command = [binary, '-style', style, '-cursor', str(cursor), '-sort-includes']
+ command = [binary, '-style', style, '-cursor', str(cursor)]
if lines != 'all':
command.extend(['-lines', lines])
if fallback_style:
CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine);
CHECK_PARSE_BOOL(DerivePointerAlignment);
CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding");
+ CHECK_PARSE_BOOL(DisableFormat);
CHECK_PARSE_BOOL(IndentCaseLabels);
CHECK_PARSE_BOOL(IndentWrappedFunctionNames);
CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks);
CHECK_PARSE_BOOL(ObjCSpaceAfterProperty);
CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList);
CHECK_PARSE_BOOL(Cpp11BracedListStyle);
+ CHECK_PARSE_BOOL(SortIncludes);
CHECK_PARSE_BOOL(SpacesInParentheses);
CHECK_PARSE_BOOL(SpacesInSquareBrackets);
CHECK_PARSE_BOOL(SpacesInAngles);
"#include \"b.h\"\n"));
}
+TEST_F(SortIncludesTest, IncludeSortingCanBeDisabled) {
+ Style.SortIncludes = false;
+ EXPECT_EQ("#include \"a.h\"\n"
+ "#include \"c.h\"\n"
+ "#include \"b.h\"\n",
+ sort("#include \"a.h\"\n"
+ "#include \"c.h\"\n"
+ "#include \"b.h\"\n"));
+}
+
TEST_F(SortIncludesTest, MixIncludeAndImport) {
EXPECT_EQ("#include \"a.h\"\n"
"#import \"b.h\"\n"