cppquickfix.h 4.86 KB
Newer Older
1 2 3 4
/**************************************************************************
**
** This file is part of Qt Creator
**
hjk's avatar
hjk committed
5
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
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
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** Commercial Usage
**
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** GNU Lesser General Public License Usage
**
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://qt.nokia.com/contact.
**
**************************************************************************/

#ifndef CPPQUICKFIX_H
#define CPPQUICKFIX_H

33
#include "cppeditor_global.h"
34
#include "cppsemanticinfo.h"
35

36
#include <ASTfwd.h>
37
#include <cplusplus/CppDocument.h>
38
#include <cpptools/cpprefactoringchanges.h>
39 40
#include <texteditor/icompletioncollector.h>
#include <texteditor/quickfix.h>
41
#include <utils/changeset.h>
42

43 44 45 46 47 48 49
#include <QtCore/QSharedPointer>
#include <QtGui/QTextCursor>

namespace CppTools {
    class CppModelManagerInterface;
} // end of namespace CppTools

50 51 52 53
namespace ExtensionSystem {
class IPlugin;
}

54
namespace CppEditor {
55

56
namespace Internal {
57 58
class CppQuickFixCollector;
} // end of namespace Internal
59

60
class CPPEDITOR_EXPORT CppQuickFixState: public TextEditor::QuickFixState
61
{
62
    friend class Internal::CppQuickFixCollector;
63 64

public:
65
    CppQuickFixState(TextEditor::BaseTextEditor *editor);
66

67 68
    const QList<CPlusPlus::AST *> &path() const;
    CPlusPlus::Snapshot snapshot() const;
Roberto Raggi's avatar
Roberto Raggi committed
69
    CPlusPlus::Document::Ptr document() const;
70
    CppEditor::Internal::SemanticInfo semanticInfo() const;
71
    const CPlusPlus::LookupContext &context() const;
Roberto Raggi's avatar
Roberto Raggi committed
72

73
    const CppTools::CppRefactoringFile currentFile() const;
74

75 76 77 78
    bool isCursorOn(unsigned tokenIndex) const
    { return currentFile().isCursorOn(tokenIndex); }
    bool isCursorOn(const CPlusPlus::AST *ast) const
    { return currentFile().isCursorOn(ast); }
Roberto Raggi's avatar
Cleanup  
Roberto Raggi committed
79

80
private:
81 82 83 84
    QList<CPlusPlus::AST *> _path;
    CPlusPlus::Snapshot _snapshot;
    CppEditor::Internal::SemanticInfo _semanticInfo;
    CPlusPlus::LookupContext _context;
85 86
};

87
class CPPEDITOR_EXPORT CppQuickFixOperation: public TextEditor::QuickFixOperation
88
{
89
    Q_DISABLE_COPY(CppQuickFixOperation)
90 91

public:
92
    explicit CppQuickFixOperation(const CppQuickFixState &state, int priority = -1);
93
    virtual ~CppQuickFixOperation();
94

95 96
    virtual void perform();

97
protected:
98 99
    virtual void performChanges(CppTools::CppRefactoringFile *currentFile,
                                CppTools::CppRefactoringChanges *refactoring) = 0;
100

101
    QString fileName() const;
102 103 104 105 106 107 108 109

    const CppQuickFixState &state() const;

protected: // Utility functions forwarding to CppQuickFixState
    typedef Utils::ChangeSet::Range Range;

private:
    CppQuickFixState _state;
Roberto Raggi's avatar
Roberto Raggi committed
110 111
};

112
class CPPEDITOR_EXPORT CppQuickFixFactory: public TextEditor::QuickFixFactory
Roberto Raggi's avatar
Roberto Raggi committed
113 114 115 116
{
    Q_OBJECT

public:
117
    CppQuickFixFactory();
Roberto Raggi's avatar
Roberto Raggi committed
118 119
    virtual ~CppQuickFixFactory();

120
    virtual QList<TextEditor::QuickFixOperation::Ptr> matchingOperations(TextEditor::QuickFixState *state);
121 122 123 124
    /*!
        Implement this method to match and create the appropriate
        CppQuickFixOperation objects.
     */
125 126 127
    virtual QList<CppQuickFixOperation::Ptr> match(const CppQuickFixState &state) = 0;

protected:
128 129 130 131 132
    /*!
        Creates a list of 1 single element: the shared-pointer to the given
        operation. This shared-pointer takes over the ownership (meaning the
        responsibility to delete the operation).
     */
133
    static QList<CppQuickFixOperation::Ptr> singleResult(CppQuickFixOperation *operation);
134 135

    /// Utility method which creates an empty list.
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
    static QList<CppQuickFixOperation::Ptr> noResult();
};

namespace Internal {

class CppQuickFixCollector: public TextEditor::QuickFixCollector
{
    Q_OBJECT

public:
    CppQuickFixCollector();
    virtual ~CppQuickFixCollector();

    virtual bool supportsEditor(TextEditor::ITextEditable *editor);
    virtual TextEditor::QuickFixState *initializeCompletion(TextEditor::BaseTextEditor *editor);

    virtual QList<TextEditor::QuickFixFactory *> quickFixFactories() const;

154
    /// Registers all quick-fixes in this plug-in as auto-released objects.
155
    static void registerQuickFixes(ExtensionSystem::IPlugin *plugIn);
156 157 158 159 160 161
};

} // end of namespace Internal
} // end of namespace CppEditor

#endif // CPPQUICKFIX_H