diff --git a/src/plugins/clangformat/clangformatbaseindenter.cpp b/src/plugins/clangformat/clangformatbaseindenter.cpp index 5138e03510f..4f572726826 100644 --- a/src/plugins/clangformat/clangformatbaseindenter.cpp +++ b/src/plugins/clangformat/clangformatbaseindenter.cpp @@ -7,15 +7,16 @@ #include +#include + #include #include #include #include +#include #include -#include - #include #include #include @@ -24,6 +25,8 @@ #include #include +#include + namespace ClangFormat { Internal::LlvmFileSystemAdapter llvmFileSystemAdapter = {}; @@ -760,7 +763,7 @@ clang::format::FormatStyle overrideStyle(const Utils::FilePath &fileName) Utils::FilePath filePath = filePathToCurrentSettings(preferences); if (!filePath.exists()) - return qtcStyle(); + return currentQtStyle(preferences); clang::format::FormatStyle currentSettingsStyle; currentSettingsStyle.Language = clang::format::FormatStyle::LK_Cpp; @@ -769,7 +772,7 @@ clang::format::FormatStyle overrideStyle(const Utils::FilePath &fileName) .toStdString(), ¤tSettingsStyle); QTC_ASSERT(error.value() == static_cast(clang::format::ParseError::Success), - return qtcStyle()); + return currentQtStyle(preferences)); return currentSettingsStyle; } diff --git a/src/plugins/clangformat/clangformatconfigwidget.cpp b/src/plugins/clangformat/clangformatconfigwidget.cpp index cfc25544e57..7c5b8ac3369 100644 --- a/src/plugins/clangformat/clangformatconfigwidget.cpp +++ b/src/plugins/clangformat/clangformatconfigwidget.cpp @@ -80,7 +80,7 @@ ClangFormatConfigWidget::ClangFormatConfigWidget(TextEditor::ICodeStylePreferenc : CppCodeStyleWidget(parent), d(new Private) { d->project = project; - d->config = std::make_unique(filePathToCurrentSettings(codeStyle->currentPreferences())); + d->config = std::make_unique(codeStyle->currentPreferences()); d->fallbackConfig = new QLabel(Tr::tr("Clang-Format Style")); d->checksScrollArea = new QScrollArea(); @@ -136,7 +136,7 @@ void ClangFormatConfigWidget::slotCodeStyleChanged( { if (!codeStyle) return; - d->config.reset(new ClangFormatFile(filePathToCurrentSettings(codeStyle))); + d->config.reset(new ClangFormatFile(codeStyle)); d->config->setIsReadOnly(codeStyle->isReadOnly()); d->style = d->config->style(); diff --git a/src/plugins/clangformat/clangformatfile.cpp b/src/plugins/clangformat/clangformatfile.cpp index fc1659b976c..16209c30943 100644 --- a/src/plugins/clangformat/clangformatfile.cpp +++ b/src/plugins/clangformat/clangformatfile.cpp @@ -4,17 +4,23 @@ #include "clangformatfile.h" #include "clangformatsettings.h" #include "clangformatutils.h" + +#include #include + #include + +#include #include + #include #include using namespace ClangFormat; -ClangFormatFile::ClangFormatFile(Utils::FilePath filePath) - : m_filePath(filePath) +ClangFormatFile::ClangFormatFile(const TextEditor::ICodeStylePreferences *preferences) + : m_filePath(filePathToCurrentSettings(preferences)) { if (!m_filePath.exists()) { // create file and folder @@ -23,7 +29,7 @@ ClangFormatFile::ClangFormatFile(Utils::FilePath filePath) if (newStyleFile.is_open()) { newStyleFile.close(); } - resetStyleToQtC(); + resetStyleToQtC(preferences); return; } @@ -33,7 +39,7 @@ ClangFormatFile::ClangFormatFile(Utils::FilePath filePath) .toStdString(), &m_style); if (error.value() != static_cast(clang::format::ParseError::Success)) { - resetStyleToQtC(); + resetStyleToQtC(preferences); } } @@ -62,9 +68,9 @@ void ClangFormatFile::setIsReadOnly(bool isReadOnly) m_isReadOnly = isReadOnly; } -void ClangFormatFile::resetStyleToQtC() +void ClangFormatFile::resetStyleToQtC(const TextEditor::ICodeStylePreferences *preferences) { - m_style = qtcStyle(); + m_style = currentQtStyle(preferences); saveStyleToFile(m_style, m_filePath); } @@ -178,48 +184,7 @@ CppEditor::CppCodeStyleSettings ClangFormatFile::toCppCodeStyleSettings( void ClangFormatFile::fromCppCodeStyleSettings(const CppEditor::CppCodeStyleSettings &settings) { - using namespace clang::format; - if (settings.indentAccessSpecifiers) - m_style.AccessModifierOffset = 0; - else - m_style.AccessModifierOffset = -1 * m_style.IndentWidth; - - if (settings.indentNamespaceBody || settings.indentNamespaceBraces) - m_style.NamespaceIndentation = FormatStyle::NamespaceIndentationKind::NI_All; - else - m_style.NamespaceIndentation = FormatStyle::NamespaceIndentationKind::NI_None; - - if (settings.indentClassBraces || settings.indentEnumBraces || settings.indentBlockBraces - || settings.indentFunctionBraces) - m_style.BreakBeforeBraces = FormatStyle::BS_Whitesmiths; - else - m_style.BreakBeforeBraces = FormatStyle::BS_Custom; - - - m_style.IndentCaseLabels = settings.indentSwitchLabels; -#if LLVM_VERSION_MAJOR >= 11 - m_style.IndentCaseBlocks = settings.indentBlocksRelativeToSwitchLabels; -#endif - - if (settings.extraPaddingForConditionsIfConfusingAlign) - m_style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; - else if (settings.alignAssignments) - m_style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; - else - m_style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; - - m_style.DerivePointerAlignment = settings.bindStarToIdentifier || settings.bindStarToTypeName - || settings.bindStarToLeftSpecifier - || settings.bindStarToRightSpecifier; - - if ((settings.bindStarToIdentifier || settings.bindStarToRightSpecifier) - && ClangFormatSettings::instance().mode() == ClangFormatSettings::Mode::Formatting) - m_style.PointerAlignment = FormatStyle::PAS_Right; - - if ((settings.bindStarToTypeName || settings.bindStarToLeftSpecifier) - && ClangFormatSettings::instance().mode() == ClangFormatSettings::Mode::Formatting) - m_style.PointerAlignment = FormatStyle::PAS_Left; - + ::fromCppCodeStyleSettings(m_style, settings); saveNewFormat(); } @@ -258,22 +223,6 @@ TextEditor::TabSettings ClangFormatFile::toTabSettings(ProjectExplorer::Project void ClangFormatFile::fromTabSettings(const TextEditor::TabSettings &settings) { - using namespace clang::format; - - m_style.IndentWidth = settings.m_indentSize; - m_style.TabWidth = settings.m_tabSize; - - switch (settings.m_tabPolicy) { - case TextEditor::TabSettings::TabPolicy::MixedTabPolicy: - m_style.UseTab = FormatStyle::UT_ForContinuationAndIndentation; - break; - case TextEditor::TabSettings::TabPolicy::SpacesOnlyTabPolicy: - m_style.UseTab = FormatStyle::UT_Never; - break; - case TextEditor::TabSettings::TabPolicy::TabsOnlyTabPolicy: - m_style.UseTab = FormatStyle::UT_Always; - break; - } - + ::fromTabSettings(m_style, settings); saveNewFormat(); } diff --git a/src/plugins/clangformat/clangformatfile.h b/src/plugins/clangformat/clangformatfile.h index dcd0e0d6c10..ad32952b0f0 100644 --- a/src/plugins/clangformat/clangformatfile.h +++ b/src/plugins/clangformat/clangformatfile.h @@ -9,18 +9,21 @@ namespace CppEditor { class CppCodeStyleSettings; } namespace ProjectExplorer { class Project; } -namespace TextEditor { class TabSettings; } +namespace TextEditor { +class ICodeStylePreferences; +class TabSettings; +} namespace ClangFormat { class ClangFormatFile { public: - explicit ClangFormatFile(Utils::FilePath file); + explicit ClangFormatFile(const TextEditor::ICodeStylePreferences *preferences); clang::format::FormatStyle style(); Utils::FilePath filePath(); - void resetStyleToQtC(); + void resetStyleToQtC(const TextEditor::ICodeStylePreferences *codeStyle); void setBasedOnStyle(QString styleName); void setStyle(clang::format::FormatStyle style); QString setStyle(QString style); diff --git a/src/plugins/clangformat/clangformatutils.cpp b/src/plugins/clangformat/clangformatutils.cpp index 9304599d999..735c0a03548 100644 --- a/src/plugins/clangformat/clangformatutils.cpp +++ b/src/plugins/clangformat/clangformatutils.cpp @@ -7,6 +7,7 @@ #include +#include #include #include @@ -179,6 +180,83 @@ clang::format::FormatStyle qtcStyle() return style; } +clang::format::FormatStyle currentQtStyle(const TextEditor::ICodeStylePreferences *preferences) +{ + clang::format::FormatStyle style = qtcStyle(); + if (!preferences) + return style; + + fromTabSettings(style, preferences->tabSettings()); + if (auto ccpPreferences = dynamic_cast(preferences)) + fromCppCodeStyleSettings(style, ccpPreferences->codeStyleSettings()); + return style; +} + +void fromCppCodeStyleSettings(clang::format::FormatStyle &style, + const CppEditor::CppCodeStyleSettings &settings) +{ + using namespace clang::format; + if (settings.indentAccessSpecifiers) + style.AccessModifierOffset = 0; + else + style.AccessModifierOffset = -1 * style.IndentWidth; + + if (settings.indentNamespaceBody || settings.indentNamespaceBraces) + style.NamespaceIndentation = FormatStyle::NamespaceIndentationKind::NI_All; + else + style.NamespaceIndentation = FormatStyle::NamespaceIndentationKind::NI_None; + + if (settings.indentClassBraces || settings.indentEnumBraces || settings.indentBlockBraces + || settings.indentFunctionBraces) + style.BreakBeforeBraces = FormatStyle::BS_Whitesmiths; + else + style.BreakBeforeBraces = FormatStyle::BS_Custom; + + style.IndentCaseLabels = settings.indentSwitchLabels; +#if LLVM_VERSION_MAJOR >= 11 + style.IndentCaseBlocks = settings.indentBlocksRelativeToSwitchLabels; +#endif + + if (settings.extraPaddingForConditionsIfConfusingAlign) + style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; + else if (settings.alignAssignments) + style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; + else + style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; + + style.DerivePointerAlignment = settings.bindStarToIdentifier || settings.bindStarToTypeName + || settings.bindStarToLeftSpecifier + || settings.bindStarToRightSpecifier; + + if ((settings.bindStarToIdentifier || settings.bindStarToRightSpecifier) + && ClangFormatSettings::instance().mode() == ClangFormatSettings::Mode::Formatting) + style.PointerAlignment = FormatStyle::PAS_Right; + + if ((settings.bindStarToTypeName || settings.bindStarToLeftSpecifier) + && ClangFormatSettings::instance().mode() == ClangFormatSettings::Mode::Formatting) + style.PointerAlignment = FormatStyle::PAS_Left; +} + +void fromTabSettings(clang::format::FormatStyle &style, const TextEditor::TabSettings &settings) +{ + using namespace clang::format; + + style.IndentWidth = settings.m_indentSize; + style.TabWidth = settings.m_tabSize; + + switch (settings.m_tabPolicy) { + case TextEditor::TabSettings::TabPolicy::MixedTabPolicy: + style.UseTab = FormatStyle::UT_ForContinuationAndIndentation; + break; + case TextEditor::TabSettings::TabPolicy::SpacesOnlyTabPolicy: + style.UseTab = FormatStyle::UT_Never; + break; + case TextEditor::TabSettings::TabPolicy::TabsOnlyTabPolicy: + style.UseTab = FormatStyle::UT_Always; + break; + } +} + QString projectUniqueId(ProjectExplorer::Project *project) { if (!project) diff --git a/src/plugins/clangformat/clangformatutils.h b/src/plugins/clangformat/clangformatutils.h index f7d67e7dc83..931ca087fae 100644 --- a/src/plugins/clangformat/clangformatutils.h +++ b/src/plugins/clangformat/clangformatutils.h @@ -14,8 +14,12 @@ #include -namespace TextEditor { class ICodeStylePreferences; } +namespace TextEditor { +class ICodeStylePreferences; +class TabSettings; +} namespace ProjectExplorer { class Project; } +namespace CppEditor { class CppCodeStyleSettings; } namespace ClangFormat { QString projectUniqueId(ProjectExplorer::Project *project); @@ -32,10 +36,15 @@ ClangFormatSettings::Mode getCurrentIndentationOrFormattingSettings(const Utils: Utils::FilePath configForFile(const Utils::FilePath &fileName); Utils::FilePath findConfig(const Utils::FilePath &fileName); +void fromTabSettings(clang::format::FormatStyle &style, const TextEditor::TabSettings &settings); +void fromCppCodeStyleSettings(clang::format::FormatStyle &style, + const CppEditor::CppCodeStyleSettings &settings); + bool getProjectOverriddenSettings(const ProjectExplorer::Project *project); void addQtcStatementMacros(clang::format::FormatStyle &style); clang::format::FormatStyle qtcStyle(); +clang::format::FormatStyle currentQtStyle(const TextEditor::ICodeStylePreferences *codeStyle); Utils::FilePath filePathToCurrentSettings(const TextEditor::ICodeStylePreferences *codeStyle); }