forked from qt-creator/qt-creator
ClangFormat: Fix project preview
Make project preview reactive to project/global settings. Fixed bug that project settings used despite use global settings checkbox is checked. Change-Id: I86aac060dc006f3514695916be6ad9792fc0cca6 Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
This commit is contained in:
@@ -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();
|
||||
|
||||
|
@@ -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:
|
||||
|
@@ -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 <cppeditor/cppcodestylepreferencesfactory.h>
|
||||
#include <cppeditor/cppcodestylesettingspage.h>
|
||||
#include <cppeditor/cppcodestylesnippets.h>
|
||||
#include <cppeditor/cppeditorconstants.h>
|
||||
|
||||
#include <projectexplorer/project.h>
|
||||
#include <projectexplorer/projecttree.h>
|
||||
|
||||
#include <texteditor/codestyleeditor.h>
|
||||
#include <texteditor/codestylepool.h>
|
||||
#include <texteditor/codestyleselectorwidget.h>
|
||||
#include <texteditor/icodestylepreferences.h>
|
||||
@@ -202,9 +205,12 @@ void ClangFormatGlobalConfigWidget::initUseGlobalSettingsCheckBox()
|
||||
isDisabled
|
||||
|| (m_indentingOrFormatting->currentIndex()
|
||||
== static_cast<int>(ClangFormatSettings::Mode::Disable)));
|
||||
m_useCustomSettingsCheckBox->setChecked(getProjectCustomSettings(m_project));
|
||||
m_useCustomSettingsCheckBox->setDisabled(isDisabled
|
||||
|| (m_indentingOrFormatting->currentIndex()
|
||||
== static_cast<int>(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("<html>"
|
||||
+ 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)
|
||||
|
@@ -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);
|
||||
}
|
||||
|
||||
|
@@ -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,
|
||||
|
@@ -626,6 +626,8 @@ public:
|
||||
void finish() final
|
||||
{
|
||||
m_codeStyleEditor->finish();
|
||||
const auto codeStyle = CppToolsSettings::cppCodeStyle();
|
||||
emit codeStyle->currentPreferencesChanged(codeStyle->currentPreferences());
|
||||
}
|
||||
|
||||
std::unique_ptr<CppCodeStylePreferences> m_pageCppCodeStylePreferences;
|
||||
|
@@ -14,6 +14,10 @@
|
||||
#include "snippets/snippeteditor.h"
|
||||
#include "snippets/snippetprovider.h"
|
||||
|
||||
#include <coreplugin/icore.h>
|
||||
|
||||
#include <projectexplorer/project.h>
|
||||
|
||||
#include <QVBoxLayout>
|
||||
#include <QTextBlock>
|
||||
#include <QLabel>
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
|
@@ -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)
|
||||
|
Reference in New Issue
Block a user