2022-08-19 15:59:36 +02:00
|
|
|
// Copyright (C) 2018 The Qt Company Ltd.
|
2022-12-21 10:12:09 +01:00
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
2018-08-29 15:58:13 +02:00
|
|
|
|
|
|
|
|
#include "clangformatconfigwidget.h"
|
2018-10-11 14:11:43 +02:00
|
|
|
|
2023-08-02 16:00:49 +02:00
|
|
|
// the file below was generated by scripts/generateClangFormatChecksLayout.py
|
2023-02-01 10:36:59 +01:00
|
|
|
#include "clangformatchecks.h"
|
2018-11-13 09:29:09 +01:00
|
|
|
#include "clangformatconstants.h"
|
2021-09-28 10:26:34 +02:00
|
|
|
#include "clangformatfile.h"
|
2023-02-01 10:36:59 +01:00
|
|
|
#include "clangformatindenter.h"
|
2023-02-04 01:46:50 +01:00
|
|
|
#include "clangformattr.h"
|
2018-10-11 14:11:43 +02:00
|
|
|
#include "clangformatutils.h"
|
2022-07-28 11:30:41 +02:00
|
|
|
|
2018-08-29 15:58:13 +02:00
|
|
|
#include <coreplugin/icore.h>
|
2022-07-22 09:44:47 +02:00
|
|
|
|
2022-11-21 13:34:15 +01:00
|
|
|
#include <cppeditor/cppcodestylepreferences.h>
|
2021-11-01 16:20:08 +01:00
|
|
|
#include <cppeditor/cppcodestylesettings.h>
|
2021-08-30 10:58:08 +02:00
|
|
|
#include <cppeditor/cppcodestylesnippets.h>
|
2022-11-21 13:34:15 +01:00
|
|
|
#include <cppeditor/cpphighlighter.h>
|
2022-08-01 17:28:42 +02:00
|
|
|
#include <cppeditor/cpptoolssettings.h>
|
2022-07-22 09:44:47 +02:00
|
|
|
|
2022-08-01 17:28:42 +02:00
|
|
|
#include <projectexplorer/editorconfiguration.h>
|
2022-11-21 13:34:15 +01:00
|
|
|
#include <projectexplorer/project.h>
|
2022-07-22 09:44:47 +02:00
|
|
|
|
2019-02-22 09:48:46 +01:00
|
|
|
#include <texteditor/displaysettings.h>
|
2022-04-04 14:53:05 +02:00
|
|
|
#include <texteditor/icodestylepreferences.h>
|
2019-02-22 09:48:46 +01:00
|
|
|
#include <texteditor/snippets/snippeteditor.h>
|
|
|
|
|
#include <texteditor/textdocument.h>
|
|
|
|
|
#include <texteditor/texteditorsettings.h>
|
2022-07-22 09:44:47 +02:00
|
|
|
|
2023-02-01 10:36:59 +01:00
|
|
|
#include <utils/guard.h>
|
2022-07-22 09:44:47 +02:00
|
|
|
#include <utils/layoutbuilder.h>
|
2019-04-24 14:44:31 +02:00
|
|
|
#include <utils/qtcassert.h>
|
2023-08-04 14:06:39 +02:00
|
|
|
#include <utils/utilsicons.h>
|
2018-08-29 15:58:13 +02:00
|
|
|
|
2022-07-22 09:44:47 +02:00
|
|
|
#include <QComboBox>
|
|
|
|
|
#include <QLabel>
|
2022-11-21 13:34:15 +01:00
|
|
|
#include <QLineEdit>
|
|
|
|
|
#include <QPushButton>
|
2023-02-01 10:36:59 +01:00
|
|
|
#include <QScrollArea>
|
2022-11-21 13:34:15 +01:00
|
|
|
#include <QSharedPointer>
|
|
|
|
|
#include <QVBoxLayout>
|
2023-08-04 14:06:39 +02:00
|
|
|
#include <QVersionNumber>
|
2022-11-21 13:34:15 +01:00
|
|
|
#include <QWeakPointer>
|
2022-07-22 09:44:47 +02:00
|
|
|
#include <QWidget>
|
2018-08-29 15:58:13 +02:00
|
|
|
|
2023-08-04 14:06:39 +02:00
|
|
|
#include <clang/Basic/Version.h>
|
2023-02-01 10:36:59 +01:00
|
|
|
#include <clang/Format/Format.h>
|
|
|
|
|
|
2018-08-29 15:58:13 +02:00
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
|
|
using namespace ProjectExplorer;
|
2022-07-22 09:44:47 +02:00
|
|
|
using namespace Utils;
|
2018-08-29 15:58:13 +02:00
|
|
|
|
|
|
|
|
namespace ClangFormat {
|
|
|
|
|
|
2023-02-01 10:36:59 +01:00
|
|
|
class ClangFormatConfigWidget::Private
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
ProjectExplorer::Project *project = nullptr;
|
|
|
|
|
QWidget *checksWidget = nullptr;
|
|
|
|
|
QScrollArea *checksScrollArea = nullptr;
|
|
|
|
|
TextEditor::SnippetEditorWidget *preview = nullptr;
|
|
|
|
|
std::unique_ptr<ClangFormatFile> config;
|
|
|
|
|
clang::format::FormatStyle style;
|
|
|
|
|
Utils::Guard ignoreChanges;
|
|
|
|
|
QLabel *fallbackConfig;
|
2023-08-04 14:06:39 +02:00
|
|
|
QLabel *clangVersion;
|
|
|
|
|
QLabel *clangWarningText;
|
|
|
|
|
QLabel *clangWarningIcon;
|
2023-02-01 10:36:59 +01:00
|
|
|
};
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-04 14:53:05 +02:00
|
|
|
ClangFormatConfigWidget::ClangFormatConfigWidget(TextEditor::ICodeStylePreferences *codeStyle,
|
|
|
|
|
ProjectExplorer::Project *project,
|
|
|
|
|
QWidget *parent)
|
2023-02-01 10:36:59 +01:00
|
|
|
: CppCodeStyleWidget(parent), d(new Private)
|
2018-08-29 15:58:13 +02:00
|
|
|
{
|
2023-02-01 10:36:59 +01:00
|
|
|
d->project = project;
|
2023-06-07 16:17:29 +02:00
|
|
|
d->config = std::make_unique<ClangFormatFile>(codeStyle->currentPreferences());
|
2022-07-22 09:44:47 +02:00
|
|
|
|
2023-02-04 01:46:50 +01:00
|
|
|
d->fallbackConfig = new QLabel(Tr::tr("Clang-Format Style"));
|
2023-02-01 10:36:59 +01:00
|
|
|
d->checksScrollArea = new QScrollArea();
|
|
|
|
|
d->checksWidget = new ClangFormatChecks();
|
2022-07-22 09:44:47 +02:00
|
|
|
|
2023-02-01 10:36:59 +01:00
|
|
|
d->checksScrollArea->setWidget(d->checksWidget);
|
|
|
|
|
d->checksScrollArea->setWidgetResizable(true);
|
2023-05-08 11:00:04 +02:00
|
|
|
d->checksWidget->setEnabled(!codeStyle->isReadOnly() && !codeStyle->isTemporarilyReadOnly()
|
|
|
|
|
&& !codeStyle->isAdditionalTabDisabled());
|
2019-04-25 10:40:08 +02:00
|
|
|
|
2023-08-04 14:06:39 +02:00
|
|
|
|
|
|
|
|
static const int expectedMajorVersion = 16;
|
|
|
|
|
d->clangVersion = new QLabel(Tr::tr("Current clang-format version: ") + LLVM_VERSION_STRING,
|
|
|
|
|
this);
|
|
|
|
|
d->clangWarningText
|
|
|
|
|
= new QLabel(Tr::tr("The widget was generated for ClangFormat %1. "
|
|
|
|
|
"If you use a different version, the widget may work incorrectly.")
|
|
|
|
|
.arg(expectedMajorVersion),
|
|
|
|
|
this);
|
|
|
|
|
|
|
|
|
|
QPalette palette = d->clangWarningText->palette();
|
|
|
|
|
palette.setColor(QPalette::WindowText, Qt::red);
|
|
|
|
|
d->clangWarningText->setPalette(palette);
|
|
|
|
|
|
|
|
|
|
d->clangWarningIcon = new QLabel(this);
|
|
|
|
|
d->clangWarningIcon->setPixmap(Utils::Icons::WARNING.icon().pixmap(16, 16));
|
|
|
|
|
|
|
|
|
|
if (LLVM_VERSION_MAJOR == expectedMajorVersion) {
|
|
|
|
|
d->clangWarningText->hide();
|
|
|
|
|
d->clangWarningIcon->hide();
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-22 09:44:47 +02:00
|
|
|
FilePath fileName;
|
2023-02-01 10:36:59 +01:00
|
|
|
if (d->project)
|
|
|
|
|
fileName = d->project->projectFilePath().pathAppended("snippet.cpp");
|
2022-07-22 09:44:47 +02:00
|
|
|
else
|
|
|
|
|
fileName = Core::ICore::userResourcePath("snippet.cpp");
|
2019-04-25 10:40:08 +02:00
|
|
|
|
2023-02-01 10:36:59 +01:00
|
|
|
d->preview = new TextEditor::SnippetEditorWidget(this);
|
|
|
|
|
TextEditor::DisplaySettings displaySettings = d->preview->displaySettings();
|
2019-04-26 10:49:48 +02:00
|
|
|
displaySettings.m_visualizeWhitespace = true;
|
2023-02-01 10:36:59 +01:00
|
|
|
d->preview->setDisplaySettings(displaySettings);
|
|
|
|
|
d->preview->setPlainText(QLatin1String(CppEditor::Constants::DEFAULT_CODE_STYLE_SNIPPETS[0]));
|
|
|
|
|
d->preview->textDocument()->setIndenter(new ClangFormatIndenter(d->preview->document()));
|
|
|
|
|
d->preview->textDocument()->setFontSettings(TextEditor::TextEditorSettings::fontSettings());
|
|
|
|
|
d->preview->textDocument()->setSyntaxHighlighter(new CppEditor::CppHighlighter);
|
|
|
|
|
d->preview->textDocument()->indenter()->setFileName(fileName);
|
2019-04-26 10:49:48 +02:00
|
|
|
|
2022-07-22 09:44:47 +02:00
|
|
|
using namespace Layouting;
|
2022-02-16 15:28:16 +01:00
|
|
|
|
2022-07-22 09:44:47 +02:00
|
|
|
Column {
|
2023-02-01 10:36:59 +01:00
|
|
|
d->fallbackConfig,
|
2023-08-04 14:06:39 +02:00
|
|
|
Row {d->clangWarningIcon, d->clangWarningText, st},
|
|
|
|
|
d->clangVersion,
|
2023-02-01 10:36:59 +01:00
|
|
|
Row { d->checksScrollArea, d->preview },
|
2022-07-22 09:44:47 +02:00
|
|
|
}.attachTo(this);
|
|
|
|
|
|
2022-08-01 17:28:42 +02:00
|
|
|
connect(codeStyle, &TextEditor::ICodeStylePreferences::currentPreferencesChanged,
|
|
|
|
|
this, &ClangFormatConfigWidget::slotCodeStyleChanged);
|
|
|
|
|
|
|
|
|
|
slotCodeStyleChanged(codeStyle->currentPreferences());
|
|
|
|
|
|
2022-07-22 09:44:47 +02:00
|
|
|
showOrHideWidgets();
|
|
|
|
|
fillTable();
|
|
|
|
|
updatePreview();
|
|
|
|
|
|
|
|
|
|
connectChecks();
|
2019-04-25 10:40:08 +02:00
|
|
|
}
|
|
|
|
|
|
2023-02-01 10:36:59 +01:00
|
|
|
ClangFormatConfigWidget::~ClangFormatConfigWidget()
|
|
|
|
|
{
|
|
|
|
|
delete d;
|
|
|
|
|
}
|
2022-07-22 09:44:47 +02:00
|
|
|
|
2022-08-01 17:28:42 +02:00
|
|
|
void ClangFormatConfigWidget::slotCodeStyleChanged(
|
|
|
|
|
TextEditor::ICodeStylePreferences *codeStyle)
|
|
|
|
|
{
|
|
|
|
|
if (!codeStyle)
|
|
|
|
|
return;
|
2023-06-07 16:17:29 +02:00
|
|
|
d->config.reset(new ClangFormatFile(codeStyle));
|
2023-02-01 10:36:59 +01:00
|
|
|
d->config->setIsReadOnly(codeStyle->isReadOnly());
|
|
|
|
|
d->style = d->config->style();
|
2022-08-01 17:28:42 +02:00
|
|
|
|
2023-05-08 11:00:04 +02:00
|
|
|
d->checksWidget->setEnabled(!codeStyle->isReadOnly() && !codeStyle->isTemporarilyReadOnly()
|
|
|
|
|
&& !codeStyle->isAdditionalTabDisabled());
|
2022-08-01 17:28:42 +02:00
|
|
|
|
|
|
|
|
fillTable();
|
|
|
|
|
updatePreview();
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-25 10:40:08 +02:00
|
|
|
void ClangFormatConfigWidget::connectChecks()
|
|
|
|
|
{
|
2022-08-15 10:09:46 +02:00
|
|
|
auto doSaveChanges = [this](QObject *sender) {
|
2023-02-01 10:36:59 +01:00
|
|
|
if (!d->ignoreChanges.isLocked())
|
2022-08-15 10:09:46 +02:00
|
|
|
saveChanges(sender);
|
|
|
|
|
};
|
|
|
|
|
|
2023-02-01 10:36:59 +01:00
|
|
|
for (QObject *child : d->checksWidget->children()) {
|
2019-04-25 10:40:08 +02:00
|
|
|
auto comboBox = qobject_cast<QComboBox *>(child);
|
|
|
|
|
if (comboBox != nullptr) {
|
2022-07-19 23:29:42 +02:00
|
|
|
connect(comboBox, &QComboBox::currentIndexChanged,
|
2022-08-15 10:09:46 +02:00
|
|
|
this, std::bind(doSaveChanges, comboBox));
|
2019-04-25 10:40:08 +02:00
|
|
|
comboBox->installEventFilter(this);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-28 10:26:34 +02:00
|
|
|
const auto button = qobject_cast<QPushButton *>(child);
|
2019-04-25 10:40:08 +02:00
|
|
|
if (button != nullptr)
|
2022-08-15 10:09:46 +02:00
|
|
|
connect(button, &QPushButton::clicked, this, std::bind(doSaveChanges, button));
|
2019-04-25 10:40:08 +02:00
|
|
|
}
|
2018-10-11 14:11:43 +02:00
|
|
|
}
|
|
|
|
|
|
2023-01-16 12:33:43 +01:00
|
|
|
static clang::format::FormatStyle constructStyle(const QByteArray &baseStyle = QByteArray())
|
|
|
|
|
{
|
|
|
|
|
if (!baseStyle.isEmpty()) {
|
|
|
|
|
// Try to get the style for this base style.
|
|
|
|
|
llvm::Expected<clang::format::FormatStyle> style
|
|
|
|
|
= clang::format::getStyle(baseStyle.toStdString(), "dummy.cpp", baseStyle.toStdString());
|
|
|
|
|
if (style)
|
|
|
|
|
return *style;
|
|
|
|
|
|
|
|
|
|
handleAllErrors(style.takeError(), [](const llvm::ErrorInfoBase &) {
|
|
|
|
|
// do nothing
|
|
|
|
|
});
|
|
|
|
|
// Fallthrough to the default style.
|
|
|
|
|
}
|
|
|
|
|
return qtcStyle();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Utils::FilePath ClangFormatConfigWidget::globalPath()
|
|
|
|
|
{
|
|
|
|
|
return Core::ICore::userResourcePath();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Utils::FilePath ClangFormatConfigWidget::projectPath()
|
|
|
|
|
{
|
2023-02-01 10:36:59 +01:00
|
|
|
if (d->project)
|
|
|
|
|
return globalPath().pathAppended("clang-format/" + projectUniqueId(d->project));
|
2023-01-16 12:33:43 +01:00
|
|
|
|
2023-08-02 08:09:26 +02:00
|
|
|
return {};
|
2023-01-16 12:33:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ClangFormatConfigWidget::createStyleFileIfNeeded(bool isGlobal)
|
|
|
|
|
{
|
|
|
|
|
const FilePath path = isGlobal ? globalPath() : projectPath();
|
|
|
|
|
const FilePath configFile = path / Constants::SETTINGS_FILE_NAME;
|
|
|
|
|
|
|
|
|
|
if (configFile.exists())
|
|
|
|
|
return;
|
|
|
|
|
|
2023-06-09 14:16:40 +02:00
|
|
|
path.ensureWritableDir();
|
2023-01-16 12:33:43 +01:00
|
|
|
if (!isGlobal) {
|
2023-02-01 10:36:59 +01:00
|
|
|
FilePath possibleProjectConfig = d->project->rootProjectDirectory()
|
2023-01-16 12:33:43 +01:00
|
|
|
/ Constants::SETTINGS_FILE_NAME;
|
|
|
|
|
if (possibleProjectConfig.exists()) {
|
|
|
|
|
// Just copy th .clang-format if current project has one.
|
|
|
|
|
possibleProjectConfig.copyFile(configFile);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-09 14:16:40 +02:00
|
|
|
const std::string config = clang::format::configurationAsText(constructStyle());
|
|
|
|
|
configFile.writeFileContents(QByteArray::fromStdString(config));
|
2023-01-16 12:33:43 +01:00
|
|
|
}
|
|
|
|
|
|
2019-04-26 10:49:48 +02:00
|
|
|
void ClangFormatConfigWidget::showOrHideWidgets()
|
2018-10-11 14:11:43 +02:00
|
|
|
{
|
2022-07-22 09:44:47 +02:00
|
|
|
auto verticalLayout = qobject_cast<QVBoxLayout *>(layout());
|
|
|
|
|
QTC_ASSERT(verticalLayout, return);
|
2018-10-11 14:11:43 +02:00
|
|
|
|
2022-07-22 09:44:47 +02:00
|
|
|
QLayoutItem *lastItem = verticalLayout->itemAt(verticalLayout->count() - 1);
|
2018-12-05 09:07:10 +01:00
|
|
|
if (lastItem->spacerItem())
|
2022-07-22 09:44:47 +02:00
|
|
|
verticalLayout->removeItem(lastItem);
|
2018-11-20 11:23:30 +01:00
|
|
|
|
2023-02-01 10:36:59 +01:00
|
|
|
createStyleFileIfNeeded(!d->project);
|
|
|
|
|
d->fallbackConfig->show();
|
|
|
|
|
d->checksScrollArea->show();
|
|
|
|
|
d->preview->show();
|
2019-02-22 09:48:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ClangFormatConfigWidget::updatePreview()
|
|
|
|
|
{
|
2023-02-01 10:36:59 +01:00
|
|
|
QTextCursor cursor(d->preview->document());
|
2019-02-22 09:48:46 +01:00
|
|
|
cursor.setPosition(0);
|
|
|
|
|
cursor.movePosition(QTextCursor::End, QTextCursor::KeepAnchor);
|
2023-02-01 10:36:59 +01:00
|
|
|
d->preview->textDocument()->autoIndent(cursor);
|
2018-10-11 14:11:43 +02:00
|
|
|
}
|
|
|
|
|
|
2023-01-16 12:33:43 +01:00
|
|
|
std::string ClangFormatConfigWidget::readFile(const QString &path)
|
|
|
|
|
{
|
|
|
|
|
const std::string defaultStyle = clang::format::configurationAsText(qtcStyle());
|
|
|
|
|
|
|
|
|
|
QFile file(path);
|
|
|
|
|
if (!file.open(QFile::ReadOnly))
|
|
|
|
|
return defaultStyle;
|
|
|
|
|
|
|
|
|
|
const std::string content = file.readAll().toStdString();
|
|
|
|
|
file.close();
|
|
|
|
|
|
|
|
|
|
clang::format::FormatStyle style;
|
|
|
|
|
style.Language = clang::format::FormatStyle::LK_Cpp;
|
|
|
|
|
const std::error_code error = clang::format::parseConfiguration(content, &style);
|
|
|
|
|
QTC_ASSERT(error.value() == static_cast<int>(clang::format::ParseError::Success),
|
|
|
|
|
return defaultStyle);
|
|
|
|
|
|
|
|
|
|
addQtcStatementMacros(style);
|
|
|
|
|
std::string settings = clang::format::configurationAsText(style);
|
|
|
|
|
|
|
|
|
|
// Needed workaround because parseConfiguration remove BasedOnStyle field
|
|
|
|
|
// ToDo: standardize this behavior for future
|
|
|
|
|
const size_t index = content.find("BasedOnStyle");
|
|
|
|
|
if (index != std::string::npos) {
|
|
|
|
|
const size_t size = content.find("\n", index) - index;
|
|
|
|
|
const size_t insert_index = settings.find("\n");
|
|
|
|
|
settings.insert(insert_index, "\n" + content.substr(index, size));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return settings;
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-02 16:00:49 +02:00
|
|
|
static std::map<QString, QString> getMapFromString(const QString &text)
|
|
|
|
|
{
|
|
|
|
|
std::map<QString, QString> objectNameMap;
|
|
|
|
|
|
|
|
|
|
QString parentName;
|
|
|
|
|
for (QString line : text.split('\n')) {
|
|
|
|
|
if (line.isEmpty())
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
QStringList list = line.split(':');
|
|
|
|
|
QString key = !list.isEmpty() ? list[0] : "";
|
|
|
|
|
QString value = line.mid(key.size() + 1).trimmed();
|
|
|
|
|
|
|
|
|
|
if (line.contains(':') && value.isEmpty()) {
|
|
|
|
|
parentName = key;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!value.isEmpty() && !line.startsWith(" "))
|
|
|
|
|
parentName = "";
|
|
|
|
|
|
|
|
|
|
if (line.startsWith(" - ") || line.startsWith(" ")) {
|
|
|
|
|
line.remove(0, 2);
|
|
|
|
|
if (objectNameMap.find(parentName) == objectNameMap.end())
|
|
|
|
|
objectNameMap[parentName] = line + "\n";
|
|
|
|
|
else
|
|
|
|
|
objectNameMap[parentName] += line + "\n";
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (line.startsWith(" ")) {
|
|
|
|
|
key.remove(0, 2);
|
|
|
|
|
key = parentName + key;
|
|
|
|
|
objectNameMap.insert(std::make_pair(key, value));
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
objectNameMap.insert(std::make_pair(key, value));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return objectNameMap;
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-08 10:35:23 +01:00
|
|
|
void ClangFormatConfigWidget::fillTable()
|
2018-10-11 14:11:43 +02:00
|
|
|
{
|
2023-02-01 10:36:59 +01:00
|
|
|
Utils::GuardLocker locker(d->ignoreChanges);
|
2018-08-29 15:58:13 +02:00
|
|
|
|
2023-08-02 16:00:49 +02:00
|
|
|
const QString configText = QString::fromStdString(readFile(d->config->filePath().path()));
|
|
|
|
|
std::map<QString, QString> objectNameMap = getMapFromString(configText);
|
2019-04-24 14:44:31 +02:00
|
|
|
|
2023-02-01 10:36:59 +01:00
|
|
|
for (QObject *child : d->checksWidget->children()) {
|
2019-04-24 14:44:31 +02:00
|
|
|
if (!qobject_cast<QComboBox *>(child) && !qobject_cast<QLineEdit *>(child)
|
|
|
|
|
&& !qobject_cast<QPlainTextEdit *>(child)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-02 16:00:49 +02:00
|
|
|
if (objectNameMap.find(child->objectName()) == objectNameMap.end())
|
|
|
|
|
continue;
|
2019-04-24 14:44:31 +02:00
|
|
|
|
2023-08-02 16:00:49 +02:00
|
|
|
if (QPlainTextEdit *plainText = qobject_cast<QPlainTextEdit *>(child)) {
|
|
|
|
|
plainText->setPlainText(objectNameMap[child->objectName()]);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (QComboBox *comboBox = qobject_cast<QComboBox *>(child)) {
|
|
|
|
|
if (comboBox->findText(objectNameMap[child->objectName()]) == -1) {
|
|
|
|
|
comboBox->setCurrentIndex(0);
|
|
|
|
|
} else {
|
|
|
|
|
comboBox->setCurrentText(objectNameMap[child->objectName()]);
|
|
|
|
|
}
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (QLineEdit *lineEdit = qobject_cast<QLineEdit *>(child)) {
|
|
|
|
|
lineEdit->setText(objectNameMap[child->objectName()]);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2019-04-24 14:44:31 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-28 10:26:34 +02:00
|
|
|
void ClangFormatConfigWidget::saveChanges(QObject *sender)
|
2019-04-24 14:44:31 +02:00
|
|
|
{
|
|
|
|
|
if (sender->objectName() == "BasedOnStyle") {
|
2023-02-01 10:36:59 +01:00
|
|
|
const auto *basedOnStyle = d->checksWidget->findChild<QComboBox *>("BasedOnStyle");
|
|
|
|
|
d->config->setBasedOnStyle(basedOnStyle->currentText());
|
2019-04-24 14:44:31 +02:00
|
|
|
} else {
|
2021-09-28 10:26:34 +02:00
|
|
|
QList<ClangFormatFile::Field> fields;
|
2023-08-02 16:00:49 +02:00
|
|
|
QString parentName;
|
2021-09-28 10:26:34 +02:00
|
|
|
|
2023-02-01 10:36:59 +01:00
|
|
|
for (QObject *child : d->checksWidget->children()) {
|
2021-09-28 10:26:34 +02:00
|
|
|
if (child->objectName() == "BasedOnStyle")
|
|
|
|
|
continue;
|
2019-04-24 14:44:31 +02:00
|
|
|
auto *label = qobject_cast<QLabel *>(child);
|
|
|
|
|
if (!label)
|
|
|
|
|
continue;
|
|
|
|
|
|
2023-08-02 16:00:49 +02:00
|
|
|
// reset parent name if label starts without " "
|
|
|
|
|
if (!label->text().startsWith(" "))
|
|
|
|
|
parentName = "";
|
|
|
|
|
|
|
|
|
|
QList<QWidget *> valueWidgets = d->checksWidget->findChildren<QWidget *>(
|
|
|
|
|
parentName + label->text().trimmed());
|
|
|
|
|
|
|
|
|
|
if (valueWidgets.empty()) {
|
2019-04-24 14:44:31 +02:00
|
|
|
// Currently BraceWrapping only.
|
2021-09-28 10:26:34 +02:00
|
|
|
fields.append({label->text(), ""});
|
2023-08-02 16:00:49 +02:00
|
|
|
// save parent name
|
|
|
|
|
parentName = label->text().trimmed();
|
2019-04-24 14:44:31 +02:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-02 16:00:49 +02:00
|
|
|
QWidget *valueWidget = valueWidgets.first();
|
|
|
|
|
if (valueWidgets.size() > 1) {
|
|
|
|
|
for (QWidget *w : valueWidgets) {
|
|
|
|
|
if (w->objectName() == parentName + label->text().trimmed()) {
|
|
|
|
|
valueWidget = w;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-24 14:44:31 +02:00
|
|
|
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;
|
|
|
|
|
|
2021-09-28 10:26:34 +02:00
|
|
|
std::stringstream content;
|
2019-04-24 14:44:31 +02:00
|
|
|
QStringList list = plainText->toPlainText().split('\n');
|
|
|
|
|
for (const QString &line : list)
|
|
|
|
|
content << "\n " << line.toStdString();
|
2021-09-28 10:26:34 +02:00
|
|
|
|
|
|
|
|
fields.append({label->text(), QString::fromStdString(content.str())});
|
2019-04-24 14:44:31 +02:00
|
|
|
} else {
|
2021-09-28 10:26:34 +02:00
|
|
|
QString text;
|
|
|
|
|
if (auto *comboBox = qobject_cast<QComboBox *>(valueWidget)) {
|
|
|
|
|
text = comboBox->currentText();
|
2019-04-24 14:44:31 +02:00
|
|
|
} else {
|
|
|
|
|
auto *lineEdit = qobject_cast<QLineEdit *>(valueWidget);
|
|
|
|
|
QTC_ASSERT(lineEdit, continue;);
|
2021-09-28 10:26:34 +02:00
|
|
|
text = lineEdit->text();
|
2019-04-24 14:44:31 +02:00
|
|
|
}
|
|
|
|
|
|
2021-09-28 10:26:34 +02:00
|
|
|
if (!text.isEmpty() && text != "Default")
|
|
|
|
|
fields.append({label->text(), text});
|
2019-04-24 14:44:31 +02:00
|
|
|
}
|
|
|
|
|
}
|
2023-02-01 10:36:59 +01:00
|
|
|
d->config->changeFields(fields);
|
2019-04-24 14:44:31 +02:00
|
|
|
}
|
|
|
|
|
|
2021-09-28 10:26:34 +02:00
|
|
|
fillTable();
|
|
|
|
|
updatePreview();
|
2021-11-01 16:20:08 +01:00
|
|
|
synchronize();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ClangFormatConfigWidget::setCodeStyleSettings(const CppEditor::CppCodeStyleSettings &settings)
|
|
|
|
|
{
|
2023-02-01 10:36:59 +01:00
|
|
|
d->config->fromCppCodeStyleSettings(settings);
|
2021-11-01 16:20:08 +01:00
|
|
|
|
|
|
|
|
fillTable();
|
|
|
|
|
updatePreview();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ClangFormatConfigWidget::setTabSettings(const TextEditor::TabSettings &settings)
|
|
|
|
|
{
|
2023-02-01 10:36:59 +01:00
|
|
|
d->config->fromTabSettings(settings);
|
2021-11-01 16:20:08 +01:00
|
|
|
|
|
|
|
|
fillTable();
|
|
|
|
|
updatePreview();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ClangFormatConfigWidget::synchronize()
|
|
|
|
|
{
|
2023-02-01 10:36:59 +01:00
|
|
|
emit codeStyleSettingsChanged(d->config->toCppCodeStyleSettings(d->project));
|
|
|
|
|
emit tabSettingsChanged(d->config->toTabSettings(d->project));
|
2018-08-29 15:58:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ClangFormatConfigWidget::apply()
|
|
|
|
|
{
|
2023-02-01 10:36:59 +01:00
|
|
|
if (!d->checksWidget->isVisible() && !d->checksWidget->isEnabled())
|
2019-03-05 17:13:45 +01:00
|
|
|
return;
|
|
|
|
|
|
2023-02-01 10:36:59 +01:00
|
|
|
d->style = d->config->style();
|
2022-08-01 17:28:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ClangFormatConfigWidget::finish()
|
|
|
|
|
{
|
2023-02-01 10:36:59 +01:00
|
|
|
if (!d->checksWidget->isVisible() && !d->checksWidget->isEnabled())
|
2022-08-01 17:28:42 +02:00
|
|
|
return;
|
|
|
|
|
|
2023-02-01 10:36:59 +01:00
|
|
|
d->config->setStyle(d->style);
|
2018-08-29 15:58:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace ClangFormat
|