2018-08-29 15:58:13 +02:00
|
|
|
/****************************************************************************
|
|
|
|
|
**
|
|
|
|
|
** Copyright (C) 2016 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 "clangformatplugin.h"
|
|
|
|
|
|
|
|
|
|
#include "clangformatconfigwidget.h"
|
2019-03-07 11:35:57 +01:00
|
|
|
#include "clangformatconstants.h"
|
2018-08-29 15:58:13 +02:00
|
|
|
#include "clangformatindenter.h"
|
2019-03-07 11:35:57 +01:00
|
|
|
#include "clangformatutils.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>
|
|
|
|
|
#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>
|
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
|
|
|
{
|
|
|
|
|
public:
|
2019-01-22 14:16:30 +01:00
|
|
|
TextEditor::CodeStyleEditorWidget *createCodeStyleEditor(
|
2021-08-17 10:25:19 +02:00
|
|
|
TextEditor::ICodeStylePreferences *preferences, QWidget *parent) override
|
2018-08-29 15:58:13 +02:00
|
|
|
{
|
2019-09-11 08:22:53 +03:00
|
|
|
Q_UNUSED(preferences);
|
2018-11-20 11:23:30 +01:00
|
|
|
if (!parent)
|
|
|
|
|
return new ClangFormatConfigWidget;
|
|
|
|
|
return new ClangFormatConfigWidget(SessionManager::startupProject());
|
2018-08-29 15:58:13 +02:00
|
|
|
}
|
|
|
|
|
|
2018-11-20 11:23:30 +01:00
|
|
|
QWidget *createEditor(TextEditor::ICodeStylePreferences *, QWidget *) const override
|
2018-08-29 15:58:13 +02:00
|
|
|
{
|
2018-11-20 11:23:30 +01:00
|
|
|
return nullptr;
|
2018-08-29 15:58:13 +02:00
|
|
|
}
|
|
|
|
|
|
2019-01-16 09:37:54 +01:00
|
|
|
TextEditor::Indenter *createIndenter(QTextDocument *doc) const override
|
2018-08-29 15:58:13 +02:00
|
|
|
{
|
2019-01-16 09:37:54 +01:00
|
|
|
return new ClangFormatIndenter(doc);
|
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())
|
|
|
|
|
EditorManager::openEditor(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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace ClangFormat
|