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 **************************************************************************/
34 #include "qmljseditor.h"
35 #include "qmljseditoreditable.h"
36 #include "qmljseditorconstants.h"
37 #include "qmljshighlighter.h"
38 #include "qmljseditorplugin.h"
39 #include "qmljsquickfix.h"
40 #include "qmloutlinemodel.h"
41 #include "qmljsfindreferences.h"
42 #include "qmljssemantichighlighter.h"
43 #include "qmljsindenter.h"
44 #include "qmljsautocompleter.h"
46 #include <qmljs/qmljsbind.h>
47 #include <qmljs/qmljsevaluate.h>
48 #include <qmljs/qmljsdocument.h>
49 #include <qmljs/qmljsicontextpane.h>
50 #include <qmljs/qmljslookupcontext.h>
51 #include <qmljs/qmljsmodelmanagerinterface.h>
52 #include <qmljs/parser/qmljsastvisitor_p.h>
53 #include <qmljs/parser/qmljsast_p.h>
54 #include <qmljs/parser/qmljsengine_p.h>
56 #include <qmljstools/qmljsqtstylecodeformatter.h>
58 #include <coreplugin/actionmanager/actionmanager.h>
59 #include <coreplugin/actionmanager/actioncontainer.h>
60 #include <coreplugin/uniqueidmanager.h>
61 #include <coreplugin/actionmanager/command.h>
62 #include <coreplugin/editormanager/editormanager.h>
63 #include <coreplugin/icore.h>
64 #include <coreplugin/mimedatabase.h>
65 #include <extensionsystem/pluginmanager.h>
66 #include <texteditor/basetextdocument.h>
67 #include <texteditor/fontsettings.h>
68 #include <texteditor/tabsettings.h>
69 #include <texteditor/texteditorconstants.h>
70 #include <texteditor/texteditorsettings.h>
71 #include <texteditor/syntaxhighlighter.h>
72 #include <texteditor/refactoroverlay.h>
73 #include <texteditor/tooltip/tooltip.h>
74 #include <qmldesigner/qmldesignerconstants.h>
75 #include <projectexplorer/projectexplorerconstants.h>
76 #include <utils/changeset.h>
77 #include <utils/uncommentselection.h>
79 #include <QtCore/QFileInfo>
80 #include <QtCore/QSignalMapper>
81 #include <QtCore/QTimer>
83 #include <QtGui/QMenu>
84 #include <QtGui/QComboBox>
85 #include <QtGui/QHeaderView>
86 #include <QtGui/QInputDialog>
87 #include <QtGui/QMainWindow>
88 #include <QtGui/QToolBar>
89 #include <QtGui/QTreeView>
92 UPDATE_DOCUMENT_DEFAULT_INTERVAL = 100,
93 UPDATE_USES_DEFAULT_INTERVAL = 150,
94 UPDATE_OUTLINE_INTERVAL = 500 // msecs after new semantic info has been arrived / cursor has moved
97 using namespace QmlJS;
98 using namespace QmlJS::AST;
99 using namespace QmlJSEditor;
100 using namespace QmlJSEditor::Internal;
104 class FindIdDeclarations: protected Visitor
107 typedef QHash<QString, QList<AST::SourceLocation> > Result;
109 Result operator()(Document::Ptr doc)
113 if (doc && doc->qmlProgram())
114 doc->qmlProgram()->accept(this);
119 QString asString(AST::UiQualifiedId *id)
122 for (; id; id = id->next) {
124 text += id->name->asString();
126 text += QLatin1Char('?');
129 text += QLatin1Char('.');
135 void accept(AST::Node *node)
136 { AST::Node::acceptChild(node, this); }
138 using Visitor::visit;
139 using Visitor::endVisit;
141 virtual bool visit(AST::UiScriptBinding *node)
143 if (asString(node->qualifiedId) == QLatin1String("id")) {
144 if (AST::ExpressionStatement *stmt = AST::cast<AST::ExpressionStatement*>(node->statement)) {
145 if (AST::IdentifierExpression *idExpr = AST::cast<AST::IdentifierExpression *>(stmt->expression)) {
147 const QString id = idExpr->name->asString();
148 QList<AST::SourceLocation> *locs = &_ids[id];
149 locs->append(idExpr->firstSourceLocation());
150 locs->append(_maybeIds.value(id));
151 _maybeIds.remove(id);
158 accept(node->statement);
163 virtual bool visit(AST::IdentifierExpression *node)
166 const QString name = node->name->asString();
168 if (_ids.contains(name))
169 _ids[name].append(node->identifierToken);
171 _maybeIds[name].append(node->identifierToken);
181 class FindDeclarations: protected Visitor
183 QList<Declaration> _declarations;
187 QList<Declaration> operator()(AST::Node *node)
190 _declarations.clear();
192 return _declarations;
196 using Visitor::visit;
197 using Visitor::endVisit;
199 QString asString(AST::UiQualifiedId *id)
202 for (; id; id = id->next) {
204 text += id->name->asString();
206 text += QLatin1Char('?');
209 text += QLatin1Char('.');
215 void accept(AST::Node *node)
216 { AST::Node::acceptChild(node, this); }
218 void init(Declaration *decl, AST::UiObjectMember *member)
220 const SourceLocation first = member->firstSourceLocation();
221 const SourceLocation last = member->lastSourceLocation();
222 decl->startLine = first.startLine;
223 decl->startColumn = first.startColumn;
224 decl->endLine = last.startLine;
225 decl->endColumn = last.startColumn + last.length;
228 void init(Declaration *decl, AST::ExpressionNode *expressionNode)
230 const SourceLocation first = expressionNode->firstSourceLocation();
231 const SourceLocation last = expressionNode->lastSourceLocation();
232 decl->startLine = first.startLine;
233 decl->startColumn = first.startColumn;
234 decl->endLine = last.startLine;
235 decl->endColumn = last.startColumn + last.length;
238 virtual bool visit(AST::UiObjectDefinition *node)
245 decl.text.fill(QLatin1Char(' '), _depth);
246 if (node->qualifiedTypeNameId)
247 decl.text.append(asString(node->qualifiedTypeNameId));
249 decl.text.append(QLatin1Char('?'));
251 _declarations.append(decl);
253 return true; // search for more bindings
256 virtual void endVisit(AST::UiObjectDefinition *)
261 virtual bool visit(AST::UiObjectBinding *node)
268 decl.text.fill(QLatin1Char(' '), _depth);
270 decl.text.append(asString(node->qualifiedId));
271 decl.text.append(QLatin1String(": "));
273 if (node->qualifiedTypeNameId)
274 decl.text.append(asString(node->qualifiedTypeNameId));
276 decl.text.append(QLatin1Char('?'));
278 _declarations.append(decl);
280 return true; // search for more bindings
283 virtual void endVisit(AST::UiObjectBinding *)
288 virtual bool visit(AST::UiScriptBinding *)
292 #if 0 // ### ignore script bindings for now.
296 decl.text.fill(QLatin1Char(' '), _depth);
297 decl.text.append(asString(node->qualifiedId));
299 _declarations.append(decl);
302 return false; // more more bindings in this subtree.
305 virtual void endVisit(AST::UiScriptBinding *)
310 virtual bool visit(AST::FunctionExpression *)
315 virtual bool visit(AST::FunctionDeclaration *ast)
323 decl.text.fill(QLatin1Char(' '), _depth);
324 decl.text += ast->name->asString();
326 decl.text += QLatin1Char('(');
327 for (FormalParameterList *it = ast->formals; it; it = it->next) {
329 decl.text += it->name->asString();
332 decl.text += QLatin1String(", ");
335 decl.text += QLatin1Char(')');
337 _declarations.append(decl);
342 virtual bool visit(AST::VariableDeclaration *ast)
348 decl.text.fill(QLatin1Char(' '), _depth);
349 decl.text += ast->name->asString();
351 const SourceLocation first = ast->identifierToken;
352 decl.startLine = first.startLine;
353 decl.startColumn = first.startColumn;
354 decl.endLine = first.startLine;
355 decl.endColumn = first.startColumn + first.length;
357 _declarations.append(decl);
363 class CreateRanges: protected AST::Visitor
365 QTextDocument *_textDocument;
366 QList<Range> _ranges;
369 QList<Range> operator()(QTextDocument *textDocument, Document::Ptr doc)
371 _textDocument = textDocument;
373 if (doc && doc->ast() != 0)
374 doc->ast()->accept(this);
379 using AST::Visitor::visit;
381 virtual bool visit(AST::UiObjectBinding *ast)
383 if (ast->initializer && ast->initializer->lbraceToken.length)
384 _ranges.append(createRange(ast, ast->initializer));
388 virtual bool visit(AST::UiObjectDefinition *ast)
390 if (ast->initializer && ast->initializer->lbraceToken.length)
391 _ranges.append(createRange(ast, ast->initializer));
395 virtual bool visit(AST::FunctionExpression *ast)
397 _ranges.append(createRange(ast));
401 virtual bool visit(AST::FunctionDeclaration *ast)
403 _ranges.append(createRange(ast));
407 Range createRange(AST::UiObjectMember *member, AST::UiObjectInitializer *ast)
413 range.begin = QTextCursor(_textDocument);
414 range.begin.setPosition(member->firstSourceLocation().begin());
416 range.end = QTextCursor(_textDocument);
417 range.end.setPosition(ast->rbraceToken.end());
421 Range createRange(AST::FunctionExpression *ast)
427 range.begin = QTextCursor(_textDocument);
428 range.begin.setPosition(ast->lbraceToken.begin());
430 range.end = QTextCursor(_textDocument);
431 range.end.setPosition(ast->rbraceToken.end());
439 class CollectASTNodes: protected AST::Visitor
442 QList<AST::UiQualifiedId *> qualifiedIds;
443 QList<AST::IdentifierExpression *> identifiers;
444 QList<AST::FieldMemberExpression *> fieldMembers;
446 void accept(AST::Node *node)
453 using AST::Visitor::visit;
455 virtual bool visit(AST::UiQualifiedId *ast)
457 qualifiedIds.append(ast);
461 virtual bool visit(AST::IdentifierExpression *ast)
463 identifiers.append(ast);
467 virtual bool visit(AST::FieldMemberExpression *ast)
469 fieldMembers.append(ast);
474 } // end of anonymous namespace
477 AST::Node *SemanticInfo::declaringMember(int cursorPosition) const
479 AST::Node *declaringMember = 0;
481 for (int i = ranges.size() - 1; i != -1; --i) {
482 const Range &range = ranges.at(i);
484 if (range.begin.isNull() || range.end.isNull()) {
486 } else if (cursorPosition >= range.begin.position() && cursorPosition <= range.end.position()) {
487 declaringMember = range.ast;
492 return declaringMember;
495 QmlJS::AST::Node *SemanticInfo::declaringMemberNoProperties(int cursorPosition) const
497 AST::Node *node = declaringMember(cursorPosition);
499 if (UiObjectDefinition *objectDefinition = cast<UiObjectDefinition*>(node)) {
500 QString name = objectDefinition->qualifiedTypeNameId->name->asString();
501 if (!name.isNull() && name.at(0).isLower()) {
502 QList<AST::Node *> path = astPath(cursorPosition);
504 return path.at(path.size() - 2);
505 } else if (name.contains("GradientStop")) {
506 QList<AST::Node *> path = astPath(cursorPosition);
508 return path.at(path.size() - 3);
510 } else if (UiObjectBinding *objectBinding = cast<UiObjectBinding*>(node)) {
511 QString name = objectBinding->qualifiedTypeNameId->name->asString();
512 if (name.contains("Gradient")) {
513 QList<AST::Node *> path = astPath(cursorPosition);
515 return path.at(path.size() - 2);
522 QList<AST::Node *> SemanticInfo::astPath(int cursorPosition) const
524 QList<AST::Node *> path;
526 foreach (const Range &range, ranges) {
527 if (range.begin.isNull() || range.end.isNull()) {
529 } else if (cursorPosition >= range.begin.position() && cursorPosition <= range.end.position()) {
537 LookupContext::Ptr SemanticInfo::lookupContext(const QList<QmlJS::AST::Node *> &path) const
539 Q_ASSERT(! m_context.isNull());
541 if (m_context.isNull())
542 return LookupContext::create(document, snapshot, path);
544 return LookupContext::create(document, snapshot, *m_context, path);
547 static bool importContainsCursor(UiImport *importAst, unsigned cursorPosition)
549 return cursorPosition >= importAst->firstSourceLocation().begin()
550 && cursorPosition <= importAst->lastSourceLocation().end();
553 AST::Node *SemanticInfo::nodeUnderCursor(int pos) const
558 const unsigned cursorPosition = pos;
560 foreach (const Interpreter::ImportInfo &import, document->bind()->imports()) {
561 if (importContainsCursor(import.ast(), cursorPosition))
565 CollectASTNodes nodes;
566 nodes.accept(document->ast());
568 foreach (AST::UiQualifiedId *q, nodes.qualifiedIds) {
569 if (cursorPosition >= q->identifierToken.begin()) {
570 for (AST::UiQualifiedId *tail = q; tail; tail = tail->next) {
571 if (! tail->next && cursorPosition <= tail->identifierToken.end())
577 foreach (AST::IdentifierExpression *id, nodes.identifiers) {
578 if (cursorPosition >= id->identifierToken.begin() && cursorPosition <= id->identifierToken.end())
582 foreach (AST::FieldMemberExpression *mem, nodes.fieldMembers) {
583 if (mem->name && cursorPosition >= mem->identifierToken.begin() && cursorPosition <= mem->identifierToken.end())
590 bool SemanticInfo::isValid() const
592 if (document && m_context)
598 int SemanticInfo::revision() const
601 return document->editorRevision();
606 QmlJSTextEditorWidget::QmlJSTextEditorWidget(QWidget *parent) :
607 TextEditor::BaseTextEditorWidget(parent),
609 m_outlineModel(new QmlOutlineModel(this)),
612 m_updateSelectedElements(false),
613 m_findReferences(new FindReferences(this))
615 qRegisterMetaType<QmlJSEditor::SemanticInfo>("QmlJSEditor::SemanticInfo");
617 m_semanticHighlighter = new SemanticHighlighter(this);
618 m_semanticHighlighter->start();
620 setParenthesesMatchingEnabled(true);
621 setMarksVisible(true);
622 setCodeFoldingSupported(true);
623 setIndenter(new Indenter);
624 setAutoCompleter(new AutoCompleter);
626 m_updateDocumentTimer = new QTimer(this);
627 m_updateDocumentTimer->setInterval(UPDATE_DOCUMENT_DEFAULT_INTERVAL);
628 m_updateDocumentTimer->setSingleShot(true);
629 connect(m_updateDocumentTimer, SIGNAL(timeout()), this, SLOT(updateDocumentNow()));
631 m_updateUsesTimer = new QTimer(this);
632 m_updateUsesTimer->setInterval(UPDATE_USES_DEFAULT_INTERVAL);
633 m_updateUsesTimer->setSingleShot(true);
634 connect(m_updateUsesTimer, SIGNAL(timeout()), this, SLOT(updateUsesNow()));
636 m_semanticRehighlightTimer = new QTimer(this);
637 m_semanticRehighlightTimer->setInterval(UPDATE_DOCUMENT_DEFAULT_INTERVAL);
638 m_semanticRehighlightTimer->setSingleShot(true);
639 connect(m_semanticRehighlightTimer, SIGNAL(timeout()), this, SLOT(forceSemanticRehighlight()));
641 connect(this, SIGNAL(textChanged()), this, SLOT(updateDocument()));
643 connect(this, SIGNAL(textChanged()), this, SLOT(updateUses()));
644 connect(this, SIGNAL(cursorPositionChanged()), this, SLOT(updateUses()));
646 m_updateOutlineTimer = new QTimer(this);
647 m_updateOutlineTimer->setInterval(UPDATE_OUTLINE_INTERVAL);
648 m_updateOutlineTimer->setSingleShot(true);
649 connect(m_updateOutlineTimer, SIGNAL(timeout()), this, SLOT(updateOutlineNow()));
651 m_updateOutlineIndexTimer = new QTimer(this);
652 m_updateOutlineIndexTimer->setInterval(UPDATE_OUTLINE_INTERVAL);
653 m_updateOutlineIndexTimer->setSingleShot(true);
654 connect(m_updateOutlineIndexTimer, SIGNAL(timeout()), this, SLOT(updateOutlineIndexNow()));
656 m_cursorPositionTimer = new QTimer(this);
657 m_cursorPositionTimer->setInterval(UPDATE_OUTLINE_INTERVAL);
658 m_cursorPositionTimer->setSingleShot(true);
659 connect(m_cursorPositionTimer, SIGNAL(timeout()), this, SLOT(updateCursorPositionNow()));
661 baseTextDocument()->setSyntaxHighlighter(new Highlighter(document()));
662 baseTextDocument()->setCodec(QTextCodec::codecForName("UTF-8")); // qml files are defined to be utf-8
664 m_modelManager = ExtensionSystem::PluginManager::instance()->getObject<ModelManagerInterface>();
665 m_contextPane = ExtensionSystem::PluginManager::instance()->getObject<QmlJS::IContextPane>();
669 connect(this, SIGNAL(cursorPositionChanged()), this, SLOT(onCursorPositionChanged()));
670 connect(m_contextPane, SIGNAL(closed()), this, SLOT(showTextMarker()));
672 m_oldCursorPosition = -1;
674 if (m_modelManager) {
675 m_semanticHighlighter->setModelManager(m_modelManager);
676 connect(m_modelManager, SIGNAL(documentUpdated(QmlJS::Document::Ptr)),
677 this, SLOT(onDocumentUpdated(QmlJS::Document::Ptr)));
678 connect(m_modelManager, SIGNAL(libraryInfoUpdated(QString,QmlJS::LibraryInfo)),
679 this, SLOT(forceSemanticRehighlightIfCurrentEditor()));
680 connect(this->document(), SIGNAL(modificationChanged(bool)), this, SLOT(modificationChanged(bool)));
683 connect(m_semanticHighlighter, SIGNAL(changed(QmlJSEditor::SemanticInfo)),
684 this, SLOT(updateSemanticInfo(QmlJSEditor::SemanticInfo)));
686 connect(this, SIGNAL(refactorMarkerClicked(TextEditor::RefactorMarker)),
687 SLOT(onRefactorMarkerClicked(TextEditor::RefactorMarker)));
689 setRequestMarkEnabled(true);
692 QmlJSTextEditorWidget::~QmlJSTextEditorWidget()
695 m_semanticHighlighter->abort();
696 m_semanticHighlighter->wait();
699 SemanticInfo QmlJSTextEditorWidget::semanticInfo() const
701 return m_semanticInfo;
704 int QmlJSTextEditorWidget::editorRevision() const
706 return document()->revision();
709 bool QmlJSTextEditorWidget::isOutdated() const
711 if (m_semanticInfo.revision() != editorRevision())
717 QmlOutlineModel *QmlJSTextEditorWidget::outlineModel() const
719 return m_outlineModel;
722 QModelIndex QmlJSTextEditorWidget::outlineModelIndex()
724 if (!m_outlineModelIndex.isValid()) {
725 m_outlineModelIndex = indexForPosition(position());
726 emit outlineModelIndexChanged(m_outlineModelIndex);
728 return m_outlineModelIndex;
731 Core::IEditor *QmlJSEditorEditable::duplicate(QWidget *parent)
733 QmlJSTextEditorWidget *newEditor = new QmlJSTextEditorWidget(parent);
734 newEditor->duplicateFrom(editorWidget());
735 QmlJSEditorPlugin::instance()->initializeEditor(newEditor);
736 return newEditor->editor();
739 QString QmlJSEditorEditable::id() const
741 return QLatin1String(QmlJSEditor::Constants::C_QMLJSEDITOR_ID);
744 bool QmlJSEditorEditable::open(const QString &fileName)
746 bool b = TextEditor::BaseTextEditor::open(fileName);
747 editorWidget()->setMimeType(Core::ICore::instance()->mimeDatabase()->findByFile(QFileInfo(fileName)).type());
751 Core::Context QmlJSEditorEditable::context() const
756 void QmlJSTextEditorWidget::updateDocument()
758 m_updateDocumentTimer->start(UPDATE_DOCUMENT_DEFAULT_INTERVAL);
761 void QmlJSTextEditorWidget::updateDocumentNow()
763 // ### move in the parser thread.
765 m_updateDocumentTimer->stop();
767 const QString fileName = file()->fileName();
769 m_modelManager->updateSourceFiles(QStringList() << fileName, false);
772 static void appendExtraSelectionsForMessages(
773 QList<QTextEdit::ExtraSelection> *selections,
774 const QList<DiagnosticMessage> &messages,
775 const QTextDocument *document)
777 foreach (const DiagnosticMessage &d, messages) {
778 const int line = d.loc.startLine;
779 const int column = qMax(1U, d.loc.startColumn);
781 QTextEdit::ExtraSelection sel;
782 QTextCursor c(document->findBlockByNumber(line - 1));
785 sel.cursor.setPosition(c.position() + column - 1);
787 if (d.loc.length == 0) {
788 if (sel.cursor.atBlockEnd())
789 sel.cursor.movePosition(QTextCursor::StartOfWord, QTextCursor::KeepAnchor);
791 sel.cursor.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor);
793 sel.cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor, d.loc.length);
797 sel.format.setUnderlineColor(Qt::darkYellow);
799 sel.format.setUnderlineColor(Qt::red);
801 sel.format.setUnderlineStyle(QTextCharFormat::WaveUnderline);
802 sel.format.setToolTip(d.message);
804 selections->append(sel);
808 void QmlJSTextEditorWidget::onDocumentUpdated(QmlJS::Document::Ptr doc)
810 if (file()->fileName() != doc->fileName()
811 || doc->editorRevision() != document()->revision()) {
816 // got a correctly parsed (or recovered) file.
818 const SemanticHighlighterSource source = currentSource(/*force = */ true);
819 m_semanticHighlighter->rehighlight(source);
821 // show parsing errors
822 QList<QTextEdit::ExtraSelection> selections;
823 appendExtraSelectionsForMessages(&selections, doc->diagnosticMessages(), document());
824 setExtraSelections(CodeWarningsSelection, selections);
828 void QmlJSTextEditorWidget::modificationChanged(bool changed)
830 if (!changed && m_modelManager)
831 m_modelManager->fileChangedOnDisk(file()->fileName());
834 void QmlJSTextEditorWidget::jumpToOutlineElement(int /*index*/)
836 QModelIndex index = m_outlineCombo->view()->currentIndex();
837 AST::SourceLocation location = m_outlineModel->sourceLocation(index);
839 if (!location.isValid())
842 Core::EditorManager *editorManager = Core::EditorManager::instance();
843 editorManager->cutForwardNavigationHistory();
844 editorManager->addCurrentPositionToNavigationHistory();
846 QTextCursor cursor = textCursor();
847 cursor.setPosition(location.offset);
848 setTextCursor(cursor);
853 void QmlJSTextEditorWidget::updateOutlineNow()
855 if (!m_semanticInfo.document)
858 if (m_semanticInfo.document->editorRevision() != editorRevision()) {
859 m_updateOutlineTimer->start();
863 m_outlineModel->update(m_semanticInfo);
865 QTreeView *treeView = static_cast<QTreeView*>(m_outlineCombo->view());
866 treeView->expandAll();
868 updateOutlineIndexNow();
871 void QmlJSTextEditorWidget::updateOutlineIndexNow()
873 if (m_updateOutlineTimer->isActive())
874 return; // updateOutlineNow will call this function soon anyway
876 if (!m_outlineModel->document())
879 if (m_outlineModel->document()->editorRevision() != editorRevision()) {
880 m_updateOutlineIndexTimer->start();
884 m_outlineModelIndex = QModelIndex(); // invalidate
885 QModelIndex comboIndex = outlineModelIndex();
887 if (comboIndex.isValid()) {
888 bool blocked = m_outlineCombo->blockSignals(true);
890 // There is no direct way to select a non-root item
891 m_outlineCombo->setRootModelIndex(comboIndex.parent());
892 m_outlineCombo->setCurrentIndex(comboIndex.row());
893 m_outlineCombo->setRootModelIndex(QModelIndex());
895 m_outlineCombo->blockSignals(blocked);
899 static UiQualifiedId *qualifiedTypeNameId(Node *m)
901 if (UiObjectDefinition *def = cast<UiObjectDefinition *>(m))
902 return def->qualifiedTypeNameId;
903 else if (UiObjectBinding *binding = cast<UiObjectBinding *>(m))
904 return binding->qualifiedTypeNameId;
908 void QmlJSTextEditorWidget::updateCursorPositionNow()
910 if (m_contextPane && document() && semanticInfo().isValid()
911 && document()->revision() == semanticInfo().document->editorRevision())
913 Node *oldNode = m_semanticInfo.declaringMemberNoProperties(m_oldCursorPosition);
914 Node *newNode = m_semanticInfo.declaringMemberNoProperties(position());
915 if (oldNode != newNode && m_oldCursorPosition != -1)
916 m_contextPane->apply(editor(), semanticInfo().document, LookupContext::Ptr(),newNode, false);
917 if (m_contextPane->isAvailable(editor(), semanticInfo().document, newNode) &&
918 !m_contextPane->widget()->isVisible()) {
919 QList<TextEditor::RefactorMarker> markers;
920 if (UiObjectMember *m = newNode->uiObjectMemberCast()) {
921 const int start = qualifiedTypeNameId(m)->identifierToken.begin();
922 for (UiQualifiedId *q = qualifiedTypeNameId(m); q; q = q->next) {
924 const int end = q->identifierToken.end();
925 if (position() >= start && position() <= end) {
926 TextEditor::RefactorMarker marker;
927 QTextCursor tc(document());
930 marker.tooltip = tr("Show Qt Quick ToolBar");
931 markers.append(marker);
933 QList<TextEditor::RefactorMarker> markers;
934 setRefactorMarkers(markers);
939 setRefactorMarkers(markers);
940 } else if (oldNode != newNode) {
941 QList<TextEditor::RefactorMarker> markers;
942 setRefactorMarkers(markers);
944 m_oldCursorPosition = position();
946 setSelectedElements();
950 void QmlJSTextEditorWidget::showTextMarker()
952 m_oldCursorPosition = -1;
953 updateCursorPositionNow();
956 void QmlJSTextEditorWidget::updateUses()
958 m_updateUsesTimer->start();
961 bool QmlJSTextEditorWidget::updateSelectedElements() const
963 return m_updateSelectedElements;
966 void QmlJSTextEditorWidget::setUpdateSelectedElements(bool value)
968 m_updateSelectedElements = value;
971 void QmlJSTextEditorWidget::renameId(const QString &oldId, const QString &newId)
973 Utils::ChangeSet changeSet;
975 foreach (const AST::SourceLocation &loc, m_semanticInfo.idLocations.value(oldId)) {
976 changeSet.replace(loc.begin(), loc.end(), newId);
979 QTextCursor tc = textCursor();
980 changeSet.apply(&tc);
983 void QmlJSTextEditorWidget::updateUsesNow()
985 if (document()->revision() != m_semanticInfo.revision()) {
990 m_updateUsesTimer->stop();
992 QList<QTextEdit::ExtraSelection> selections;
993 foreach (const AST::SourceLocation &loc, m_semanticInfo.idLocations.value(wordUnderCursor())) {
997 QTextEdit::ExtraSelection sel;
998 sel.format = m_occurrencesFormat;
999 sel.cursor = textCursor();
1000 sel.cursor.setPosition(loc.begin());
1001 sel.cursor.setPosition(loc.end(), QTextCursor::KeepAnchor);
1002 selections.append(sel);
1005 setExtraSelections(CodeSemanticsSelection, selections);
1008 class SelectedElement: protected Visitor
1010 unsigned m_cursorPositionStart;
1011 unsigned m_cursorPositionEnd;
1012 QList<UiObjectMember *> m_selectedMembers;
1013 LookupContext::Ptr m_lookupContext;
1017 : m_cursorPositionStart(0), m_cursorPositionEnd(0) {}
1019 QList<UiObjectMember *> operator()(LookupContext::Ptr lookupContext, unsigned startPosition, unsigned endPosition)
1021 m_lookupContext = lookupContext;
1022 m_cursorPositionStart = startPosition;
1023 m_cursorPositionEnd = endPosition;
1024 m_selectedMembers.clear();
1025 Node::accept(lookupContext->document()->qmlProgram(), this);
1026 return m_selectedMembers;
1031 bool isSelectable(UiObjectMember *member) const
1033 UiQualifiedId *id = 0;
1034 if (UiObjectDefinition *def = cast<UiObjectDefinition *>(member))
1035 id = def->qualifiedTypeNameId;
1036 else if (UiObjectBinding *binding = cast<UiObjectBinding *>(member))
1037 id = binding->qualifiedTypeNameId;
1040 QString name = id->name->asString();
1041 if (!name.isEmpty() && name.at(0).isUpper()) {
1049 inline UiObjectInitializer *initializer(UiObjectMember *member) const
1051 if (UiObjectDefinition *def = cast<UiObjectDefinition *>(member))
1052 return def->initializer;
1053 else if (UiObjectBinding *binding = cast<UiObjectBinding *>(member))
1054 return binding->initializer;
1058 inline bool isIdBinding(UiObjectMember *member) const
1060 if (UiScriptBinding *script = cast<UiScriptBinding *>(member)) {
1061 if (! script->qualifiedId)
1063 else if (! script->qualifiedId->name)
1065 else if (script->qualifiedId->next)
1068 const QString propertyName = script->qualifiedId->name->asString();
1070 if (propertyName == QLatin1String("id"))
1077 inline bool containsCursor(unsigned begin, unsigned end)
1079 return m_cursorPositionStart >= begin && m_cursorPositionEnd <= end;
1082 inline bool intersectsCursor(unsigned begin, unsigned end)
1084 return (m_cursorPositionEnd >= begin && m_cursorPositionStart <= end);
1087 inline bool isRangeSelected() const
1089 return (m_cursorPositionStart != m_cursorPositionEnd);
1092 void postVisit(Node *ast)
1094 if (!isRangeSelected() && !m_selectedMembers.isEmpty())
1095 return; // nothing to do, we already have the results.
1097 if (UiObjectMember *member = ast->uiObjectMemberCast()) {
1098 unsigned begin = member->firstSourceLocation().begin();
1099 unsigned end = member->lastSourceLocation().end();
1101 if ((isRangeSelected() && intersectsCursor(begin, end))
1102 || (!isRangeSelected() && containsCursor(begin, end)))
1104 if (initializer(member) && isSelectable(member)) {
1105 m_selectedMembers << member;
1106 // move start towards end; this facilitates multiselection so that root is usually ignored.
1107 m_cursorPositionStart = qMin(end, m_cursorPositionEnd);
1114 void QmlJSTextEditorWidget::setSelectedElements()
1116 if (!m_updateSelectedElements)
1119 QTextCursor tc = textCursor();
1120 QString wordAtCursor;
1126 if (tc.hasSelection()) {
1127 startPos = tc.selectionStart();
1128 endPos = tc.selectionEnd();
1130 tc.movePosition(QTextCursor::StartOfWord);
1131 tc.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor);
1133 startPos = textCursor().position();
1134 endPos = textCursor().position();
1137 if (m_semanticInfo.isValid()) {
1138 SelectedElement selectedMembers;
1139 QList<UiObjectMember *> members = selectedMembers(m_semanticInfo.lookupContext(),
1141 if (!members.isEmpty()) {
1142 foreach(UiObjectMember *m, members) {
1143 offsets << m->firstSourceLocation().begin();
1147 wordAtCursor = tc.selectedText();
1149 emit selectedElementsChanged(offsets, wordAtCursor);
1152 void QmlJSTextEditorWidget::updateFileName()
1156 void QmlJSTextEditorWidget::renameIdUnderCursor()
1158 const QString id = wordUnderCursor();
1160 const QString newId = QInputDialog::getText(Core::ICore::instance()->mainWindow(),
1166 renameId(id, newId);
1170 void QmlJSTextEditorWidget::setFontSettings(const TextEditor::FontSettings &fs)
1172 TextEditor::BaseTextEditorWidget::setFontSettings(fs);
1173 Highlighter *highlighter = qobject_cast<Highlighter*>(baseTextDocument()->syntaxHighlighter());
1177 highlighter->setFormats(fs.toTextCharFormats(highlighterFormatCategories()));
1178 highlighter->rehighlight();
1180 m_occurrencesFormat = fs.toTextCharFormat(QLatin1String(TextEditor::Constants::C_OCCURRENCES));
1181 m_occurrencesUnusedFormat = fs.toTextCharFormat(QLatin1String(TextEditor::Constants::C_OCCURRENCES_UNUSED));
1182 m_occurrencesUnusedFormat.setUnderlineStyle(QTextCharFormat::WaveUnderline);
1183 m_occurrencesUnusedFormat.setUnderlineColor(m_occurrencesUnusedFormat.foreground().color());
1184 m_occurrencesUnusedFormat.clearForeground();
1185 m_occurrencesUnusedFormat.setToolTip(tr("Unused variable"));
1186 m_occurrenceRenameFormat = fs.toTextCharFormat(QLatin1String(TextEditor::Constants::C_OCCURRENCES_RENAME));
1188 // only set the background, we do not want to modify foreground properties set by the syntax highlighter or the link
1189 m_occurrencesFormat.clearForeground();
1190 m_occurrenceRenameFormat.clearForeground();
1194 QString QmlJSTextEditorWidget::wordUnderCursor() const
1196 QTextCursor tc = textCursor();
1197 const QChar ch = characterAt(tc.position() - 1);
1198 // make sure that we're not at the start of the next word.
1199 if (ch.isLetterOrNumber() || ch == QLatin1Char('_'))
1200 tc.movePosition(QTextCursor::Left);
1201 tc.movePosition(QTextCursor::StartOfWord);
1202 tc.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor);
1203 const QString word = tc.selectedText();
1207 bool QmlJSTextEditorWidget::isClosingBrace(const QList<Token> &tokens) const
1210 if (tokens.size() == 1) {
1211 const Token firstToken = tokens.first();
1213 return firstToken.is(Token::RightBrace) || firstToken.is(Token::RightBracket);
1219 TextEditor::BaseTextEditor *QmlJSTextEditorWidget::createEditor()
1221 QmlJSEditorEditable *editable = new QmlJSEditorEditable(this);
1222 createToolBar(editable);
1226 void QmlJSTextEditorWidget::createToolBar(QmlJSEditorEditable *editor)
1228 m_outlineCombo = new QComboBox;
1229 m_outlineCombo->setMinimumContentsLength(22);
1230 m_outlineCombo->setModel(m_outlineModel);
1232 QTreeView *treeView = new QTreeView;
1233 treeView->header()->hide();
1234 treeView->setItemsExpandable(false);
1235 treeView->setRootIsDecorated(false);
1236 m_outlineCombo->setView(treeView);
1237 treeView->expandAll();
1239 //m_outlineCombo->setSizeAdjustPolicy(QComboBox::AdjustToContents);
1241 // Make the combo box prefer to expand
1242 QSizePolicy policy = m_outlineCombo->sizePolicy();
1243 policy.setHorizontalPolicy(QSizePolicy::Expanding);
1244 m_outlineCombo->setSizePolicy(policy);
1246 connect(m_outlineCombo, SIGNAL(activated(int)), this, SLOT(jumpToOutlineElement(int)));
1247 connect(this, SIGNAL(cursorPositionChanged()), m_updateOutlineIndexTimer, SLOT(start()));
1249 connect(file(), SIGNAL(changed()), this, SLOT(updateFileName()));
1251 editor->insertExtraToolBarWidget(TextEditor::BaseTextEditor::Left, m_outlineCombo);
1254 TextEditor::BaseTextEditorWidget::Link QmlJSTextEditorWidget::findLinkAt(const QTextCursor &cursor, bool /*resolveTarget*/)
1256 const SemanticInfo semanticInfo = m_semanticInfo;
1257 if (! semanticInfo.isValid())
1260 const unsigned cursorPosition = cursor.position();
1262 AST::Node *node = semanticInfo.nodeUnderCursor(cursorPosition);
1264 if (AST::UiImport *importAst = cast<AST::UiImport *>(node)) {
1265 // if it's a file import, link to the file
1266 foreach (const Interpreter::ImportInfo &import, semanticInfo.document->bind()->imports()) {
1267 if (import.ast() == importAst && import.type() == Interpreter::ImportInfo::FileImport) {
1268 BaseTextEditorWidget::Link link(import.name());
1269 link.begin = importAst->firstSourceLocation().begin();
1270 link.end = importAst->lastSourceLocation().end();
1277 LookupContext::Ptr lookupContext = semanticInfo.lookupContext(semanticInfo.astPath(cursorPosition));
1278 Evaluate evaluator(lookupContext->context());
1279 const Interpreter::Value *value = evaluator.reference(node);
1282 int line = 0, column = 0;
1284 if (! (value && value->getSourceLocation(&fileName, &line, &column)))
1287 BaseTextEditorWidget::Link link;
1288 link.fileName = fileName;
1290 link.column = column - 1; // adjust the column
1292 if (AST::UiQualifiedId *q = AST::cast<AST::UiQualifiedId *>(node)) {
1293 for (AST::UiQualifiedId *tail = q; tail; tail = tail->next) {
1294 if (! tail->next && cursorPosition <= tail->identifierToken.end()) {
1295 link.begin = tail->identifierToken.begin();
1296 link.end = tail->identifierToken.end();
1301 } else if (AST::IdentifierExpression *id = AST::cast<AST::IdentifierExpression *>(node)) {
1302 link.begin = id->firstSourceLocation().begin();
1303 link.end = id->lastSourceLocation().end();
1306 } else if (AST::FieldMemberExpression *mem = AST::cast<AST::FieldMemberExpression *>(node)) {
1307 link.begin = mem->lastSourceLocation().begin();
1308 link.end = mem->lastSourceLocation().end();
1315 void QmlJSTextEditorWidget::followSymbolUnderCursor()
1317 openLink(findLinkAt(textCursor()));
1320 void QmlJSTextEditorWidget::findUsages()
1322 m_findReferences->findUsages(file()->fileName(), textCursor().position());
1325 void QmlJSTextEditorWidget::showContextPane()
1327 if (m_contextPane && m_semanticInfo.isValid()) {
1328 Node *newNode = m_semanticInfo.declaringMemberNoProperties(position());
1329 m_contextPane->apply(editor(), m_semanticInfo.document, m_semanticInfo.lookupContext(), newNode, false, true);
1330 m_oldCursorPosition = position();
1331 QList<TextEditor::RefactorMarker> markers;
1332 setRefactorMarkers(markers);
1336 void QmlJSTextEditorWidget::performQuickFix(int index)
1338 TextEditor::QuickFixOperation::Ptr op = m_quickFixes.at(index);
1342 void QmlJSTextEditorWidget::contextMenuEvent(QContextMenuEvent *e)
1344 QMenu *menu = new QMenu();
1346 QMenu *refactoringMenu = new QMenu(tr("Refactoring"), menu);
1348 // Conditionally add the rename-id action:
1349 const QString id = wordUnderCursor();
1350 const QList<AST::SourceLocation> &locations = m_semanticInfo.idLocations.value(id);
1351 if (! locations.isEmpty()) {
1352 QAction *a = refactoringMenu->addAction(tr("Rename id '%1'...").arg(id));
1353 connect(a, SIGNAL(triggered()), this, SLOT(renameIdUnderCursor()));
1356 // Add other refactoring actions:
1357 QmlJSQuickFixCollector *quickFixCollector = QmlJSEditorPlugin::instance()->quickFixCollector();
1358 QSignalMapper mapper;
1359 connect(&mapper, SIGNAL(mapped(int)), this, SLOT(performQuickFix(int)));
1361 if (! isOutdated()) {
1362 if (quickFixCollector->startCompletion(editor()) != -1) {
1363 m_quickFixes = quickFixCollector->quickFixes();
1365 for (int index = 0; index < m_quickFixes.size(); ++index) {
1366 TextEditor::QuickFixOperation::Ptr op = m_quickFixes.at(index);
1367 QAction *action = refactoringMenu->addAction(op->description());
1368 mapper.setMapping(action, index);
1369 connect(action, SIGNAL(triggered()), &mapper, SLOT(map()));
1374 refactoringMenu->setEnabled(!refactoringMenu->isEmpty());
1376 if (Core::ActionContainer *mcontext = Core::ICore::instance()->actionManager()->actionContainer(QmlJSEditor::Constants::M_CONTEXT)) {
1377 QMenu *contextMenu = mcontext->menu();
1378 foreach (QAction *action, contextMenu->actions()) {
1379 menu->addAction(action);
1380 if (action->objectName() == QmlJSEditor::Constants::M_REFACTORING_MENU_INSERTION_POINT)
1381 menu->addMenu(refactoringMenu);
1385 appendStandardContextMenuActions(menu);
1387 menu->exec(e->globalPos());
1388 menu->deleteLater();
1389 quickFixCollector->cleanup();
1390 m_quickFixes.clear();
1393 bool QmlJSTextEditorWidget::event(QEvent *e)
1395 switch (e->type()) {
1396 case QEvent::ShortcutOverride:
1397 if (static_cast<QKeyEvent*>(e)->key() == Qt::Key_Escape && m_contextPane) {
1398 if (hideContextPane()) {
1408 return BaseTextEditorWidget::event(e);
1412 void QmlJSTextEditorWidget::wheelEvent(QWheelEvent *event)
1414 bool visible = false;
1415 if (m_contextPane && m_contextPane->widget()->isVisible())
1418 BaseTextEditorWidget::wheelEvent(event);
1421 LookupContext::Ptr lookupContext;
1422 if (m_semanticInfo.isValid())
1423 lookupContext = m_semanticInfo.lookupContext();
1424 m_contextPane->apply(editor(), semanticInfo().document, QmlJS::LookupContext::Ptr(), m_semanticInfo.declaringMemberNoProperties(m_oldCursorPosition), false, true);
1428 void QmlJSTextEditorWidget::resizeEvent(QResizeEvent *event)
1430 BaseTextEditorWidget::resizeEvent(event);
1434 void QmlJSTextEditorWidget::scrollContentsBy(int dx, int dy)
1436 BaseTextEditorWidget::scrollContentsBy(dx, dy);
1440 void QmlJSTextEditorWidget::unCommentSelection()
1442 Utils::unCommentSelection(this);
1445 void QmlJSTextEditorWidget::forceSemanticRehighlight()
1447 m_semanticHighlighter->rehighlight(currentSource(/* force = */ true));
1450 void QmlJSEditor::QmlJSTextEditorWidget::forceSemanticRehighlightIfCurrentEditor()
1452 Core::EditorManager *editorManager = Core::EditorManager::instance();
1453 if (editorManager->currentEditor() == editor())
1454 forceSemanticRehighlight();
1457 void QmlJSTextEditorWidget::semanticRehighlight()
1459 m_semanticHighlighter->rehighlight(currentSource());
1462 void QmlJSTextEditorWidget::updateSemanticInfo(const SemanticInfo &semanticInfo)
1464 if (semanticInfo.revision() != document()->revision()) {
1465 // got outdated semantic info
1466 semanticRehighlight();
1470 m_semanticInfo = semanticInfo;
1471 Document::Ptr doc = semanticInfo.document;
1473 // create the ranges
1474 CreateRanges createRanges;
1475 m_semanticInfo.ranges = createRanges(document(), doc);
1478 FindIdDeclarations updateIds;
1479 m_semanticInfo.idLocations = updateIds(doc);
1481 FindDeclarations findDeclarations;
1482 m_semanticInfo.declarations = findDeclarations(doc->ast());
1484 if (m_contextPane) {
1485 Node *newNode = m_semanticInfo.declaringMemberNoProperties(position());
1487 m_contextPane->apply(editor(), semanticInfo.document, LookupContext::Ptr(), newNode, true);
1488 m_cursorPositionTimer->start(); //update text marker
1493 m_updateOutlineTimer->start();
1495 // update warning/error extra selections
1496 QList<QTextEdit::ExtraSelection> selections;
1497 appendExtraSelectionsForMessages(&selections, doc->diagnosticMessages(), document());
1498 appendExtraSelectionsForMessages(&selections, m_semanticInfo.semanticMessages, document());
1499 setExtraSelections(CodeWarningsSelection, selections);
1502 void QmlJSTextEditorWidget::onRefactorMarkerClicked(const TextEditor::RefactorMarker &)
1507 void QmlJSTextEditorWidget::onCursorPositionChanged()
1509 m_cursorPositionTimer->start();
1512 QModelIndex QmlJSTextEditorWidget::indexForPosition(unsigned cursorPosition, const QModelIndex &rootIndex) const
1514 QModelIndex lastIndex = rootIndex;
1517 const int rowCount = m_outlineModel->rowCount(rootIndex);
1518 for (int i = 0; i < rowCount; ++i) {
1519 QModelIndex childIndex = m_outlineModel->index(i, 0, rootIndex);
1520 AST::SourceLocation location = m_outlineModel->sourceLocation(childIndex);
1522 if ((cursorPosition >= location.offset)
1523 && (cursorPosition <= location.offset + location.length)) {
1524 lastIndex = childIndex;
1529 if (lastIndex != rootIndex) {
1531 lastIndex = indexForPosition(cursorPosition, lastIndex);
1536 bool QmlJSTextEditorWidget::hideContextPane()
1538 bool b = (m_contextPane) && m_contextPane->widget()->isVisible();
1540 m_contextPane->apply(editor(), semanticInfo().document, LookupContext::Ptr(), 0, false);
1545 QVector<QString> QmlJSTextEditorWidget::highlighterFormatCategories()
1556 static QVector<QString> categories;
1557 if (categories.isEmpty()) {
1558 categories << QLatin1String(TextEditor::Constants::C_NUMBER)
1559 << QLatin1String(TextEditor::Constants::C_STRING)
1560 << QLatin1String(TextEditor::Constants::C_TYPE)
1561 << QLatin1String(TextEditor::Constants::C_KEYWORD)
1562 << QLatin1String(TextEditor::Constants::C_FIELD)
1563 << QLatin1String(TextEditor::Constants::C_COMMENT)
1564 << QLatin1String(TextEditor::Constants::C_VISUAL_WHITESPACE);
1569 SemanticHighlighterSource QmlJSTextEditorWidget::currentSource(bool force)
1571 int line = 0, column = 0;
1572 convertPosition(position(), &line, &column);
1574 const Snapshot snapshot = m_modelManager->snapshot();
1575 const QString fileName = file()->fileName();
1578 if (force || m_semanticInfo.revision() != document()->revision())
1579 code = toPlainText(); // get the source code only when needed.
1581 const unsigned revision = document()->revision();
1582 SemanticHighlighterSource source(snapshot, fileName, code,
1583 line, column, revision);
1584 source.force = force;