cppeditor.h 9.35 KB
Newer Older
1
/**************************************************************************
con's avatar
con committed
2 3 4
**
** This file is part of Qt Creator
**
con's avatar
con committed
5
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
con's avatar
con committed
6
**
hjk's avatar
hjk committed
7
** Contact: Nokia Corporation (info@qt.nokia.com)
con's avatar
con committed
8
**
9
**
10
** GNU Lesser General Public License Usage
11
**
hjk's avatar
hjk committed
12 13 14 15 16 17
** 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.
18
**
con's avatar
con committed
19
** In addition, as a special exception, Nokia gives you certain additional
hjk's avatar
hjk committed
20
** rights. These rights are described in the Nokia Qt LGPL Exception
con's avatar
con committed
21 22
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
23 24 25 26 27
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
con's avatar
con committed
28
** If you have questions regarding the use of this file, please contact
Tobias Hunger's avatar
Tobias Hunger committed
29
** Nokia at info@qt.nokia.com.
con's avatar
con committed
30
**
31
**************************************************************************/
hjk's avatar
hjk committed
32

con's avatar
con committed
33 34 35 36
#ifndef CPPEDITOR_H
#define CPPEDITOR_H

#include "cppeditorenums.h"
37
#include "cppquickfix.h"
38 39
#include "cppsemanticinfo.h"

40
#include <cplusplus/ModelManagerInterface.h>
con's avatar
con committed
41
#include <cplusplus/CppDocument.h>
Roberto Raggi's avatar
Roberto Raggi committed
42
#include <cplusplus/LookupContext.h>
con's avatar
con committed
43 44
#include <texteditor/basetexteditor.h>

Roberto Raggi's avatar
Roberto Raggi committed
45 46 47
#include <QtCore/QThread>
#include <QtCore/QMutex>
#include <QtCore/QWaitCondition>
48
#include <QtCore/QFutureWatcher>
49
#include <QtCore/QModelIndex>
50
#include <QtCore/QVector>
Roberto Raggi's avatar
Roberto Raggi committed
51

con's avatar
con committed
52 53
QT_BEGIN_NAMESPACE
class QComboBox;
54
class QSortFilterProxyModel;
con's avatar
con committed
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
QT_END_NAMESPACE

namespace CPlusPlus {
class OverviewModel;
class Symbol;
}

namespace CppTools {
class CppModelManagerInterface;
}

namespace TextEditor {
class FontSettings;
}

