2022-08-19 15:59:36 +02:00
|
|
|
// Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0
|
2018-08-29 15:58:13 +02:00
|
|
|
|
|
|
|
|
#include "clangformatplugin.h"
|
|
|
|
|
|
|
|
|
|
#include "clangformatconfigwidget.h"
|
2022-07-28 16:13:35 +02:00
|
|
|
#include "clangformatglobalconfigwidget.h"
|
2019-03-07 11:35:57 +01:00
|
|
|
#include "clangformatconstants.h"
|
2018-08-29 15:58:13 +02:00
|
|
|
#include "clangformatindenter.h"
|
2022-05-12 12:51:51 +02:00
|
|
|
#include "clangformatsettings.h"
|
2019-03-07 11:35:57 +01:00
|
|
|
#include "clangformatutils.h"
|
2022-01-14 15:08:11 +01:00
|
|
|
#include "tests/clangformat-test.h"
|
2018-08-29 15:58:13 +02:00
|
|
|
|
|
|
|
|
#include <utils/qtcassert.h>
|
|
|
|
|
|
2019-03-07 11:35:57 +01:00
|
|
|
#include <coreplugin/actionmanager/actioncontainer.h>
|
2018-08-29 15:58:13 +02:00
|
|
|
#include <coreplugin/actionmanager/actionmanager.h>
|
|
|
|
|
#include <coreplugin/actionmanager/command.h>
|
|
|
|
|
#include <coreplugin/coreconstants.h>
|
2019-03-07 11:35:57 +01:00
|
|
|
#include <coreplugin/editormanager/editormanager.h>
|
|
|
|
|
#include <coreplugin/editormanager/ieditor.h>
|
|
|
|
|
#include <coreplugin/icontext.h>
|
|
|
|
|
#include <coreplugin/icore.h>
|
|
|
|
|
#include <coreplugin/idocument.h>
|
|
|
|
|
|
|
|
|
|
#include <cppeditor/cppeditorconstants.h>
|
2018-08-29 15:58:13 +02:00
|
|
|
|
2021-08-30 10:58:08 +02:00
|
|
|
#include <cppeditor/cppcodestylepreferencesfactory.h>
|
2021-11-01 16:20:08 +01:00
|
|
|
#include <cppeditor/cppcodestylesettingspage.h>
|
2021-08-30 10:58:08 +02:00
|
|
|
#include <cppeditor/cppeditorconstants.h>
|
|
|
|
|
#include <cppeditor/cppmodelmanager.h>
|
2018-08-29 15:58:13 +02:00
|
|
|
|
|
|
|
|
#include <projectexplorer/kitinformation.h>
|
2018-11-20 11:23:30 +01:00
|
|
|
#include <projectexplorer/project.h>
|
|
|
|
|
#include <projectexplorer/session.h>
|
2018-08-29 15:58:13 +02:00
|
|
|
#include <projectexplorer/target.h>
|
|
|
|
|
|
2019-01-22 14:16:30 +01:00
|
|
|
#include <texteditor/icodestylepreferences.h>
|
2022-05-12 12:51:51 +02:00
|
|
|
#include <texteditor/textindenter.h>
|
2018-11-08 10:35:23 +01:00
|
|
|
#include <texteditor/texteditorsettings.h>
|
|
|
|
|
|
2018-10-11 14:11:43 +02:00
|
|
|
#include <clang/Format/Format.h>
|
|
|
|
|
|
2018-11-08 10:35:23 +01:00
|
|
|
#include <utils/algorithm.h>
|
2020-12-18 11:10:09 +01:00
|
|
|
#include <utils/infobar.h>
|
2018-11-08 10:35:23 +01:00
|
|
|
|
2018-08-29 15:58:13 +02:00
|
|
|
#include <QAction>
|
|
|
|
|
#include <QDebug>
|
|
|
|
|
#include <QMainWindow>
|
|
|
|
|
#include <QMessageBox>
|
|
|
|
|
#include <QMenu>
|
|
|
|
|
|
2021-08-17 10:25:19 +02:00
|
|
|
using namespace Core;
|
2018-08-29 15:58:13 +02:00
|
|
|
using namespace ProjectExplorer;
|
2021-08-17 10:25:19 +02:00
|
|
|
using namespace Utils;
|
2018-08-29 15:58:13 +02:00
|
|
|
|
|
|
|
|
namespace ClangFormat {
|
|
|
|
|
|
2021-08-30 10:58:08 +02:00
|
|
|
class ClangFormatStyleFactory : public CppEditor::CppCodeStylePreferencesFactory
|
2018-08-29 15:58:13 +02:00
|
|
|
{
|
2021-10-07 15:08:46 +02:00
|
|
|
Q_DECLARE_TR_FUNCTIONS(ClangFormatStyleFactory)
|
2018-08-29 15:58:13 +02:00
|
|
|
public:
|
2021-10-07 15:08:46 +02:00
|
|
|
TextEditor::Indenter *createIndenter(QTextDocument *doc) const override
|
2018-08-29 15:58:13 +02:00
|
|
|
{
|
2022-05-12 12:51:51 +02:00
|
|
|
if (ClangFormatSettings::instance().mode() == ClangFormatSettings::Disable)
|
|
|
|
|
return CppEditor::CppCodeStylePreferencesFactory::createIndenter(doc);
|
2021-10-07 15:08:46 +02:00
|
|
|
return new ClangFormatIndenter(doc);
|
2018-08-29 15:58:13 +02:00
|
|
|
}
|
|
|
|
|
|
2022-04-04 14:53:05 +02:00
|
|
|
std::pair<CppEditor::CppCodeStyleWidget *, QString> additionalTab(
|
2022-05-10 12:13:36 +02:00
|
|
|
TextEditor::ICodeStylePreferences *codeStyle,
|
|
|
|
|
ProjectExplorer::Project *project,
|
|
|
|
|
QWidget *parent) const override
|
2018-08-29 15:58:13 +02:00
|
|
|
{
|
2022-05-10 12:13:36 +02:00
|
|
|
return {new ClangFormatConfigWidget(codeStyle, project, parent), tr("ClangFormat")};
|
2018-08-29 15:58:13 +02:00
|
|
|
}
|
2022-07-28 16:13:35 +02:00
|
|
|
|
|
|
|
|
TextEditor::CodeStyleEditorWidget *createAdditionalGlobalSettings(
|
|
|
|
|
ProjectExplorer::Project *project, QWidget *parent)
|
|
|
|
|
{
|
|
|
|
|
return new ClangFormatGlobalConfigWidget(project, parent);
|
|
|
|
|
}
|
2018-08-29 15:58:13 +02:00
|
|
|
};
|
|
|
|
|
|
2018-11-20 11:23:30 +01:00
|
|
|
static void replaceCppCodeStyle()
|
2018-11-08 10:35:23 +01:00
|
|
|
{
|
|
|
|
|
using namespace TextEditor;
|
2021-08-30 10:58:08 +02:00
|
|
|
TextEditorSettings::unregisterCodeStyleFactory(CppEditor::Constants::CPP_SETTINGS_ID);
|
2021-08-17 10:25:19 +02:00
|
|
|
TextEditorSettings::registerCodeStyleFactory(new ClangFormatStyleFactory);
|
2018-11-08 10:35:23 +01:00
|
|
|
}
|
|
|
|
|
|
2018-08-29 15:58:13 +02:00
|
|
|
bool ClangFormatPlugin::initialize(const QStringList &arguments, QString *errorString)
|
|
|
|
|
{
|
2019-05-07 16:51:22 +02:00
|
|
|
Q_UNUSED(arguments)
|
|
|
|
|
Q_UNUSED(errorString)
|
2018-11-20 11:23:30 +01:00
|
|
|
replaceCppCodeStyle();
|
2019-03-07 11:35:57 +01:00
|
|
|
|
2021-08-17 10:25:19 +02:00
|
|
|
ActionContainer *contextMenu = ActionManager::actionContainer(CppEditor::Constants::M_CONTEXT);
|
2019-03-07 11:35:57 +01:00
|
|
|
if (contextMenu) {
|
|
|
|
|
auto openClangFormatConfigAction
|
|
|
|
|
= new QAction(tr("Open Used .clang-format Configuration File"), this);
|
2021-08-17 10:25:19 +02:00
|
|
|
Command *command = ActionManager::registerAction(openClangFormatConfigAction,
|
|
|
|
|
Constants::OPEN_CURRENT_CONFIG_ID);
|
2019-03-07 11:35:57 +01:00
|
|
|
contextMenu->addSeparator();
|
|
|
|
|
contextMenu->addAction(command);
|
|
|
|
|
|
2021-08-17 10:25:19 +02:00
|
|
|
if (EditorManager::currentEditor()) {
|
|
|
|
|
if (const IDocument *doc = EditorManager::currentEditor()->document())
|
|
|
|
|
openClangFormatConfigAction->setData(doc->filePath().toVariant());
|
2019-03-07 11:35:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
connect(openClangFormatConfigAction,
|
|
|
|
|
&QAction::triggered,
|
|
|
|
|
this,
|
|
|
|
|
[openClangFormatConfigAction]() {
|
2021-08-17 10:25:19 +02:00
|
|
|
const FilePath fileName = FilePath::fromVariant(openClangFormatConfigAction->data());
|
|
|
|
|
if (!fileName.isEmpty())
|
2021-11-01 17:02:02 +01:00
|
|
|
EditorManager::openEditor(FilePath::fromString(configForFile(fileName)));
|
2019-03-07 11:35:57 +01:00
|
|
|
});
|
|
|
|
|
|
2021-08-17 10:25:19 +02:00
|
|
|
connect(EditorManager::instance(),
|
|
|
|
|
&EditorManager::currentEditorChanged,
|
2019-03-07 11:35:57 +01:00
|
|
|
this,
|
2021-08-17 10:25:19 +02:00
|
|
|
[openClangFormatConfigAction](IEditor *editor) {
|
2019-03-07 11:35:57 +01:00
|
|
|
if (!editor)
|
|
|
|
|
return;
|
|
|
|
|
|
2021-08-17 10:25:19 +02:00
|
|
|
if (const IDocument *doc = editor->document())
|
|
|
|
|
openClangFormatConfigAction->setData(doc->filePath().toVariant());
|
2019-03-07 11:35:57 +01:00
|
|
|
});
|
|
|
|
|
}
|
2020-12-18 11:10:09 +01:00
|
|
|
#ifndef KEEP_LINE_BREAKS_FOR_NON_EMPTY_LINES_BACKPORTED
|
2020-06-13 20:40:03 +02:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
|
#pragma message( \
|
2020-12-18 11:10:09 +01:00
|
|
|
"ClangFormat: building against unmodified Clang, see README.md for more info")
|
2020-06-13 20:40:03 +02:00
|
|
|
#else
|
2020-12-18 11:10:09 +01:00
|
|
|
#warning ClangFormat: building against unmodified Clang, see README.md for more info
|
2020-06-13 20:40:03 +02:00
|
|
|
#endif
|
2021-08-17 10:25:19 +02:00
|
|
|
static const Id clangFormatFormatWarningKey = "ClangFormatFormatWarning";
|
|
|
|
|
if (!ICore::infoBar()->canInfoBeAdded(clangFormatFormatWarningKey))
|
2020-12-18 11:10:09 +01:00
|
|
|
return true;
|
2021-08-17 10:25:19 +02:00
|
|
|
InfoBarEntry
|
2020-12-18 11:10:09 +01:00
|
|
|
info(clangFormatFormatWarningKey,
|
|
|
|
|
tr("The ClangFormat plugin has been built against an unmodified Clang. "
|
|
|
|
|
"You might experience formatting glitches in certain circumstances. "
|
|
|
|
|
"See https://code.qt.io/cgit/qt-creator/qt-creator.git/tree/README.md for more "
|
|
|
|
|
"information."),
|
2021-08-17 10:25:19 +02:00
|
|
|
InfoBarEntry::GlobalSuppression::Enabled);
|
|
|
|
|
ICore::infoBar()->addInfo(info);
|
2019-05-07 11:28:50 +02:00
|
|
|
#endif
|
2021-01-07 07:52:14 +01:00
|
|
|
return true;
|
2018-08-29 15:58:13 +02:00
|
|
|
}
|
|
|
|
|
|
2022-01-14 15:08:11 +01:00
|
|
|
QVector<QObject *> ClangFormatPlugin::createTestObjects() const
|
|
|
|
|
{
|
|
|
|
|
return {
|
|
|
|
|
#ifdef WITH_TESTS
|
|
|
|
|
new Internal::ClangFormatTest,
|
|
|
|
|
#endif
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-29 15:58:13 +02:00
|
|
|
} // namespace ClangFormat
|