cppeditor.h 8.6 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"
35
36
#include "cppsemanticinfo.h"

con's avatar
con committed
37
#include <cplusplus/CppDocument.h>
Roberto Raggi's avatar
Roberto Raggi committed
38
#include <cplusplus/LookupContext.h>
con's avatar
con committed
39
40
#include <texteditor/basetexteditor.h>

Roberto Raggi's avatar
Roberto Raggi committed
41
42
43
#include <QtCore/QThread>
#include <QtCore/QMutex>
#include <QtCore/QWaitCondition>
44
#include <QtCore/QFutureWatcher>
Roberto Raggi's avatar
Roberto Raggi committed
45

con's avatar
con committed
46
47
QT_BEGIN_NAMESPACE
class QComboBox;
48
class QSortFilterProxyModel;
con's avatar
con committed
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86

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
87
        bool force;
Roberto Raggi's avatar
Roberto Raggi committed
88
89

        Source()
Roberto Raggi's avatar
Roberto Raggi committed
90
            : line(0), column(0), revision(0), force(false)
Roberto Raggi's avatar
Roberto Raggi committed
91
92
93
94
95
96
97
98
99
        { }

        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
100
              revision(revision), force(false)
Roberto Raggi's avatar
Roberto Raggi committed
101
102
103
104
        { }

        void clear()
        {
105
            snapshot = CPlusPlus::Snapshot();
Roberto Raggi's avatar
Roberto Raggi committed
106
107
108
109
110
            fileName.clear();
            code.clear();
            line = 0;
            column = 0;
            revision = 0;
Roberto Raggi's avatar
Roberto Raggi committed
111
            force = false;
Roberto Raggi's avatar
Roberto Raggi committed
112
113
114
        }
    };

115
    SemanticInfo semanticInfo(const Source &source);
Roberto Raggi's avatar
Roberto Raggi committed
116
117
118
119

    void rehighlight(const Source &source);

Q_SIGNALS:
120
    void changed(const CppEditor::Internal::SemanticInfo &semanticInfo);
Roberto Raggi's avatar
Roberto Raggi committed
121
122
123
124

protected:
    virtual void run();

125
126
127
private:
    bool isOutdated();

Roberto Raggi's avatar
Roberto Raggi committed
128
129
130
131
132
private:
    QMutex m_mutex;
    QWaitCondition m_condition;
    bool m_done;
    Source m_source;
133
    SemanticInfo m_lastSemanticInfo;
Roberto Raggi's avatar
Roberto Raggi committed
134
};
con's avatar
con committed
135
136
137
138
139
140

class CPPEditorEditable : public TextEditor::BaseTextEditorEditable
{
    Q_OBJECT
public:
    CPPEditorEditable(CPPEditor *);
141
    Core::Context context() const;
con's avatar
con committed
142
143
144

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

con's avatar
con committed
147
    bool isTemporary() const { return false; }
dt's avatar
dt committed
148
    virtual bool open(const QString & fileName);
dt's avatar
dt committed
149

con's avatar
con committed
150
private:
151
    Core::Context m_context;
con's avatar
con committed
152
153
154
155
156
157
158
159
160
161
162
163
164
};

class CPPEditor : public TextEditor::BaseTextEditor
{
    Q_OBJECT

public:
    typedef TextEditor::TabSettings TabSettings;

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

165
    unsigned editorRevision() const;
166
    bool isOutdated() const;
Roberto Raggi's avatar
Roberto Raggi committed
167
168
    SemanticInfo semanticInfo() const;

mae's avatar
mae committed
169
170
171
    virtual void paste(); // reimplemented from BaseTextEditor
    virtual void cut(); // reimplemented from BaseTextEditor

172
173
    CppTools::CppModelManagerInterface *modelManager() const;

174
175
176
177
178
    virtual void setMimeType(const QString &mt);

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

Roberto Raggi's avatar
Roberto Raggi committed
179
public Q_SLOTS:
con's avatar
con committed
180
    virtual void setFontSettings(const TextEditor::FontSettings &);
Christian Kamm's avatar
Christian Kamm committed
181
    virtual void setTabSettings(const TextEditor::TabSettings &);
182
    void setSortedMethodOverview(bool sort);
con's avatar
con committed
183
184
    void switchDeclarationDefinition();
    void jumpToDefinition();
185
    void renameSymbolUnderCursor();
186
187
    void renameUsages();
    void findUsages();
188
    void renameUsagesNow();
189
    void hideRenameNotification();
con's avatar
con committed
190
191

protected:
Thorbjørn Lindeijer's avatar
Thorbjørn Lindeijer committed
192
    bool event(QEvent *e);
193
    void contextMenuEvent(QContextMenuEvent *);
Thorbjørn Lindeijer's avatar
Thorbjørn Lindeijer committed
194
    void keyPressEvent(QKeyEvent *);
195

con's avatar
con committed
196
197
    TextEditor::BaseTextEditorEditable *createEditableInterface();

198
    // These override BaseTextEditor
199
    virtual bool isElectricCharacter(QChar ch) const;
200

201
    virtual QString insertMatchingBrace(const QTextCursor &tc, const QString &text,
202
                                        QChar la, int *skippedChars) const;
con's avatar
con committed
203

204
205
206
207
    virtual QString insertParagraphSeparator(const QTextCursor &tc) const;