namespace CppEditor {
namespace Internal {

73
class CPPEditorWidget;
Roberto Raggi's avatar
Roberto Raggi committed
74

75
class SemanticHighlighter: public QThread, CPlusPlus::TopLevelDeclarationProcessor
Roberto Raggi's avatar
Roberto Raggi committed
76 77 78 79 80 81 82
{
    Q_OBJECT

public:
    SemanticHighlighter(QObject *parent = 0);
    virtual ~SemanticHighlighter();

83 84
    virtual bool processDeclaration(CPlusPlus::DeclarationAST *) { return m_done; }

Roberto Raggi's avatar
Roberto Raggi committed
85 86 87 88 89 90 91 92 93 94
    void abort();

    struct Source
    {
        CPlusPlus::Snapshot snapshot;
        QString fileName;
        QString code;
        int line;
        int column;
        int revision;
Roberto Raggi's avatar
Roberto Raggi committed
95
        bool force;
Roberto Raggi's avatar
Roberto Raggi committed
96 97

        Source()
Roberto Raggi's avatar
Roberto Raggi committed
98
            : line(0), column(0), revision(0), force(false)
Roberto Raggi's avatar
Roberto Raggi committed
99 100 101 102 103 104 105 106 107
        { }

        Source(const CPlusPlus::Snapshot &snapshot,
               const QString &fileName,
               const QString &code,
               int line, int column,
               int revision)
            : snapshot(snapshot), fileName(fileName),
              code(code), line(line), column(column),
Roberto Raggi's avatar
Roberto Raggi committed
108
              revision(revision), force(false)
Roberto Raggi's avatar
Roberto Raggi committed
109 110 111 112
        { }

        void clear()
        {
113
            snapshot = CPlusPlus::Snapshot();
Roberto Raggi's avatar
Roberto Raggi committed
114 115 116 117 118
            fileName.clear();
            code.clear();
            line = 0;
            column = 0;
            revision = 0;
Roberto Raggi's avatar
Roberto Raggi committed
119
            force = false;
Roberto Raggi's avatar
Roberto Raggi committed
120 121 122
        }
    };

123
    SemanticInfo semanticInfo(const Source &source);
Roberto Raggi's avatar
Roberto Raggi committed
124 125 126 127

    void rehighlight(const Source &source);

Q_SIGNALS:
128
    void changed(const CppEditor::Internal::SemanticInfo &semanticInfo);
Roberto Raggi's avatar
Roberto Raggi committed
129 130 131 132

protected:
    virtual void run();

133 134 135
private:
    bool isOutdated();

Roberto Raggi's avatar
Roberto Raggi committed
136 137 138 139 140
private:
    QMutex m_mutex;
    QWaitCondition m_condition;
    bool m_done;
    Source m_source;
141
    SemanticInfo m_lastSemanticInfo;
Roberto Raggi's avatar
Roberto Raggi committed
142
};
con's avatar
con committed
143

144
class CPPEditor : public TextEditor::BaseTextEditor
con's avatar
con committed
145 146 147
{
    Q_OBJECT
public:
148
    CPPEditor(CPPEditorWidget *);
con's avatar
con committed
149 150 151

    bool duplicateSupported() const { return true; }
    Core::IEditor *duplicate(QWidget *parent);
152
    QString id() const;
con's avatar
con committed
153

con's avatar
con committed
154
    bool isTemporary() const { return false; }
155
    virtual bool open(QString *errorString, const QString &fileName, const QString &realFileName);
con's avatar
con committed
156 157
};

158
class CPPEditorWidget : public TextEditor::BaseTextEditorWidget
con's avatar
con committed
159 160 161 162 163 164
{
    Q_OBJECT

public:
    typedef TextEditor::TabSettings TabSettings;

165 166
    CPPEditorWidget(QWidget *parent);
    ~CPPEditorWidget();
con's avatar
con committed
167 168
    void unCommentSelection();

169
    unsigned editorRevision() const;
170
    bool isOutdated() const;
Roberto Raggi's avatar
Roberto Raggi committed
171
    SemanticInfo semanticInfo() const;
172

Kai Koehne's avatar
Kai Koehne committed
173 174
    CPlusPlus::OverviewModel *outlineModel() const;
    QModelIndex outlineModelIndex();
Roberto Raggi's avatar
Roberto Raggi committed
175

176 177
    virtual void paste(); // reimplemented from BaseTextEditorWidget
    virtual void cut(); // reimplemented from BaseTextEditorWidget
mae's avatar
mae committed
178

179
    CPlusPlus::CppModelManagerInterface *modelManager() const;
180

181 182 183 184 185
    virtual void setMimeType(const QString &mt);

    void setObjCEnabled(bool onoff);
    bool isObjCEnabled() const;

186 187 188 189
    bool openLink(const Link &link) { return openCppEditorAt(link); }

    static Link linkToSymbol(CPlusPlus::Symbol *symbol);

190 191
    static QVector<QString> highlighterFormatCategories();

192
Q_SIGNALS:
Kai Koehne's avatar
Kai Koehne committed
193
    void outlineModelIndexChanged(const QModelIndex &index);
194

Roberto Raggi's avatar
Roberto Raggi committed
195
public Q_SLOTS:
con's avatar
con committed
196
    virtual void setFontSettings(const TextEditor::FontSettings &);
Christian Kamm's avatar
Christian Kamm committed
197
    virtual void setTabSettings(const TextEditor::TabSettings &);
Kai Koehne's avatar
Kai Koehne committed
198
    void setSortedOutline(bool sort);
con's avatar
con committed
199 200
    void switchDeclarationDefinition();
    void jumpToDefinition();
201
    void renameSymbolUnderCursor();
202 203
    void renameUsages();
    void findUsages();
204
    void renameUsagesNow(const QString &replacement = QString());
205
    void rehighlight(bool force = false);
con's avatar
con committed
206 207

protected:
Thorbjørn Lindeijer's avatar
Thorbjørn Lindeijer committed
208
    bool event(QEvent *e);
209
    void contextMenuEvent(QContextMenuEvent *);
Thorbjørn Lindeijer's avatar
Thorbjørn Lindeijer committed
210
    void keyPressEvent(QKeyEvent *);
211

212
    TextEditor::BaseTextEditor *createEditor();
con's avatar
con committed
213

Christian Kamm's avatar
Christian Kamm committed
214 215
    const CPlusPlus::Macro *findCanonicalMacro(const QTextCursor &cursor,
                                               CPlusPlus::Document::Ptr doc) const;
216

Roberto Raggi's avatar
Roberto Raggi committed
217
private Q_SLOTS:
con's avatar
con committed
218
    void updateFileName();
Kai Koehne's avatar
Kai Koehne committed
219
    void jumpToOutlineElement(int index);
220
    void updateOutlineNow();
Kai Koehne's avatar
Kai Koehne committed
221 222 223
    void updateOutlineIndex();
    void updateOutlineIndexNow();
    void updateOutlineToolTip();
Thorbjørn Lindeijer's avatar
Thorbjørn Lindeijer committed
224 225
    void updateUses();
    void updateUsesNow();
con's avatar
con committed
226
    void onDocumentUpdated(CPlusPlus::Document::Ptr doc);
227
    void onContentsChanged(int position, int charsRemoved, int charsAdded);
con's avatar
con committed
228

Roberto Raggi's avatar
Roberto Raggi committed
229
    void semanticRehighlight();
230
    void updateSemanticInfo(const CppEditor::Internal::SemanticInfo &semanticInfo);
231 232
    void highlightSymbolUsages(int from, int to);
    void finishHighlightSymbolUsages();
Roberto Raggi's avatar
Roberto Raggi committed
233

234 235
    void markSymbolsNow();

236 237
    void performQuickFix(int index);

con's avatar
con committed
238
private:
239
    void markSymbols(const QTextCursor &tc, const SemanticInfo &info);
Kai Koehne's avatar
Kai Koehne committed
240
    bool sortedOutline() const;
241
    CPlusPlus::Symbol *findDefinition(CPlusPlus::Symbol *symbol, const CPlusPlus::Snapshot &snapshot) const;
con's avatar
con committed
242

243 244 245
    TextEditor::ITextEditor *openCppEditorAt(const QString &fileName, int line,
                                             int column = 0);

Roberto Raggi's avatar
Roberto Raggi committed
246
    SemanticHighlighter::Source currentSource(bool force = false);
Roberto Raggi's avatar
Roberto Raggi committed
247

248
    void highlightUses(const QList<SemanticInfo::Use> &uses,
249
                       const SemanticInfo &semanticInfo,
250 251
                       QList<QTextEdit::ExtraSelection> *selections);

252
    void createToolBar(CPPEditor *editable);
con's avatar
con committed
253

mae's avatar
mae committed
254 255
    void startRename();
    void finishRename();
256 257
    void abortRename();

258 259 260
    Link attemptFuncDeclDef(const QTextCursor &cursor,
                            const CPlusPlus::Document::Ptr &doc,
                            CPlusPlus::Snapshot snapshot) const;
261
    Link findLinkAt(const QTextCursor &, bool resolveTarget = true);
262 263 264 265
    Link findMacroLink(const QByteArray &name) const;
    Link findMacroLink(const QByteArray &name, CPlusPlus::Document::Ptr doc, const CPlusPlus::Snapshot &snapshot,
                       QSet<QString> *processed) const;
    QString identifierUnderCursor(QTextCursor *macroCursor) const;
266
    bool openCppEditorAt(const Link &);
267

268
    QModelIndex indexForPosition(int line, int column, const QModelIndex &rootIndex = QModelIndex()) const;
269

270
    CPlusPlus::CppModelManagerInterface *m_modelManager;
con's avatar
con committed
271

Kai Koehne's avatar
Kai Koehne committed
272 273 274
    QComboBox *m_outlineCombo;
    CPlusPlus::OverviewModel *m_outlineModel;
    QModelIndex m_outlineModelIndex;
275 276
    QSortFilterProxyModel *m_proxyModel;
    QAction *m_sortAction;
277
    QTimer *m_updateOutlineTimer;
Kai Koehne's avatar
Kai Koehne committed
278
    QTimer *m_updateOutlineIndexTimer;
Thorbjørn Lindeijer's avatar
Thorbjørn Lindeijer committed
279
    QTimer *m_updateUsesTimer;
280
    QTextCharFormat m_occurrencesFormat;
Roberto Raggi's avatar
Roberto Raggi committed
281
    QTextCharFormat m_occurrencesUnusedFormat;
282
    QTextCharFormat m_occurrenceRenameFormat;
Roberto Raggi's avatar
Roberto Raggi committed
283
    QTextCharFormat m_typeFormat;
284 285
    QTextCharFormat m_localFormat;
    QTextCharFormat m_fieldFormat;
286
    QTextCharFormat m_staticFormat;
287
    QTextCharFormat m_keywordFormat;
288
    QTextCharFormat m_virtualMethodFormat;
Thorbjørn Lindeijer's avatar
Thorbjørn Lindeijer committed
289 290 291

    QList<QTextEdit::ExtraSelection> m_renameSelections;
    int m_currentRenameSelection;
292
    static const int NoCurrentRenameSelection = -1;
mae's avatar
mae committed
293 294 295
    bool m_inRename, m_inRenameChanged, m_firstRenameChange;
    QTextCursor m_currentRenameSelectionBegin;
    QTextCursor m_currentRenameSelectionEnd;
Roberto Raggi's avatar
Roberto Raggi committed
296 297

    SemanticHighlighter *m_semanticHighlighter;
Roberto Raggi's avatar
Roberto Raggi committed
298
    SemanticInfo m_lastSemanticInfo;
299
    QList<TextEditor::QuickFixOperation::Ptr> m_quickFixes;
300
    bool m_objcEnabled;
Roberto Raggi's avatar
Roberto Raggi committed
301
    bool m_initialized;
302 303 304

    QFuture<SemanticInfo::Use> m_highlighter;
    QFutureWatcher<SemanticInfo::Use> m_highlightWatcher;
305 306
    unsigned m_highlightRevision; // the editor revision that requested the highlight
    int m_nextHighlightBlockNumber;
307 308 309 310 311

    QFuture<QList<int> > m_references;
    QFutureWatcher<QList<int> > m_referencesWatcher;
    unsigned m_referencesRevision;
    int m_referencesCursorPosition;
con's avatar
con committed
312 313
};

Roberto Raggi's avatar
Roberto Raggi committed
314

con's avatar
con committed
315 316 317 318
} // namespace Internal
} // namespace CppEditor

#endif // CPPEDITOR_H