forked from qt-creator/qt-creator
ClangFormat: Fix override checkbox doesn't affect behavior
Override checkbox allows use clangFormat settings from widget even if there is .clang-format file in file's folder or parent folder. It works for global settings and for project settings. Change-Id: I03152f4b0e1b62b3ac038024e76cc9082fa37ec3 Reviewed-by: Christian Kandeler <christian.kandeler@qt.io> Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
This commit is contained in:
@@ -25,10 +25,15 @@
|
||||
|
||||
#include "clangformatbaseindenter.h"
|
||||
#include "clangformatconstants.h"
|
||||
#include "clangformatsettings.h"
|
||||
#include "clangformatutils.h"
|
||||
|
||||
#include <coreplugin/icore.h>
|
||||
#include <texteditor/texteditorsettings.h>
|
||||
#include <projectexplorer/editorconfiguration.h>
|
||||
#include <projectexplorer/project.h>
|
||||
#include <projectexplorer/session.h>
|
||||
#include <texteditor/icodestylepreferences.h>
|
||||
#include <texteditor/texteditorsettings.h>
|
||||
|
||||
#include <clang/Tooling/Core/Replacement.h>
|
||||
|
||||
@@ -731,31 +736,43 @@ void ClangFormatBaseIndenter::autoIndent(const QTextCursor &cursor,
|
||||
|
||||
clang::format::FormatStyle ClangFormatBaseIndenter::styleForFile() const
|
||||
{
|
||||
llvm::Expected<clang::format::FormatStyle> style
|
||||
llvm::Expected<clang::format::FormatStyle> styleFromProjectFolder
|
||||
= clang::format::getStyle("file", m_fileName.path().toStdString(), "none");
|
||||
|
||||
if (style) {
|
||||
if (*style == clang::format::getNoStyle()) {
|
||||
Utils::FilePath filePath = filePathToCurrentSettings(
|
||||
TextEditor::TextEditorSettings::codeStyle("Cpp")->currentPreferences());
|
||||
const ProjectExplorer::Project *projectForFile
|
||||
= ProjectExplorer::SessionManager::projectForFile(m_fileName);
|
||||
const bool overrideStyleFile
|
||||
= projectForFile ? projectForFile->namedSettings(Constants::OVERRIDE_FILE_ID).toBool()
|
||||
: ClangFormatSettings::instance().overrideDefaultFile();
|
||||
const TextEditor::ICodeStylePreferences *preferences
|
||||
= projectForFile
|
||||
? projectForFile->editorConfiguration()->codeStyle("Cpp")->currentPreferences()
|
||||
: TextEditor::TextEditorSettings::codeStyle("Cpp")->currentPreferences();
|
||||
|
||||
if (!filePath.exists())
|
||||
return qtcStyle();
|
||||
if (overrideStyleFile || !styleFromProjectFolder
|
||||
|| *styleFromProjectFolder == clang::format::getNoStyle()) {
|
||||
Utils::FilePath filePath = filePathToCurrentSettings(preferences);
|
||||
|
||||
clang::format::FormatStyle style;
|
||||
style.Language = clang::format::FormatStyle::LK_Cpp;
|
||||
const std::error_code error
|
||||
= clang::format::parseConfiguration(filePath.fileContents().toStdString(), &style);
|
||||
QTC_ASSERT(error.value() == static_cast<int>(clang::format::ParseError::Success),
|
||||
return qtcStyle());
|
||||
if (!filePath.exists())
|
||||
return qtcStyle();
|
||||
|
||||
return style;
|
||||
}
|
||||
addQtcStatementMacros(*style);
|
||||
return *style;
|
||||
clang::format::FormatStyle currentSettingsStyle;
|
||||
currentSettingsStyle.Language = clang::format::FormatStyle::LK_Cpp;
|
||||
const std::error_code error
|
||||
= clang::format::parseConfiguration(filePath.fileContents().toStdString(),
|
||||
¤tSettingsStyle);
|
||||
QTC_ASSERT(error.value() == static_cast<int>(clang::format::ParseError::Success),
|
||||
return qtcStyle());
|
||||
|
||||
return currentSettingsStyle;
|
||||
}
|
||||
|
||||
handleAllErrors(style.takeError(), [](const llvm::ErrorInfoBase &) {
|
||||
if (styleFromProjectFolder) {
|
||||
addQtcStatementMacros(*styleFromProjectFolder);
|
||||
return *styleFromProjectFolder;
|
||||
}
|
||||
|
||||
handleAllErrors(styleFromProjectFolder.takeError(), [](const llvm::ErrorInfoBase &) {
|
||||
// do nothing
|
||||
});
|
||||
|
||||
|
||||
@@ -92,7 +92,6 @@ static bool isBeautifierOnSaveActivated()
|
||||
static int indentIndex() { return 0; }
|
||||
static int formatIndex() { return 1; }
|
||||
|
||||
|
||||
bool ClangFormatConfigWidget::eventFilter(QObject *object, QEvent *event)
|
||||
{
|
||||
if (event->type() == QEvent::Wheel && qobject_cast<QComboBox *>(object)) {
|
||||
@@ -111,27 +110,14 @@ ClangFormatConfigWidget::ClangFormatConfigWidget(TextEditor::ICodeStylePreferenc
|
||||
, m_ui(std::make_unique<Ui::ClangFormatConfigWidget>())
|
||||
{
|
||||
m_ui->setupUi(this);
|
||||
setEnabled(!codeStyle->isReadOnly());
|
||||
|
||||
m_config = std::make_unique<ClangFormatFile>(filePathToCurrentSettings(codeStyle),
|
||||
codeStyle->isReadOnly());
|
||||
|
||||
initChecksAndPreview();
|
||||
showCombobox();
|
||||
initChecksAndPreview(!codeStyle->isReadOnly());
|
||||
initOverrideCheckBox();
|
||||
initIndentationOrFormattingCombobox();
|
||||
|
||||
if (m_project) {
|
||||
m_ui->overrideDefault->setChecked(
|
||||
m_project->namedSettings(Constants::OVERRIDE_FILE_ID).toBool());
|
||||
} else {
|
||||
m_ui->overrideDefault->setChecked(ClangFormatSettings::instance().overrideDefaultFile());
|
||||
m_ui->overrideDefault->setToolTip(
|
||||
tr("Override Clang Format configuration file with the fallback configuration."));
|
||||
}
|
||||
|
||||
connect(m_ui->overrideDefault, &QCheckBox::toggled,
|
||||
this, &ClangFormatConfigWidget::showOrHideWidgets);
|
||||
showOrHideWidgets();
|
||||
|
||||
fillTable();
|
||||
updatePreview();
|
||||
|
||||
@@ -140,13 +126,14 @@ ClangFormatConfigWidget::ClangFormatConfigWidget(TextEditor::ICodeStylePreferenc
|
||||
|
||||
ClangFormatConfigWidget::~ClangFormatConfigWidget() = default;
|
||||
|
||||
void ClangFormatConfigWidget::initChecksAndPreview()
|
||||
void ClangFormatConfigWidget::initChecksAndPreview(bool enabled)
|
||||
{
|
||||
m_checksScrollArea = new QScrollArea();
|
||||
m_checksWidget = new QWidget;
|
||||
m_checks->setupUi(m_checksWidget);
|
||||
m_checksScrollArea->setWidget(m_checksWidget);
|
||||
m_checksScrollArea->setMaximumWidth(500);
|
||||
m_checksWidget->setEnabled(enabled);
|
||||
|
||||
m_ui->horizontalLayout_2->addWidget(m_checksScrollArea);
|
||||
|
||||
@@ -171,6 +158,29 @@ void ClangFormatConfigWidget::initChecksAndPreview()
|
||||
m_preview->textDocument()->indenter()->setFileName(fileName);
|
||||
}
|
||||
|
||||
void ClangFormatConfigWidget::initOverrideCheckBox()
|
||||
{
|
||||
if (m_project) {
|
||||
m_ui->overrideDefault->setChecked(
|
||||
m_project->namedSettings(Constants::OVERRIDE_FILE_ID).toBool());
|
||||
} else {
|
||||
m_ui->overrideDefault->setChecked(ClangFormatSettings::instance().overrideDefaultFile());
|
||||
m_ui->overrideDefault->setToolTip(
|
||||
tr("Override Clang Format configuration file with the fallback configuration."));
|
||||
}
|
||||
|
||||
connect(m_ui->overrideDefault, &QCheckBox::toggled,
|
||||
this, &ClangFormatConfigWidget::showOrHideWidgets);
|
||||
connect(m_ui->overrideDefault, &QCheckBox::toggled, this, [this](bool checked) {
|
||||
if (m_project)
|
||||
m_project->setNamedSettings(Constants::OVERRIDE_FILE_ID, checked);
|
||||
else {
|
||||
ClangFormatSettings::instance().setOverrideDefaultFile(checked);
|
||||
ClangFormatSettings::instance().write();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void ClangFormatConfigWidget::connectChecks()
|
||||
{
|
||||
for (QObject *child : m_checksWidget->children()) {
|
||||
@@ -198,7 +208,7 @@ void ClangFormatConfigWidget::onTableChanged()
|
||||
saveChanges(sender());
|
||||
}
|
||||
|
||||
void ClangFormatConfigWidget::showCombobox()
|
||||
void ClangFormatConfigWidget::initIndentationOrFormattingCombobox()
|
||||
{
|
||||
m_ui->indentingOrFormatting->insertItem(indentIndex(), tr("Indenting only"));
|
||||
m_ui->indentingOrFormatting->insertItem(formatIndex(), tr("Full formatting"));
|
||||
@@ -209,6 +219,17 @@ void ClangFormatConfigWidget::showCombobox()
|
||||
m_ui->indentingOrFormatting->setCurrentIndex(indentIndex());
|
||||
|
||||
m_ui->indentingOrFormatting->show();
|
||||
|
||||
connect(m_ui->indentingOrFormatting, &QComboBox::currentIndexChanged, this, [](int index) {
|
||||
ClangFormatSettings &settings = ClangFormatSettings::instance();
|
||||
const bool isFormatting = index == formatIndex();
|
||||
settings.setFormatCodeInsteadOfIndent(isFormatting);
|
||||
|
||||
if (!isBeautifierOnSaveActivated())
|
||||
settings.setFormatOnSave(isFormatting);
|
||||
|
||||
settings.write();
|
||||
});
|
||||
}
|
||||
|
||||
static bool projectConfigExists()
|
||||
@@ -435,22 +456,7 @@ void ClangFormatConfigWidget::synchronize()
|
||||
|
||||
void ClangFormatConfigWidget::apply()
|
||||
{
|
||||
ClangFormatSettings &settings = ClangFormatSettings::instance();
|
||||
const bool isFormatting = m_ui->indentingOrFormatting->currentIndex()
|
||||
== formatIndex();
|
||||
settings.setFormatCodeInsteadOfIndent(isFormatting);
|
||||
settings.setOverrideDefaultFile(m_ui->overrideDefault->isChecked());
|
||||
|
||||
if (!isBeautifierOnSaveActivated()) {
|
||||
settings.setFormatOnSave(isFormatting);
|
||||
}
|
||||
|
||||
if (m_project) {
|
||||
m_project->setNamedSettings(Constants::OVERRIDE_FILE_ID, m_ui->overrideDefault->isChecked());
|
||||
}
|
||||
settings.write();
|
||||
|
||||
if (!m_checksWidget->isVisible())
|
||||
if (!m_checksWidget->isVisible() && !m_checksWidget->isEnabled())
|
||||
return;
|
||||
|
||||
saveChanges(this);
|
||||
|
||||
@@ -63,13 +63,14 @@ private:
|
||||
bool eventFilter(QObject *object, QEvent *event) override;
|
||||
|
||||
void showOrHideWidgets();
|
||||
void initChecksAndPreview();
|
||||
void initChecksAndPreview(bool enabled);
|
||||
void initOverrideCheckBox();
|
||||
void connectChecks();
|
||||
|
||||
void fillTable();
|
||||
void saveChanges(QObject *sender);
|
||||
|
||||
void showCombobox();
|
||||
void initIndentationOrFormattingCombobox();
|
||||
void updatePreview();
|
||||
|
||||
ProjectExplorer::Project *m_project;
|
||||
|
||||
Reference in New Issue
Block a user