aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/cppeditor/quickfixes/cppquickfix.h
blob: 71630e5bc2926d4e67ade9be647a2394e0bc980b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#pragma once

#include "../cppeditor_global.h"
#include "cppquickfixassistant.h"

#include <extensionsystem/iplugin.h>
#include <texteditor/quickfix.h>

#include <QVersionNumber>

#include <optional>

#ifdef WITH_TESTS
#include "cppquickfix_test.h"
#endif

namespace CppEditor {
namespace Internal {

class CppQuickFixOperation
    : public TextEditor::QuickFixOperation,
      public Internal::CppQuickFixInterface
{
public:
    explicit CppQuickFixOperation(const CppQuickFixInterface &interface, int priority = -1)
        : QuickFixOperation(priority), CppQuickFixInterface(interface)
    {}
    ~CppQuickFixOperation() override;
};

void createCppQuickFixFactories();
void destroyCppQuickFixFactories();

} // namespace Internal

/*!
    The QuickFixFactory is responsible for generating QuickFixOperation s which are
    applicable to the given QuickFixState.

    A QuickFixFactory should not have any state -- it can be invoked multiple times
    for different QuickFixState objects to create the matching operations, before any
    of those operations are applied (or released).

    This way, a single factory can be used by multiple editors, and a single editor
    can have multiple QuickFixCollector objects for different parts of the code.
 */

class CPPEDITOR_EXPORT CppQuickFixFactory : public QObject
{
    Q_OBJECT

public:
    CppQuickFixFactory();
    ~CppQuickFixFactory() override;

    using QuickFixOperations = TextEditor::QuickFixOperations;

    void match(const Internal::CppQuickFixInterface &interface, QuickFixOperations &result);

    static const QList<CppQuickFixFactory *> &cppQuickFixFactories();

    std::optional<QVersionNumber> clangdReplacement() const { return m_clangdReplacement; }
    void setClangdReplacement(const QVersionNumber &version) { m_clangdReplacement = version; }

    template<class Factory> static void registerFactory()
    {
        new Factory;
#ifdef WITH_TESTS
        cppEditor()->addTestCreator(Factory::createTest);
#endif
    }

#ifdef WITH_TESTS
    template<class Factory, class Test> static void registerFactoryWithStandardTest()
    {
        new Factory;
        cppEditor()->addTestCreator([] {
            auto factory = std::make_unique<Factory>();
            factory->enableTestMode();
            return new Test(std::move(factory));
        });
    }
#endif

    void enableTestMode() { m_testMode = true; }

    static ExtensionSystem::IPlugin *cppEditor();

protected:
    bool testMode() const { return m_testMode; }

private:
    /*!
        Implement this function to match and create the appropriate
        CppQuickFixOperation objects.
        Make sure that the function is "cheap". Otherwise, since the match()
        functions are also called to generate context menu entries,
        the user might experience a delay opening the context menu.
     */
    virtual void doMatch(const Internal::CppQuickFixInterface &interface,
                         QuickFixOperations &result) = 0;

    std::optional<QVersionNumber> m_clangdReplacement;
    bool m_testMode = false;
};

} // namespace CppEditor

#ifdef WITH_TESTS
#define REGISTER_QUICKFIX_FACTORY_WITH_STANDARD_TEST(Factory) \
    CppQuickFixFactory::registerFactoryWithStandardTest<Factory, Factory##Test>()
#else
#define REGISTER_QUICKFIX_FACTORY_WITH_STANDARD_TEST(Factory) \
    CppQuickFixFactory::registerFactory<Factory>()
#endif