OSDN Git Service

3ff3d2a663028c7992a21091568ce10bf3732e37
[qt-creator-jp/qt-creator-jp.git] / src / plugins / qmljseditor / qmljseditor.cpp
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 #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"
45
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>
55
56 #include <qmljstools/qmljsqtstylecodeformatter.h>
57
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>
78
79 #include <QtCore/QFileInfo>
80 #include <QtCore/QSignalMapper>
81 #include <QtCore/QTimer>
82
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>
90
91 enum {
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
95 };
96
97 using namespace QmlJS;
98 using namespace QmlJS::AST;
99 using namespace QmlJSEditor;
100 using namespace QmlJSEditor::Internal;
101
102 namespace {
103
104 class FindIdDeclarations: protected Visitor
105 {
106 public:
107     typedef QHash<QString, QList<AST::SourceLocation> > Result;
108
109     Result operator()(Document::Ptr doc)
110     {
111         _ids.clear();
112         _maybeIds.clear();
113         if (doc && doc->qmlProgram())
114             doc->qmlProgram()->accept(this);
115         return _ids;
116     }
117
118 protected:
119     QString asString(AST::UiQualifiedId *id)
120     {
121         QString text;
122         for (; id; id = id->next) {
123             if (id->name)
124                 text += id->name->asString();
125             else
126                 text += QLatin1Char('?');
127
128             if (id->next)
129                 text += QLatin1Char('.');
130         }
131
132         return text;
133     }
134
135     void accept(AST::Node *node)
136     { AST::Node::acceptChild(node, this); }
137
138     using Visitor::visit;
139     using Visitor::endVisit;
140
141     virtual bool visit(AST::UiScriptBinding *node)
142     {
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)) {
146                     if (idExpr->name) {
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);
152                         return false;
153                     }
154                 }
155             }
156         }
157
158         accept(node->statement);
159
160         return false;
161     }
162
163     virtual bool visit(AST::IdentifierExpression *node)
164     {
165         if (node->name) {
166             const QString name = node->name->asString();
167
168             if (_ids.contains(name))
169                 _ids[name].append(node->identifierToken);
170             else
171                 _maybeIds[name].append(node->identifierToken);
172         }
173         return false;
174     }
175
176 private:
177     Result _ids;
178     Result _maybeIds;
179 };
180
181 class FindDeclarations: protected Visitor
182 {
183     QList<Declaration> _declarations;
184     int _depth;
185
186 public:
187     QList<Declaration> operator()(AST::Node *node)
188     {
189         _depth = -1;
190         _declarations.clear();
191         accept(node);
192         return _declarations;
193     }
194
195 protected:
196     using Visitor::visit;
197     using Visitor::endVisit;
198
199     QString asString(AST::UiQualifiedId *id)
200     {
201         QString text;
202         for (; id; id = id->next) {
203             if (id->name)
204                 text += id->name->asString();
205             else
206                 text += QLatin1Char('?');
207
208             if (id->next)
209                 text += QLatin1Char('.');
210         }
211
212         return text;
213     }
214
215     void accept(AST::Node *node)
216     { AST::Node::acceptChild(node, this); }
217
218     void init(Declaration *decl, AST::UiObjectMember *member)
219     {
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;
226     }
227
228     void init(Declaration *decl, AST::ExpressionNode *expressionNode)
229     {
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;
236     }
237
238     virtual bool visit(AST::UiObjectDefinition *node)
239     {
240         ++_depth;
241
242         Declaration decl;
243         init(&decl, node);
244
245         decl.text.fill(QLatin1Char(' '), _depth);
246         if (node->qualifiedTypeNameId)
247             decl.text.append(asString(node->qualifiedTypeNameId));
248         else
249             decl.text.append(QLatin1Char('?'));
250
251         _declarations.append(decl);
252
253         return true; // search for more bindings
254     }
255
256     virtual void endVisit(AST::UiObjectDefinition *)
257     {
258         --_depth;
259     }
260
261     virtual bool visit(AST::UiObjectBinding *node)
262     {
263         ++_depth;
264
265         Declaration decl;
266         init(&decl, node);
267
268         decl.text.fill(QLatin1Char(' '), _depth);
269
270         decl.text.append(asString(node->qualifiedId));
271         decl.text.append(QLatin1String(": "));
272
273         if (node->qualifiedTypeNameId)
274             decl.text.append(asString(node->qualifiedTypeNameId));
275         else
276             decl.text.append(QLatin1Char('?'));
277
278         _declarations.append(decl);
279
280         return true; // search for more bindings
281     }
282
283     virtual void endVisit(AST::UiObjectBinding *)
284     {
285         --_depth;
286     }
287
288     virtual bool visit(AST::UiScriptBinding *)
289     {
290         ++_depth;
291
292 #if 0 // ### ignore script bindings for now.
293         Declaration decl;
294         init(&decl, node);
295
296         decl.text.fill(QLatin1Char(' '), _depth);
297         decl.text.append(asString(node->qualifiedId));
298
299         _declarations.append(decl);
300 #endif
301
302         return false; // more more bindings in this subtree.
303     }
304
305     virtual void endVisit(AST::UiScriptBinding *)
306     {
307         --_depth;
308     }
309
310     virtual bool visit(AST::FunctionExpression *)
311     {
312         return false;
313     }
314
315     virtual bool visit(AST::FunctionDeclaration *ast)
316     {
317         if (! ast->name)
318             return false;
319
320         Declaration decl;
321         init(&decl, ast);
322
323         decl.text.fill(QLatin1Char(' '), _depth);
324         decl.text += ast->name->asString();
325
326         decl.text += QLatin1Char('(');
327         for (FormalParameterList *it = ast->formals; it; it = it->next) {
328             if (it->name)
329                 decl.text += it->name->asString();
330
331             if (it->next)
332                 decl.text += QLatin1String(", ");
333         }
334
335         decl.text += QLatin1Char(')');
336
337         _declarations.append(decl);
338
339         return false;
340     }
341
342     virtual bool visit(AST::VariableDeclaration *ast)
343     {
344         if (! ast->name)
345             return false;
346
347         Declaration decl;
348         decl.text.fill(QLatin1Char(' '), _depth);
349         decl.text += ast->name->asString();
350
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;
356
357         _declarations.append(decl);
358
359         return false;
360     }
361 };
362
363 class CreateRanges: protected AST::Visitor
364 {
365     QTextDocument *_textDocument;
366     QList<Range> _ranges;
367
368 public:
369     QList<Range> operator()(QTextDocument *textDocument, Document::Ptr doc)
370     {
371         _textDocument = textDocument;
372         _ranges.clear();
373         if (doc && doc->ast() != 0)
374             doc->ast()->accept(this);
375         return _ranges;
376     }
377
378 protected:
379     using AST::Visitor::visit;
380
381     virtual bool visit(AST::UiObjectBinding *ast)
382     {
383         if (ast->initializer && ast->initializer->lbraceToken.length)
384             _ranges.append(createRange(ast, ast->initializer));
385         return true;
386     }
387
388     virtual bool visit(AST::UiObjectDefinition *ast)
389     {
390         if (ast->initializer && ast->initializer->lbraceToken.length)
391             _ranges.append(createRange(ast, ast->initializer));
392         return true;
393     }
394
395     virtual bool visit(AST::FunctionExpression *ast)
396     {
397         _ranges.append(createRange(ast));
398         return true;
399     }
400
401     virtual bool visit(AST::FunctionDeclaration *ast)
402     {
403         _ranges.append(createRange(ast));
404         return true;
405     }
406
407     Range createRange(AST::UiObjectMember *member, AST::UiObjectInitializer *ast)
408     {
409         Range range;
410
411         range.ast = member;
412
413         range.begin = QTextCursor(_textDocument);
414         range.begin.setPosition(member->firstSourceLocation().begin());
415
416         range.end = QTextCursor(_textDocument);
417         range.end.setPosition(ast->rbraceToken.end());
418         return range;
419     }
420
421     Range createRange(AST::FunctionExpression *ast)
422     {
423         Range range;
424
425         range.ast = ast;
426
427         range.begin = QTextCursor(_textDocument);
428         range.begin.setPosition(ast->lbraceToken.begin());
429
430         range.end = QTextCursor(_textDocument);
431         range.end.setPosition(ast->rbraceToken.end());
432
433         return range;
434     }
435
436 };
437
438
439 class CollectASTNodes: protected AST::Visitor
440 {
441 public:
442     QList<AST::UiQualifiedId *> qualifiedIds;
443     QList<AST::IdentifierExpression *> identifiers;
444     QList<AST::FieldMemberExpression *> fieldMembers;
445
446     void accept(AST::Node *node)
447     {
448         if (node)
449             node->accept(this);
450     }
451
452 protected:
453     using AST::Visitor::visit;
454
455     virtual bool visit(AST::UiQualifiedId *ast)
456     {
457         qualifiedIds.append(ast);
458         return false;
459     }
460
461     virtual bool visit(AST::IdentifierExpression *ast)
462     {
463         identifiers.append(ast);
464         return false;
465     }
466
467     virtual bool visit(AST::FieldMemberExpression *ast)
468     {
469         fieldMembers.append(ast);
470         return true;
471     }
472 };
473
474 } // end of anonymous namespace
475
476
477 AST::Node *SemanticInfo::declaringMember(int cursorPosition) const
478 {
479     AST::Node *declaringMember = 0;
480
481     for (int i = ranges.size() - 1; i != -1; --i) {
482         const Range &range = ranges.at(i);
483
484         if (range.begin.isNull() || range.end.isNull()) {
485             continue;
486         } else if (cursorPosition >= range.begin.position() && cursorPosition <= range.end.position()) {
487             declaringMember = range.ast;
488             break;
489         }
490     }
491
492     return declaringMember;
493 }
494
495 QmlJS::AST::Node *SemanticInfo::declaringMemberNoProperties(int cursorPosition) const
496 {
497    AST::Node *node = declaringMember(cursorPosition);
498
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);
503            if (path.size() > 1)
504                return path.at(path.size() - 2);
505        } else if (name.contains("GradientStop")) {
506            QList<AST::Node *> path = astPath(cursorPosition);
507            if (path.size() > 2)
508                return path.at(path.size() - 3);
509        }
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);
514            if (path.size() > 1)
515                return path.at(path.size() - 2);
516        }
517    }
518
519    return node;
520 }
521
522 QList<AST::Node *> SemanticInfo::astPath(int cursorPosition) const
523 {
524     QList<AST::Node *> path;
525
526     foreach (const Range &range, ranges) {
527         if (range.begin.isNull() || range.end.isNull()) {
528             continue;
529         } else if (cursorPosition >= range.begin.position() && cursorPosition <= range.end.position()) {
530             path += range.ast;
531         }
532     }
533
534     return path;
535 }
536
537 LookupContext::Ptr SemanticInfo::lookupContext(const QList<QmlJS::AST::Node *> &path) const
538 {
539     Q_ASSERT(! m_context.isNull());
540
541     if (m_context.isNull())
542         return LookupContext::create(document, snapshot, path);
543
544     return LookupContext::create(document, snapshot, *m_context, path);
545 }
546
547 static bool importContainsCursor(UiImport *importAst, unsigned cursorPosition)
548 {
549     return cursorPosition >= importAst->firstSourceLocation().begin()
550            && cursorPosition <= importAst->lastSourceLocation().end();
551 }
552
553 AST::Node *SemanticInfo::nodeUnderCursor(int pos) const
554 {
555     if (! document)
556         return 0;
557
558     const unsigned cursorPosition = pos;
559
560     foreach (const Interpreter::ImportInfo &import, document->bind()->imports()) {
561         if (importContainsCursor(import.ast(), cursorPosition))
562             return import.ast();
563     }
564
565     CollectASTNodes nodes;
566     nodes.accept(document->ast());
567
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())
572                     return q;
573             }
574         }
575     }
576
577     foreach (AST::IdentifierExpression *id, nodes.identifiers) {
578         if (cursorPosition >= id->identifierToken.begin() && cursorPosition <= id->identifierToken.end())
579             return id;
580     }
581
582     foreach (AST::FieldMemberExpression *mem, nodes.fieldMembers) {
583         if (mem->name && cursorPosition >= mem->identifierToken.begin() && cursorPosition <= mem->identifierToken.end())
584             return mem;
585     }
586
587     return 0;
588 }
589
590 bool SemanticInfo::isValid() const
591 {
592     if (document && m_context)
593         return true;
594
595     return false;
596 }
597
598 int SemanticInfo::revision() const
599 {
600     if (document)
601         return document->editorRevision();
602
603     return 0;
604 }
605
606 QmlJSTextEditorWidget::QmlJSTextEditorWidget(QWidget *parent) :
607     TextEditor::BaseTextEditorWidget(parent),
608     m_outlineCombo(0),
609     m_outlineModel(new QmlOutlineModel(this)),
610     m_modelManager(0),
611     m_contextPane(0),
612     m_updateSelectedElements(false),
613     m_findReferences(new FindReferences(this))
614 {
615     qRegisterMetaType<QmlJSEditor::SemanticInfo>("QmlJSEditor::SemanticInfo");
616
617     m_semanticHighlighter = new SemanticHighlighter(this);
618     m_semanticHighlighter->start();
619
620     setParenthesesMatchingEnabled(true);
621     setMarksVisible(true);
622     setCodeFoldingSupported(true);
623     setIndenter(new Indenter);
624     setAutoCompleter(new AutoCompleter);
625
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()));
630
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()));
635
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()));
640
641     connect(this, SIGNAL(textChanged()), this, SLOT(updateDocument()));
642
643     connect(this, SIGNAL(textChanged()), this, SLOT(updateUses()));
644     connect(this, SIGNAL(cursorPositionChanged()), this, SLOT(updateUses()));
645
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()));
650
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()));
655
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()));
660
661     baseTextDocument()->setSyntaxHighlighter(new Highlighter(document()));
662     baseTextDocument()->setCodec(QTextCodec::codecForName("UTF-8")); // qml files are defined to be utf-8
663
664     m_modelManager = ExtensionSystem::PluginManager::instance()->getObject<ModelManagerInterface>();
665     m_contextPane = ExtensionSystem::PluginManager::instance()->getObject<QmlJS::IContextPane>();
666
667
668     if (m_contextPane) {
669         connect(this, SIGNAL(cursorPositionChanged()), this, SLOT(onCursorPositionChanged()));
670         connect(m_contextPane, SIGNAL(closed()), this, SLOT(showTextMarker()));
671     }
672     m_oldCursorPosition = -1;
673
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)));
681     }
682
683     connect(m_semanticHighlighter, SIGNAL(changed(QmlJSEditor::SemanticInfo)),
684             this, SLOT(updateSemanticInfo(QmlJSEditor::SemanticInfo)));
685
686     connect(this, SIGNAL(refactorMarkerClicked(TextEditor::RefactorMarker)),
687             SLOT(onRefactorMarkerClicked(TextEditor::RefactorMarker)));
688
689     setRequestMarkEnabled(true);
690 }
691
692 QmlJSTextEditorWidget::~QmlJSTextEditorWidget()
693 {
694     hideContextPane();
695     m_semanticHighlighter->abort();
696     m_semanticHighlighter->wait();
697 }
698
699 SemanticInfo QmlJSTextEditorWidget::semanticInfo() const
700 {
701     return m_semanticInfo;
702 }
703
704 int QmlJSTextEditorWidget::editorRevision() const
705 {
706     return document()->revision();
707 }
708
709 bool QmlJSTextEditorWidget::isOutdated() const
710 {
711     if (m_semanticInfo.revision() != editorRevision())
712         return true;
713
714     return false;
715 }
716
717 QmlOutlineModel *QmlJSTextEditorWidget::outlineModel() const
718 {
719     return m_outlineModel;
720 }
721
722 QModelIndex QmlJSTextEditorWidget::outlineModelIndex()
723 {
724     if (!m_outlineModelIndex.isValid()) {
725         m_outlineModelIndex = indexForPosition(position());
726         emit outlineModelIndexChanged(m_outlineModelIndex);
727     }
728     return m_outlineModelIndex;
729 }
730
731 Core::IEditor *QmlJSEditorEditable::duplicate(QWidget *parent)
732 {
733     QmlJSTextEditorWidget *newEditor = new QmlJSTextEditorWidget(parent);
734     newEditor->duplicateFrom(editorWidget());
735     QmlJSEditorPlugin::instance()->initializeEditor(newEditor);
736     return newEditor->editor();
737 }
738
739 QString QmlJSEditorEditable::id() const
740 {
741     return QLatin1String(QmlJSEditor::Constants::C_QMLJSEDITOR_ID);
742 }
743
744 bool QmlJSEditorEditable::open(const QString &fileName)
745 {
746     bool b = TextEditor::BaseTextEditor::open(fileName);
747     editorWidget()->setMimeType(Core::ICore::instance()->mimeDatabase()->findByFile(QFileInfo(fileName)).type());
748     return b;
749 }
750
751 Core::Context QmlJSEditorEditable::context() const
752 {
753     return m_context;
754 }
755
756 void QmlJSTextEditorWidget::updateDocument()
757 {
758     m_updateDocumentTimer->start(UPDATE_DOCUMENT_DEFAULT_INTERVAL);
759 }
760
761 void QmlJSTextEditorWidget::updateDocumentNow()
762 {
763     // ### move in the parser thread.
764
765     m_updateDocumentTimer->stop();
766
767     const QString fileName = file()->fileName();
768
769     m_modelManager->updateSourceFiles(QStringList() << fileName, false);
770 }
771
772 static void appendExtraSelectionsForMessages(
773         QList<QTextEdit::ExtraSelection> *selections,
774         const QList<DiagnosticMessage> &messages,
775         const QTextDocument *document)
776 {
777     foreach (const DiagnosticMessage &d, messages) {
778         const int line = d.loc.startLine;
779         const int column = qMax(1U, d.loc.startColumn);
780
781         QTextEdit::ExtraSelection sel;
782         QTextCursor c(document->findBlockByNumber(line - 1));
783         sel.cursor = c;
784
785         sel.cursor.setPosition(c.position() + column - 1);
786
787         if (d.loc.length == 0) {
788             if (sel.cursor.atBlockEnd())
789                 sel.cursor.movePosition(QTextCursor::StartOfWord, QTextCursor::KeepAnchor);
790             else
791                 sel.cursor.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor);
792         } else {
793             sel.cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor, d.loc.length);
794         }
795
796         if (d.isWarning())
797             sel.format.setUnderlineColor(Qt::darkYellow);
798         else
799             sel.format.setUnderlineColor(Qt::red);
800
801         sel.format.setUnderlineStyle(QTextCharFormat::WaveUnderline);
802         sel.format.setToolTip(d.message);
803
804         selections->append(sel);
805     }
806 }
807
808 void QmlJSTextEditorWidget::onDocumentUpdated(QmlJS::Document::Ptr doc)
809 {
810     if (file()->fileName() != doc->fileName()
811             || doc->editorRevision() != document()->revision()) {
812         return;
813     }
814
815     if (doc->ast()) {
816         // got a correctly parsed (or recovered) file.
817
818         const SemanticHighlighterSource source = currentSource(/*force = */ true);
819         m_semanticHighlighter->rehighlight(source);
820     } else {
821         // show parsing errors
822         QList<QTextEdit::ExtraSelection> selections;
823         appendExtraSelectionsForMessages(&selections, doc->diagnosticMessages(), document());
824         setExtraSelections(CodeWarningsSelection, selections);
825     }
826 }
827
828 void QmlJSTextEditorWidget::modificationChanged(bool changed)
829 {
830     if (!changed && m_modelManager)
831         m_modelManager->fileChangedOnDisk(file()->fileName());
832 }
833
834 void QmlJSTextEditorWidget::jumpToOutlineElement(int /*index*/)
835 {
836     QModelIndex index = m_outlineCombo->view()->currentIndex();
837     AST::SourceLocation location = m_outlineModel->sourceLocation(index);
838
839     if (!location.isValid())
840         return;
841
842     Core::EditorManager *editorManager = Core::EditorManager::instance();
843     editorManager->cutForwardNavigationHistory();
844     editorManager->addCurrentPositionToNavigationHistory();
845
846     QTextCursor cursor = textCursor();
847     cursor.setPosition(location.offset);
848     setTextCursor(cursor);
849
850     setFocus();
851 }
852
853 void QmlJSTextEditorWidget::updateOutlineNow()
854 {
855     if (!m_semanticInfo.document)
856         return;
857
858     if (m_semanticInfo.document->editorRevision() != editorRevision()) {
859         m_updateOutlineTimer->start();
860         return;
861     }
862
863     m_outlineModel->update(m_semanticInfo);
864
865     QTreeView *treeView = static_cast<QTreeView*>(m_outlineCombo->view());
866     treeView->expandAll();
867
868     updateOutlineIndexNow();
869 }
870
871 void QmlJSTextEditorWidget::updateOutlineIndexNow()
872 {
873     if (m_updateOutlineTimer->isActive())
874         return; // updateOutlineNow will call this function soon anyway
875
876     if (!m_outlineModel->document())
877         return;
878
879     if (m_outlineModel->document()->editorRevision() != editorRevision()) {
880         m_updateOutlineIndexTimer->start();
881         return;
882     }
883
884     m_outlineModelIndex = QModelIndex(); // invalidate
885     QModelIndex comboIndex = outlineModelIndex();
886
887     if (comboIndex.isValid()) {
888         bool blocked = m_outlineCombo->blockSignals(true);
889
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());
894
895         m_outlineCombo->blockSignals(blocked);
896     }
897 }
898
899 static UiQualifiedId *qualifiedTypeNameId(Node *m)
900 {
901     if (UiObjectDefinition *def = cast<UiObjectDefinition *>(m))
902         return def->qualifiedTypeNameId;
903     else if (UiObjectBinding *binding = cast<UiObjectBinding *>(m))
904         return binding->qualifiedTypeNameId;
905     return 0;
906 }
907
908 void QmlJSTextEditorWidget::updateCursorPositionNow()
909 {
910     if (m_contextPane && document() && semanticInfo().isValid()
911             && document()->revision() == semanticInfo().document->editorRevision())
912     {
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) {
923                     if (! q->next) {
924                         const int end = q->identifierToken.end();
925                         if (position() >= start && position() <= end) {
926                             TextEditor::RefactorMarker marker;
927                             QTextCursor tc(document());
928                             tc.setPosition(end);
929                             marker.cursor = tc;
930                             marker.tooltip = tr("Show Qt Quick ToolBar");
931                             markers.append(marker);
932                         } else {
933                              QList<TextEditor::RefactorMarker> markers;
934                              setRefactorMarkers(markers);
935                         }
936                     }
937                 }
938             }
939             setRefactorMarkers(markers);
940         } else if (oldNode != newNode) {
941             QList<TextEditor::RefactorMarker> markers;
942             setRefactorMarkers(markers);
943         }
944         m_oldCursorPosition = position();
945
946         setSelectedElements();
947     }
948 }
949
950 void QmlJSTextEditorWidget::showTextMarker()
951 {
952     m_oldCursorPosition = -1;
953     updateCursorPositionNow();
954 }
955
956 void QmlJSTextEditorWidget::updateUses()
957 {
958     m_updateUsesTimer->start();
959 }
960
961 bool QmlJSTextEditorWidget::updateSelectedElements() const
962 {
963     return m_updateSelectedElements;
964 }
965
966 void QmlJSTextEditorWidget::setUpdateSelectedElements(bool value)
967 {
968     m_updateSelectedElements = value;
969 }
970
971 void QmlJSTextEditorWidget::renameId(const QString &oldId, const QString &newId)
972 {
973     Utils::ChangeSet changeSet;
974
975     foreach (const AST::SourceLocation &loc, m_semanticInfo.idLocations.value(oldId)) {
976         changeSet.replace(loc.begin(), loc.end(), newId);
977     }
978
979     QTextCursor tc = textCursor();
980     changeSet.apply(&tc);
981 }
982
983 void QmlJSTextEditorWidget::updateUsesNow()
984 {
985     if (document()->revision() != m_semanticInfo.revision()) {
986         updateUses();
987         return;
988     }
989
990     m_updateUsesTimer->stop();
991
992     QList<QTextEdit::ExtraSelection> selections;
993     foreach (const AST::SourceLocation &loc, m_semanticInfo.idLocations.value(wordUnderCursor())) {
994         if (! loc.isValid())
995             continue;
996
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);
1003     }
1004
1005     setExtraSelections(CodeSemanticsSelection, selections);
1006 }
1007
1008 class SelectedElement: protected Visitor
1009 {
1010     unsigned m_cursorPositionStart;
1011     unsigned m_cursorPositionEnd;
1012     QList<UiObjectMember *> m_selectedMembers;
1013     LookupContext::Ptr m_lookupContext;
1014
1015 public:
1016     SelectedElement()
1017         : m_cursorPositionStart(0), m_cursorPositionEnd(0) {}
1018
1019     QList<UiObjectMember *> operator()(LookupContext::Ptr lookupContext, unsigned startPosition, unsigned endPosition)
1020     {
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;
1027     }
1028
1029 protected:
1030
1031     bool isSelectable(UiObjectMember *member) const
1032     {
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;
1038
1039         if (id) {
1040             QString name = id->name->asString();
1041             if (!name.isEmpty() && name.at(0).isUpper()) {
1042                 return true;
1043             }
1044         }
1045
1046         return false;
1047     }
1048
1049     inline UiObjectInitializer *initializer(UiObjectMember *member) const
1050     {
1051         if (UiObjectDefinition *def = cast<UiObjectDefinition *>(member))
1052             return def->initializer;
1053         else if (UiObjectBinding *binding = cast<UiObjectBinding *>(member))
1054             return binding->initializer;
1055         return 0;
1056     }
1057
1058     inline bool isIdBinding(UiObjectMember *member) const
1059     {
1060         if (UiScriptBinding *script = cast<UiScriptBinding *>(member)) {
1061             if (! script->qualifiedId)
1062                 return false;
1063             else if (! script->qualifiedId->name)
1064                 return false;
1065             else if (script->qualifiedId->next)
1066                 return false;
1067
1068             const QString propertyName = script->qualifiedId->name->asString();
1069
1070             if (propertyName == QLatin1String("id"))
1071                 return true;
1072         }
1073
1074         return false;
1075     }
1076
1077     inline bool containsCursor(unsigned begin, unsigned end)
1078     {
1079         return m_cursorPositionStart >= begin && m_cursorPositionEnd <= end;
1080     }
1081
1082     inline bool intersectsCursor(unsigned begin, unsigned end)
1083     {
1084         return (m_cursorPositionEnd >= begin && m_cursorPositionStart <= end);
1085     }
1086
1087     inline bool isRangeSelected() const
1088     {
1089         return (m_cursorPositionStart != m_cursorPositionEnd);
1090     }
1091
1092     void postVisit(Node *ast)
1093     {
1094         if (!isRangeSelected() && !m_selectedMembers.isEmpty())
1095             return; // nothing to do, we already have the results.
1096
1097         if (UiObjectMember *member = ast->uiObjectMemberCast()) {
1098             unsigned begin = member->firstSourceLocation().begin();
1099             unsigned end = member->lastSourceLocation().end();
1100
1101             if ((isRangeSelected() && intersectsCursor(begin, end))
1102             || (!isRangeSelected() && containsCursor(begin, end)))
1103             {
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);
1108                 }
1109             }
1110         }
1111     }
1112 };
1113
1114 void QmlJSTextEditorWidget::setSelectedElements()
1115 {
1116     if (!m_updateSelectedElements)
1117         return;
1118
1119     QTextCursor tc = textCursor();
1120     QString wordAtCursor;
1121     QList<int> offsets;
1122
1123     unsigned startPos;
1124     unsigned endPos;
1125
1126     if (tc.hasSelection()) {
1127         startPos = tc.selectionStart();
1128         endPos = tc.selectionEnd();
1129     } else {
1130         tc.movePosition(QTextCursor::StartOfWord);
1131         tc.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor);
1132
1133         startPos = textCursor().position();
1134         endPos = textCursor().position();
1135     }
1136
1137     if (m_semanticInfo.isValid()) {
1138         SelectedElement selectedMembers;
1139         QList<UiObjectMember *> members = selectedMembers(m_semanticInfo.lookupContext(),
1140                                                           startPos, endPos);
1141         if (!members.isEmpty()) {
1142             foreach(UiObjectMember *m, members) {
1143                 offsets << m->firstSourceLocation().begin();
1144             }
1145         }
1146     }
1147     wordAtCursor = tc.selectedText();
1148
1149     emit selectedElementsChanged(offsets, wordAtCursor);
1150 }
1151
1152 void QmlJSTextEditorWidget::updateFileName()
1153 {
1154 }
1155
1156 void QmlJSTextEditorWidget::renameIdUnderCursor()
1157 {
1158     const QString id = wordUnderCursor();
1159     bool ok = false;
1160     const QString newId = QInputDialog::getText(Core::ICore::instance()->mainWindow(),
1161                                                 tr("Rename..."),
1162                                                 tr("New id:"),
1163                                                 QLineEdit::Normal,
1164                                                 id, &ok);
1165     if (ok) {
1166         renameId(id, newId);
1167     }
1168 }
1169
1170 void QmlJSTextEditorWidget::setFontSettings(const TextEditor::FontSettings &fs)
1171 {
1172     TextEditor::BaseTextEditorWidget::setFontSettings(fs);
1173     Highlighter *highlighter = qobject_cast<Highlighter*>(baseTextDocument()->syntaxHighlighter());
1174     if (!highlighter)
1175         return;
1176
1177     highlighter->setFormats(fs.toTextCharFormats(highlighterFormatCategories()));
1178     highlighter->rehighlight();
1179
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));
1187
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();
1191 }
1192
1193
1194 QString QmlJSTextEditorWidget::wordUnderCursor() const
1195 {
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();
1204     return word;
1205 }
1206
1207 bool QmlJSTextEditorWidget::isClosingBrace(const QList<Token> &tokens) const
1208 {
1209
1210     if (tokens.size() == 1) {
1211         const Token firstToken = tokens.first();
1212
1213         return firstToken.is(Token::RightBrace) || firstToken.is(Token::RightBracket);
1214     }
1215
1216     return false;
1217 }
1218
1219 TextEditor::BaseTextEditor *QmlJSTextEditorWidget::createEditor()
1220 {
1221     QmlJSEditorEditable *editable = new QmlJSEditorEditable(this);
1222     createToolBar(editable);
1223     return editable;
1224 }
1225
1226 void QmlJSTextEditorWidget::createToolBar(QmlJSEditorEditable *editor)
1227 {
1228     m_outlineCombo = new QComboBox;
1229     m_outlineCombo->setMinimumContentsLength(22);
1230     m_outlineCombo->setModel(m_outlineModel);
1231
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();
1238
1239     //m_outlineCombo->setSizeAdjustPolicy(QComboBox::AdjustToContents);
1240
1241     // Make the combo box prefer to expand
1242     QSizePolicy policy = m_outlineCombo->sizePolicy();
1243     policy.setHorizontalPolicy(QSizePolicy::Expanding);
1244     m_outlineCombo->setSizePolicy(policy);
1245
1246     connect(m_outlineCombo, SIGNAL(activated(int)), this, SLOT(jumpToOutlineElement(int)));
1247     connect(this, SIGNAL(cursorPositionChanged()), m_updateOutlineIndexTimer, SLOT(start()));
1248
1249     connect(file(), SIGNAL(changed()), this, SLOT(updateFileName()));
1250
1251     editor->insertExtraToolBarWidget(TextEditor::BaseTextEditor::Left, m_outlineCombo);
1252 }
1253
1254 TextEditor::BaseTextEditorWidget::Link QmlJSTextEditorWidget::findLinkAt(const QTextCursor &cursor, bool /*resolveTarget*/)
1255 {
1256     const SemanticInfo semanticInfo = m_semanticInfo;
1257     if (! semanticInfo.isValid())
1258         return Link();
1259
1260     const unsigned cursorPosition = cursor.position();
1261
1262     AST::Node *node = semanticInfo.nodeUnderCursor(cursorPosition);
1263
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();
1271                 return link;
1272             }
1273         }
1274         return Link();
1275     }
1276
1277     LookupContext::Ptr lookupContext = semanticInfo.lookupContext(semanticInfo.astPath(cursorPosition));
1278     Evaluate evaluator(lookupContext->context());
1279     const Interpreter::Value *value = evaluator.reference(node);
1280
1281     QString fileName;
1282     int line = 0, column = 0;
1283
1284     if (! (value && value->getSourceLocation(&fileName, &line, &column)))
1285         return Link();
1286
1287     BaseTextEditorWidget::Link link;
1288     link.fileName = fileName;
1289     link.line = line;
1290     link.column = column - 1; // adjust the column
1291
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();
1297                 return link;
1298             }
1299         }
1300
1301     } else if (AST::IdentifierExpression *id = AST::cast<AST::IdentifierExpression *>(node)) {
1302         link.begin = id->firstSourceLocation().begin();
1303         link.end = id->lastSourceLocation().end();
1304         return link;
1305
1306     } else if (AST::FieldMemberExpression *mem = AST::cast<AST::FieldMemberExpression *>(node)) {
1307         link.begin = mem->lastSourceLocation().begin();
1308         link.end = mem->lastSourceLocation().end();
1309         return link;
1310     }
1311
1312     return Link();
1313 }
1314
1315 void QmlJSTextEditorWidget::followSymbolUnderCursor()
1316 {
1317     openLink(findLinkAt(textCursor()));
1318 }
1319
1320 void QmlJSTextEditorWidget::findUsages()
1321 {
1322     m_findReferences->findUsages(file()->fileName(), textCursor().position());
1323 }
1324
1325 void QmlJSTextEditorWidget::showContextPane()
1326 {
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);
1333     }
1334 }
1335
1336 void QmlJSTextEditorWidget::performQuickFix(int index)
1337 {
1338     TextEditor::QuickFixOperation::Ptr op = m_quickFixes.at(index);
1339     op->perform();
1340 }
1341
1342 void QmlJSTextEditorWidget::contextMenuEvent(QContextMenuEvent *e)
1343 {
1344     QMenu *menu = new QMenu();
1345
1346     QMenu *refactoringMenu = new QMenu(tr("Refactoring"), menu);
1347
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()));
1354     }
1355
1356     // Add other refactoring actions:
1357     QmlJSQuickFixCollector *quickFixCollector = QmlJSEditorPlugin::instance()->quickFixCollector();
1358     QSignalMapper mapper;
1359     connect(&mapper, SIGNAL(mapped(int)), this, SLOT(performQuickFix(int)));
1360
1361     if (! isOutdated()) {
1362         if (quickFixCollector->startCompletion(editor()) != -1) {
1363             m_quickFixes = quickFixCollector->quickFixes();
1364
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()));
1370             }
1371         }
1372     }
1373
1374     refactoringMenu->setEnabled(!refactoringMenu->isEmpty());
1375
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);
1382         }
1383     }
1384
1385     appendStandardContextMenuActions(menu);
1386
1387     menu->exec(e->globalPos());
1388     menu->deleteLater();
1389     quickFixCollector->cleanup();
1390     m_quickFixes.clear();
1391 }
1392
1393 bool QmlJSTextEditorWidget::event(QEvent *e)
1394 {
1395     switch (e->type()) {
1396     case QEvent::ShortcutOverride:
1397         if (static_cast<QKeyEvent*>(e)->key() == Qt::Key_Escape && m_contextPane) {
1398             if (hideContextPane()) {
1399                 e->accept();
1400                 return true;
1401             }
1402         }
1403         break;
1404     default:
1405         break;
1406     }
1407
1408     return BaseTextEditorWidget::event(e);
1409 }
1410
1411
1412 void QmlJSTextEditorWidget::wheelEvent(QWheelEvent *event)
1413 {
1414     bool visible = false;
1415     if (m_contextPane && m_contextPane->widget()->isVisible())
1416         visible = true;
1417
1418     BaseTextEditorWidget::wheelEvent(event);
1419
1420     if (visible) {
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);
1425     }
1426 }
1427
1428 void QmlJSTextEditorWidget::resizeEvent(QResizeEvent *event)
1429 {
1430     BaseTextEditorWidget::resizeEvent(event);
1431     hideContextPane();
1432 }
1433
1434  void QmlJSTextEditorWidget::scrollContentsBy(int dx, int dy)
1435  {
1436      BaseTextEditorWidget::scrollContentsBy(dx, dy);
1437      hideContextPane();
1438  }
1439
1440 void QmlJSTextEditorWidget::unCommentSelection()
1441 {
1442     Utils::unCommentSelection(this);
1443 }
1444
1445 void QmlJSTextEditorWidget::forceSemanticRehighlight()
1446 {
1447     m_semanticHighlighter->rehighlight(currentSource(/* force = */ true));
1448 }
1449
1450 void QmlJSEditor::QmlJSTextEditorWidget::forceSemanticRehighlightIfCurrentEditor()
1451 {
1452     Core::EditorManager *editorManager = Core::EditorManager::instance();
1453     if (editorManager->currentEditor() == editor())
1454         forceSemanticRehighlight();
1455 }
1456
1457 void QmlJSTextEditorWidget::semanticRehighlight()
1458 {
1459     m_semanticHighlighter->rehighlight(currentSource());
1460 }
1461
1462 void QmlJSTextEditorWidget::updateSemanticInfo(const SemanticInfo &semanticInfo)
1463 {
1464     if (semanticInfo.revision() != document()->revision()) {
1465         // got outdated semantic info
1466         semanticRehighlight();
1467         return;
1468     }
1469
1470     m_semanticInfo = semanticInfo;
1471     Document::Ptr doc = semanticInfo.document;
1472
1473     // create the ranges
1474     CreateRanges createRanges;
1475     m_semanticInfo.ranges = createRanges(document(), doc);
1476
1477     // Refresh the ids
1478     FindIdDeclarations updateIds;
1479     m_semanticInfo.idLocations = updateIds(doc);
1480
1481     FindDeclarations findDeclarations;
1482     m_semanticInfo.declarations = findDeclarations(doc->ast());
1483
1484     if (m_contextPane) {
1485         Node *newNode = m_semanticInfo.declaringMemberNoProperties(position());
1486         if (newNode) {
1487             m_contextPane->apply(editor(), semanticInfo.document, LookupContext::Ptr(), newNode, true);
1488             m_cursorPositionTimer->start(); //update text marker
1489         }
1490     }
1491
1492     // update outline
1493     m_updateOutlineTimer->start();
1494
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);
1500 }
1501
1502 void QmlJSTextEditorWidget::onRefactorMarkerClicked(const TextEditor::RefactorMarker &)
1503 {
1504     showContextPane();
1505 }
1506
1507 void QmlJSTextEditorWidget::onCursorPositionChanged()
1508 {
1509     m_cursorPositionTimer->start();
1510 }
1511
1512 QModelIndex QmlJSTextEditorWidget::indexForPosition(unsigned cursorPosition, const QModelIndex &rootIndex) const
1513 {
1514     QModelIndex lastIndex = rootIndex;
1515
1516
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);
1521
1522         if ((cursorPosition >= location.offset)
1523               && (cursorPosition <= location.offset + location.length)) {
1524             lastIndex = childIndex;
1525             break;
1526         }
1527     }
1528
1529     if (lastIndex != rootIndex) {
1530         // recurse
1531         lastIndex = indexForPosition(cursorPosition, lastIndex);
1532     }
1533     return lastIndex;
1534 }
1535
1536 bool QmlJSTextEditorWidget::hideContextPane()
1537 {
1538     bool b = (m_contextPane) && m_contextPane->widget()->isVisible();
1539     if (b) {
1540         m_contextPane->apply(editor(), semanticInfo().document, LookupContext::Ptr(), 0, false);
1541     }
1542     return b;
1543 }
1544
1545 QVector<QString> QmlJSTextEditorWidget::highlighterFormatCategories()
1546 {
1547     /*
1548         NumberFormat,
1549         StringFormat,
1550         TypeFormat,
1551         KeywordFormat,
1552         LabelFormat,
1553         CommentFormat,
1554         VisualWhitespace,
1555      */
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);
1565     }
1566     return categories;
1567 }
1568
1569 SemanticHighlighterSource QmlJSTextEditorWidget::currentSource(bool force)
1570 {
1571     int line = 0, column = 0;
1572     convertPosition(position(), &line, &column);
1573
1574     const Snapshot snapshot = m_modelManager->snapshot();
1575     const QString fileName = file()->fileName();
1576
1577     QString code;
1578     if (force || m_semanticInfo.revision() != document()->revision())
1579         code = toPlainText(); // get the source code only when needed.
1580
1581     const unsigned revision = document()->revision();
1582     SemanticHighlighterSource source(snapshot, fileName, code,
1583                                        line, column, revision);
1584     source.force = force;
1585     return source;
1586 }