1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
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
16 ** GNU Lesser General Public License Usage
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.
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.
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
32 **************************************************************************/
37 #include "cppeditorenums.h"
38 #include "cppquickfix.h"
39 #include "cppsemanticinfo.h"
41 #include <cplusplus/ModelManagerInterface.h>
42 #include <cplusplus/CppDocument.h>
43 #include <cplusplus/LookupContext.h>
44 #include <texteditor/basetexteditor.h>
46 #include <QtCore/QThread>
47 #include <QtCore/QMutex>
48 #include <QtCore/QWaitCondition>
49 #include <QtCore/QFutureWatcher>
50 #include <QtCore/QModelIndex>
51 #include <QtCore/QVector>
55 class QSortFilterProxyModel;
64 class CppModelManagerInterface;
67 namespace TextEditor {
74 class CPPEditorWidget;
76 class SemanticHighlighter: public QThread, CPlusPlus::TopLevelDeclarationProcessor
81 SemanticHighlighter(QObject *parent = 0);
82 virtual ~SemanticHighlighter();
84 virtual bool processDeclaration(CPlusPlus::DeclarationAST *) { return m_done; }
90 CPlusPlus::Snapshot snapshot;
99 : line(0), column(0), revision(0), force(false)
102 Source(const CPlusPlus::Snapshot &snapshot,
103 const QString &fileName,
105 int line, int column,
107 : snapshot(snapshot), fileName(fileName),
108 code(code), line(line), column(column),
109 revision(revision), force(false)
114 snapshot = CPlusPlus::Snapshot();
124 SemanticInfo semanticInfo(const Source &source);
126 void rehighlight(const Source &source);
129 void changed(const CppEditor::Internal::SemanticInfo &semanticInfo);
139 QWaitCondition m_condition;
142 SemanticInfo m_lastSemanticInfo;
145 class CPPEditor : public TextEditor::BaseTextEditor
149 CPPEditor(CPPEditorWidget *);
150 Core::Context context() const;
152 bool duplicateSupported() const { return true; }
153 Core::IEditor *duplicate(QWidget *parent);
156 bool isTemporary() const { return false; }
157 virtual bool open(const QString & fileName);
160 Core::Context m_context;
163 class CPPEditorWidget : public TextEditor::BaseTextEditorWidget
168 typedef TextEditor::TabSettings TabSettings;
170 CPPEditorWidget(QWidget *parent);
172 void unCommentSelection();
174 unsigned editorRevision() const;
175 bool isOutdated() const;
176 SemanticInfo semanticInfo() const;
178 CPlusPlus::OverviewModel *outlineModel() const;
179 QModelIndex outlineModelIndex();
181 virtual void paste(); // reimplemented from BaseTextEditorWidget
182 virtual void cut(); // reimplemented from BaseTextEditorWidget
184 CPlusPlus::CppModelManagerInterface *modelManager() const;
186 virtual void setMimeType(const QString &mt);
188 void setObjCEnabled(bool onoff);
189 bool isObjCEnabled() const;
191 bool openLink(const Link &link) { return openCppEditorAt(link); }
193 static Link linkToSymbol(CPlusPlus::Symbol *symbol);
195 static QVector<QString> highlighterFormatCategories();
198 void outlineModelIndexChanged(const QModelIndex &index);
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();
209 void renameUsagesNow(const QString &replacement = QString());
210 void hideRenameNotification();
211 void rehighlight(bool force = false);
214 bool event(QEvent *e);
215 void contextMenuEvent(QContextMenuEvent *);
216 void keyPressEvent(QKeyEvent *);
218 TextEditor::BaseTextEditor *createEditor();
220 const CPlusPlus::Macro *findCanonicalMacro(const QTextCursor &cursor,
221 CPlusPlus::Document::Ptr doc) const;
224 void updateFileName();
225 void jumpToOutlineElement(int index);
226 void updateOutlineNow();
227 void updateOutlineIndex();
228 void updateOutlineIndexNow();
229 void updateOutlineToolTip();
231 void updateUsesNow();
232 void onDocumentUpdated(CPlusPlus::Document::Ptr doc);
233 void onContentsChanged(int position, int charsRemoved, int charsAdded);
235 void semanticRehighlight();
236 void updateSemanticInfo(const CppEditor::Internal::SemanticInfo &semanticInfo);
237 void highlightSymbolUsages(int from, int to);
238 void finishHighlightSymbolUsages();
240 void markSymbolsNow();
242 void performQuickFix(int index);
245 bool showWarningMessage() const;
246 void setShowWarningMessage(bool showWarningMessage);
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;
252 TextEditor::ITextEditor *openCppEditorAt(const QString &fileName, int line,
255 SemanticHighlighter::Source currentSource(bool force = false);
257 void highlightUses(const QList<SemanticInfo::Use> &uses,
258 const SemanticInfo &semanticInfo,
259 QList<QTextEdit::ExtraSelection> *selections);
261 void createToolBar(CPPEditor *editable);
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 &);
277 QModelIndex indexForPosition(int line, int column, const QModelIndex &rootIndex = QModelIndex()) const;
279 CPlusPlus::CppModelManagerInterface *m_modelManager;
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;
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;
306 SemanticHighlighter *m_semanticHighlighter;
307 SemanticInfo m_lastSemanticInfo;
308 QList<TextEditor::QuickFixOperation::Ptr> m_quickFixes;
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;
317 QFuture<QList<int> > m_references;
318 QFutureWatcher<QList<int> > m_referencesWatcher;
319 unsigned m_referencesRevision;
320 int m_referencesCursorPosition;
324 } // namespace Internal
325 } // namespace CppEditor
327 #endif // CPPEDITOR_H