diff --git a/src/plugins/clangformat/clangformatbaseindenter.cpp b/src/plugins/clangformat/clangformatbaseindenter.cpp index cd05a5efd24..a0dd39d0ad5 100644 --- a/src/plugins/clangformat/clangformatbaseindenter.cpp +++ b/src/plugins/clangformat/clangformatbaseindenter.cpp @@ -772,7 +772,7 @@ void ClangFormatBaseIndenter::autoIndent(const QTextCursor &cursor, } } -clang::format::FormatStyle ClangFormatBaseIndenter::overrideStyle( +clang::format::FormatStyle ClangFormatBaseIndenter::customSettingsStyle( const Utils::FilePath &fileName) const { const ProjectExplorer::Project *projectForFile @@ -820,8 +820,8 @@ const clang::format::FormatStyle &ClangFormatBaseIndenter::styleForFile() const if (m_cachedStyle.expirationTime > time && !(m_cachedStyle.style == clang::format::getNoStyle())) return m_cachedStyle.style; - if (getCurrentOverriddenSettings(m_fileName)) { - clang::format::FormatStyle style = overrideStyle(m_fileName); + if (getCurrentCustomSettings(m_fileName)) { + clang::format::FormatStyle style = customSettingsStyle(m_fileName); m_cachedStyle.setCache(style, cacheTimeout); return m_cachedStyle.style; } diff --git a/src/plugins/clangformat/clangformatbaseindenter.h b/src/plugins/clangformat/clangformatbaseindenter.h index 69eac8a3ba2..c7dc7e86817 100644 --- a/src/plugins/clangformat/clangformatbaseindenter.h +++ b/src/plugins/clangformat/clangformatbaseindenter.h @@ -87,7 +87,7 @@ private: mutable CachedStyle m_cachedStyle; - clang::format::FormatStyle overrideStyle(const Utils::FilePath &fileName) const; + clang::format::FormatStyle customSettingsStyle(const Utils::FilePath &fileName) const; TextEditor::ICodeStylePreferences *m_overriddenPreferences = nullptr; }; diff --git a/src/plugins/clangformat/clangformatconstants.h b/src/plugins/clangformat/clangformatconstants.h index 572effee21e..905da1e3294 100644 --- a/src/plugins/clangformat/clangformatconstants.h +++ b/src/plugins/clangformat/clangformatconstants.h @@ -9,7 +9,7 @@ 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"; static const char SETTINGS_ID[] = "ClangFormat"; -static const char OVERRIDE_FILE_ID[] = "ClangFormat.OverrideFile"; +static const char USE_CUSTOM_SETTINGS_ID[] = "ClangFormat.OverrideFile"; static const char FORMAT_CODE_ON_SAVE_ID[] = "ClangFormat.FormatCodeOnSave"; static const char FORMAT_WHILE_TYPING_ID[] = "ClangFormat.FormatWhileTyping"; static const char MODE_ID[] = "ClangFormat.Mode"; diff --git a/src/plugins/clangformat/clangformatglobalconfigwidget.cpp b/src/plugins/clangformat/clangformatglobalconfigwidget.cpp index 0898109c1e8..75fa1f91cd8 100644 --- a/src/plugins/clangformat/clangformatglobalconfigwidget.cpp +++ b/src/plugins/clangformat/clangformatglobalconfigwidget.cpp @@ -9,6 +9,7 @@ #include "clangformatutils.h" #include +#include #include #include @@ -19,8 +20,6 @@ #include #include -#include - using namespace ProjectExplorer; using namespace Utils; @@ -43,10 +42,16 @@ ClangFormatGlobalConfigWidget::ClangFormatGlobalConfigWidget( m_indentingOrFormatting = new QComboBox(this); m_formatWhileTyping = new QCheckBox(Tr::tr("Format while typing")); m_formatOnSave = new QCheckBox(Tr::tr("Format edited code on file save")); - m_overrideDefault = new QCheckBox(Tr::tr("Override .clang-format file")); + m_useCustomSettingsCheckBox = new QCheckBox(Tr::tr("Use custom settings")); m_useGlobalSettings = new QCheckBox(Tr::tr("Use global settings")); m_useGlobalSettings->hide(); - m_overrideDefaultFile = ClangFormatSettings::instance().overrideDefaultFile(); + m_useCustomSettings = ClangFormatSettings::instance().useCustomSettings(); + + m_currentProjectLabel = new QLabel( + Tr::tr("Please note that the current project includes a .clang-format file, which will be " + "used for code indenting and formatting.")); + m_currentProjectLabel->setStyleSheet("QLabel { color : red; }"); + m_currentProjectLabel->setWordWrap(true); using namespace Layouting; @@ -64,7 +69,8 @@ ClangFormatGlobalConfigWidget::ClangFormatGlobalConfigWidget( m_formatWhileTyping, m_formatOnSave, m_projectHasClangFormat, - m_overrideDefault + m_useCustomSettingsCheckBox, + m_currentProjectLabel } }; @@ -75,9 +81,10 @@ ClangFormatGlobalConfigWidget::ClangFormatGlobalConfigWidget( initCheckBoxes(); initIndentationOrFormattingCombobox(); - initOverrideCheckBox(); + initCustomSettingsCheckBox(); initUseGlobalSettingsCheckBox(); initFileSizeThresholdSpinBox(); + initCurrentProjectLabel(); if (project) { m_formatOnSave->hide(); @@ -139,7 +146,7 @@ void ClangFormatGlobalConfigWidget::initUseGlobalSettingsCheckBox() isDisabled || (m_indentingOrFormatting->currentIndex() == static_cast(ClangFormatSettings::Mode::Disable))); - m_overrideDefault->setDisabled(isDisabled + m_useCustomSettingsCheckBox->setDisabled(isDisabled || (m_indentingOrFormatting->currentIndex() == static_cast(ClangFormatSettings::Mode::Disable))); }; @@ -173,6 +180,30 @@ void ClangFormatGlobalConfigWidget::initFileSizeThresholdSpinBox() }); } +void ClangFormatGlobalConfigWidget::initCurrentProjectLabel() +{ + auto setCurrentProjectLabelVisible = [this]() { + ProjectExplorer::Project *currentProject + = m_project ? m_project : ProjectExplorer::ProjectTree::currentProject(); + + if (currentProject) { + Utils::FilePath settingsFilePath = currentProject->projectDirectory() + / Constants::SETTINGS_FILE_NAME; + Utils::FilePath settingsAltFilePath = currentProject->projectDirectory() + / Constants::SETTINGS_FILE_ALT_NAME; + + if ((settingsFilePath.exists() || settingsAltFilePath.exists()) + && m_useCustomSettingsCheckBox->checkState() == Qt::CheckState::Unchecked) { + m_currentProjectLabel->show(); + return; + } + } + m_currentProjectLabel->hide(); + }; + setCurrentProjectLabelVisible(); + connect(m_useCustomSettingsCheckBox, &QCheckBox::toggled, this, setCurrentProjectLabelVisible); +} + bool ClangFormatGlobalConfigWidget::projectClangFormatFileExists() { llvm::Expected styleFromProjectFolder @@ -181,7 +212,7 @@ bool ClangFormatGlobalConfigWidget::projectClangFormatFileExists() return styleFromProjectFolder && !(*styleFromProjectFolder == clang::format::getNoStyle()); } -void ClangFormatGlobalConfigWidget::initOverrideCheckBox() +void ClangFormatGlobalConfigWidget::initCustomSettingsCheckBox() { if (!m_project || !projectClangFormatFileExists()) { m_projectHasClangFormat->hide(); @@ -195,24 +226,24 @@ void ClangFormatGlobalConfigWidget::initOverrideCheckBox() if (m_ignoreChanges.isLocked()) return; Utils::GuardLocker locker(m_ignoreChanges); - m_codeStyle->currentPreferences()->setTemporarilyReadOnly(!m_overrideDefault->isChecked()); - m_codeStyle->currentPreferences()->setIsAdditionalTabDisabled(!m_overrideDefault->isEnabled()); + m_codeStyle->currentPreferences()->setTemporarilyReadOnly(!m_useCustomSettingsCheckBox->isChecked()); + m_codeStyle->currentPreferences()->setIsAdditionalTabDisabled(!m_useCustomSettingsCheckBox->isEnabled()); ClangFormatSettings::instance().write(); emit m_codeStyle->currentPreferencesChanged(m_codeStyle->currentPreferences()); }; - auto setEnableOverrideCheckBox = [this, setTemporarilyReadOnly](int index) { + auto setEnableCustomSettingsCheckBox = [this, setTemporarilyReadOnly](int index) { bool isDisable = index == static_cast(ClangFormatSettings::Mode::Disable); - m_overrideDefault->setDisabled(isDisable); + m_useCustomSettingsCheckBox->setDisabled(isDisable); m_projectHasClangFormat->setDisabled(isDisable); setTemporarilyReadOnly(); }; - setEnableOverrideCheckBox(m_indentingOrFormatting->currentIndex()); + setEnableCustomSettingsCheckBox(m_indentingOrFormatting->currentIndex()); connect(m_indentingOrFormatting, &QComboBox::currentIndexChanged, - this, setEnableOverrideCheckBox); + this, setEnableCustomSettingsCheckBox); - m_overrideDefault->setToolTip("" + m_useCustomSettingsCheckBox->setToolTip("" + Tr::tr("When this option is enabled, ClangFormat will use a " "user-specified configuration from the widget below, " "instead of the project .clang-format file. You can " @@ -222,17 +253,20 @@ void ClangFormatGlobalConfigWidget::initOverrideCheckBox() "configuration, and will not modify the project " ".clang-format file.")); - m_overrideDefault->setChecked(getProjectOverriddenSettings(m_project)); + m_useCustomSettingsCheckBox->setChecked(getProjectCustomSettings(m_project)); setTemporarilyReadOnly(); - connect(m_overrideDefault, &QCheckBox::toggled, this, [this, setTemporarilyReadOnly](bool checked) { - if (m_project) { - m_project->setNamedSettings(Constants::OVERRIDE_FILE_ID, checked); - } else { - ClangFormatSettings::instance().setOverrideDefaultFile(checked); - setTemporarilyReadOnly(); - } - }); + connect(m_useCustomSettingsCheckBox, + &QCheckBox::toggled, + this, + [this, setTemporarilyReadOnly](bool checked) { + if (m_project) { + m_project->setNamedSettings(Constants::USE_CUSTOM_SETTINGS_ID, checked); + } else { + ClangFormatSettings::instance().setUseCustomSettings(checked); + setTemporarilyReadOnly(); + } + }); connect(m_codeStyle, &TextEditor::ICodeStylePreferences::currentPreferencesChanged, @@ -249,18 +283,18 @@ void ClangFormatGlobalConfigWidget::apply() if (!m_project) { settings.setMode( static_cast(m_indentingOrFormatting->currentIndex())); - settings.setOverrideDefaultFile(m_overrideDefault->isChecked()); + settings.setUseCustomSettings(m_useCustomSettingsCheckBox->isChecked()); settings.setFileSizeThreshold(m_fileSizeThresholdSpinBox->value()); - m_overrideDefaultFile = m_overrideDefault->isChecked(); + m_useCustomSettings = m_useCustomSettingsCheckBox->isChecked(); } settings.write(); } void ClangFormatGlobalConfigWidget::finish() { - ClangFormatSettings::instance().setOverrideDefaultFile(m_overrideDefaultFile); + ClangFormatSettings::instance().setUseCustomSettings(m_useCustomSettings); m_codeStyle->currentPreferences()->setTemporarilyReadOnly( - !ClangFormatSettings::instance().overrideDefaultFile()); + !ClangFormatSettings::instance().useCustomSettings()); } } // namespace ClangFormat diff --git a/src/plugins/clangformat/clangformatglobalconfigwidget.h b/src/plugins/clangformat/clangformatglobalconfigwidget.h index e4a7be50478..f2d16113881 100644 --- a/src/plugins/clangformat/clangformatglobalconfigwidget.h +++ b/src/plugins/clangformat/clangformatglobalconfigwidget.h @@ -7,8 +7,6 @@ #include -#include - QT_BEGIN_NAMESPACE class QCheckBox; class QComboBox; @@ -36,16 +34,17 @@ public: private: void initCheckBoxes(); void initIndentationOrFormattingCombobox(); - void initOverrideCheckBox(); + void initCustomSettingsCheckBox(); void initUseGlobalSettingsCheckBox(); void initFileSizeThresholdSpinBox(); + void initCurrentProjectLabel(); bool projectClangFormatFileExists(); ProjectExplorer::Project *m_project; TextEditor::ICodeStylePreferences *m_codeStyle; Utils::Guard m_ignoreChanges; - bool m_overrideDefaultFile; + bool m_useCustomSettings; QLabel *m_projectHasClangFormat; QLabel *m_formattingModeLabel; @@ -54,8 +53,9 @@ private: QComboBox *m_indentingOrFormatting; QCheckBox *m_formatWhileTyping; QCheckBox *m_formatOnSave; - QCheckBox *m_overrideDefault; + QCheckBox *m_useCustomSettingsCheckBox; QCheckBox *m_useGlobalSettings; + QLabel *m_currentProjectLabel; }; } // namespace ClangFormat diff --git a/src/plugins/clangformat/clangformatsettings.cpp b/src/plugins/clangformat/clangformatsettings.cpp index 48e34dc573e..2668a90586b 100644 --- a/src/plugins/clangformat/clangformatsettings.cpp +++ b/src/plugins/clangformat/clangformatsettings.cpp @@ -22,7 +22,8 @@ ClangFormatSettings::ClangFormatSettings() { QtcSettings *settings = Core::ICore::settings(); settings->beginGroup(Constants::SETTINGS_ID); - m_overrideDefaultFile = settings->value(Constants::OVERRIDE_FILE_ID, false).toBool(); + m_useCustomSettings + = settings->value(Constants::USE_CUSTOM_SETTINGS_ID, false).toBool(); m_formatWhileTyping = settings->value(Constants::FORMAT_WHILE_TYPING_ID, false).toBool(); m_formatOnSave = settings->value(Constants::FORMAT_CODE_ON_SAVE_ID, false).toBool(); m_fileSizeThreshold = settings->value(Constants::FILE_SIZE_THREDSHOLD, @@ -47,7 +48,7 @@ void ClangFormatSettings::write() const { QtcSettings *settings = Core::ICore::settings(); settings->beginGroup(Constants::SETTINGS_ID); - settings->setValue(Constants::OVERRIDE_FILE_ID, m_overrideDefaultFile); + settings->setValue(Constants::USE_CUSTOM_SETTINGS_ID, m_useCustomSettings); settings->setValue(Constants::FORMAT_WHILE_TYPING_ID, m_formatWhileTyping); settings->setValue(Constants::FORMAT_CODE_ON_SAVE_ID, m_formatOnSave); settings->setValue(Constants::MODE_ID, static_cast(m_mode)); @@ -55,14 +56,14 @@ void ClangFormatSettings::write() const settings->endGroup(); } -void ClangFormatSettings::setOverrideDefaultFile(bool enable) +void ClangFormatSettings::setUseCustomSettings(bool enable) { - m_overrideDefaultFile = enable; + m_useCustomSettings = enable; } -bool ClangFormatSettings::overrideDefaultFile() const +bool ClangFormatSettings::useCustomSettings() const { - return m_overrideDefaultFile; + return m_useCustomSettings; } void ClangFormatSettings::setFormatWhileTyping(bool enable) diff --git a/src/plugins/clangformat/clangformatsettings.h b/src/plugins/clangformat/clangformatsettings.h index b2ecd744688..47d37f4eb0f 100644 --- a/src/plugins/clangformat/clangformatsettings.h +++ b/src/plugins/clangformat/clangformatsettings.h @@ -15,8 +15,8 @@ public: ClangFormatSettings(); void write() const; - void setOverrideDefaultFile(bool enable); - bool overrideDefaultFile() const; + void setUseCustomSettings(bool enable); + bool useCustomSettings() const; enum Mode { Indenting = 0, @@ -38,7 +38,7 @@ public: private: Mode m_mode; - bool m_overrideDefaultFile = false; + bool m_useCustomSettings = false; bool m_formatWhileTyping = false; bool m_formatOnSave = false; int m_fileSizeThreshold = 200; diff --git a/src/plugins/clangformat/clangformatutils.cpp b/src/plugins/clangformat/clangformatutils.cpp index f3e5c8f6749..183d9c9cd4f 100644 --- a/src/plugins/clangformat/clangformatutils.cpp +++ b/src/plugins/clangformat/clangformatutils.cpp @@ -287,24 +287,25 @@ bool getProjectUseGlobalSettings(const ProjectExplorer::Project *project) return projectUseGlobalSettings.isValid() ? projectUseGlobalSettings.toBool() : true; } -bool getProjectOverriddenSettings(const ProjectExplorer::Project *project) +bool getProjectCustomSettings(const ProjectExplorer::Project *project) { - const QVariant projectOverride = project ? project->namedSettings(Constants::OVERRIDE_FILE_ID) - : QVariant(); + const QVariant projectCustomSettings = project ? project->namedSettings( + Constants::USE_CUSTOM_SETTINGS_ID) + : QVariant(); - return projectOverride.isValid() - ? projectOverride.toBool() - : ClangFormatSettings::instance().overrideDefaultFile(); + return projectCustomSettings.isValid() + ? projectCustomSettings.toBool() + : ClangFormatSettings::instance().useCustomSettings(); } -bool getCurrentOverriddenSettings(const Utils::FilePath &filePath) +bool getCurrentCustomSettings(const Utils::FilePath &filePath) { const ProjectExplorer::Project *project = ProjectExplorer::ProjectManager::projectForFile( filePath); return getProjectUseGlobalSettings(project) - ? ClangFormatSettings::instance().overrideDefaultFile() - : getProjectOverriddenSettings(project); + ? ClangFormatSettings::instance().useCustomSettings() + : getProjectCustomSettings(project); } ClangFormatSettings::Mode getProjectIndentationOrFormattingSettings( @@ -348,7 +349,7 @@ Utils::FilePath findConfig(const Utils::FilePath &fileName) Utils::FilePath configForFile(const Utils::FilePath &fileName) { - if (!getCurrentOverriddenSettings(fileName)) + if (!getCurrentCustomSettings(fileName)) return findConfig(fileName); const ProjectExplorer::Project *projectForFile diff --git a/src/plugins/clangformat/clangformatutils.h b/src/plugins/clangformat/clangformatutils.h index 931ca087fae..e59046014f9 100644 --- a/src/plugins/clangformat/clangformatutils.h +++ b/src/plugins/clangformat/clangformatutils.h @@ -26,8 +26,8 @@ QString projectUniqueId(ProjectExplorer::Project *project); bool getProjectUseGlobalSettings(const ProjectExplorer::Project *project); -bool getProjectOverriddenSettings(const ProjectExplorer::Project *project); -bool getCurrentOverriddenSettings(const Utils::FilePath &filePath); +bool getProjectCustomSettings(const ProjectExplorer::Project *project); +bool getCurrentCustomSettings(const Utils::FilePath &filePath); ClangFormatSettings::Mode getProjectIndentationOrFormattingSettings( const ProjectExplorer::Project *project); @@ -40,7 +40,7 @@ void fromTabSettings(clang::format::FormatStyle &style, const TextEditor::TabSet void fromCppCodeStyleSettings(clang::format::FormatStyle &style, const CppEditor::CppCodeStyleSettings &settings); -bool getProjectOverriddenSettings(const ProjectExplorer::Project *project); +bool getProjectCustomSettings(const ProjectExplorer::Project *project); void addQtcStatementMacros(clang::format::FormatStyle &style); clang::format::FormatStyle qtcStyle(); diff --git a/src/plugins/texteditor/codestyleselectorwidget.cpp b/src/plugins/texteditor/codestyleselectorwidget.cpp index 2b21fd12c31..a219be271af 100644 --- a/src/plugins/texteditor/codestyleselectorwidget.cpp +++ b/src/plugins/texteditor/codestyleselectorwidget.cpp @@ -50,7 +50,7 @@ CodeStyleSelectorWidget::CodeStyleSelectorWidget(ICodeStylePreferencesFactory *f Column { Grid { - Tr::tr("Current settings:"), + Tr::tr("Custom settings:"), m_delegateComboBox, copyButton, m_removeButton, @@ -286,6 +286,9 @@ QString CodeStyleSelectorWidget::displayName(ICodeStylePreferences *codeStyle) c name = Tr::tr("%1 [proxy: %2]").arg(name).arg(codeStyle->currentDelegate()->displayName()); if (codeStyle->isReadOnly()) name = Tr::tr("%1 [built-in]").arg(name); + else + name = Tr::tr("%1 [customizable]").arg(name); + return name; }