ClangFormat: Move global settings in main page

The settings: indenting instead formatting, format while
typing, format edited on file save are moved to Code Style
page.

Change-Id: I28787b44602ce288961c778fe704ad2a1a05913f
Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
This commit is contained in:
Artem Sokolovskii
2022-07-28 16:13:35 +02:00
parent e24591dbe5
commit e572bdd90c
14 changed files with 278 additions and 113 deletions

View File

@@ -6,6 +6,7 @@ add_qtc_plugin(ClangFormat
clangformatbaseindenter.cpp clangformatbaseindenter.h clangformatbaseindenter.cpp clangformatbaseindenter.h
clangformatchecks.ui clangformatchecks.ui
clangformatconfigwidget.cpp clangformatconfigwidget.h clangformatconfigwidget.ui clangformatconfigwidget.cpp clangformatconfigwidget.h clangformatconfigwidget.ui
clangformatglobalconfigwidget.cpp clangformatglobalconfigwidget.h clangformatglobalconfigwidget.ui
clangformatconstants.h clangformatconstants.h
clangformatfile.cpp clangformatfile.h clangformatfile.cpp clangformatfile.h
clangformatindenter.cpp clangformatindenter.h clangformatindenter.cpp clangformatindenter.h

View File

@@ -39,6 +39,9 @@ QtcPlugin {
"clangformatconfigwidget.h", "clangformatconfigwidget.h",
"clangformatconfigwidget.ui", "clangformatconfigwidget.ui",
"clangformatconstants.h", "clangformatconstants.h",
"clangformatglobalconfigwidget.cpp",
"clangformatglobalconfigwidget.h",
"clangformatglobalconfigwidget.ui",
"clangformatfile.cpp", "clangformatfile.cpp",
"clangformatfile.h", "clangformatfile.h",
"clangformatindenter.cpp", "clangformatindenter.cpp",

View File

@@ -42,11 +42,8 @@
#include <cppeditor/cpphighlighter.h> #include <cppeditor/cpphighlighter.h>
#include <cppeditor/cppcodestylesettings.h> #include <cppeditor/cppcodestylesettings.h>
#include <cppeditor/cppcodestylesnippets.h> #include <cppeditor/cppcodestylesnippets.h>
#include <extensionsystem/pluginmanager.h>
#include <extensionsystem/pluginspec.h>
#include <projectexplorer/project.h> #include <projectexplorer/project.h>
#include <projectexplorer/session.h> #include <projectexplorer/session.h>
#include <projectexplorer/editorconfiguration.h>
#include <texteditor/displaysettings.h> #include <texteditor/displaysettings.h>
#include <texteditor/icodestylepreferences.h> #include <texteditor/icodestylepreferences.h>
#include <texteditor/snippets/snippeteditor.h> #include <texteditor/snippets/snippeteditor.h>
@@ -54,7 +51,6 @@
#include <texteditor/texteditorsettings.h> #include <texteditor/texteditorsettings.h>
#include <utils/executeondestruction.h> #include <utils/executeondestruction.h>
#include <utils/qtcassert.h> #include <utils/qtcassert.h>
#include <utils/genericconstants.h>
#include <QFile> #include <QFile>
#include <QMessageBox> #include <QMessageBox>
@@ -88,8 +84,6 @@ ClangFormatConfigWidget::ClangFormatConfigWidget(TextEditor::ICodeStylePreferenc
initChecksAndPreview(!codeStyle->isReadOnly()); initChecksAndPreview(!codeStyle->isReadOnly());
initOverrideCheckBox(); initOverrideCheckBox();
initCheckBoxes();
initIndentationOrFormattingCombobox();
showOrHideWidgets(); showOrHideWidgets();
fillTable(); fillTable();
@@ -132,45 +126,6 @@ void ClangFormatConfigWidget::initChecksAndPreview(bool enabled)
m_preview->textDocument()->indenter()->setFileName(fileName); m_preview->textDocument()->indenter()->setFileName(fileName);
} }
void ClangFormatConfigWidget::initCheckBoxes()
{
if (m_project) {
m_ui->formatOnSave->hide();
m_ui->formatWhileTyping->hide();
return;
}
m_ui->formatOnSave->show();
m_ui->formatWhileTyping->show();
auto setEnableCheckBoxes = [this](int index) {
bool isFormatting = index == static_cast<int>(ClangFormatSettings::Mode::Formatting);
m_ui->formatOnSave->setEnabled(isFormatting);
m_ui->formatWhileTyping->setEnabled(isFormatting);
};
setEnableCheckBoxes(m_ui->indentingOrFormatting->currentIndex());
connect(m_ui->indentingOrFormatting, &QComboBox::currentIndexChanged,
this, setEnableCheckBoxes);
m_ui->formatOnSave->setChecked(ClangFormatSettings::instance().formatOnSave());
m_ui->formatWhileTyping->setChecked(ClangFormatSettings::instance().formatWhileTyping());
connect(m_ui->formatOnSave, &QCheckBox::toggled,
this, [](bool checked) {
ClangFormatSettings &settings = ClangFormatSettings::instance();
settings.setFormatOnSave(checked);
settings.write();
});
connect(m_ui->formatWhileTyping, &QCheckBox::toggled,
this, [](bool checked) {
ClangFormatSettings &settings = ClangFormatSettings::instance();
settings.setFormatWhileTyping(checked);
settings.write();
});
}
void ClangFormatConfigWidget::initOverrideCheckBox() void ClangFormatConfigWidget::initOverrideCheckBox()
{ {
if (m_project) { if (m_project) {
@@ -219,29 +174,6 @@ void ClangFormatConfigWidget::onTableChanged()
saveChanges(sender()); saveChanges(sender());
} }
void ClangFormatConfigWidget::initIndentationOrFormattingCombobox()
{
m_ui->indentingOrFormatting->insertItem(static_cast<int>(ClangFormatSettings::Mode::Indenting),
tr("Indenting only"));
m_ui->indentingOrFormatting->insertItem(static_cast<int>(ClangFormatSettings::Mode::Formatting),
tr("Full formatting"));
m_ui->indentingOrFormatting->insertItem(static_cast<int>(ClangFormatSettings::Mode::Disable),
tr("Disable"));
m_ui->indentingOrFormatting->setCurrentIndex(
static_cast<int>(ClangFormatSettings::instance().mode()));
const bool isGlobal = !m_project;
m_ui->indentingOrFormatting->setVisible(isGlobal);
m_ui->formattingModeLabel->setVisible(isGlobal);
connect(m_ui->indentingOrFormatting, &QComboBox::currentIndexChanged, this, [](int index) {
ClangFormatSettings &settings = ClangFormatSettings::instance();
settings.setMode(static_cast<ClangFormatSettings::Mode>(index));
settings.write();
});
}
static bool projectConfigExists() static bool projectConfigExists()
{ {
return Core::ICore::userResourcePath() return Core::ICore::userResourcePath()

View File

@@ -65,13 +65,10 @@ private:
void showOrHideWidgets(); void showOrHideWidgets();
void initChecksAndPreview(bool enabled); void initChecksAndPreview(bool enabled);
void initOverrideCheckBox(); void initOverrideCheckBox();
void initCheckBoxes();
void connectChecks(); void connectChecks();
void fillTable(); void fillTable();
void saveChanges(QObject *sender); void saveChanges(QObject *sender);
void initIndentationOrFormattingCombobox();
void updatePreview(); void updatePreview();
ProjectExplorer::Project *m_project; ProjectExplorer::Project *m_project;

View File

@@ -26,47 +26,6 @@
<property name="bottomMargin"> <property name="bottomMargin">
<number>8</number> <number>8</number>
</property> </property>
<item>
<layout class="QHBoxLayout" name="formattingModeLayout">
<item>
<widget class="QLabel" name="formattingModeLabel">
<property name="text">
<string>Formatting mode:</string>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="indentingOrFormatting"/>
</item>
<item>
<spacer name="formattingModeSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
<item>
<widget class="QCheckBox" name="formatWhileTyping">
<property name="text">
<string>Format while typing</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="formatOnSave">
<property name="text">
<string>Format edited code on file save</string>
</property>
</widget>
</item>
<item> <item>
<widget class="QLabel" name="projectHasClangFormat"> <widget class="QLabel" name="projectHasClangFormat">
<property name="text"> <property name="text">

View File

@@ -0,0 +1,99 @@
/****************************************************************************
**
** Copyright (C) 2022 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
#include "clangformatglobalconfigwidget.h"
#include "clangformatconstants.h"
#include "clangformatsettings.h"
#include "ui_clangformatglobalconfigwidget.h"
#include <projectexplorer/project.h>
#include <sstream>
using namespace ProjectExplorer;
namespace ClangFormat {
ClangFormatGlobalConfigWidget::ClangFormatGlobalConfigWidget(ProjectExplorer::Project *project,
QWidget *parent)
: CppCodeStyleWidget(parent)
, m_ui(std::make_unique<Ui::ClangFormatGlobalConfigWidget>())
{
m_ui->setupUi(this);
initCheckBoxes();
initIndentationOrFormattingCombobox();
if (project) {
m_ui->settingsGroupBox->hide();
return;
}
m_ui->settingsGroupBox->show();
}
ClangFormatGlobalConfigWidget::~ClangFormatGlobalConfigWidget() = default;
void ClangFormatGlobalConfigWidget::initCheckBoxes()
{
auto setEnableCheckBoxes = [this](int index) {
bool isFormatting = index == static_cast<int>(ClangFormatSettings::Mode::Formatting);
m_ui->formatOnSave->setEnabled(isFormatting);
m_ui->formatWhileTyping->setEnabled(isFormatting);
};
setEnableCheckBoxes(m_ui->indentingOrFormatting->currentIndex());
connect(m_ui->indentingOrFormatting, &QComboBox::currentIndexChanged,
this, setEnableCheckBoxes);
m_ui->formatOnSave->setChecked(ClangFormatSettings::instance().formatOnSave());
m_ui->formatWhileTyping->setChecked(ClangFormatSettings::instance().formatWhileTyping());
}
void ClangFormatGlobalConfigWidget::initIndentationOrFormattingCombobox()
{
m_ui->indentingOrFormatting->insertItem(static_cast<int>(ClangFormatSettings::Mode::Indenting),
tr("Indenting only"));
m_ui->indentingOrFormatting->insertItem(static_cast<int>(ClangFormatSettings::Mode::Formatting),
tr("Full formatting"));
m_ui->indentingOrFormatting->insertItem(static_cast<int>(ClangFormatSettings::Mode::Disable),
tr("Disable"));
m_ui->indentingOrFormatting->setCurrentIndex(
static_cast<int>(ClangFormatSettings::instance().mode()));
}
void ClangFormatGlobalConfigWidget::apply()
{
ClangFormatSettings &settings = ClangFormatSettings::instance();
settings.setFormatOnSave(m_ui->formatOnSave->isChecked());
settings.setFormatWhileTyping(m_ui->formatWhileTyping->isChecked());
settings.setMode(static_cast<ClangFormatSettings::Mode>(m_ui->indentingOrFormatting->currentIndex()));
settings.write();
}
} // namespace ClangFormat

View File

@@ -0,0 +1,55 @@
/****************************************************************************
**
** Copyright (C) 2022 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
#pragma once
#include <cppeditor/cppcodestylesettingspage.h>
#include <memory>
namespace ProjectExplorer { class Project; }
namespace ClangFormat {
namespace Ui { class ClangFormatGlobalConfigWidget; }
class ClangFormatGlobalConfigWidget : public CppEditor::CppCodeStyleWidget
{
Q_OBJECT
public:
explicit ClangFormatGlobalConfigWidget(ProjectExplorer::Project *project = nullptr,
QWidget *parent = nullptr);
~ClangFormatGlobalConfigWidget() override;
void apply() override;
private:
void initCheckBoxes();
void initIndentationOrFormattingCombobox();
std::unique_ptr<Ui::ClangFormatGlobalConfigWidget> m_ui;
};
} // namespace ClangFormat

View File

@@ -0,0 +1,89 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>ClangFormat::ClangFormatGlobalConfigWidget</class>
<widget class="QWidget" name="ClangFormat::ClangFormatGlobalConfigWidget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>489</width>
<height>145</height>
</rect>
</property>
<property name="windowTitle">
<string/>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<property name="leftMargin">
<number>8</number>
</property>
<property name="topMargin">
<number>8</number>
</property>
<property name="rightMargin">
<number>8</number>
</property>
<property name="bottomMargin">
<number>8</number>
</property>
<item>
<widget class="QGroupBox" name="settingsGroupBox">
<property name="layoutDirection">
<enum>Qt::LeftToRight</enum>
</property>
<property name="title">
<string>ClangFormat global setting:</string>
</property>
<property name="alignment">
<set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter</set>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<layout class="QHBoxLayout" name="formattingModeLayout">
<item>
<widget class="QLabel" name="formattingModeLabel">
<property name="text">
<string>Formatting mode:</string>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="indentingOrFormatting"/>
</item>
<item>
<spacer name="formattingModeSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
<item>
<widget class="QCheckBox" name="formatWhileTyping">
<property name="text">
<string>Format while typing</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="formatOnSave">
<property name="text">
<string>Format edited code on file save</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>

View File

@@ -26,6 +26,7 @@
#include "clangformatplugin.h" #include "clangformatplugin.h"
#include "clangformatconfigwidget.h" #include "clangformatconfigwidget.h"
#include "clangformatglobalconfigwidget.h"
#include "clangformatconstants.h" #include "clangformatconstants.h"
#include "clangformatindenter.h" #include "clangformatindenter.h"
#include "clangformatsettings.h" #include "clangformatsettings.h"
@@ -95,6 +96,12 @@ public:
{ {
return {new ClangFormatConfigWidget(codeStyle, project, parent), tr("ClangFormat")}; return {new ClangFormatConfigWidget(codeStyle, project, parent), tr("ClangFormat")};
} }
TextEditor::CodeStyleEditorWidget *createAdditionalGlobalSettings(
ProjectExplorer::Project *project, QWidget *parent)
{
return new ClangFormatGlobalConfigWidget(project, parent);
}
}; };
static void replaceCppCodeStyle() static void replaceCppCodeStyle()

View File

@@ -349,7 +349,8 @@ void CppCodeStylePreferencesWidget::addTab(CppCodeStyleWidget *page, QString tab
if (!page) if (!page)
return; return;
m_ui->categoryTab->addTab(page, tabName); m_ui->categoryTab->insertTab(0, page, tabName);
m_ui->categoryTab->setCurrentIndex(0);
connect(page, &CppEditor::CppCodeStyleWidget::codeStyleSettingsChanged, connect(page, &CppEditor::CppCodeStyleWidget::codeStyleSettingsChanged,
this, [this](const CppEditor::CppCodeStyleSettings &settings) { this, [this](const CppEditor::CppCodeStyleSettings &settings) {

View File

@@ -66,6 +66,10 @@ CodeStyleEditor::CodeStyleEditor(ICodeStylePreferencesFactory *factory,
font.setItalic(true); font.setItalic(true);
label->setFont(font); label->setFont(font);
label->setWordWrap(true); label->setWordWrap(true);
m_additionalGlobalSettingsWidget = factory->createAdditionalGlobalSettings(project, parent);
if (m_additionalGlobalSettingsWidget)
m_layout->addWidget(m_additionalGlobalSettingsWidget);
m_layout->addWidget(selector); m_layout->addWidget(selector);
m_layout->addWidget(m_preview); m_layout->addWidget(m_preview);
m_layout->addWidget(label); m_layout->addWidget(label);
@@ -98,3 +102,11 @@ void CodeStyleEditor::updatePreview()
} }
tc.endEditBlock(); tc.endEditBlock();
} }
void CodeStyleEditor::apply()
{
if (!m_additionalGlobalSettingsWidget)
return;
m_additionalGlobalSettingsWidget->apply();
}

View File

@@ -49,6 +49,7 @@ public:
ProjectExplorer::Project *project = nullptr, ProjectExplorer::Project *project = nullptr,
QWidget *parent = nullptr); QWidget *parent = nullptr);
void apply() override;
private: private:
void updatePreview(); void updatePreview();
@@ -56,6 +57,7 @@ private:
ICodeStylePreferencesFactory *m_factory; ICodeStylePreferencesFactory *m_factory;
ICodeStylePreferences *m_codeStyle; ICodeStylePreferences *m_codeStyle;
SnippetEditorWidget *m_preview; SnippetEditorWidget *m_preview;
CodeStyleEditorWidget *m_additionalGlobalSettingsWidget;
}; };
} // namespace TextEditor } // namespace TextEditor

View File

@@ -38,3 +38,9 @@ CodeStyleEditorWidget *ICodeStylePreferencesFactory::createCodeStyleEditor(
{ {
return new CodeStyleEditor(this, codeStyle, project, parent); return new CodeStyleEditor(this, codeStyle, project, parent);
} }
CodeStyleEditorWidget *ICodeStylePreferencesFactory::createAdditionalGlobalSettings(
ProjectExplorer::Project *, QWidget *)
{
return nullptr;
}

View File

@@ -61,6 +61,8 @@ public:
virtual CodeStyleEditorWidget *createCodeStyleEditor(ICodeStylePreferences *codeStyle, virtual CodeStyleEditorWidget *createCodeStyleEditor(ICodeStylePreferences *codeStyle,
ProjectExplorer::Project *project = nullptr, ProjectExplorer::Project *project = nullptr,
QWidget *parent = nullptr); QWidget *parent = nullptr);
virtual CodeStyleEditorWidget *createAdditionalGlobalSettings(
ProjectExplorer::Project *project = nullptr, QWidget *parent = nullptr);
virtual Utils::Id languageId() = 0; virtual Utils::Id languageId() = 0;
virtual QString displayName() = 0; virtual QString displayName() = 0;
virtual ICodeStylePreferences *createCodeStyle() const = 0; virtual ICodeStylePreferences *createCodeStyle() const = 0;