[ClangFormat] Remove redundant checkboxes

- Removed redundant checkboxes
- Removed apply button
- Combined checkboxes to combobox

ToDo:
- Specify behavior for global and project settings

Change-Id: I39a00ac8439ae7be3041890f7fc882849685d102
Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
This commit is contained in:
Artem Sokolovskii
2022-02-16 15:28:16 +01:00
parent ed94e0c066
commit 4022ed547d
4 changed files with 36 additions and 104 deletions

View File

@@ -585,7 +585,7 @@ Utils::Text::Replacements ClangFormatBaseIndenter::indentsFor(QTextBlock startBl
const QByteArray buffer = m_doc->toPlainText().toUtf8();
ReplacementsToKeep replacementsToKeep = ReplacementsToKeep::OnlyIndent;
if (formatWhileTyping()
if (formatCodeInsteadOfIndent()
&& (cursorPositionInEditor == -1 || cursorPositionInEditor >= startBlockPosition)
&& (typedChar == ';' || typedChar == '}')) {
// Format before current position only in case the cursor is inside the indented block.

View File

@@ -60,9 +60,6 @@ using namespace ProjectExplorer;
namespace ClangFormat {
static const char kFileSaveWarning[]
= "Disable formatting on file save in the Beautifier plugin to enable this check";
static bool isBeautifierPluginActivated()
{
const QVector<ExtensionSystem::PluginSpec *> specs = ExtensionSystem::PluginManager::plugins();
@@ -90,6 +87,10 @@ static bool isBeautifierOnSaveActivated()
return activated;
}
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)) {
@@ -99,22 +100,6 @@ bool ClangFormatConfigWidget::eventFilter(QObject *object, QEvent *event)
return QWidget::eventFilter(object, event);
}
void ClangFormatConfigWidget::showEvent(QShowEvent *event)
{
TextEditor::CodeStyleEditorWidget::showEvent(event);
if (isBeautifierOnSaveActivated()) {
bool wasEnabled = m_ui->formatOnSave->isEnabled();
m_ui->formatOnSave->setChecked(false);
m_ui->formatOnSave->setEnabled(false);
m_ui->fileSaveWarning->setText(tr(kFileSaveWarning));
if (wasEnabled)
apply();
} else {
m_ui->formatOnSave->setEnabled(true);
m_ui->fileSaveWarning->setText("");
}
}
ClangFormatConfigWidget::ClangFormatConfigWidget(ProjectExplorer::Project *project, QWidget *parent)
: CppCodeStyleWidget(parent)
, m_project(project)
@@ -130,16 +115,12 @@ ClangFormatConfigWidget::ClangFormatConfigWidget(ProjectExplorer::Project *proje
m_config = std::make_unique<ClangFormatFile>(filePath);
initChecksAndPreview();
showCombobox();
if (m_project) {
m_ui->applyButton->show();
hideGlobalCheckboxes();
m_ui->fallbackConfig->hide();
m_ui->overrideDefault->setChecked(
m_project->namedSettings(Constants::OVERRIDE_FILE_ID).toBool());
} else {
m_ui->applyButton->hide();
showGlobalCheckboxes();
m_ui->overrideDefault->setChecked(ClangFormatSettings::instance().overrideDefaultFile());
m_ui->overrideDefault->setToolTip(
tr("Override Clang Format configuration file with the fallback configuration."));
@@ -180,11 +161,11 @@ void ClangFormatConfigWidget::initChecksAndPreview()
Utils::FilePath fileName;
if (m_project) {
connect(m_ui->applyButton, &QPushButton::clicked, this, &ClangFormatConfigWidget::apply);
fileName = m_project->projectFilePath().pathAppended("snippet.cpp");
} else {
fileName = Core::ICore::userResourcePath("snippet.cpp");
}
m_preview->textDocument()->indenter()->setFileName(fileName);
}
@@ -215,28 +196,22 @@ void ClangFormatConfigWidget::onTableChanged()
saveChanges(sender());
}
void ClangFormatConfigWidget::hideGlobalCheckboxes()
void ClangFormatConfigWidget::showCombobox()
{
m_ui->formatAlways->hide();
m_ui->formatWhileTyping->hide();
m_ui->formatOnSave->hide();
}
m_ui->indentingOrFormatting->insertItem(indentIndex(), tr("Indenting only"));
m_ui->indentingOrFormatting->insertItem(formatIndex(), tr("Full formatting"));
void ClangFormatConfigWidget::showGlobalCheckboxes()
{
m_ui->formatAlways->setChecked(ClangFormatSettings::instance().formatCodeInsteadOfIndent());
m_ui->formatAlways->show();
connect(m_ui->indentingOrFormatting, &QComboBox::currentIndexChanged, this, [this](int) {
if (m_project)
apply();
});
m_ui->formatWhileTyping->setChecked(ClangFormatSettings::instance().formatWhileTyping());
m_ui->formatWhileTyping->show();
if (ClangFormatSettings::instance().formatCodeInsteadOfIndent())
m_ui->indentingOrFormatting->setCurrentIndex(formatIndex());
else
m_ui->indentingOrFormatting->setCurrentIndex(indentIndex());
m_ui->formatOnSave->setChecked(ClangFormatSettings::instance().formatOnSave());
m_ui->formatOnSave->show();
if (isBeautifierOnSaveActivated()) {
m_ui->formatOnSave->setChecked(false);
m_ui->formatOnSave->setEnabled(false);
m_ui->fileSaveWarning->setText(tr(kFileSaveWarning));
}
m_ui->indentingOrFormatting->show();
}
static bool projectConfigExists()
@@ -268,9 +243,7 @@ void ClangFormatConfigWidget::showOrHideWidgets()
m_checksScrollArea->show();
m_preview->show();
if (m_project) {
m_ui->projectHasClangFormat->hide();
} else {
if (!m_project) {
const Project *currentProject = SessionManager::startupProject();
if (!currentProject || !projectConfigExists()) {
m_ui->projectHasClangFormat->hide();
@@ -467,12 +440,16 @@ void ClangFormatConfigWidget::synchronize()
void ClangFormatConfigWidget::apply()
{
ClangFormatSettings &settings = ClangFormatSettings::instance();
if (!m_project) {
settings.setFormatCodeInsteadOfIndent(m_ui->formatAlways->isChecked());
settings.setFormatWhileTyping(m_ui->formatWhileTyping->isChecked());
settings.setFormatOnSave(m_ui->formatOnSave->isChecked());
const bool isFormatting = m_ui->indentingOrFormatting->currentIndex()
== formatIndex();
settings.setFormatCodeInsteadOfIndent(isFormatting);
settings.setOverrideDefaultFile(m_ui->overrideDefault->isChecked());
} else {
if (!isBeautifierOnSaveActivated()) {
settings.setFormatOnSave(isFormatting);
}
if (m_project) {
m_project->setNamedSettings(Constants::OVERRIDE_FILE_ID, m_ui->overrideDefault->isChecked());
}
settings.write();

View File

@@ -60,7 +60,6 @@ private:
void onTableChanged();
bool eventFilter(QObject *object, QEvent *event) override;
void showEvent(QShowEvent *event) override;
void showOrHideWidgets();
void initChecksAndPreview();
@@ -69,8 +68,7 @@ private:
void fillTable();
void saveChanges(QObject *sender);
void hideGlobalCheckboxes();
void showGlobalCheckboxes();
void showCombobox();
void updatePreview();
ProjectExplorer::Project *m_project;

View File

@@ -26,35 +26,17 @@
<property name="bottomMargin">
<number>8</number>
</property>
<item>
<widget class="QCheckBox" name="formatAlways">
<property name="text">
<string>Format instead of indenting</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="formatWhileTyping">
<property name="text">
<string>Format while typing</string>
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_3">
<item>
<widget class="QCheckBox" name="formatOnSave">
<widget class="QLabel" name="formattingModeLabel">
<property name="text">
<string>Format edited code on file save</string>
<string>Formatting mode:</string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="fileSaveWarning">
<property name="text">
<string/>
</property>
</widget>
<widget class="QComboBox" name="indentingOrFormatting"/>
</item>
<item>
<spacer name="horizontalSpacer_2">
@@ -88,37 +70,12 @@
<item>
<widget class="QLabel" name="fallbackConfig">
<property name="text">
<string>Fallback configuration</string>
<string>Clang-Format Style</string>
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_2">
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QPushButton" name="applyButton">
<property name="text">
<string>Apply</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<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>
<layout class="QHBoxLayout" name="horizontalLayout_2"/>
</item>
</layout>
</widget>