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);
|
= ProjectExplorer::ProjectManager::projectForFile(fileName);
|
||||||
|
|
||||||
const ICodeStylePreferences *preferences
|
const ICodeStylePreferences *preferences
|
||||||
= projectForFile
|
= !getProjectUseGlobalSettings(projectForFile) && projectForFile
|
||||||
? projectForFile->editorConfiguration()->codeStyle("Cpp")->currentPreferences()
|
? projectForFile->editorConfiguration()->codeStyle("Cpp")->currentPreferences()
|
||||||
: TextEditorSettings::codeStyle("Cpp")->currentPreferences();
|
: TextEditorSettings::codeStyle("Cpp")->currentPreferences();
|
||||||
|
|
||||||
|
@@ -52,7 +52,7 @@ public:
|
|||||||
|
|
||||||
const clang::format::FormatStyle &styleForFile() const;
|
const clang::format::FormatStyle &styleForFile() const;
|
||||||
|
|
||||||
void setOverriddenPreferences(TextEditor::ICodeStylePreferences *preferences);
|
void setOverriddenPreferences(TextEditor::ICodeStylePreferences *preferences) final;
|
||||||
void setOverriddenStyle(const clang::format::FormatStyle &style);
|
void setOverriddenStyle(const clang::format::FormatStyle &style);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
@@ -6,6 +6,7 @@
|
|||||||
#include "clangformatconfigwidget.h"
|
#include "clangformatconfigwidget.h"
|
||||||
#include "clangformatconstants.h"
|
#include "clangformatconstants.h"
|
||||||
#include "clangformatfile.h"
|
#include "clangformatfile.h"
|
||||||
|
#include "clangformatglobalconfigwidget.h"
|
||||||
#include "clangformatindenter.h"
|
#include "clangformatindenter.h"
|
||||||
#include "clangformatsettings.h"
|
#include "clangformatsettings.h"
|
||||||
#include "clangformattr.h"
|
#include "clangformattr.h"
|
||||||
@@ -13,11 +14,13 @@
|
|||||||
|
|
||||||
#include <cppeditor/cppcodestylepreferencesfactory.h>
|
#include <cppeditor/cppcodestylepreferencesfactory.h>
|
||||||
#include <cppeditor/cppcodestylesettingspage.h>
|
#include <cppeditor/cppcodestylesettingspage.h>
|
||||||
|
#include <cppeditor/cppcodestylesnippets.h>
|
||||||
#include <cppeditor/cppeditorconstants.h>
|
#include <cppeditor/cppeditorconstants.h>
|
||||||
|
|
||||||
#include <projectexplorer/project.h>
|
#include <projectexplorer/project.h>
|
||||||
#include <projectexplorer/projecttree.h>
|
#include <projectexplorer/projecttree.h>
|
||||||
|
|
||||||
|
#include <texteditor/codestyleeditor.h>
|
||||||
#include <texteditor/codestylepool.h>
|
#include <texteditor/codestylepool.h>
|
||||||
#include <texteditor/codestyleselectorwidget.h>
|
#include <texteditor/codestyleselectorwidget.h>
|
||||||
#include <texteditor/icodestylepreferences.h>
|
#include <texteditor/icodestylepreferences.h>
|
||||||
@@ -202,9 +205,12 @@ void ClangFormatGlobalConfigWidget::initUseGlobalSettingsCheckBox()
|
|||||||
isDisabled
|
isDisabled
|
||||||
|| (m_indentingOrFormatting->currentIndex()
|
|| (m_indentingOrFormatting->currentIndex()
|
||||||
== static_cast<int>(ClangFormatSettings::Mode::Disable)));
|
== static_cast<int>(ClangFormatSettings::Mode::Disable)));
|
||||||
|
m_useCustomSettingsCheckBox->setChecked(getProjectCustomSettings(m_project));
|
||||||
m_useCustomSettingsCheckBox->setDisabled(isDisabled
|
m_useCustomSettingsCheckBox->setDisabled(isDisabled
|
||||||
|| (m_indentingOrFormatting->currentIndex()
|
|| (m_indentingOrFormatting->currentIndex()
|
||||||
== static_cast<int>(ClangFormatSettings::Mode::Disable)));
|
== static_cast<int>(ClangFormatSettings::Mode::Disable)));
|
||||||
|
|
||||||
|
emit m_codeStyle->currentPreferencesChanged(m_codeStyle->currentPreferences());
|
||||||
};
|
};
|
||||||
|
|
||||||
m_useGlobalSettings->setChecked(getProjectUseGlobalSettings(m_project));
|
m_useGlobalSettings->setChecked(getProjectUseGlobalSettings(m_project));
|
||||||
@@ -298,10 +304,7 @@ void ClangFormatGlobalConfigWidget::initCustomSettingsCheckBox()
|
|||||||
setTemporarilyReadOnly();
|
setTemporarilyReadOnly();
|
||||||
};
|
};
|
||||||
|
|
||||||
setEnableCustomSettingsCheckBox(m_indentingOrFormatting->currentIndex());
|
m_useCustomSettingsCheckBox->setChecked(getProjectCustomSettings(m_project));
|
||||||
connect(m_indentingOrFormatting, &QComboBox::currentIndexChanged,
|
|
||||||
this, setEnableCustomSettingsCheckBox);
|
|
||||||
|
|
||||||
m_useCustomSettingsCheckBox->setToolTip("<html>"
|
m_useCustomSettingsCheckBox->setToolTip("<html>"
|
||||||
+ Tr::tr("When this option is enabled, ClangFormat will use a "
|
+ Tr::tr("When this option is enabled, ClangFormat will use a "
|
||||||
"user-specified configuration from the widget below, "
|
"user-specified configuration from the widget below, "
|
||||||
@@ -312,9 +315,12 @@ void ClangFormatGlobalConfigWidget::initCustomSettingsCheckBox()
|
|||||||
"configuration, and will not modify the project "
|
"configuration, and will not modify the project "
|
||||||
".clang-format file."));
|
".clang-format file."));
|
||||||
|
|
||||||
m_useCustomSettingsCheckBox->setChecked(getProjectCustomSettings(m_project));
|
|
||||||
setTemporarilyReadOnly();
|
setTemporarilyReadOnly();
|
||||||
|
|
||||||
|
setEnableCustomSettingsCheckBox(m_indentingOrFormatting->currentIndex());
|
||||||
|
connect(m_indentingOrFormatting, &QComboBox::currentIndexChanged,
|
||||||
|
this, setEnableCustomSettingsCheckBox);
|
||||||
|
|
||||||
connect(m_useCustomSettingsCheckBox,
|
connect(m_useCustomSettingsCheckBox,
|
||||||
&QCheckBox::toggled,
|
&QCheckBox::toggled,
|
||||||
this,
|
this,
|
||||||
@@ -434,6 +440,11 @@ public:
|
|||||||
{
|
{
|
||||||
return new ClangFormatSelectorWidget(this, project, parent);
|
return new ClangFormatSelectorWidget(this, project, parent);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QString previewText() const override
|
||||||
|
{
|
||||||
|
return QLatin1String(CppEditor::Constants::DEFAULT_CODE_STYLE_SNIPPETS[0]);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
void setupClangFormatStyleFactory(QObject *guard)
|
void setupClangFormatStyleFactory(QObject *guard)
|
||||||
|
@@ -313,9 +313,9 @@ ClangFormatSettings::Mode getCurrentIndentationOrFormattingSettings(const Utils:
|
|||||||
: getProjectIndentationOrFormattingSettings(project);
|
: 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()) {
|
while (parentDirectory.exists()) {
|
||||||
Utils::FilePath settingsFilePath = parentDirectory / Constants::SETTINGS_FILE_NAME;
|
Utils::FilePath settingsFilePath = parentDirectory / Constants::SETTINGS_FILE_NAME;
|
||||||
if (settingsFilePath.exists())
|
if (settingsFilePath.exists())
|
||||||
@@ -330,19 +330,22 @@ Utils::FilePath findConfig(const Utils::FilePath &fileName)
|
|||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
Utils::FilePath configForFile(const Utils::FilePath &fileName)
|
ICodeStylePreferences *preferencesForFile(const Utils::FilePath &filePath)
|
||||||
{
|
{
|
||||||
if (!getCurrentCustomSettings(fileName))
|
const ProjectExplorer::Project *project = ProjectExplorer::ProjectManager::projectForFile(
|
||||||
return findConfig(fileName);
|
filePath);
|
||||||
|
|
||||||
const ProjectExplorer::Project *projectForFile
|
return !getProjectUseGlobalSettings(project) && project
|
||||||
= ProjectExplorer::ProjectManager::projectForFile(fileName);
|
? project->editorConfiguration()->codeStyle("Cpp")->currentPreferences()
|
||||||
|
: TextEditor::TextEditorSettings::codeStyle("Cpp")->currentPreferences();
|
||||||
|
}
|
||||||
|
|
||||||
const TextEditor::ICodeStylePreferences *preferences
|
Utils::FilePath configForFile(const Utils::FilePath &filePath)
|
||||||
= projectForFile
|
{
|
||||||
? projectForFile->editorConfiguration()->codeStyle("Cpp")->currentPreferences()
|
if (!getCurrentCustomSettings(filePath))
|
||||||
: TextEditor::TextEditorSettings::codeStyle("Cpp")->currentPreferences();
|
return findConfig(filePath);
|
||||||
|
|
||||||
|
const TextEditor::ICodeStylePreferences *preferences = preferencesForFile(filePath);
|
||||||
return filePathToCurrentSettings(preferences);
|
return filePathToCurrentSettings(preferences);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -32,8 +32,9 @@ ClangFormatSettings::Mode getProjectIndentationOrFormattingSettings(
|
|||||||
const ProjectExplorer::Project *project);
|
const ProjectExplorer::Project *project);
|
||||||
ClangFormatSettings::Mode getCurrentIndentationOrFormattingSettings(const Utils::FilePath &filePath);
|
ClangFormatSettings::Mode getCurrentIndentationOrFormattingSettings(const Utils::FilePath &filePath);
|
||||||
|
|
||||||
Utils::FilePath configForFile(const Utils::FilePath &fileName);
|
TextEditor::ICodeStylePreferences *preferencesForFile(const Utils::FilePath &filePath);
|
||||||
Utils::FilePath findConfig(const Utils::FilePath &fileName);
|
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 fromTabSettings(clang::format::FormatStyle &style, const TextEditor::TabSettings &settings);
|
||||||
void fromCppCodeStyleSettings(clang::format::FormatStyle &style,
|
void fromCppCodeStyleSettings(clang::format::FormatStyle &style,
|
||||||
|
@@ -626,6 +626,8 @@ public:
|
|||||||
void finish() final
|
void finish() final
|
||||||
{
|
{
|
||||||
m_codeStyleEditor->finish();
|
m_codeStyleEditor->finish();
|
||||||
|
const auto codeStyle = CppToolsSettings::cppCodeStyle();
|
||||||
|
emit codeStyle->currentPreferencesChanged(codeStyle->currentPreferences());
|
||||||
}
|
}
|
||||||
|
|
||||||
std::unique_ptr<CppCodeStylePreferences> m_pageCppCodeStylePreferences;
|
std::unique_ptr<CppCodeStylePreferences> m_pageCppCodeStylePreferences;
|
||||||
|
@@ -14,6 +14,10 @@
|
|||||||
#include "snippets/snippeteditor.h"
|
#include "snippets/snippeteditor.h"
|
||||||
#include "snippets/snippetprovider.h"
|
#include "snippets/snippetprovider.h"
|
||||||
|
|
||||||
|
#include <coreplugin/icore.h>
|
||||||
|
|
||||||
|
#include <projectexplorer/project.h>
|
||||||
|
|
||||||
#include <QVBoxLayout>
|
#include <QVBoxLayout>
|
||||||
#include <QTextBlock>
|
#include <QTextBlock>
|
||||||
#include <QLabel>
|
#include <QLabel>
|
||||||
@@ -72,9 +76,19 @@ CodeStyleEditor::CodeStyleEditor(ICodeStylePreferencesFactory *factory,
|
|||||||
this, &CodeStyleEditor::updatePreview);
|
this, &CodeStyleEditor::updatePreview);
|
||||||
connect(codeStyle, &ICodeStylePreferences::currentPreferencesChanged,
|
connect(codeStyle, &ICodeStylePreferences::currentPreferencesChanged,
|
||||||
this, &CodeStyleEditor::updatePreview);
|
this, &CodeStyleEditor::updatePreview);
|
||||||
m_preview->setCodeStyle(m_codeStyle);
|
|
||||||
m_preview->setPlainText(factory->previewText());
|
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();
|
updatePreview();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -46,6 +46,9 @@ public:
|
|||||||
|
|
||||||
virtual void invalidateCache() {}
|
virtual void invalidateCache() {}
|
||||||
|
|
||||||
|
// needed for preview in project mode
|
||||||
|
virtual void setOverriddenPreferences(ICodeStylePreferences */*preferences*/) {}
|
||||||
|
|
||||||
virtual int indentFor(const QTextBlock & /*block*/,
|
virtual int indentFor(const QTextBlock & /*block*/,
|
||||||
const TabSettings & /*tabSettings*/,
|
const TabSettings & /*tabSettings*/,
|
||||||
int /*cursorPositionInEditor*/ = -1)
|
int /*cursorPositionInEditor*/ = -1)
|
||||||
|
Reference in New Issue
Block a user