2017-12-31 22:15:03 +00:00
|
|
|
#if USE_CLANG_CXX
|
|
|
|
|
2017-12-25 17:04:10 +00:00
|
|
|
#include "clang_format.h"
|
|
|
|
|
2017-12-29 21:44:28 +00:00
|
|
|
#include <loguru.hpp>
|
|
|
|
|
2017-12-25 17:04:10 +00:00
|
|
|
using namespace clang::format;
|
|
|
|
using namespace clang::tooling;
|
|
|
|
|
|
|
|
ClangFormat::ClangFormat(llvm::StringRef document_filename,
|
|
|
|
llvm::StringRef document,
|
|
|
|
llvm::ArrayRef<clang::tooling::Range> ranges,
|
|
|
|
int tab_size,
|
|
|
|
bool insert_spaces)
|
|
|
|
: document_filename_(document_filename),
|
|
|
|
document_(document),
|
|
|
|
ranges_(ranges),
|
|
|
|
tab_size_(tab_size),
|
|
|
|
insert_spaces_(insert_spaces) {}
|
|
|
|
ClangFormat::~ClangFormat(){};
|
|
|
|
|
2017-12-25 17:06:30 +00:00
|
|
|
static FormatStyle::LanguageKind getLanguageKindFromFilename(
|
|
|
|
llvm::StringRef filename) {
|
|
|
|
if (filename.endswith(".m") || filename.endswith(".mm")) {
|
|
|
|
return FormatStyle::LK_ObjC;
|
|
|
|
}
|
|
|
|
return FormatStyle::LK_Cpp;
|
|
|
|
}
|
|
|
|
|
2017-12-25 17:04:10 +00:00
|
|
|
std::vector<Replacement> ClangFormat::FormatWholeDocument() {
|
2017-12-25 17:06:30 +00:00
|
|
|
const auto language_kind = getLanguageKindFromFilename(document_filename_);
|
|
|
|
FormatStyle predefined_style;
|
|
|
|
getPredefinedStyle("chromium", language_kind, &predefined_style);
|
2017-12-29 21:44:28 +00:00
|
|
|
llvm::Expected<FormatStyle> style =
|
|
|
|
getStyle("file", document_filename_, "chromium");
|
|
|
|
if (!style) {
|
|
|
|
LOG_S(ERROR) << llvm::toString(style.takeError());
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*style == predefined_style) {
|
|
|
|
style->UseTab = insert_spaces_ ? FormatStyle::UseTabStyle::UT_Never
|
2017-12-25 17:06:30 +00:00
|
|
|
: FormatStyle::UseTabStyle::UT_Always;
|
2017-12-29 21:44:28 +00:00
|
|
|
style->IndentWidth = tab_size_;
|
2017-12-25 17:06:30 +00:00
|
|
|
}
|
2017-12-29 21:44:28 +00:00
|
|
|
auto format_result = reformat(*style, document_, ranges_, document_filename_);
|
2017-12-25 17:04:10 +00:00
|
|
|
return std::vector<Replacement>(format_result.begin(), format_result.end());
|
|
|
|
}
|
2017-12-31 22:15:03 +00:00
|
|
|
|
|
|
|
#endif
|