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"
|
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>
|
2019-02-25 22:39:22 +02:00
|
|
|
#include <cpptools/cppcodestylesnippets.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>
|
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-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)
|
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-03-05 13:12:44 +01:00
|
|
|
m_preview = new TextEditor::SnippetEditorWidget(this);
|
|
|
|
|
m_ui->horizontalLayout_2->addWidget(m_preview);
|
|
|
|
|
if (m_project) {
|
|
|
|
|
m_ui->applyButton->show();
|
|
|
|
|
hideGlobalCheckboxes();
|
|
|
|
|
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());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
connect(m_ui->overrideDefault, &QCheckBox::toggled, this, [this](bool checked) {
|
|
|
|
|
if (checked)
|
|
|
|
|
createStyleFileIfNeeded(!m_project);
|
|
|
|
|
initialize();
|
|
|
|
|
});
|
2018-10-11 14:11:43 +02:00
|
|
|
initialize();
|
|
|
|
|
}
|
|
|
|
|
|
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-01-28 08:13:33 +01:00
|
|
|
}
|
|
|
|
|
|
2019-03-05 13:12:44 +01:00
|
|
|
static bool projectConfigExists()
|
|
|
|
|
{
|
|
|
|
|
return Utils::FileName::fromString(Core::ICore::userResourcePath())
|
2019-03-06 14:45:47 +01:00
|
|
|
.appendPath("clang-format")
|
2019-03-05 13:12:44 +01:00
|
|
|
.appendPath(currentProjectUniqueId())
|
|
|
|
|
.appendPath((Constants::SETTINGS_FILE_NAME))
|
|
|
|
|
.exists();
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-11 14:11:43 +02:00
|
|
|
void ClangFormatConfigWidget::initialize()
|
|
|
|
|
{
|
2019-03-05 13:12:44 +01:00
|
|
|
m_ui->projectHasClangFormat->hide();
|
2018-10-11 14:11:43 +02:00
|
|
|
|
2019-02-22 09:48:46 +01:00
|
|
|
m_preview->setPlainText(QLatin1String(CppTools::Constants::DEFAULT_CODE_STYLE_SNIPPETS[0]));
|
|
|
|
|
m_preview->textDocument()->setIndenter(new ClangFormatIndenter(m_preview->document()));
|
|
|
|
|
m_preview->textDocument()->setFontSettings(TextEditor::TextEditorSettings::fontSettings());
|
|
|
|
|
m_preview->textDocument()->setSyntaxHighlighter(new CppEditor::CppHighlighter);
|
|
|
|
|
|
|
|
|
|
TextEditor::DisplaySettings displaySettings = m_preview->displaySettings();
|
|
|
|
|
displaySettings.m_visualizeWhitespace = true;
|
|
|
|
|
m_preview->setDisplaySettings(displaySettings);
|
|
|
|
|
|
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-05 13:12:44 +01:00
|
|
|
if (!m_ui->overrideDefault->isChecked()) {
|
2018-08-29 15:58:13 +02:00
|
|
|
m_ui->clangFormatOptionsTable->hide();
|
2019-03-05 13:12:44 +01:00
|
|
|
m_preview->hide();
|
2018-11-20 11:23:30 +01:00
|
|
|
m_ui->verticalLayout->addStretch(1);
|
2018-08-29 15:58:13 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-05 13:12:44 +01:00
|
|
|
m_ui->clangFormatOptionsTable->show();
|
|
|
|
|
m_preview->show();
|
2018-10-11 14:11:43 +02:00
|
|
|
|
2019-02-22 09:48:46 +01:00
|
|
|
Utils::FileName fileName;
|
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
|
|
|
connect(m_ui->applyButton, &QPushButton::clicked, this, &ClangFormatConfigWidget::apply);
|
2019-02-22 09:48:46 +01:00
|
|
|
fileName = m_project->projectFilePath().appendPath("snippet.cpp");
|
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 {
|
|
|
|
|
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
|
|
|
}
|
2019-02-22 09:48:46 +01:00
|
|
|
fileName = Utils::FileName::fromString(Core::ICore::userResourcePath())
|
|
|
|
|
.appendPath("snippet.cpp");
|
2018-08-29 15:58:13 +02:00
|
|
|
}
|
|
|
|
|
|
2019-02-22 09:48:46 +01:00
|
|
|
m_preview->textDocument()->indenter()->setFileName(fileName);
|
2018-11-08 10:35:23 +01:00
|
|
|
fillTable();
|
2019-02-22 09:48:46 +01:00
|
|
|
updatePreview();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ClangFormatConfigWidget::updatePreview()
|
|
|
|
|
{
|
|
|
|
|
QTextCursor cursor(m_preview->document());
|
|
|
|
|
cursor.setPosition(0);
|
|
|
|
|
cursor.movePosition(QTextCursor::End, QTextCursor::KeepAnchor);
|
|
|
|
|
m_preview->textDocument()->autoFormatOrIndent(cursor);
|
2018-10-11 14:11:43 +02:00
|
|
|
}
|
|
|
|
|
|
2018-11-08 10:35:23 +01:00
|
|
|
void ClangFormatConfigWidget::fillTable()
|
2018-10-11 14:11:43 +02:00
|
|
|
{
|
2018-11-08 10:35:23 +01:00
|
|
|
clang::format::FormatStyle style = m_project ? currentProjectStyle() : currentGlobalStyle();
|
2018-08-29 15:58:13 +02:00
|
|
|
|
2019-03-05 13:12:44 +01:00
|
|
|
const std::string configText = clang::format::configurationAsText(style);
|
2019-02-21 15:00:49 +01:00
|
|
|
m_ui->clangFormatOptionsTable->setPlainText(QString::fromStdString(configText));
|
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-03-05 17:13:45 +01:00
|
|
|
if (!m_ui->overrideDefault->isChecked())
|
|
|
|
|
return;
|
|
|
|
|
|
2019-02-21 15:00:49 +01:00
|
|
|
const QString text = m_ui->clangFormatOptionsTable->toPlainText();
|
2019-02-21 10:26:08 +01:00
|
|
|
clang::format::FormatStyle style;
|
|
|
|
|
style.Language = clang::format::FormatStyle::LK_Cpp;
|
2019-02-21 15:00:49 +01:00
|
|
|
const std::error_code error = clang::format::parseConfiguration(text.toStdString(), &style);
|
2019-02-21 10:26:08 +01:00
|
|
|
if (error.value() != static_cast<int>(clang::format::ParseError::Success)) {
|
|
|
|
|
QMessageBox::warning(this,
|
|
|
|
|
tr("Error in ClangFormat configuration"),
|
|
|
|
|
QString::fromStdString(error.message()));
|
2019-03-05 13:12:44 +01:00
|
|
|
if (m_ui->overrideDefault->isChecked()) {
|
|
|
|
|
fillTable();
|
|
|
|
|
updatePreview();
|
|
|
|
|
}
|
2019-02-21 10:26:08 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-05 13:12:44 +01:00
|
|
|
QString filePath = Core::ICore::userResourcePath();
|
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-02-21 15:00:49 +01:00
|
|
|
file.write(text.toUtf8());
|
2018-08-29 15:58:13 +02:00
|
|
|
file.close();
|
2019-02-22 09:48:46 +01:00
|
|
|
|
2019-03-05 13:12:44 +01:00
|
|
|
if (m_ui->overrideDefault->isChecked())
|
|
|
|
|
updatePreview();
|
2018-08-29 15:58:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace ClangFormat
|