OSDN Git Service

11634168fd85ccb96563fde84a0c566b5b730ca0
[qt-creator-jp/qt-creator-jp.git] / src / plugins / cppeditor / cppeditor.h
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 **
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file.  Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights.  These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33
34 #ifndef CPPEDITOR_H
35 #define CPPEDITOR_H
36
37 #include "cppeditorenums.h"
38 #include "cppquickfix.h"
39 #include "cppsemanticinfo.h"
40
41 #include <cplusplus/ModelManagerInterface.h>
42 #include <cplusplus/CppDocument.h>
43 #include <cplusplus/LookupContext.h>
44 #include <texteditor/basetexteditor.h>
45
46 #include <QtCore/QThread>
47 #include <QtCore/QMutex>
48 #include <QtCore/QWaitCondition>
49 #include <QtCore/QFutureWatcher>
50 #include <QtCore/QModelIndex>
51 #include <QtCore/QVector>
52
53 QT_BEGIN_NAMESPACE
54 class QComboBox;
55 class QSortFilterProxyModel;
56 QT_END_NAMESPACE
57
58 namespace CPlusPlus {
59 class OverviewModel;
60 class Symbol;
61 }
62
63 namespace CppTools {
64 class CppModelManagerInterface;
65 }
66
67 namespace TextEditor {
68 class FontSettings;
69 }
70
71 namespace CppEditor {
72 namespace Internal {
73
74 class CPPEditorWidget;
75
76 class SemanticHighlighter: public QThread, CPlusPlus::TopLevelDeclarationProcessor
77 {
78     Q_OBJECT
79
80 public:
81     SemanticHighlighter(QObject *parent = 0);
82     virtual ~SemanticHighlighter();
83
84     virtual bool processDeclaration(CPlusPlus::DeclarationAST *) { return m_done; }
85
86     void abort();
87
88     struct Source
89     {
90         CPlusPlus::Snapshot snapshot;
91         QString fileName;
92         QString code;
93         int line;
94         int column;
95         int revision;
96         bool force;
97
98         Source()
99             : line(0), column(0), revision(0), force(false)
100         { }
101
102         Source(const CPlusPlus::Snapshot &snapshot,
103                const QString &fileName,
104                const QString &code,
105                int line, int column,
106                int revision)
107             : snapshot(snapshot), fileName(fileName),
108               code(code), line(line), column(column),
109               revision(revision), force(false)
110         { }
111
112         void clear()
113         {
114             snapshot = CPlusPlus::Snapshot();
115             fileName.clear();
116             code.clear();
117             line = 0;
118             column = 0;
119             revision = 0;
120             force = false;
121         }
122     };
123
124     SemanticInfo semanticInfo(const Source &source);
125
126     void rehighlight(const Source &source);
127
128 Q_SIGNALS:
129     void changed(const CppEditor::Internal::SemanticInfo &semanticInfo);
130
131 protected:
132     virtual void run();
133
134 private:
135     bool isOutdated();
136
137 private:
138     QMutex m_mutex;
139     QWaitCondition m_condition;
140     bool m_done;
141     Source m_source;
142     SemanticInfo m_lastSemanticInfo;
143 };
144
145 class CPPEditor : public TextEditor::BaseTextEditor
146 {
147     Q_OBJECT
148 public:
149     CPPEditor(CPPEditorWidget *);
150     Core::Context context() const;
151
152     bool duplicateSupported() const { return true; }
153     Core::IEditor *duplicate(QWidget *parent);
154     QString id() const;
155
156     bool isTemporary() const { return false; }
157     virtual bool open(const QString & fileName);
158
159 private:
160     Core::Context m_context;
161 };
162
163 class CPPEditorWidget : public TextEditor::BaseTextEditorWidget
164 {
165     Q_OBJECT
166
167 public:
168     typedef TextEditor::TabSettings TabSettings;
169
170     CPPEditorWidget(QWidget *parent);
171     ~CPPEditorWidget();
172     void unCommentSelection();
173
174     unsigned editorRevision() const;
175     bool isOutdated() const;
176     SemanticInfo semanticInfo() const;
177
178     CPlusPlus::OverviewModel *outlineModel() const;
179     QModelIndex outlineModelIndex();
180
181     virtual void paste(); // reimplemented from BaseTextEditorWidget
182     virtual void cut(); // reimplemented from BaseTextEditorWidget
183
184     CPlusPlus::CppModelManagerInterface *modelManager() const;
185
186     virtual void setMimeType(const QString &mt);
187
188     void setObjCEnabled(bool onoff);
189     bool isObjCEnabled() const;
190
191     bool openLink(const Link &link) { return openCppEditorAt(link); }
192
193     static Link linkToSymbol(CPlusPlus::Symbol *symbol);
194
195     static QVector<QString> highlighterFormatCategories();
196
197 Q_SIGNALS:
198     void outlineModelIndexChanged(const QModelIndex &index);
199
200 public Q_SLOTS:
201     virtual void setFontSettings(const TextEditor::FontSettings &);
202     virtual void setTabSettings(const TextEditor::TabSettings &);
203     void setSortedOutline(bool sort);
204     void switchDeclarationDefinition();
205     void jumpToDefinition();
206     void renameSymbolUnderCursor();
207     void renameUsages();
208     void findUsages();
209     void renameUsagesNow(const QString &replacement = QString());
210     void hideRenameNotification();
211     void rehighlight(bool force = false);
212
213 protected:
214     bool event(QEvent *e);
215     void contextMenuEvent(QContextMenuEvent *);
216     void keyPressEvent(QKeyEvent *);
217
218     TextEditor::BaseTextEditor *createEditor();
219
220     const CPlusPlus::Macro *findCanonicalMacro(const QTextCursor &cursor,
221                                                CPlusPlus::Document::Ptr doc) const;
222
223 private Q_SLOTS:
224     void updateFileName();
225     void jumpToOutlineElement(int index);
226     void updateOutlineNow();
227     void updateOutlineIndex();
228     void updateOutlineIndexNow();
229     void updateOutlineToolTip();
230     void updateUses();
231     void updateUsesNow();
232     void onDocumentUpdated(CPlusPlus::Document::Ptr doc);
233     void onContentsChanged(int position, int charsRemoved, int charsAdded);
234
235     void semanticRehighlight();
236     void updateSemanticInfo(const CppEditor::Internal::SemanticInfo &semanticInfo);
237     void highlightSymbolUsages(int from, int to);
238     void finishHighlightSymbolUsages();
239
240     void markSymbolsNow();
241
242     void performQuickFix(int index);
243
244 private:
245     bool showWarningMessage() const;
246     void setShowWarningMessage(bool showWarningMessage);
247
248     void markSymbols(const QTextCursor &tc, const SemanticInfo &info);
249     bool sortedOutline() const;
250     CPlusPlus::Symbol *findDefinition(CPlusPlus::Symbol *symbol, const CPlusPlus::Snapshot &snapshot) const;
251
252     TextEditor::ITextEditor *openCppEditorAt(const QString &fileName, int line,
253                                              int column = 0);
254
255     SemanticHighlighter::Source currentSource(bool force = false);
256
257     void highlightUses(const QList<SemanticInfo::Use> &uses,
258                        const SemanticInfo &semanticInfo,
259                        QList<QTextEdit::ExtraSelection> *selections);
260
261     void createToolBar(CPPEditor *editable);
262
263     void startRename();
264     void finishRename();
265     void abortRename();
266
267     Link attemptFuncDeclDef(const QTextCursor &cursor,
268                             const CPlusPlus::Document::Ptr &doc,
269                             CPlusPlus::Snapshot snapshot) const;
270     Link findLinkAt(const QTextCursor &, bool resolveTarget = true);
271     Link findMacroLink(const QByteArray &name) const;
272     Link findMacroLink(const QByteArray &name, CPlusPlus::Document::Ptr doc, const CPlusPlus::Snapshot &snapshot,
273                        QSet<QString> *processed) const;
274     QString identifierUnderCursor(QTextCursor *macroCursor) const;
275     bool openCppEditorAt(const Link &);
276
277     QModelIndex indexForPosition(int line, int column, const QModelIndex &rootIndex = QModelIndex()) const;
278
279     CPlusPlus::CppModelManagerInterface *m_modelManager;
280
281     QComboBox *m_outlineCombo;
282     CPlusPlus::OverviewModel *m_outlineModel;
283     QModelIndex m_outlineModelIndex;
284     QSortFilterProxyModel *m_proxyModel;
285     QAction *m_sortAction;
286     QTimer *m_updateOutlineTimer;
287     QTimer *m_updateOutlineIndexTimer;
288     QTimer *m_updateUsesTimer;
289     QTextCharFormat m_occurrencesFormat;
290     QTextCharFormat m_occurrencesUnusedFormat;
291     QTextCharFormat m_occurrenceRenameFormat;
292     QTextCharFormat m_typeFormat;
293     QTextCharFormat m_localFormat;
294     QTextCharFormat m_fieldFormat;
295     QTextCharFormat m_staticFormat;
296     QTextCharFormat m_keywordFormat;
297     QTextCharFormat m_virtualMethodFormat;
298
299     QList<QTextEdit::ExtraSelection> m_renameSelections;
300     int m_currentRenameSelection;
301     static const int NoCurrentRenameSelection = -1;
302     bool m_inRename, m_inRenameChanged, m_firstRenameChange;
303     QTextCursor m_currentRenameSelectionBegin;
304     QTextCursor m_currentRenameSelectionEnd;
305
306     SemanticHighlighter *m_semanticHighlighter;
307     SemanticInfo m_lastSemanticInfo;
308     QList<TextEditor::QuickFixOperation::Ptr> m_quickFixes;
309     bool m_objcEnabled;
310     bool m_initialized;
311
312     QFuture<SemanticInfo::Use> m_highlighter;
313     QFutureWatcher<SemanticInfo::Use> m_highlightWatcher;
314     unsigned m_highlightRevision; // the editor revision that requested the highlight
315     int m_nextHighlightBlockNumber;
316
317     QFuture<QList<int> > m_references;
318     QFutureWatcher<QList<int> > m_referencesWatcher;
319     unsigned m_referencesRevision;
320     int m_referencesCursorPosition;
321 };
322
323
324 } // namespace Internal
325 } // namespace CppEditor
326
327 #endif // CPPEDITOR_H