diff --git a/src/plugins/clangformat/clangformatbaseindenter.cpp b/src/plugins/clangformat/clangformatbaseindenter.cpp index ee89619225c..6506252d62b 100644 --- a/src/plugins/clangformat/clangformatbaseindenter.cpp +++ b/src/plugins/clangformat/clangformatbaseindenter.cpp @@ -913,7 +913,7 @@ clang::format::FormatStyle ClangFormatBaseIndenterPrivate::customSettingsStyle( = ProjectExplorer::ProjectManager::projectForFile(fileName); const ICodeStylePreferences *preferences - = projectForFile + = !getProjectUseGlobalSettings(projectForFile) && projectForFile ? projectForFile->editorConfiguration()->codeStyle("Cpp")->currentPreferences() : TextEditorSettings::codeStyle("Cpp")->currentPreferences(); diff --git a/src/plugins/clangformat/clangformatbaseindenter.h b/src/plugins/clangformat/clangformatbaseindenter.h index e6512f78621..8d72bd4e0fe 100644 --- a/src/plugins/clangformat/clangformatbaseindenter.h +++ b/src/plugins/clangformat/clangformatbaseindenter.h @@ -52,7 +52,7 @@ public: const clang::format::FormatStyle &styleForFile() const; - void setOverriddenPreferences(TextEditor::ICodeStylePreferences *preferences); + void setOverriddenPreferences(TextEditor::ICodeStylePreferences *preferences) final; void setOverriddenStyle(const clang::format::FormatStyle &style); protected: diff --git a/src/plugins/clangformat/clangformatglobalconfigwidget.cpp b/src/plugins/clangformat/clangformatglobalconfigwidget.cpp index 6df5cff4699..7134ecd2e37 100644 --- a/src/plugins/clangformat/clangformatglobalconfigwidget.cpp +++ b/src/plugins/clangformat/clangformatglobalconfigwidget.cpp @@ -6,6 +6,7 @@ #include "clangformatconfigwidget.h" #include "clangformatconstants.h" #include "clangformatfile.h" +#include "clangformatglobalconfigwidget.h" #include "clangformatindenter.h" #include "clangformatsettings.h" #include "clangformattr.h" @@ -13,11 +14,13 @@ #include #include +#include #include #include #include +#include #include #include #include @@ -202,9 +205,12 @@ void ClangFormatGlobalConfigWidget::initUseGlobalSettingsCheckBox() isDisabled || (m_indentingOrFormatting->currentIndex() == static_cast(ClangFormatSettings::Mode::Disable))); + m_useCustomSettingsCheckBox->setChecked(getProjectCustomSettings(m_project)); m_useCustomSettingsCheckBox->setDisabled(isDisabled || (m_indentingOrFormatting->currentIndex() == static_cast(ClangFormatSettings::Mode::Disable))); + + emit m_codeStyle->currentPreferencesChanged(m_codeStyle->currentPreferences()); }; m_useGlobalSettings->setChecked(getProjectUseGlobalSettings(m_project)); @@ -298,10 +304,7 @@ void ClangFormatGlobalConfigWidget::initCustomSettingsCheckBox() setTemporarilyReadOnly(); }; - setEnableCustomSettingsCheckBox(m_indentingOrFormatting->currentIndex()); - connect(m_indentingOrFormatting, &QComboBox::currentIndexChanged, - this, setEnableCustomSettingsCheckBox); - + m_useCustomSettingsCheckBox->setChecked(getProjectCustomSettings(m_project)); m_useCustomSettingsCheckBox->setToolTip("" + Tr::tr("When this option is enabled, ClangFormat will use a " "user-specified configuration from the widget below, " @@ -312,9 +315,12 @@ void ClangFormatGlobalConfigWidget::initCustomSettingsCheckBox() "configuration, and will not modify the project " ".clang-format file.")); - m_useCustomSettingsCheckBox->setChecked(getProjectCustomSettings(m_project)); setTemporarilyReadOnly(); + setEnableCustomSettingsCheckBox(m_indentingOrFormatting->currentIndex()); + connect(m_indentingOrFormatting, &QComboBox::currentIndexChanged, + this, setEnableCustomSettingsCheckBox); + connect(m_useCustomSettingsCheckBox, &QCheckBox::toggled, this, @@ -434,6 +440,11 @@ public: { return new ClangFormatSelectorWidget(this, project, parent); } + + QString previewText() const override + { + return QLatin1String(CppEditor::Constants::DEFAULT_CODE_STYLE_SNIPPETS[0]); + } }; void setupClangFormatStyleFactory(QObject *guard) diff --git a/src/plugins/clangformat/clangformatutils.cpp b/src/plugins/clangformat/clangformatutils.cpp index b2d256bed2d..469cab688ef 100644 --- a/src/plugins/clangformat/clangformatutils.cpp +++ b/src/plugins/clangformat/clangformatutils.cpp @@ -313,9 +313,9 @@ ClangFormatSettings::Mode getCurrentIndentationOrFormattingSettings(const Utils: : getProjectIndentationOrFormattingSettings(project); } -Utils::FilePath findConfig(const Utils::FilePath &fileName) +Utils::FilePath findConfig(const Utils::FilePath &filePath) { - Utils::FilePath parentDirectory = fileName.parentDir(); + Utils::FilePath parentDirectory = filePath.parentDir(); while (parentDirectory.exists()) { Utils::FilePath settingsFilePath = parentDirectory / Constants::SETTINGS_FILE_NAME; if (settingsFilePath.exists()) @@ -330,19 +330,22 @@ Utils::FilePath findConfig(const Utils::FilePath &fileName) return {}; } -Utils::FilePath configForFile(const Utils::FilePath &fileName) +ICodeStylePreferences *preferencesForFile(const Utils::FilePath &filePath) { - if (!getCurrentCustomSettings(fileName)) - return findConfig(fileName); + const ProjectExplorer::Project *project = ProjectExplorer::ProjectManager::projectForFile( + filePath); - const ProjectExplorer::Project *projectForFile - = ProjectExplorer::ProjectManager::projectForFile(fileName); + return !getProjectUseGlobalSettings(project) && project + ? project->editorConfiguration()->codeStyle("Cpp")->currentPreferences() + : TextEditor::TextEditorSettings::codeStyle("Cpp")->currentPreferences(); +} - const TextEditor::ICodeStylePreferences *preferences - = projectForFile - ? projectForFile->editorConfiguration()->codeStyle("Cpp")->currentPreferences() - : TextEditor::TextEditorSettings::codeStyle("Cpp")->currentPreferences(); +Utils::FilePath configForFile(const Utils::FilePath &filePath) +{ + if (!getCurrentCustomSettings(filePath)) + return findConfig(filePath); + const TextEditor::ICodeStylePreferences *preferences = preferencesForFile(filePath); return filePathToCurrentSettings(preferences); } diff --git a/src/plugins/clangformat/clangformatutils.h b/src/plugins/clangformat/clangformatutils.h index 45373df3235..121a71d9acf 100644 --- a/src/plugins/clangformat/clangformatutils.h +++ b/src/plugins/clangformat/clangformatutils.h @@ -32,8 +32,9 @@ ClangFormatSettings::Mode getProjectIndentationOrFormattingSettings( const ProjectExplorer::Project *project); ClangFormatSettings::Mode getCurrentIndentationOrFormattingSettings(const Utils::FilePath &filePath); -Utils::FilePath configForFile(const Utils::FilePath &fileName); -Utils::FilePath findConfig(const Utils::FilePath &fileName); +TextEditor::ICodeStylePreferences *preferencesForFile(const Utils::FilePath &filePath); +Utils::FilePath configForFile(const Utils::FilePath &filePath); +Utils::FilePath findConfig(const Utils::FilePath &filePath); void fromTabSettings(clang::format::FormatStyle &style, const TextEditor::TabSettings &settings); void fromCppCodeStyleSettings(clang::format::FormatStyle &style, diff --git a/src/plugins/cppeditor/cppcodestylesettingspage.cpp b/src/plugins/cppeditor/cppcodestylesettingspage.cpp index 2364dee8145..55c3e5b8235 100644 --- a/src/plugins/cppeditor/cppcodestylesettingspage.cpp +++ b/src/plugins/cppeditor/cppcodestylesettingspage.cpp @@ -626,6 +626,8 @@ public: void finish() final { m_codeStyleEditor->finish(); + const auto codeStyle = CppToolsSettings::cppCodeStyle(); + emit codeStyle->currentPreferencesChanged(codeStyle->currentPreferences()); } std::unique_ptr m_pageCppCodeStylePreferences; diff --git a/src/plugins/texteditor/codestyleeditor.cpp b/src/plugins/texteditor/codestyleeditor.cpp index 98e36bf5984..c740505895b 100644 --- a/src/plugins/texteditor/codestyleeditor.cpp +++ b/src/plugins/texteditor/codestyleeditor.cpp @@ -14,6 +14,10 @@ #include "snippets/snippeteditor.h" #include "snippets/snippetprovider.h" +#include + +#include + #include #include #include @@ -72,9 +76,19 @@ CodeStyleEditor::CodeStyleEditor(ICodeStylePreferencesFactory *factory, this, &CodeStyleEditor::updatePreview); connect(codeStyle, &ICodeStylePreferences::currentPreferencesChanged, this, &CodeStyleEditor::updatePreview); - m_preview->setCodeStyle(m_codeStyle); m_preview->setPlainText(factory->previewText()); + Indenter *indenter = factory->createIndenter(m_preview->document()); + if (indenter) { + indenter->setOverriddenPreferences(codeStyle); + Utils::FilePath fileName = project ? project->projectFilePath().pathAppended("snippet.cpp") + : Core::ICore::userResourcePath("snippet.cpp"); + indenter->setFileName(fileName); + m_preview->textDocument()->setIndenter(indenter); + } else { + m_preview->setCodeStyle(codeStyle); + } + updatePreview(); } diff --git a/src/plugins/texteditor/indenter.h b/src/plugins/texteditor/indenter.h index 4cd542a9efd..645150fc6e6 100644 --- a/src/plugins/texteditor/indenter.h +++ b/src/plugins/texteditor/indenter.h @@ -46,6 +46,9 @@ public: virtual void invalidateCache() {} + // needed for preview in project mode + virtual void setOverriddenPreferences(ICodeStylePreferences */*preferences*/) {} + virtual int indentFor(const QTextBlock & /*block*/, const TabSettings & /*tabSettings*/, int /*cursorPositionInEditor*/ = -1)