cppeditor.h 8.59 KB
Newer Older
1
/**************************************************************************
con's avatar
con committed
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).
con's avatar
con committed
6
**
7
** Contact: Nokia Corporation (qt-info@nokia.com)
con's avatar
con committed
8
**
9
** Commercial Usage
10
**
11
12
13
14
** 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.
15
**
16
** GNU Lesser General Public License Usage
17
**
18
19
20
21
22
23
** 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.
24
**
25
** If you are unsure which license is appropriate for your use, please
hjk's avatar
hjk committed
26
** contact the sales department at http://qt.nokia.com/contact.
con's avatar
con committed
27
**
28
**************************************************************************/
hjk's avatar
hjk committed
29

con's avatar
con committed
30
31
32
33
#ifndef CPPEDITOR_H
#define CPPEDITOR_H

#include "cppeditorenums.h"
34
#include "cppquickfix.h"
con's avatar
con committed
35
36
37
#include <cplusplus/CppDocument.h>
#include <texteditor/basetexteditor.h>

Roberto Raggi's avatar
Roberto Raggi committed
38
39
40
41
#include <QtCore/QThread>
#include <QtCore/QMutex>
#include <QtCore/QWaitCondition>

con's avatar
con committed
42
43
QT_BEGIN_NAMESPACE
class QComboBox;
44
class QSortFilterProxyModel;
con's avatar
con committed
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
QT_END_NAMESPACE

namespace CPlusPlus {
class OverviewModel;
class Symbol;
}

namespace CppTools {
class CppModelManagerInterface;
}

namespace TextEditor {
class FontSettings;
}

namespace CppEditor {
namespace Internal {

class CPPEditor;
Roberto Raggi's avatar
Roberto Raggi committed
64
65
66
67
68
69
70
71
72
73
class SemanticHighlighter;

class SemanticInfo
{
public:
    struct Use {
        unsigned line;
        unsigned column;
        unsigned length;

74
75
        Use(unsigned line = 0, unsigned column = 0, unsigned length = 0)
            : line(line), column(column), length(length) {}
Roberto Raggi's avatar
Roberto Raggi committed
76
77
78
79
80
81
    };

    typedef QHash<CPlusPlus::Symbol *, QList<Use> > LocalUseMap;
    typedef QHashIterator<CPlusPlus::Symbol *, QList<Use> > LocalUseIterator;

    SemanticInfo()
82
        : revision(0), hasQ(false), hasD(false)
Roberto Raggi's avatar
Roberto Raggi committed
83
84
    { }

85
    unsigned revision;
86
87
    bool hasQ: 1;
    bool hasD: 1;
88
    CPlusPlus::Snapshot snapshot;
Roberto Raggi's avatar
Roberto Raggi committed
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
    CPlusPlus::Document::Ptr doc;
    LocalUseMap localUses;
};

class SemanticHighlighter: public QThread
{
    Q_OBJECT

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

    void abort();

    struct Source
    {
        CPlusPlus::Snapshot snapshot;
        QString fileName;
        QString code;
        int line;
        int column;
        int revision;
Roberto Raggi's avatar
Roberto Raggi committed
111
        bool force;
Roberto Raggi's avatar
Roberto Raggi committed
112
113

        Source()
Roberto Raggi's avatar
Roberto Raggi committed
114
            : line(0), column(0), revision(0), force(false)
Roberto Raggi's avatar
Roberto Raggi committed
115
116
117
118
119
120
121
122
123
        { }

        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
124
              revision(revision), force(false)
Roberto Raggi's avatar
Roberto Raggi committed
125
126
127
128
        { }

        void clear()
        {
129
            snapshot = CPlusPlus::Snapshot();
Roberto Raggi's avatar
Roberto Raggi committed
130
131
132
133
134
            fileName.clear();
            code.clear();
            line = 0;
            column = 0;
            revision = 0;
Roberto Raggi's avatar
Roberto Raggi committed
135
            force = false;
Roberto Raggi's avatar
Roberto Raggi committed
136
137
138
        }
    };

139
    SemanticInfo semanticInfo(const Source &source);
Roberto Raggi's avatar
Roberto Raggi committed
140
141
142
143

