ClangFormat: Synchronize with C++ code style settings

And remove UI for default code style settings because
it does not affect anything when ClangFormat plugin is
enabled.

Change-Id: Ie348b7d2691b09ea2b4868da987f2a27347ea0f3
Reviewed-by: Marco Bubke <marco.bubke@qt.io>
This commit is contained in:
Ivan Donchevskii
2018-11-08 10:35:23 +01:00
parent 3de607f4e0
commit 0e5c7f51fa
9 changed files with 253 additions and 69 deletions

View File

@@ -30,10 +30,7 @@
#include <clang/Format/Format.h>
#include <clang/Tooling/Core/Replacement.h>
#include <coreplugin/icore.h>
#include <cpptools/cppmodelmanager.h>
#include <projectexplorer/project.h>
#include <projectexplorer/session.h>
#include <texteditor/textdocument.h>
#include <texteditor/texteditor.h>
@@ -95,31 +92,6 @@ Replacements filteredReplacements(const Replacements &replacements,
return filtered;
}
Utils::FileName styleConfigPath()
{
const Project *project = SessionManager::startupProject();
if (project && project->projectDirectory().appendPath(".clang-format").exists())
return project->projectDirectory();
return Utils::FileName::fromString(Core::ICore::userResourcePath());
}
FormatStyle formatStyle(Utils::FileName styleConfigPath)
{
createStyleFileIfNeeded(styleConfigPath);
Expected<FormatStyle> style = format::getStyle(
"file", styleConfigPath.appendPath("test.cpp").toString().toStdString(), "LLVM");
if (style)
return *style;
handleAllErrors(style.takeError(), [](const ErrorInfoBase &) {
// do nothing
});
return format::getLLVMStyle();
}
void trimFirstNonEmptyBlock(const QTextBlock &currentBlock)
{
QTextBlock prevBlock = currentBlock.previous();
@@ -198,8 +170,7 @@ Replacements replacements(QByteArray buffer,
const QTextBlock *block = nullptr,
const QChar &typedChar = QChar::Null)
{
Utils::FileName stylePath = styleConfigPath();
FormatStyle style = formatStyle(stylePath);
FormatStyle style = currentStyle();
int extraOffset = 0;
if (block) {
@@ -227,6 +198,7 @@ Replacements replacements(QByteArray buffer,
static_cast<unsigned int>(utf8Length)}};
FormattingAttemptStatus status;
Utils::FileName stylePath = currentStyleConfigPath();
const std::string assumedFilePath
= stylePath.appendPath("test.cpp").toString().toStdString();
Replacements replacements = reformat(style, buffer.data(), ranges, assumedFilePath, &status);
@@ -451,7 +423,7 @@ int ClangFormatIndenter::indentFor(const QTextBlock &block, const TextEditor::Ta
TabSettings ClangFormatIndenter::tabSettings() const
{
FormatStyle style = formatStyle(styleConfigPath());
FormatStyle style = currentStyle();
TabSettings tabSettings;
switch (style.UseTab) {
@@ -468,8 +440,8 @@ TabSettings ClangFormatIndenter::tabSettings() const
tabSettings.m_tabSize = static_cast<int>(style.TabWidth);
tabSettings.m_indentSize = static_cast<int>(style.IndentWidth);
if (style.AlignAfterOpenBracket)
tabSettings.m_continuationAlignBehavior = TabSettings::ContinuationAlignWithSpaces;
if (style.AlignAfterOpenBracket == FormatStyle::BAS_DontAlign)
tabSettings.m_continuationAlignBehavior = TabSettings::NoContinuationAlign;
else
tabSettings.m_continuationAlignBehavior = TabSettings::ContinuationAlignWithIndent;