From 9a85ce88a7254d9d54c42930a5514f0dd6308ce0 Mon Sep 17 00:00:00 2001 From: Ivan Donchevskii Date: Tue, 13 Nov 2018 11:47:02 +0100 Subject: [PATCH] ClangFormat: Use relevant settings for each file Existing Clang Format settings may not follow the project/global pattern but can be expected to be used for the indentation/formatting. So let's proceed with UI for global/project settings but use global settings only if there's no configuration found for the current file. Change-Id: I87c25ab3feb7e2e3deb0290848088657783cf972 Reviewed-by: Marco Bubke --- .../clangformat/clangformatconfigwidget.cpp | 5 +- .../clangformat/clangformatconstants.h | 2 + .../clangformat/clangformatindenter.cpp | 27 ++++---- src/plugins/clangformat/clangformatutils.cpp | 61 +++++++++---------- src/plugins/clangformat/clangformatutils.h | 7 ++- 5 files changed, 54 insertions(+), 48 deletions(-) diff --git a/src/plugins/clangformat/clangformatconfigwidget.cpp b/src/plugins/clangformat/clangformatconfigwidget.cpp index ea439a0f43d..6d81bde9721 100644 --- a/src/plugins/clangformat/clangformatconfigwidget.cpp +++ b/src/plugins/clangformat/clangformatconfigwidget.cpp @@ -139,7 +139,7 @@ void ClangFormatConfigWidget::initialize() connect(m_ui->createFileButton, &QPushButton::clicked, this, [this]() { - createStyleFileIfNeeded(m_project->projectDirectory(), false); + createStyleFileIfNeeded(false); initialize(); }); return; @@ -160,8 +160,7 @@ void ClangFormatConfigWidget::initialize() tr("Current project has its own .clang-format file " "and can be configured in Projects > Clang Format.")); } - const QString settingsDir = Core::ICore::userResourcePath(); - createStyleFileIfNeeded(Utils::FileName::fromString(settingsDir), true); + createStyleFileIfNeeded(true); m_ui->applyButton->hide(); } diff --git a/src/plugins/clangformat/clangformatconstants.h b/src/plugins/clangformat/clangformatconstants.h index 1024341c224..7b616f74f62 100644 --- a/src/plugins/clangformat/clangformatconstants.h +++ b/src/plugins/clangformat/clangformatconstants.h @@ -27,4 +27,6 @@ namespace ClangFormat { static const char SETTINGS_FILE_NAME[] = ".clang-format"; +static const char SETTINGS_FILE_ALT_NAME[] = "_clang-format"; +static const char SAMPLE_FILE_NAME[] = "test.cpp"; } diff --git a/src/plugins/clangformat/clangformatindenter.cpp b/src/plugins/clangformat/clangformatindenter.cpp index ddd786fc22f..0d93059152a 100644 --- a/src/plugins/clangformat/clangformatindenter.cpp +++ b/src/plugins/clangformat/clangformatindenter.cpp @@ -164,13 +164,14 @@ void modifyToIndentEmptyLines(QByteArray &buffer, int &offset, int &length, cons static const int kMaxLinesFromCurrentBlock = 200; -Replacements replacements(QByteArray buffer, +Replacements replacements(const Utils::FileName &fileName, + QByteArray buffer, int utf8Offset, int utf8Length, const QTextBlock *block = nullptr, const QChar &typedChar = QChar::Null) { - FormatStyle style = currentStyle(); + FormatStyle style = styleForFile(fileName); int extraOffset = 0; if (block) { @@ -198,10 +199,8 @@ Replacements replacements(QByteArray buffer, static_cast(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); + Replacements replacements = reformat(style, buffer.data(), ranges, + fileName.toString().toStdString(), &status); if (!status.FormatComplete) Replacements(); @@ -337,6 +336,10 @@ void ClangFormatIndenter::indent(QTextDocument *doc, bool autoTriggered) { if (typedChar == QChar::Null && (cursor.hasSelection() || !autoTriggered)) { + TextEditorWidget *editor = TextEditorWidget::currentTextEditorWidget(); + if (!editor) + return; + const Utils::FileName fileName = editor->textDocument()->filePath(); int utf8Offset; int utf8Length; const QByteArray buffer = doc->toPlainText().toUtf8(); @@ -349,7 +352,7 @@ void ClangFormatIndenter::indent(QTextDocument *doc, applyReplacements(start, utf8Offset, buffer, - replacements(buffer, utf8Offset, utf8Length)); + replacements(fileName, buffer, utf8Offset, utf8Length)); } else { const QTextBlock block = cursor.block(); utf8Offset = Utils::Text::utf8NthLineOffset(doc, buffer, block.blockNumber() + 1); @@ -358,7 +361,7 @@ void ClangFormatIndenter::indent(QTextDocument *doc, applyReplacements(block, utf8Offset, buffer, - replacements(buffer, utf8Offset, utf8Length)); + replacements(fileName, buffer, utf8Offset, utf8Length)); } } else { indentBlock(doc, cursor.block(), typedChar, tabSettings); @@ -383,6 +386,7 @@ void ClangFormatIndenter::indentBlock(QTextDocument *doc, if (!editor) return; + const Utils::FileName fileName = editor->textDocument()->filePath(); trimFirstNonEmptyBlock(block); const QByteArray buffer = doc->toPlainText().toUtf8(); const int utf8Offset = Utils::Text::utf8NthLineOffset(doc, buffer, block.blockNumber() + 1); @@ -392,7 +396,7 @@ void ClangFormatIndenter::indentBlock(QTextDocument *doc, applyReplacements(block, utf8Offset, buffer, - replacements(buffer, utf8Offset, utf8Length, &block, typedChar)); + replacements(fileName, buffer, utf8Offset, utf8Length, &block, typedChar)); } int ClangFormatIndenter::indentFor(const QTextBlock &block, const TextEditor::TabSettings &) @@ -401,6 +405,7 @@ int ClangFormatIndenter::indentFor(const QTextBlock &block, const TextEditor::Ta if (!editor) return -1; + const Utils::FileName fileName = editor->textDocument()->filePath(); trimFirstNonEmptyBlock(block); const QTextDocument *doc = block.document(); const QByteArray buffer = doc->toPlainText().toUtf8(); @@ -408,7 +413,7 @@ int ClangFormatIndenter::indentFor(const QTextBlock &block, const TextEditor::Ta QTC_ASSERT(utf8Offset >= 0, return 0;); const int utf8Length = block.text().toUtf8().size(); - Replacements toReplace = replacements(buffer, utf8Offset, utf8Length, &block); + Replacements toReplace = replacements(fileName, buffer, utf8Offset, utf8Length, &block); if (toReplace.empty()) return -1; @@ -423,7 +428,7 @@ int ClangFormatIndenter::indentFor(const QTextBlock &block, const TextEditor::Ta TabSettings ClangFormatIndenter::tabSettings() const { - FormatStyle style = currentStyle(); + FormatStyle style = currentProjectStyle(); TabSettings tabSettings; switch (style.UseTab) { diff --git a/src/plugins/clangformat/clangformatutils.cpp b/src/plugins/clangformat/clangformatutils.cpp index f0e5f785575..76af9956c0d 100644 --- a/src/plugins/clangformat/clangformatutils.cpp +++ b/src/plugins/clangformat/clangformatutils.cpp @@ -92,7 +92,7 @@ static void applyCppCodeStyleSettings(clang::format::FormatStyle &style, : - static_cast(style.IndentWidth); } -static Utils::FileName projectStylePath() +static Utils::FileName projectPath() { const Project *project = SessionManager::startupProject(); if (project) @@ -101,18 +101,18 @@ static Utils::FileName projectStylePath() return Utils::FileName(); } -static Utils::FileName globalStylePath() +static Utils::FileName globalPath() { return Utils::FileName::fromString(Core::ICore::userResourcePath()); } -Utils::FileName currentStyleConfigPath() -{ - Utils::FileName path = projectStylePath(); - if (!path.isEmpty()) - return path; - - return globalStylePath(); +static bool configForFileExists(Utils::FileName fileName) { + QDir projectDir(fileName.parentDir().toString()); + while (!projectDir.exists(SETTINGS_FILE_NAME) && !projectDir.exists(SETTINGS_FILE_ALT_NAME)) { + if (!projectDir.cdUp()) + return false; + } + return true; } static clang::format::FormatStyle constructStyle(bool isGlobal) @@ -134,9 +134,11 @@ static clang::format::FormatStyle constructStyle(bool isGlobal) return style; } -void createStyleFileIfNeeded(Utils::FileName styleConfigPath, bool isGlobal) +void createStyleFileIfNeeded(bool isGlobal) { - const QString configFile = styleConfigPath.appendPath(SETTINGS_FILE_NAME).toString(); + Utils::FileName path = isGlobal ? globalPath() : projectPath(); + const QString configFile = path.appendPath(SETTINGS_FILE_NAME).toString(); + if (QFile::exists(configFile)) return; @@ -147,13 +149,23 @@ void createStyleFileIfNeeded(Utils::FileName styleConfigPath, bool isGlobal) } } -static clang::format::FormatStyle currentStyle(bool isGlobal) +clang::format::FormatStyle styleForFile(Utils::FileName fileName) { - Utils::FileName styleConfigPath = isGlobal ? globalStylePath() : projectStylePath(); - createStyleFileIfNeeded(styleConfigPath, isGlobal); + bool isGlobal = false; + if (!configForFileExists(fileName)) { + if (fileName.isChildOf(projectPath()) && CppCodeStyleSettings::currentProjectCodeStyle()) { + fileName = projectPath(); + } else { + fileName = globalPath(); + isGlobal = true; + } + fileName.appendPath(SAMPLE_FILE_NAME); + createStyleFileIfNeeded(isGlobal); + } - Expected style = format::getStyle( - "file", styleConfigPath.appendPath("test.cpp").toString().toStdString(), "LLVM"); + Expected style = format::getStyle("file", + fileName.toString().toStdString(), + "none"); if (style) return *style; @@ -166,25 +178,12 @@ static clang::format::FormatStyle currentStyle(bool isGlobal) clang::format::FormatStyle currentProjectStyle() { - return currentStyle(false); + return styleForFile(projectPath().appendPath(SAMPLE_FILE_NAME)); } clang::format::FormatStyle currentGlobalStyle() { - return currentStyle(true); -} - -static bool isCurrentStyleGlobal() -{ - Utils::FileName path = projectStylePath(); - if (path.appendPath(SETTINGS_FILE_NAME).exists()) - return false; - return !CppCodeStyleSettings::currentProjectCodeStyle().has_value(); -} - -clang::format::FormatStyle currentStyle() -{ - return currentStyle(isCurrentStyleGlobal()); + return styleForFile(globalPath().appendPath(SAMPLE_FILE_NAME)); } } diff --git a/src/plugins/clangformat/clangformatutils.h b/src/plugins/clangformat/clangformatutils.h index 8634685971e..41e2b5611fa 100644 --- a/src/plugins/clangformat/clangformatutils.h +++ b/src/plugins/clangformat/clangformatutils.h @@ -34,12 +34,13 @@ namespace ClangFormat { -void createStyleFileIfNeeded(Utils::FileName styleConfigPath, bool isGlobal); +// Creates the style for the current project or the global style if needed. +void createStyleFileIfNeeded(bool isGlobal); clang::format::FormatStyle currentProjectStyle(); clang::format::FormatStyle currentGlobalStyle(); -Utils::FileName currentStyleConfigPath(); -clang::format::FormatStyle currentStyle(); +// Is the style from the matching .clang-format file or global one if it's not found. +clang::format::FormatStyle styleForFile(Utils::FileName fileName); }