1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
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.
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 ** If you are unsure which license is appropriate for your use, please
26 ** contact the sales department at http://qt.nokia.com/contact.
28 **************************************************************************/
33 #include "cppeditorenums.h"
34 #include "cppquickfix.h"
35 #include <cplusplus/CppDocument.h>
36 #include <cplusplus/LookupContext.h>
37 #include <texteditor/basetexteditor.h>
39 #include <QtCore/QThread>
40 #include <QtCore/QMutex>
41 #include <QtCore/QWaitCondition>
45 class QSortFilterProxyModel;
55 class CppModelManagerInterface;
58 namespace TextEditor {
66 class SemanticHighlighter;
76 Use(unsigned line = 0, unsigned column = 0, unsigned length = 0)
77 : line(line), column(column), length(length) {}
80 typedef QHash<CPlusPlus::Symbol *, QList<Use> > LocalUseMap;
81 typedef QHashIterator<CPlusPlus::Symbol *, QList<Use> > LocalUseIterator;
84 : revision(0), hasQ(false), hasD(false), forced(false)
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;
99 class SemanticHighlighter: public QThread
104 SemanticHighlighter(QObject *parent = 0);
105 virtual ~SemanticHighlighter();
111 CPlusPlus::Snapshot snapshot;
120 : line(0), column(0), revision(0), force(false)
123 Source(const CPlusPlus::Snapshot &snapshot,
124 const QString &fileName,
126 int line, int column,
128 : snapshot(snapshot), fileName(fileName),
129 code(code), line(line), column(column),
130 revision(revision), force(false)
135 snapshot = CPlusPlus::Snapshot();
145 SemanticInfo semanticInfo(const Source &source);
147 void rehighlight(const Source &source);
150 void changed(const CppEditor::Internal::SemanticInfo &semanticInfo);
160 QWaitCondition m_condition;
163 SemanticInfo m_lastSemanticInfo;
166 class CPPEditorEditable : public TextEditor::BaseTextEditorEditable
170 CPPEditorEditable(CPPEditor *);
171 QList<int> context() const;
173 bool duplicateSupported() const { return true; }
174 Core::IEditor *duplicate(QWidget *parent);
177 bool isTemporary() const { return false; }
178 virtual bool open(const QString & fileName);
181 QList<int> m_context;
184 class CPPEditor : public TextEditor::BaseTextEditor
189 typedef TextEditor::TabSettings TabSettings;
191 CPPEditor(QWidget *parent);
193 void unCommentSelection();
195 unsigned editorRevision() const;
196 bool isOutdated() const;
197 SemanticInfo semanticInfo() const;
199 virtual void paste(); // reimplemented from BaseTextEditor
200 virtual void cut(); // reimplemented from BaseTextEditor
202 CPlusPlus::TokenCache *tokenCache() const;
204 CppTools::CppModelManagerInterface *modelManager() const;
207 virtual void setFontSettings(const TextEditor::FontSettings &);
208 void setSortedMethodOverview(bool sort);
209 void switchDeclarationDefinition();
210 void jumpToDefinition();
211 void renameSymbolUnderCursor();
214 void renameUsagesNow();
215 void hideRenameNotification();
218 bool event(QEvent *e);
219 void contextMenuEvent(QContextMenuEvent *);
220 void keyPressEvent(QKeyEvent *);
222 TextEditor::BaseTextEditorEditable *createEditableInterface();
224 // These override BaseTextEditor
225 virtual bool isElectricCharacter(QChar ch) const;
227 virtual QString insertMatchingBrace(const QTextCursor &tc, const QString &text,
228 QChar la, int *skippedChars) const;
230 virtual QString insertParagraphSeparator(const QTextCursor &tc) const;
232 virtual bool contextAllowsAutoParentheses(const QTextCursor &cursor,
233 const QString &textToInsert = QString()) const;
235 virtual bool isInComment(const QTextCursor &cursor) const;
237 const CPlusPlus::Macro *findCanonicalMacro(const QTextCursor &cursor,
238 CPlusPlus::Document::Ptr doc) const;
241 void updateFileName();
242 void jumpToMethod(int index);
243 void updateMethodBoxIndex();
244 void updateMethodBoxIndexNow();
245 void updateMethodBoxToolTip();
247 void updateUsesNow();
248 void onDocumentUpdated(CPlusPlus::Document::Ptr doc);
249 void onContentsChanged(int position, int charsRemoved, int charsAdded);
251 void semanticRehighlight();
252 void updateSemanticInfo(const CppEditor::Internal::SemanticInfo &semanticInfo);
254 void performQuickFix(int index);
257 bool showWarningMessage() const;
258 void setShowWarningMessage(bool showWarningMessage);
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);
265 TextEditor::ITextEditor *openCppEditorAt(const QString &fileName, int line,
268 SemanticHighlighter::Source currentSource(bool force = false);
270 void highlightUses(const QList<SemanticInfo::Use> &uses,
271 const SemanticInfo &semanticInfo,
272 QList<QTextEdit::ExtraSelection> *selections);
274 void createToolBar(CPPEditorEditable *editable);
280 Link findLinkAt(const QTextCursor &, bool resolveTarget = true);
281 bool openLink(const Link &link) { return openCppEditorAt(link); }
282 bool openCppEditorAt(const Link &);
284 static Link linkToSymbol(CPlusPlus::Symbol *symbol);
286 CppTools::CppModelManagerInterface *m_modelManager;
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;
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;
306 SemanticHighlighter *m_semanticHighlighter;
307 SemanticInfo m_lastSemanticInfo;
308 QList<TextEditor::QuickFixOperation::Ptr> m_quickFixes;
313 } // namespace Internal
314 } // namespace CppEditor
316 #endif // CPPEDITOR_H