Implement .clang-format style management

If we can't find a .clang-format file for the given file, default to
Chromium style with the tab/spaces configuration that was provided by
the client.
This commit is contained in:
Daniel Martín 2017-12-25 18:06:30 +01:00
parent 19341c18cd
commit aa4e5e7e5b

View File

@ -15,12 +15,24 @@ ClangFormat::ClangFormat(llvm::StringRef document_filename,
insert_spaces_(insert_spaces) {}
ClangFormat::~ClangFormat(){};
static FormatStyle::LanguageKind getLanguageKindFromFilename(
llvm::StringRef filename) {
if (filename.endswith(".m") || filename.endswith(".mm")) {
return FormatStyle::LK_ObjC;
}
return FormatStyle::LK_Cpp;
}
std::vector<Replacement> ClangFormat::FormatWholeDocument() {
// TODO: Construct a valid style.
FormatStyle style = getChromiumStyle(FormatStyle::LK_Cpp);
style.UseTab = insert_spaces_ ? FormatStyle::UseTabStyle::UT_Never
: FormatStyle::UseTabStyle::UT_Always;
style.IndentWidth = tab_size_;
const auto language_kind = getLanguageKindFromFilename(document_filename_);
FormatStyle predefined_style;
getPredefinedStyle("chromium", language_kind, &predefined_style);
auto style = getStyle("file", document_filename_, "chromium");
if (style == predefined_style) {
style.UseTab = insert_spaces_ ? FormatStyle::UseTabStyle::UT_Never
: FormatStyle::UseTabStyle::UT_Always;
style.IndentWidth = tab_size_;
}
auto format_result = reformat(style, document_, ranges_, document_filename_);
return std::vector<Replacement>(format_result.begin(), format_result.end());
}