    void rehighlight(const Source &source);

Q_SIGNALS:
144
    void changed(const CppEditor::Internal::SemanticInfo &semanticInfo);
Roberto Raggi's avatar
Roberto Raggi committed
145
146
147
148

protected:
    virtual void run();

149
150
151
private:
    bool isOutdated();

Roberto Raggi's avatar
Roberto Raggi committed
152
153
154
155
156
private:
    QMutex m_mutex;
    QWaitCondition m_condition;
    bool m_done;
    Source m_source;
157
    SemanticInfo m_lastSemanticInfo;
Roberto Raggi's avatar
Roberto Raggi committed
158
};
con's avatar
con committed
159
160
161
162
163
164
165
166
167
168

class CPPEditorEditable : public TextEditor::BaseTextEditorEditable
{
    Q_OBJECT
public:
    CPPEditorEditable(CPPEditor *);
    QList<int> context() const;

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

con's avatar
con committed
171
    bool isTemporary() const { return false; }
dt's avatar
dt committed
172
    virtual bool open(const QString & fileName);
dt's avatar
dt committed
173

con's avatar
con committed
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
private:
    QList<int> m_context;
};

class CPPEditor : public TextEditor::BaseTextEditor
{
    Q_OBJECT

public:
    typedef TextEditor::TabSettings TabSettings;

