OSDN Git Service

Merge branch 'origin/2.0' (early part)
[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) 2010 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** Commercial Usage
10 **
11 ** Licensees holding valid Qt Commercial licenses may use this file in
12 ** accordance with the Qt Commercial License Agreement provided with the
13 ** Software or, alternatively, in accordance with the terms contained in
14 ** a written agreement between you and Nokia.
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 ** If you are unsure which license is appropriate for your use, please
26 ** contact the sales department at http://qt.nokia.com/contact.
27 **
28 **************************************************************************/
29
30 #ifndef CPPEDITOR_H
31 #define CPPEDITOR_H
32
33 #include "cppeditorenums.h"
34 #include "cppquickfix.h"
35 #include <cplusplus/CppDocument.h>
36 #include <cplusplus/LookupContext.h>
37 #include <texteditor/basetexteditor.h>
38
39 #include <QtCore/QThread>
40 #include <QtCore/QMutex>
41 #include <QtCore/QWaitCondition>
42
43 QT_BEGIN_NAMESPACE
44 class QComboBox;
45 class QSortFilterProxyModel;
46 QT_END_NAMESPACE
47
48 namespace CPlusPlus {
49 class OverviewModel;
50 class Symbol;
51 class TokenCache;
52 }
53
54 namespace CppTools {
55 class CppModelManagerInterface;
56 }
57
58 namespace TextEditor {
59 class FontSettings;
60 }
61
62 namespace CppEditor {
63 namespace Internal {
64
65 class CPPEditor;
66 class SemanticHighlighter;
67
68 class SemanticInfo
69 {
70 public:
71     struct Use {
72         unsigned line;
73         unsigned column;
74         unsigned length;
75
76         Use(unsigned line = 0, unsigned column = 0, unsigned length = 0)
77             : line(line), column(column), length(length) {}
78     };
79
80     typedef QHash<CPlusPlus::Symbol *, QList<Use> > LocalUseMap;
81     typedef QHashIterator<CPlusPlus::Symbol *, QList<Use> > LocalUseIterator;
82
83     SemanticInfo()
84         : revision(0), hasQ(false), hasD(false), forced(false)
85     { }
86
87     unsigned revision;
88     bool hasQ: 1;
89     bool hasD: 1;
90     bool forced: 1;
91     CPlusPlus::Snapshot snapshot; // ### remove
92     CPlusPlus::Document::Ptr doc; // ### remove
93     CPlusPlus::LookupContext context;
94     LocalUseMap localUses; // ### rename
95     QList<Use> typeUsages;
96     QList<CPlusPlus::Document::DiagnosticMessage> diagnosticMessages;
97 };
98
99 class SemanticHighlighter: public QThread
100 {
101     Q_OBJECT
102
103 public:
104     SemanticHighlighter(QObject *parent = 0);
105     virtual ~SemanticHighlighter();
106
107     void abort();
108
109     struct Source
110     {
111         CPlusPlus::Snapshot snapshot;
112         QString fileName;
113         QString code;
114         int line;
115         int column;
116         int revision;
117         bool force;
118
119         Source()
120             : line(0), column(0), revision(0), force(false)
121         { }
122
123         Source(const CPlusPlus::Snapshot &snapshot,
124                const QString &fileName,
125                const QString &code,
126                int line, int column,
127                int revision)
128             : snapshot(snapshot), fileName(fileName),
129               code(code), line(line), column(column),
130               revision(revision), force(false)
131         { }
132
133         void clear()
134         {
135             snapshot = CPlusPlus::Snapshot();
136             fileName.clear();
137             code.clear();
138             line = 0;
139             column = 0;
140             revision = 0;
141             force = false;
142         }
143     };
144
145     SemanticInfo semanticInfo(const Source &source);
146
147     void rehighlight(const Source &source);
148
149 Q_SIGNALS:
150     void changed(const CppEditor::Internal::SemanticInfo &semanticInfo);
151
152 protected:
153     virtual void run();
154
155 private:
156     bool isOutdated();
157
158 private:
159     QMutex m_mutex;
160     QWaitCondition m_condition;
161     bool m_done;
162     Source m_source;
163     SemanticInfo m_lastSemanticInfo;
164 };
165
166 class CPPEditorEditable : public TextEditor::BaseTextEditorEditable
167 {
168     Q_OBJECT
169 public:
170     CPPEditorEditable(CPPEditor *);
171     QList<int> context() const;
172
173     bool duplicateSupported() const { return true; }
174     Core::IEditor *duplicate(QWidget *parent);
175     QString id() const;
176
177     bool isTemporary() const { return false; }
178     virtual bool open(const QString & fileName);
179
180 private:
181     QList<int> m_context;
182 };
183
184 class CPPEditor : public TextEditor::BaseTextEditor
185 {
186     Q_OBJECT
187
188 public:
189     typedef TextEditor::TabSettings TabSettings;
190
191     CPPEditor(QWidget *parent);
192     ~CPPEditor();
193     void unCommentSelection();
194
195     unsigned editorRevision() const;
196     bool isOutdated() const;
197     SemanticInfo semanticInfo() const;
198
199     virtual void paste(); // reimplemented from BaseTextEditor
200     virtual void cut(); // reimplemented from BaseTextEditor
201
202     CPlusPlus::TokenCache *tokenCache() const;
203
204     CppTools::CppModelManagerInterface *modelManager() const;
205
206 public Q_SLOTS:
207     virtual void setFontSettings(const TextEditor::FontSettings &);
208     void setSortedMethodOverview(bool sort);
209     void switchDeclarationDefinition();
210     void jumpToDefinition();
211     void renameSymbolUnderCursor();
212     void renameUsages();
213     void findUsages();
214     void renameUsagesNow();
215     void hideRenameNotification();
216
217 protected:
218     bool event(QEvent *e);
219     void contextMenuEvent(QContextMenuEvent *);
220     void keyPressEvent(QKeyEvent *);
221
222     TextEditor::BaseTextEditorEditable *createEditableInterface();
223
224     // These override BaseTextEditor
225     virtual bool isElectricCharacter(QChar ch) const;
226
227     virtual QString insertMatchingBrace(const QTextCursor &tc, const QString &text,
228                                         QChar la, int *skippedChars) const;
229
230     virtual QString insertParagraphSeparator(const QTextCursor &tc) const;
231
232     virtual bool contextAllowsAutoParentheses(const QTextCursor &cursor,
233                                               const QString &textToInsert = QString()) const;
234
235     virtual bool isInComment(const QTextCursor &cursor) const;
236
237     const CPlusPlus::Macro *findCanonicalMacro(const QTextCursor &cursor,
238                                                CPlusPlus::Document::Ptr doc) const;
239
240 private Q_SLOTS:
241     void updateFileName();
242     void jumpToMethod(int index);
243     void updateMethodBoxIndex();
244     void updateMethodBoxIndexNow();
245     void updateMethodBoxToolTip();
246     void updateUses();
247     void updateUsesNow();
248     void onDocumentUpdated(CPlusPlus::Document::Ptr doc);
249     void onContentsChanged(int position, int charsRemoved, int charsAdded);
250
251     void semanticRehighlight();
252     void updateSemanticInfo(const CppEditor::Internal::SemanticInfo &semanticInfo);
253
254     void performQuickFix(int index);
255
256 private:
257     bool showWarningMessage() const;
258     void setShowWarningMessage(bool showWarningMessage);
259
260     void markSymbols(CPlusPlus::Symbol *canonicalSymbol, const SemanticInfo &info);
261     bool sortedMethodOverview() const;
262     CPlusPlus::Symbol *findDefinition(CPlusPlus::Symbol *symbol, const CPlusPlus::Snapshot &snapshot);
263     virtual void indentBlock(QTextDocument *doc, QTextBlock block, QChar typedChar);
264
265     TextEditor::ITextEditor *openCppEditorAt(const QString &fileName, int line,
266                                              int column = 0);
267
268     SemanticHighlighter::Source currentSource(bool force = false);
269
270     void highlightUses(const QList<SemanticInfo::Use> &uses,
271                        const SemanticInfo &semanticInfo,
272                        QList<QTextEdit::ExtraSelection> *selections);
273
274     void createToolBar(CPPEditorEditable *editable);
275
276     void startRename();
277     void finishRename();
278     void abortRename();
279
280     Link findLinkAt(const QTextCursor &, bool resolveTarget = true);
281     bool openLink(const Link &link) { return openCppEditorAt(link); }
282     bool openCppEditorAt(const Link &);
283
284     static Link linkToSymbol(CPlusPlus::Symbol *symbol);
285
286     CppTools::CppModelManagerInterface *m_modelManager;
287
288     QList<int> m_contexts;
289     QComboBox *m_methodCombo;
290     CPlusPlus::OverviewModel *m_overviewModel;
291     QSortFilterProxyModel *m_proxyModel;
292     QAction *m_sortAction;
293     QTimer *m_updateMethodBoxTimer;
294     QTimer *m_updateUsesTimer;
295     QTextCharFormat m_occurrencesFormat;
296     QTextCharFormat m_occurrencesUnusedFormat;
297     QTextCharFormat m_occurrenceRenameFormat;
298     QTextCharFormat m_typeFormat;
299
300     QList<QTextEdit::ExtraSelection> m_renameSelections;
301     int m_currentRenameSelection;
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_initialized;
310 };
311
312
313 } // namespace Internal
314 } // namespace CppEditor
315
316 #endif // CPPEDITOR_H