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) {} insert_spaces_(insert_spaces) {}
ClangFormat::~ClangFormat(){}; 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() { std::vector<Replacement> ClangFormat::FormatWholeDocument() {
// TODO: Construct a valid style. const auto language_kind = getLanguageKindFromFilename(document_filename_);
FormatStyle style = getChromiumStyle(FormatStyle::LK_Cpp); FormatStyle predefined_style;
style.UseTab = insert_spaces_ ? FormatStyle::UseTabStyle::UT_Never getPredefinedStyle("chromium", language_kind, &predefined_style);
: FormatStyle::UseTabStyle::UT_Always; auto style = getStyle("file", document_filename_, "chromium");
style.IndentWidth = tab_size_; 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_); auto format_result = reformat(style, document_, ranges_, document_filename_);
return std::vector<Replacement>(format_result.begin(), format_result.end()); return std::vector<Replacement>(format_result.begin(), format_result.end());
} }