    CPPEditor(QWidget *parent);
    ~CPPEditor();
    void unCommentSelection();

189
    unsigned editorRevision() const;
190
    bool isOutdated() const;
Roberto Raggi's avatar
Roberto Raggi committed
191
192
    SemanticInfo semanticInfo() const;

mae's avatar
mae committed
193
194
195
    virtual void paste(); // reimplemented from BaseTextEditor
    virtual void cut(); // reimplemented from BaseTextEditor

Roberto Raggi's avatar
Roberto Raggi committed
196
public Q_SLOTS:
con's avatar
con committed
197
    virtual void setFontSettings(const TextEditor::FontSettings &);
198
    void setSortedMethodOverview(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();
205
    void hideRenameNotification();
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

con's avatar
con committed
212
213
    TextEditor::BaseTextEditorEditable *createEditableInterface();

214
    // These override BaseTextEditor
215
    virtual bool isElectricCharacter(const QChar &ch) const;
216

217
218
    virtual QString insertMatchingBrace(const QTextCursor &tc, const QString &text,
                                        const QChar &la, int *skippedChars) const;
con's avatar
con committed
219

220
221
222
223
224
225
    virtual QString insertParagraphSeparator(const QTextCursor &tc) const;

    virtual bool contextAllowsAutoParentheses(const QTextCursor &cursor,
                                              const QString &textToInsert = QString()) const;

    virtual bool isInComment(const QTextCursor &cursor) const;
226

227
228
229
    CPlusPlus::Symbol *findCanonicalSymbol(const QTextCursor &cursor,
                                           CPlusPlus::Document::Ptr doc,
                                           const CPlusPlus::Snapshot &snapshot) const;
Christian Kamm's avatar
Christian Kamm committed
230
231
    const CPlusPlus::Macro *findCanonicalMacro(const QTextCursor &cursor,
                                               CPlusPlus::Document::Ptr doc) const;
232

Roberto Raggi's avatar
Roberto Raggi committed
233
private Q_SLOTS:
con's avatar
con committed
234
235
236
    void updateFileName();
    void jumpToMethod(int index);
    void updateMethodBoxIndex();
Roberto Raggi's avatar
Roberto Raggi committed
237
    void updateMethodBoxIndexNow();
238
    void updateMethodBoxToolTip();
Thorbjørn Lindeijer's avatar
Thorbjørn Lindeijer committed
239
240
    void updateUses();
    void updateUsesNow();
con's avatar
con committed
241
    void onDocumentUpdated(CPlusPlus::Document::Ptr doc);
242
    void onContentsChanged(int position, int charsRemoved, int charsAdded);
con's avatar
con committed
243

Roberto Raggi's avatar
Roberto Raggi committed
244
    void semanticRehighlight();
245
    void updateSemanticInfo(const CppEditor::Internal::SemanticInfo &semanticInfo);
Roberto Raggi's avatar
Roberto Raggi committed
246

247
248
    void performQuickFix(int index);

con's avatar
con committed
249
private:
250
251
252
    bool showWarningMessage() const;
    void setShowWarningMessage(bool showWarningMessage);

253
    CPlusPlus::Symbol *markSymbols();
254
    bool sortedMethodOverview() const;
con's avatar
con committed
255
256
257
    CPlusPlus::Symbol *findDefinition(CPlusPlus::Symbol *symbol);
    virtual void indentBlock(QTextDocument *doc, QTextBlock block, QChar typedChar);

258
259
260
    TextEditor::ITextEditor *openCppEditorAt(const QString &fileName, int line,
                                             int column = 0);

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

263
    void highlightUses(const QList<SemanticInfo::Use> &uses,
264
                       const SemanticInfo &semanticInfo,
265
266
                       QList<QTextEdit::ExtraSelection> *selections);

con's avatar
con committed
267
268
    void createToolBar(CPPEditorEditable *editable);

mae's avatar
mae committed
269
270
    void startRename();
    void finishRename();
271
272
    void abortRename();

273
274
    Link findLinkAt(const QTextCursor &, bool resolveTarget = true);
    bool openLink(const Link &link) { return openCppEditorAt(link); }
275
    bool openCppEditorAt(const Link &);
276

277
    static Link linkToSymbol(CPlusPlus::Symbol *symbol);
con's avatar
con committed
278
279
280
281
282
283

    CppTools::CppModelManagerInterface *m_modelManager;

    QList<int> m_contexts;
    QComboBox *m_methodCombo;
    CPlusPlus::OverviewModel *m_overviewModel;
284
285
    QSortFilterProxyModel *m_proxyModel;
    QAction *m_sortAction;
Roberto Raggi's avatar
Roberto Raggi committed
286
    QTimer *m_updateMethodBoxTimer;
Thorbjørn Lindeijer's avatar
Thorbjørn Lindeijer committed
287
    QTimer *m_updateUsesTimer;
288
    QTextCharFormat m_occurrencesFormat;
Roberto Raggi's avatar
Roberto Raggi committed
289
    QTextCharFormat m_occurrencesUnusedFormat;
290
    QTextCharFormat m_occurrenceRenameFormat;
Thorbjørn Lindeijer's avatar
Thorbjørn Lindeijer committed
291
292
293

    QList<QTextEdit::ExtraSelection> m_renameSelections;
    int m_currentRenameSelection;
mae's avatar
mae committed
294
295
296
    bool m_inRename, m_inRenameChanged, m_firstRenameChange;
    QTextCursor m_currentRenameSelectionBegin;
    QTextCursor m_currentRenameSelectionEnd;
Roberto Raggi's avatar
Roberto Raggi committed
297
298

    SemanticHighlighter *m_semanticHighlighter;
Roberto Raggi's avatar
Roberto Raggi committed
299
    SemanticInfo m_lastSemanticInfo;
300
    QList<QuickFixOperationPtr> m_quickFixes;
Roberto Raggi's avatar
Roberto Raggi committed
301
    bool m_initialized;
con's avatar
con committed
302
303
};

Roberto Raggi's avatar
Roberto Raggi committed
304

con's avatar
con committed
305
306
307
308
} // namespace Internal
} // namespace CppEditor

#endif // CPPEDITOR_H