    virtual bool contextAllowsAutoParentheses(const QTextCursor &cursor,
                                              const QString &textToInsert = QString()) const;
208
    virtual bool contextAllowsElectricCharacters(const QTextCursor &cursor) const;
209
210

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

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

Roberto Raggi's avatar
Roberto Raggi committed
215
private Q_SLOTS:
con's avatar
con committed
216
217
218
    void updateFileName();
    void jumpToMethod(int index);
    void updateMethodBoxIndex();
Roberto Raggi's avatar
Roberto Raggi committed
219
    void updateMethodBoxIndexNow();
220
    void updateMethodBoxToolTip();
Thorbjørn Lindeijer's avatar
Thorbjørn Lindeijer committed
221
222
    void updateUses();
    void updateUsesNow();
con's avatar
con committed
223
    void onDocumentUpdated(CPlusPlus::Document::Ptr doc);
224
    void onContentsChanged(int position, int charsRemoved, int charsAdded);
con's avatar
con committed
225

Roberto Raggi's avatar
Roberto Raggi committed
226
    void semanticRehighlight();
227
    void updateSemanticInfo(const CppEditor::Internal::SemanticInfo &semanticInfo);
228
    void highlightTypeUsages();
Roberto Raggi's avatar
Roberto Raggi committed
229

230
231
    void performQuickFix(int index);

con's avatar
con committed
232
private:
233
234
235
    bool showWarningMessage() const;
    void setShowWarningMessage(bool showWarningMessage);

236
    void markSymbols(CPlusPlus::Symbol *canonicalSymbol, const SemanticInfo &info);
237
    bool sortedMethodOverview() const;
238
    CPlusPlus::Symbol *findDefinition(CPlusPlus::Symbol *symbol, const CPlusPlus::Snapshot &snapshot);
con's avatar
con committed
239
    virtual void indentBlock(QTextDocument *doc, QTextBlock block, QChar typedChar);
240
    virtual void indent(QTextDocument *doc, const QTextCursor &cursor, QChar typedChar);
con's avatar
con committed
241

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

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

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

con's avatar
con committed
251
252
    void createToolBar(CPPEditorEditable *editable);

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

257
258
    Link findLinkAt(const QTextCursor &, bool resolveTarget = true);
    bool openLink(const Link &link) { return openCppEditorAt(link); }
259
    bool openCppEditorAt(const Link &);
260

261
    static Link linkToSymbol(CPlusPlus::Symbol *symbol);
con's avatar
con committed
262
263
264
265
266

    CppTools::CppModelManagerInterface *m_modelManager;

    QComboBox *m_methodCombo;
    CPlusPlus::OverviewModel *m_overviewModel;
267
268
    QSortFilterProxyModel *m_proxyModel;
    QAction *m_sortAction;
Roberto Raggi's avatar
Roberto Raggi committed
269
    QTimer *m_updateMethodBoxTimer;
Thorbjørn Lindeijer's avatar
Thorbjørn Lindeijer committed
270
    QTimer *m_updateUsesTimer;
271
    QTextCharFormat m_occurrencesFormat;
Roberto Raggi's avatar
Roberto Raggi committed
272
    QTextCharFormat m_occurrencesUnusedFormat;
273
    QTextCharFormat m_occurrenceRenameFormat;
Roberto Raggi's avatar
Roberto Raggi committed
274
    QTextCharFormat m_typeFormat;
275
    QTextCharFormat m_keywordFormat;
Thorbjørn Lindeijer's avatar
Thorbjørn Lindeijer committed
276
277
278

    QList<QTextEdit::ExtraSelection> m_renameSelections;
    int m_currentRenameSelection;
mae's avatar
mae committed
279
280
281
    bool m_inRename, m_inRenameChanged, m_firstRenameChange;
    QTextCursor m_currentRenameSelectionBegin;
    QTextCursor m_currentRenameSelectionEnd;
Roberto Raggi's avatar
Roberto Raggi committed
282
283

    SemanticHighlighter *m_semanticHighlighter;
Roberto Raggi's avatar
Roberto Raggi committed
284
    SemanticInfo m_lastSemanticInfo;
285
    QList<TextEditor::QuickFixOperation::Ptr> m_quickFixes;
286
    bool m_objcEnabled;
Roberto Raggi's avatar
Roberto Raggi committed
287
    bool m_initialized;
288
289
290
291

    QFuture<SemanticInfo::Use> m_highlighter;
    QFutureWatcher<SemanticInfo::Use> m_highlightWatcher;
    unsigned m_highlighteRevision; // the editor revision that requested the highlight
con's avatar
con committed
292
293
};

Roberto Raggi's avatar
Roberto Raggi committed
294

con's avatar
con committed
295
296
297
298
} // namespace Internal
} // namespace CppEditor

#endif // CPPEDITOR_H