2018-08-29 15:58:13 +02:00
|
|
|
/****************************************************************************
|
|
|
|
|
**
|
|
|
|
|
** Copyright (C) 2018 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 "clangformatconfigwidget.h"
|
2018-10-11 14:11:43 +02:00
|
|
|
|
2018-11-13 09:29:09 +01:00
|
|
|
#include "clangformatconstants.h"
|
2019-02-22 09:48:46 +01:00
|
|
|
#include "clangformatindenter.h"
|
2019-01-28 07:54:05 +01:00
|
|
|
#include "clangformatsettings.h"
|
2018-10-11 14:11:43 +02:00
|
|
|
#include "clangformatutils.h"
|
2019-04-24 14:44:31 +02:00
|
|
|
#include "ui_clangformatchecks.h"
|
2018-08-29 15:58:13 +02:00
|
|
|
#include "ui_clangformatconfigwidget.h"
|
|
|
|
|
|
|
|
|
|
#include <clang/Format/Format.h>
|
|
|
|
|
|
|
|
|
|
#include <coreplugin/icore.h>
|
2019-02-22 09:48:46 +01:00
|
|
|
#include <cppeditor/cpphighlighter.h>
|
2021-08-30 10:58:08 +02:00
|
|
|
#include <cppeditor/cppcodestylesnippets.h>
|
2019-04-18 16:13:32 +02:00
|
|
|
#include <extensionsystem/pluginmanager.h>
|
|
|
|
|
#include <extensionsystem/pluginspec.h>
|
2018-08-29 15:58:13 +02:00
|
|
|
#include <projectexplorer/project.h>
|
|
|
|
|
#include <projectexplorer/session.h>
|
2019-02-22 09:48:46 +01:00
|
|
|
#include <texteditor/displaysettings.h>
|
|
|
|
|
#include <texteditor/snippets/snippeteditor.h>
|
|
|
|
|
#include <texteditor/textdocument.h>
|
|
|
|
|
#include <texteditor/texteditorsettings.h>
|
2019-04-25 10:40:08 +02:00
|
|
|
#include <utils/executeondestruction.h>
|
2019-04-24 14:44:31 +02:00
|
|
|
#include <utils/qtcassert.h>
|
2019-04-18 16:13:32 +02:00
|
|
|
#include <utils/genericconstants.h>
|
2018-08-29 15:58:13 +02:00
|
|
|
|
|
|
|
|
#include <QFile>
|
2019-02-21 10:26:08 +01:00
|
|
|
#include <QMessageBox>
|
2018-08-29 15:58:13 +02:00
|
|
|
|
|
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
|
|
using namespace ProjectExplorer;
|
|
|
|
|
|
|
|
|
|
namespace ClangFormat {
|
|
|
|
|
|
2019-04-18 16:13:32 +02:00
|
|
|
static const char kFileSaveWarning[]
|
|
|
|
|
= "Disable formatting on file save in the Beautifier plugin to enable this check";
|
|
|
|
|
|
|
|
|
|
static bool isBeautifierPluginActivated()
|
|
|
|
|
{
|
2019-05-27 14:12:11 +02:00
|
|
|
const QVector<ExtensionSystem::PluginSpec *> specs = ExtensionSystem::PluginManager::plugins();
|
2019-04-18 16:13:32 +02:00
|
|
|
return std::find_if(specs.begin(),
|
|
|
|
|
specs.end(),
|
|
|
|
|
[](ExtensionSystem::PluginSpec *spec) {
|
|
|
|
|
return spec->name() == "Beautifier";
|
|
|
|
|
})
|
|
|
|
|
!= specs.end();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool isBeautifierOnSaveActivated()
|
|
|
|
|
{
|
|
|
|
|
if (!isBeautifierPluginActivated())
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
QSettings *s = Core::ICore::settings();
|
|
|
|
|
bool activated = false;
|
|
|
|
|
s->beginGroup(Utils::Constants::BEAUTIFIER_SETTINGS_GROUP);
|
|
|
|
|
s->beginGroup(Utils::Constants::BEAUTIFIER_GENERAL_GROUP);
|
|
|
|
|
if (s->value(Utils::Constants::BEAUTIFIER_AUTO_FORMAT_ON_SAVE, false).toBool())
|
|
|
|
|
activated = true;
|
|
|
|
|
s->endGroup();
|
|
|
|
|
s->endGroup();
|
|
|
|
|
return activated;
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-24 14:44:31 +02:00
|
|
|
bool ClangFormatConfigWidget::eventFilter(QObject *object, QEvent *event)
|
|
|
|
|
{
|
|
|
|
|
if (event->type() == QEvent::Wheel && qobject_cast<QComboBox *>(object)) {
|
|
|
|
|
event->ignore();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return QWidget::eventFilter(object, event);
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-18 16:13:32 +02:00
|
|
|
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("");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-21 15:00:49 +01:00
|
|
|
ClangFormatConfigWidget::ClangFormatConfigWidget(ProjectExplorer::Project *project, QWidget *parent)
|
2019-01-22 14:16:30 +01:00
|
|
|
: CodeStyleEditorWidget(parent)
|
2018-08-29 15:58:13 +02:00
|
|
|
, m_project(project)
|
2019-04-24 14:44:31 +02:00
|
|
|
, m_checks(std::make_unique<Ui::ClangFormatChecksWidget>())
|
2018-09-12 12:57:59 +02:00
|
|
|
, m_ui(std::make_unique<Ui::ClangFormatConfigWidget>())
|
2018-08-29 15:58:13 +02:00
|
|
|
{
|
|
|
|
|
m_ui->setupUi(this);
|
|
|
|
|
|
2019-04-25 10:40:08 +02:00
|
|
|
initChecksAndPreview();
|
2019-04-24 14:44:31 +02:00
|
|
|
|
2019-03-05 13:12:44 +01:00
|
|
|
if (m_project) {
|
|
|
|
|
m_ui->applyButton->show();
|
|
|
|
|
hideGlobalCheckboxes();
|
2019-03-15 14:30:51 +01:00
|
|
|
m_ui->fallbackConfig->hide();
|
2019-03-05 13:12:44 +01:00
|
|
|
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());
|
2019-03-15 14:30:51 +01:00
|
|
|
m_ui->overrideDefault->setToolTip(
|
|
|
|
|
tr("Override Clang Format configuration file with the fallback configuration."));
|
2019-03-05 13:12:44 +01:00
|
|
|
}
|
|
|
|
|
|
2019-09-11 08:22:53 +03:00
|
|
|
connect(m_ui->overrideDefault, &QCheckBox::toggled,
|
|
|
|
|
this, &ClangFormatConfigWidget::showOrHideWidgets);
|
2019-04-26 10:49:48 +02:00
|
|
|
showOrHideWidgets();
|
|
|
|
|
|
|
|
|
|
fillTable();
|
|
|
|
|
updatePreview();
|
2019-04-25 10:40:08 +02:00
|
|
|
|
|
|
|
|
connectChecks();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ClangFormatConfigWidget::initChecksAndPreview()
|
|
|
|
|
{
|
|
|
|
|
m_checksScrollArea = new QScrollArea();
|
|
|
|
|
m_checksWidget = new QWidget;
|
|
|
|
|
m_checks->setupUi(m_checksWidget);
|
|
|
|
|
m_checksScrollArea->setWidget(m_checksWidget);
|
|
|
|
|
m_checksScrollArea->setMaximumWidth(500);
|
|
|
|
|
|
|
|
|
|
m_ui->horizontalLayout_2->addWidget(m_checksScrollArea);
|
|
|
|
|
|
|
|
|
|
m_preview = new TextEditor::SnippetEditorWidget(this);
|
|
|
|
|
m_ui->horizontalLayout_2->addWidget(m_preview);
|
2019-04-26 10:49:48 +02:00
|
|
|
|
|
|
|
|
TextEditor::DisplaySettings displaySettings = m_preview->displaySettings();
|
|
|
|
|
displaySettings.m_visualizeWhitespace = true;
|
|
|
|
|
m_preview->setDisplaySettings(displaySettings);
|
2021-08-30 10:58:08 +02:00
|
|
|
m_preview->setPlainText(QLatin1String(CppEditor::Constants::DEFAULT_CODE_STYLE_SNIPPETS[0]));
|
2019-04-26 10:49:48 +02:00
|
|
|
m_preview->textDocument()->setIndenter(new ClangFormatIndenter(m_preview->document()));
|
|
|
|
|
m_preview->textDocument()->setFontSettings(TextEditor::TextEditorSettings::fontSettings());
|
|
|
|
|
m_preview->textDocument()->setSyntaxHighlighter(new CppEditor::CppHighlighter);
|
|
|
|
|
|
2019-05-28 13:49:26 +02:00
|
|
|
Utils::FilePath fileName;
|
2019-04-26 10:49:48 +02:00
|
|
|
if (m_project) {
|
|
|
|
|
connect(m_ui->applyButton, &QPushButton::clicked, this, &ClangFormatConfigWidget::apply);
|
2019-05-16 14:49:56 +02:00
|
|
|
fileName = m_project->projectFilePath().pathAppended("snippet.cpp");
|
2019-04-26 10:49:48 +02:00
|
|
|
} else {
|
2021-04-26 15:46:09 +02:00
|
|
|
fileName = Core::ICore::userResourcePath("snippet.cpp");
|
2019-04-26 10:49:48 +02:00
|
|
|
}
|
|
|
|
|
m_preview->textDocument()->indenter()->setFileName(fileName);
|
2019-04-25 10:40:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ClangFormatConfigWidget::connectChecks()
|
|
|
|
|
{
|
|
|
|
|
for (QObject *child : m_checksWidget->children()) {
|
|
|
|
|
auto comboBox = qobject_cast<QComboBox *>(child);
|
|
|
|
|
if (comboBox != nullptr) {
|
|
|
|
|
connect(comboBox,
|
2019-04-29 07:58:56 +02:00
|
|
|
QOverload<int>::of(&QComboBox::currentIndexChanged),
|
2019-04-25 10:40:08 +02:00
|
|
|
this,
|
|
|
|
|
&ClangFormatConfigWidget::onTableChanged);
|
|
|
|
|
comboBox->installEventFilter(this);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto button = qobject_cast<QPushButton *>(child);
|
|
|
|
|
if (button != nullptr)
|
|
|
|
|
connect(button, &QPushButton::clicked, this, &ClangFormatConfigWidget::onTableChanged);
|
|
|
|
|
}
|
2018-10-11 14:11:43 +02:00
|
|
|
}
|
|
|
|
|
|
2019-04-24 14:44:31 +02:00
|
|
|
void ClangFormatConfigWidget::onTableChanged()
|
|
|
|
|
{
|
2019-04-25 10:40:08 +02:00
|
|
|
if (m_disableTableUpdate)
|
|
|
|
|
return;
|
|
|
|
|
|
2019-04-24 14:44:31 +02:00
|
|
|
const std::string newConfig = tableToString(sender());
|
|
|
|
|
if (newConfig.empty())
|
|
|
|
|
return;
|
2019-04-25 10:40:08 +02:00
|
|
|
const std::string oldConfig = m_project ? currentProjectConfigText()
|
|
|
|
|
: currentGlobalConfigText();
|
2019-04-24 14:44:31 +02:00
|
|
|
saveConfig(newConfig);
|
|
|
|
|
fillTable();
|
|
|
|
|
updatePreview();
|
|
|
|
|
saveConfig(oldConfig);
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-28 08:13:33 +01:00
|
|
|
void ClangFormatConfigWidget::hideGlobalCheckboxes()
|
|
|
|
|
{
|
|
|
|
|
m_ui->formatAlways->hide();
|
|
|
|
|
m_ui->formatWhileTyping->hide();
|
2019-01-28 12:25:36 +01:00
|
|
|
m_ui->formatOnSave->hide();
|
2019-01-28 08:13:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ClangFormatConfigWidget::showGlobalCheckboxes()
|
|
|
|
|
{
|
|
|
|
|
m_ui->formatAlways->setChecked(ClangFormatSettings::instance().formatCodeInsteadOfIndent());
|
|
|
|
|
m_ui->formatAlways->show();
|
|
|
|
|
|
|
|
|
|
m_ui->formatWhileTyping->setChecked(ClangFormatSettings::instance().formatWhileTyping());
|
|
|
|
|
m_ui->formatWhileTyping->show();
|
2019-01-28 12:25:36 +01:00
|
|
|
|
|
|
|
|
m_ui->formatOnSave->setChecked(ClangFormatSettings::instance().formatOnSave());
|
|
|
|
|
m_ui->formatOnSave->show();
|
2019-04-18 16:13:32 +02:00
|
|
|
if (isBeautifierOnSaveActivated()) {
|
|
|
|
|
m_ui->formatOnSave->setChecked(false);
|
|
|
|
|
m_ui->formatOnSave->setEnabled(false);
|
|
|
|
|
m_ui->fileSaveWarning->setText(tr(kFileSaveWarning));
|
|
|
|
|
}
|
2019-01-28 08:13:33 +01:00
|
|
|
}
|
|
|
|
|
|
2019-03-05 13:12:44 +01:00
|
|
|
static bool projectConfigExists()
|
|
|
|
|
{
|
2021-04-22 16:15:26 +02:00
|
|
|
return Core::ICore::userResourcePath()
|
2019-05-20 17:13:51 +02:00
|
|
|
.pathAppended("clang-format")
|
|
|
|
|
.pathAppended(currentProjectUniqueId())
|
2021-04-22 16:15:26 +02:00
|
|
|
.pathAppended(Constants::SETTINGS_FILE_NAME)
|
2019-03-05 13:12:44 +01:00
|
|
|
.exists();
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-26 10:49:48 +02:00
|
|
|
void ClangFormatConfigWidget::showOrHideWidgets()
|
2018-10-11 14:11:43 +02:00
|
|
|
{
|
2019-03-05 13:12:44 +01:00
|
|
|
m_ui->projectHasClangFormat->hide();
|
2018-10-11 14:11:43 +02:00
|
|
|
|
2018-11-20 11:23:30 +01:00
|
|
|
QLayoutItem *lastItem = m_ui->verticalLayout->itemAt(m_ui->verticalLayout->count() - 1);
|
2018-12-05 09:07:10 +01:00
|
|
|
if (lastItem->spacerItem())
|
2018-11-20 11:23:30 +01:00
|
|
|
m_ui->verticalLayout->removeItem(lastItem);
|
|
|
|
|
|
2019-03-15 14:30:51 +01:00
|
|
|
if (!m_ui->overrideDefault->isChecked() && m_project) {
|
|
|
|
|
// Show the fallback configuration only globally.
|
2019-04-25 10:40:08 +02:00
|
|
|
m_checksScrollArea->hide();
|
2019-03-15 14:30:51 +01:00
|
|
|
m_preview->hide();
|
|
|
|
|
m_ui->verticalLayout->addStretch(1);
|
|
|
|
|
return;
|
2018-08-29 15:58:13 +02:00
|
|
|
}
|
|
|
|
|
|
2019-04-26 10:49:48 +02:00
|
|
|
createStyleFileIfNeeded(!m_project);
|
2019-04-25 10:40:08 +02:00
|
|
|
m_checksScrollArea->show();
|
2019-03-05 13:12:44 +01:00
|
|
|
m_preview->show();
|
2018-10-11 14:11:43 +02:00
|
|
|
|
2018-08-29 15:58:13 +02:00
|
|
|
if (m_project) {
|
2018-10-11 14:11:43 +02:00
|
|
|
m_ui->projectHasClangFormat->hide();
|
2018-08-29 15:58:13 +02:00
|
|
|
} else {
|
2018-10-11 14:11:43 +02:00
|
|
|
const Project *currentProject = SessionManager::startupProject();
|
2019-03-05 13:12:44 +01:00
|
|
|
if (!currentProject || !projectConfigExists()) {
|
2018-10-11 14:11:43 +02:00
|
|
|
m_ui->projectHasClangFormat->hide();
|
|
|
|
|
} else {
|
2019-03-15 14:31:43 +01:00
|
|
|
m_ui->projectHasClangFormat->show();
|
2018-10-11 14:11:43 +02:00
|
|
|
m_ui->projectHasClangFormat->setText(
|
2019-03-05 13:12:44 +01:00
|
|
|
tr("Current project has its own overridden .clang-format file "
|
2019-02-21 15:00:49 +01:00
|
|
|
"and can be configured in Projects > Code Style > C++."));
|
2018-10-11 14:11:43 +02:00
|
|
|
}
|
2018-08-29 15:58:13 +02:00
|
|
|
}
|
2019-02-22 09:48:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ClangFormatConfigWidget::updatePreview()
|
|
|
|
|
{
|
|
|
|
|
QTextCursor cursor(m_preview->document());
|
|
|
|
|
cursor.setPosition(0);
|
|
|
|
|
cursor.movePosition(QTextCursor::End, QTextCursor::KeepAnchor);
|
2019-11-18 10:38:35 +01:00
|
|
|
m_preview->textDocument()->autoIndent(cursor);
|
2018-10-11 14:11:43 +02:00
|
|
|
}
|
|
|
|
|
|
2019-04-24 14:44:31 +02:00
|
|
|
static inline void ltrim(std::string &s)
|
|
|
|
|
{
|
|
|
|
|
s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](int ch) { return !std::isspace(ch); }));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline void rtrim(std::string &s)
|
|
|
|
|
{
|
|
|
|
|
s.erase(std::find_if(s.rbegin(), s.rend(), [](int ch) { return !std::isspace(ch); }).base(),
|
|
|
|
|
s.end());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline void trim(std::string &s)
|
|
|
|
|
{
|
|
|
|
|
ltrim(s);
|
|
|
|
|
rtrim(s);
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-25 10:40:08 +02:00
|
|
|
static void fillPlainText(QPlainTextEdit *plainText, const std::string &text, size_t index)
|
|
|
|
|
{
|
|
|
|
|
if (index == std::string::npos) {
|
|
|
|
|
plainText->setPlainText("");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
size_t valueStart = text.find('\n', index + 1);
|
|
|
|
|
size_t valueEnd;
|
|
|
|
|
std::string value;
|
|
|
|
|
QTC_ASSERT(valueStart != std::string::npos, return;);
|
|
|
|
|
do {
|
|
|
|
|
valueEnd = text.find('\n', valueStart + 1);
|
|
|
|
|
if (valueEnd == std::string::npos)
|
|
|
|
|
break;
|
|
|
|
|
// Skip also 2 spaces - start with valueStart + 1 + 2.
|
|
|
|
|
std::string line = text.substr(valueStart + 3, valueEnd - valueStart - 3);
|
|
|
|
|
rtrim(line);
|
|
|
|
|
value += value.empty() ? line : '\n' + line;
|
|
|
|
|
valueStart = valueEnd;
|
|
|
|
|
} while (valueEnd < text.size() - 1 && text.at(valueEnd + 1) == ' ');
|
|
|
|
|
plainText->setPlainText(QString::fromStdString(value));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void fillComboBoxOrLineEdit(QObject *object, const std::string &text, size_t index)
|
|
|
|
|
{
|
|
|
|
|
auto *comboBox = qobject_cast<QComboBox *>(object);
|
|
|
|
|
auto *lineEdit = qobject_cast<QLineEdit *>(object);
|
|
|
|
|
if (index == std::string::npos) {
|
|
|
|
|
if (comboBox)
|
|
|
|
|
comboBox->setCurrentIndex(0);
|
|
|
|
|
else
|
|
|
|
|
lineEdit->setText("");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const size_t valueStart = text.find(':', index + 1);
|
|
|
|
|
QTC_ASSERT(valueStart != std::string::npos, return;);
|
|
|
|
|
const size_t valueEnd = text.find('\n', valueStart + 1);
|
|
|
|
|
QTC_ASSERT(valueEnd != std::string::npos, return;);
|
|
|
|
|
std::string value = text.substr(valueStart + 1, valueEnd - valueStart - 1);
|
|
|
|
|
trim(value);
|
|
|
|
|
|
|
|
|
|
if (comboBox)
|
|
|
|
|
comboBox->setCurrentText(QString::fromStdString(value));
|
|
|
|
|
else
|
|
|
|
|
lineEdit->setText(QString::fromStdString(value));
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-08 10:35:23 +01:00
|
|
|
void ClangFormatConfigWidget::fillTable()
|
2018-10-11 14:11:43 +02:00
|
|
|
{
|
2019-04-25 10:40:08 +02:00
|
|
|
Utils::ExecuteOnDestruction executeOnDestruction([this]() { m_disableTableUpdate = false; });
|
|
|
|
|
m_disableTableUpdate = true;
|
2018-08-29 15:58:13 +02:00
|
|
|
|
2019-04-25 10:40:08 +02:00
|
|
|
const std::string configText = m_project ? currentProjectConfigText()
|
|
|
|
|
: currentGlobalConfigText();
|
2019-04-24 14:44:31 +02:00
|
|
|
|
|
|
|
|
for (QObject *child : m_checksWidget->children()) {
|
|
|
|
|
if (!qobject_cast<QComboBox *>(child) && !qobject_cast<QLineEdit *>(child)
|
|
|
|
|
&& !qobject_cast<QPlainTextEdit *>(child)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-25 10:40:08 +02:00
|
|
|
size_t index = configText.find('\n' + child->objectName().toStdString());
|
|
|
|
|
if (index == std::string::npos)
|
|
|
|
|
index = configText.find("\n " + child->objectName().toStdString());
|
2019-04-24 14:44:31 +02:00
|
|
|
|
2019-04-25 10:40:08 +02:00
|
|
|
if (qobject_cast<QPlainTextEdit *>(child))
|
|
|
|
|
fillPlainText(qobject_cast<QPlainTextEdit *>(child), configText, index);
|
|
|
|
|
else
|
|
|
|
|
fillComboBoxOrLineEdit(child, configText, index);
|
2019-04-24 14:44:31 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string ClangFormatConfigWidget::tableToString(QObject *sender)
|
|
|
|
|
{
|
|
|
|
|
std::stringstream content;
|
|
|
|
|
content << "---";
|
|
|
|
|
|
|
|
|
|
if (sender->objectName() == "BasedOnStyle") {
|
|
|
|
|
auto *basedOnStyle = m_checksWidget->findChild<QComboBox *>("BasedOnStyle");
|
|
|
|
|
content << "\nBasedOnStyle: " << basedOnStyle->currentText().toStdString() << '\n';
|
|
|
|
|
} else {
|
|
|
|
|
for (QObject *child : m_checksWidget->children()) {
|
|
|
|
|
auto *label = qobject_cast<QLabel *>(child);
|
|
|
|
|
if (!label)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
QWidget *valueWidget = m_checksWidget->findChild<QWidget *>(label->text().trimmed());
|
|
|
|
|
if (!valueWidget) {
|
|
|
|
|
// Currently BraceWrapping only.
|
|
|
|
|
content << '\n' << label->text().toStdString() << ":";
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!qobject_cast<QComboBox *>(valueWidget) && !qobject_cast<QLineEdit *>(valueWidget)
|
|
|
|
|
&& !qobject_cast<QPlainTextEdit *>(valueWidget)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto *plainText = qobject_cast<QPlainTextEdit *>(valueWidget);
|
|
|
|
|
if (plainText) {
|
|
|
|
|
if (plainText->toPlainText().trimmed().isEmpty())
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
content << '\n' << label->text().toStdString() << ":";
|
|
|
|
|
QStringList list = plainText->toPlainText().split('\n');
|
|
|
|
|
for (const QString &line : list)
|
|
|
|
|
content << "\n " << line.toStdString();
|
|
|
|
|
} else {
|
|
|
|
|
auto *comboBox = qobject_cast<QComboBox *>(valueWidget);
|
|
|
|
|
std::string text;
|
|
|
|
|
if (comboBox) {
|
|
|
|
|
text = comboBox->currentText().toStdString();
|
|
|
|
|
} else {
|
|
|
|
|
auto *lineEdit = qobject_cast<QLineEdit *>(valueWidget);
|
|
|
|
|
QTC_ASSERT(lineEdit, continue;);
|
|
|
|
|
text = lineEdit->text().toStdString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!text.empty() && text != "Default")
|
|
|
|
|
content << '\n' << label->text().toStdString() << ": " << text;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
content << '\n';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string text = content.str();
|
|
|
|
|
clang::format::FormatStyle style;
|
|
|
|
|
style.Language = clang::format::FormatStyle::LK_Cpp;
|
|
|
|
|
const std::error_code error = clang::format::parseConfiguration(text, &style);
|
|
|
|
|
if (error.value() != static_cast<int>(clang::format::ParseError::Success)) {
|
|
|
|
|
QMessageBox::warning(this,
|
|
|
|
|
tr("Error in ClangFormat configuration"),
|
|
|
|
|
QString::fromStdString(error.message()));
|
|
|
|
|
fillTable();
|
|
|
|
|
updatePreview();
|
|
|
|
|
return std::string();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return text;
|
2018-08-29 15:58:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ClangFormatConfigWidget::~ClangFormatConfigWidget() = default;
|
|
|
|
|
|
|
|
|
|
void ClangFormatConfigWidget::apply()
|
|
|
|
|
{
|
2019-03-05 13:12:44 +01:00
|
|
|
ClangFormatSettings &settings = ClangFormatSettings::instance();
|
2019-01-28 07:54:05 +01:00
|
|
|
if (!m_project) {
|
|
|
|
|
settings.setFormatCodeInsteadOfIndent(m_ui->formatAlways->isChecked());
|
2019-01-28 08:13:33 +01:00
|
|
|
settings.setFormatWhileTyping(m_ui->formatWhileTyping->isChecked());
|
2019-01-28 12:25:36 +01:00
|
|
|
settings.setFormatOnSave(m_ui->formatOnSave->isChecked());
|
2019-03-05 13:12:44 +01:00
|
|
|
settings.setOverrideDefaultFile(m_ui->overrideDefault->isChecked());
|
|
|
|
|
} else {
|
|
|
|
|
m_project->setNamedSettings(Constants::OVERRIDE_FILE_ID, m_ui->overrideDefault->isChecked());
|
2019-01-28 07:54:05 +01:00
|
|
|
}
|
2019-03-05 13:12:44 +01:00
|
|
|
settings.write();
|
2019-01-28 07:54:05 +01:00
|
|
|
|
2019-04-24 14:44:31 +02:00
|
|
|
if (!m_checksWidget->isVisible())
|
2019-03-05 17:13:45 +01:00
|
|
|
return;
|
|
|
|
|
|
2019-04-24 14:44:31 +02:00
|
|
|
const std::string config = tableToString(this);
|
|
|
|
|
if (config.empty())
|
2019-02-21 10:26:08 +01:00
|
|
|
return;
|
|
|
|
|
|
2019-04-24 14:44:31 +02:00
|
|
|
saveConfig(config);
|
|
|
|
|
fillTable();
|
|
|
|
|
updatePreview();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ClangFormatConfigWidget::saveConfig(const std::string &text) const
|
|
|
|
|
{
|
2021-04-22 16:15:26 +02:00
|
|
|
QString filePath = Core::ICore::userResourcePath().toString();
|
2018-08-29 15:58:13 +02:00
|
|
|
if (m_project)
|
2019-03-06 14:45:47 +01:00
|
|
|
filePath += "/clang-format/" + currentProjectUniqueId();
|
2019-03-05 13:12:44 +01:00
|
|
|
filePath += "/" + QLatin1String(Constants::SETTINGS_FILE_NAME);
|
|
|
|
|
|
2018-08-29 15:58:13 +02:00
|
|
|
QFile file(filePath);
|
|
|
|
|
if (!file.open(QFile::WriteOnly))
|
|
|
|
|
return;
|
|
|
|
|
|
2019-04-24 14:44:31 +02:00
|
|
|
file.write(text.c_str());
|
2018-08-29 15:58:13 +02:00
|
|
|
file.close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace ClangFormat
|