ClangFormat: Code cosmetics

Change-Id: I2aa83d4121a2c255547e14a32b0fe4bfd11dc72b
Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
This commit is contained in:
hjk
2021-08-17 10:25:19 +02:00
parent 9a73d394c3
commit 54b372a082

View File

@@ -67,7 +67,9 @@
#include <QMessageBox> #include <QMessageBox>
#include <QMenu> #include <QMenu>
using namespace Core;
using namespace ProjectExplorer; using namespace ProjectExplorer;
using namespace Utils;
namespace ClangFormat { namespace ClangFormat {
@@ -75,7 +77,7 @@ class ClangFormatStyleFactory : public CppTools::CppCodeStylePreferencesFactory
{ {
public: public:
TextEditor::CodeStyleEditorWidget *createCodeStyleEditor( TextEditor::CodeStyleEditorWidget *createCodeStyleEditor(
TextEditor::ICodeStylePreferences *preferences, QWidget *parent = nullptr) override TextEditor::ICodeStylePreferences *preferences, QWidget *parent) override
{ {
Q_UNUSED(preferences); Q_UNUSED(preferences);
if (!parent) if (!parent)
@@ -98,8 +100,7 @@ static void replaceCppCodeStyle()
{ {
using namespace TextEditor; using namespace TextEditor;
TextEditorSettings::unregisterCodeStyleFactory(CppTools::Constants::CPP_SETTINGS_ID); TextEditorSettings::unregisterCodeStyleFactory(CppTools::Constants::CPP_SETTINGS_ID);
ICodeStylePreferencesFactory *factory = new ClangFormatStyleFactory(); TextEditorSettings::registerCodeStyleFactory(new ClangFormatStyleFactory);
TextEditorSettings::registerCodeStyleFactory(factory);
} }
bool ClangFormatPlugin::initialize(const QStringList &arguments, QString *errorString) bool ClangFormatPlugin::initialize(const QStringList &arguments, QString *errorString)
@@ -108,45 +109,38 @@ bool ClangFormatPlugin::initialize(const QStringList &arguments, QString *errorS
Q_UNUSED(errorString) Q_UNUSED(errorString)
replaceCppCodeStyle(); replaceCppCodeStyle();
Core::ActionContainer *contextMenu = Core::ActionManager::actionContainer( ActionContainer *contextMenu = ActionManager::actionContainer(CppEditor::Constants::M_CONTEXT);
CppEditor::Constants::M_CONTEXT);
if (contextMenu) { if (contextMenu) {
auto openClangFormatConfigAction auto openClangFormatConfigAction
= new QAction(tr("Open Used .clang-format Configuration File"), this); = new QAction(tr("Open Used .clang-format Configuration File"), this);
Core::Command *command Command *command = ActionManager::registerAction(openClangFormatConfigAction,
= Core::ActionManager::registerAction(openClangFormatConfigAction,
Constants::OPEN_CURRENT_CONFIG_ID); Constants::OPEN_CURRENT_CONFIG_ID);
contextMenu->addSeparator(); contextMenu->addSeparator();
contextMenu->addAction(command); contextMenu->addAction(command);
if (Core::EditorManager::currentEditor()) { if (EditorManager::currentEditor()) {
const Core::IDocument *doc = Core::EditorManager::currentEditor()->document(); if (const IDocument *doc = EditorManager::currentEditor()->document())
if (doc) openClangFormatConfigAction->setData(doc->filePath().toVariant());
openClangFormatConfigAction->setData(doc->filePath().toString());
} }
connect(openClangFormatConfigAction, connect(openClangFormatConfigAction,
&QAction::triggered, &QAction::triggered,
this, this,
[openClangFormatConfigAction]() { [openClangFormatConfigAction]() {
const QString fileName = openClangFormatConfigAction->data().toString(); const FilePath fileName = FilePath::fromVariant(openClangFormatConfigAction->data());
if (!fileName.isEmpty()) { if (!fileName.isEmpty())
const QString clangFormatConfigPath = configForFile( EditorManager::openEditor(configForFile(fileName));
Utils::FilePath::fromString(fileName));
Core::EditorManager::openEditor(clangFormatConfigPath);
}
}); });
connect(Core::EditorManager::instance(), connect(EditorManager::instance(),
&Core::EditorManager::currentEditorChanged, &EditorManager::currentEditorChanged,
this, this,
[openClangFormatConfigAction](Core::IEditor *editor) { [openClangFormatConfigAction](IEditor *editor) {
if (!editor) if (!editor)
return; return;
const Core::IDocument *doc = editor->document(); if (const IDocument *doc = editor->document())
if (doc) openClangFormatConfigAction->setData(doc->filePath().toVariant());
openClangFormatConfigAction->setData(doc->filePath().toString());
}); });
} }
#ifndef KEEP_LINE_BREAKS_FOR_NON_EMPTY_LINES_BACKPORTED #ifndef KEEP_LINE_BREAKS_FOR_NON_EMPTY_LINES_BACKPORTED
@@ -156,17 +150,17 @@ bool ClangFormatPlugin::initialize(const QStringList &arguments, QString *errorS
#else #else
#warning ClangFormat: building against unmodified Clang, see README.md for more info #warning ClangFormat: building against unmodified Clang, see README.md for more info
#endif #endif
static const char clangFormatFormatWarningKey[] = "ClangFormatFormatWarning"; static const Id clangFormatFormatWarningKey = "ClangFormatFormatWarning";
if (!Core::ICore::infoBar()->canInfoBeAdded(clangFormatFormatWarningKey)) if (!ICore::infoBar()->canInfoBeAdded(clangFormatFormatWarningKey))
return true; return true;
Utils::InfoBarEntry InfoBarEntry
info(clangFormatFormatWarningKey, info(clangFormatFormatWarningKey,
tr("The ClangFormat plugin has been built against an unmodified Clang. " tr("The ClangFormat plugin has been built against an unmodified Clang. "
"You might experience formatting glitches in certain circumstances. " "You might experience formatting glitches in certain circumstances. "
"See https://code.qt.io/cgit/qt-creator/qt-creator.git/tree/README.md for more " "See https://code.qt.io/cgit/qt-creator/qt-creator.git/tree/README.md for more "
"information."), "information."),
Utils::InfoBarEntry::GlobalSuppression::Enabled); InfoBarEntry::GlobalSuppression::Enabled);
Core::ICore::infoBar()->addInfo(info); ICore::infoBar()->addInfo(info);
#endif #endif
return true; return true;
} }