2009-04-22 15:21:04 +02:00
|
|
|
/**************************************************************************
|
|
|
|
**
|
|
|
|
** This file is part of Qt Creator
|
|
|
|
**
|
|
|
|
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
|
|
**
|
2009-06-17 00:01:27 +10:00
|
|
|
** Contact: Nokia Corporation (qt-info@nokia.com)
|
2009-04-22 15:21:04 +02:00
|
|
|
**
|
|
|
|
** Commercial Usage
|
|
|
|
**
|
|
|
|
** Licensees holding valid Qt Commercial licenses may use this file in
|
|
|
|
** accordance with the Qt Commercial License Agreement provided with the
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
|
|
|
** a written agreement between you and Nokia.
|
|
|
|
**
|
|
|
|
** GNU Lesser General Public License Usage
|
|
|
|
**
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU Lesser
|
|
|
|
** General Public License version 2.1 as published by the Free Software
|
|
|
|
** Foundation and appearing in the file LICENSE.LGPL included in the
|
|
|
|
** packaging of this file. Please review the following information to
|
|
|
|
** ensure the GNU Lesser General Public License version 2.1 requirements
|
|
|
|
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|
|
|
**
|
|
|
|
** If you are unsure which license is appropriate for your use, please
|
2009-08-14 09:30:56 +02:00
|
|
|
** contact the sales department at http://qt.nokia.com/contact.
|
2009-04-22 15:21:04 +02:00
|
|
|
**
|
|
|
|
**************************************************************************/
|
|
|
|
|
2010-01-15 17:20:03 +01:00
|
|
|
#include "qmljseditor.h"
|
|
|
|
#include "qmljseditorconstants.h"
|
2009-09-30 17:43:21 +02:00
|
|
|
#include "qmlhighlighter.h"
|
2010-01-15 17:20:03 +01:00
|
|
|
#include "qmljseditorplugin.h"
|
2009-09-30 17:43:21 +02:00
|
|
|
#include "qmlmodelmanager.h"
|
2009-04-22 15:21:04 +02:00
|
|
|
|
2009-09-22 17:25:18 +02:00
|
|
|
#include "qmlexpressionundercursor.h"
|
|
|
|
#include "qmllookupcontext.h"
|
2009-09-23 12:21:43 +02:00
|
|
|
#include "qmlresolveexpression.h"
|
2009-09-11 14:42:50 +02:00
|
|
|
|
2010-01-12 18:01:39 +01:00
|
|
|
#include <qscripthighlighter/qscriptindenter.h>
|
|
|
|
|
2010-01-15 13:39:54 +01:00
|
|
|
#include <qmljs/qmltypesystem.h>
|
|
|
|
#include <qmljs/parser/qmljsastvisitor_p.h>
|
|
|
|
#include <qmljs/parser/qmljsast_p.h>
|
|
|
|
#include <qmljs/parser/qmljsengine_p.h>
|
|
|
|
#include <qmljs/qmldocument.h>
|
|
|
|
#include <qmljs/qmlidcollector.h>
|
2009-09-11 14:42:50 +02:00
|
|
|
|
2009-04-22 15:21:04 +02:00
|
|
|
#include <coreplugin/icore.h>
|
|
|
|
#include <coreplugin/actionmanager/actionmanager.h>
|
2009-10-01 13:34:02 +02:00
|
|
|
#include <coreplugin/uniqueidmanager.h>
|
2009-09-04 16:51:11 +02:00
|
|
|
#include <extensionsystem/pluginmanager.h>
|
2009-04-22 15:21:04 +02:00
|
|
|
#include <texteditor/basetextdocument.h>
|
|
|
|
#include <texteditor/fontsettings.h>
|
|
|
|
#include <texteditor/textblockiterator.h>
|
|
|
|
#include <texteditor/texteditorconstants.h>
|
|
|
|
#include <texteditor/texteditorsettings.h>
|
|
|
|
|
2009-12-02 17:59:27 +01:00
|
|
|
#include <utils/changeset.h>
|
2009-05-14 16:37:17 +02:00
|
|
|
#include <utils/uncommentselection.h>
|
|
|
|
|
2009-04-22 15:21:04 +02:00
|
|
|
#include <QtCore/QTimer>
|
|
|
|
|
|
|
|
#include <QtGui/QMenu>
|
|
|
|
#include <QtGui/QComboBox>
|
2009-05-05 15:33:39 +02:00
|
|
|
#include <QtGui/QInputDialog>
|
2009-05-05 16:18:36 +02:00
|
|
|
#include <QtGui/QMainWindow>
|
2009-07-16 17:34:04 +02:00
|
|
|
#include <QtGui/QToolBar>
|
2009-04-22 15:21:04 +02:00
|
|
|
|
|
|
|
enum {
|
2010-01-14 16:30:48 +01:00
|
|
|
UPDATE_DOCUMENT_DEFAULT_INTERVAL = 250,
|
|
|
|
UPDATE_USES_DEFAULT_INTERVAL = 150
|
2009-04-22 15:21:04 +02:00
|
|
|
};
|
|
|
|
|
2009-12-07 16:57:31 +01:00
|
|
|
using namespace Qml;
|
2009-06-11 11:22:26 +02:00
|
|
|
using namespace QmlJS;
|
|
|
|
using namespace QmlJS::AST;
|
2009-11-16 12:38:47 +01:00
|
|
|
using namespace SharedTools;
|
2009-04-22 15:21:04 +02:00
|
|
|
|
2010-01-12 18:01:39 +01:00
|
|
|
namespace {
|
|
|
|
int blockBraceDepth(const QTextBlock &block)
|
|
|
|
{
|
|
|
|
int state = block.userState();
|
|
|
|
if (state == -1)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return (state >> 8) & 0xFF;
|
|
|
|
}
|
|
|
|
|
|
|
|
int blockStartState(const QTextBlock &block)
|
|
|
|
{
|
|
|
|
int state = block.userState();
|
|
|
|
|
|
|
|
if (state == -1)
|
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return state & 0xff;
|
|
|
|
}
|
|
|
|
} // end of anonymous namespace
|
|
|
|
|
2010-01-15 17:20:03 +01:00
|
|
|
namespace QmlJSEditor {
|
2009-04-22 15:21:04 +02:00
|
|
|
namespace Internal {
|
|
|
|
|
2009-05-06 17:26:29 +02:00
|
|
|
class FindIdDeclarations: protected Visitor
|
2009-05-04 18:58:36 +02:00
|
|
|
{
|
|
|
|
public:
|
2009-09-16 13:56:06 +02:00
|
|
|
typedef QMap<QString, QList<AST::SourceLocation> > Result;
|
|
|
|
|
|
|
|
Result operator()(AST::Node *node)
|
|
|
|
{
|
|
|
|
_ids.clear();
|
|
|
|
_maybeIds.clear();
|
|
|
|
accept(node);
|
|
|
|
return _ids;
|
|
|
|
}
|
2009-05-04 18:58:36 +02:00
|
|
|
|
|
|
|
protected:
|
2009-09-16 13:56:06 +02:00
|
|
|
QString asString(AST::UiQualifiedId *id)
|
|
|
|
{
|
|
|
|
QString text;
|
|
|
|
for (; id; id = id->next) {
|
|
|
|
if (id->name)
|
|
|
|
text += id->name->asString();
|
|
|
|
else
|
|
|
|
text += QLatin1Char('?');
|
|
|
|
|
|
|
|
if (id->next)
|
|
|
|
text += QLatin1Char('.');
|
|
|
|
}
|
|
|
|
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
|
|
|
void accept(AST::Node *node)
|
|
|
|
{ AST::Node::acceptChild(node, this); }
|
|
|
|
|
|
|
|
using Visitor::visit;
|
|
|
|
using Visitor::endVisit;
|
|
|
|
|
|
|
|
virtual bool visit(AST::UiScriptBinding *node)
|
|
|
|
{
|
|
|
|
if (asString(node->qualifiedId) == QLatin1String("id")) {
|
|
|
|
if (AST::ExpressionStatement *stmt = AST::cast<AST::ExpressionStatement*>(node->statement)) {
|
|
|
|
if (AST::IdentifierExpression *idExpr = AST::cast<AST::IdentifierExpression *>(stmt->expression)) {
|
|
|
|
if (idExpr->name) {
|
|
|
|
const QString id = idExpr->name->asString();
|
|
|
|
QList<AST::SourceLocation> *locs = &_ids[id];
|
|
|
|
locs->append(idExpr->firstSourceLocation());
|
|
|
|
locs->append(_maybeIds.value(id));
|
|
|
|
_maybeIds.remove(id);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
accept(node->statement);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool visit(AST::IdentifierExpression *node)
|
|
|
|
{
|
|
|
|
if (node->name) {
|
|
|
|
const QString name = node->name->asString();
|
|
|
|
|
|
|
|
if (_ids.contains(name))
|
|
|
|
_ids[name].append(node->identifierToken);
|
|
|
|
else
|
|
|
|
_maybeIds[name].append(node->identifierToken);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2009-05-04 18:58:36 +02:00
|
|
|
|
|
|
|
private:
|
2009-09-16 13:56:06 +02:00
|
|
|
Result _ids;
|
|
|
|
Result _maybeIds;
|
2009-05-04 18:58:36 +02:00
|
|
|
};
|
|
|
|
|
2009-04-22 15:21:04 +02:00
|
|
|
class FindDeclarations: protected Visitor
|
|
|
|
{
|
2009-09-16 13:56:06 +02:00
|
|
|
QList<Declaration> _declarations;
|
|
|
|
int _depth;
|
2009-04-22 15:21:04 +02:00
|
|
|
|
|
|
|
public:
|
2009-09-16 13:56:06 +02:00
|
|
|
QList<Declaration> operator()(AST::Node *node)
|
|
|
|
{
|
|
|
|
_depth = -1;
|
|
|
|
_declarations.clear();
|
|
|
|
accept(node);
|
|
|
|
return _declarations;
|
|
|
|
}
|
2009-04-22 15:21:04 +02:00
|
|
|
|
|
|
|
protected:
|
2009-09-16 13:56:06 +02:00
|
|
|
using Visitor::visit;
|
|
|
|
using Visitor::endVisit;
|
|
|
|
|
|
|
|
QString asString(AST::UiQualifiedId *id)
|
|
|
|
{
|
|
|
|
QString text;
|
|
|
|
for (; id; id = id->next) {
|
|
|
|
if (id->name)
|
|
|
|
text += id->name->asString();
|
|
|
|
else
|
|
|
|
text += QLatin1Char('?');
|
|
|
|
|
|
|
|
if (id->next)
|
|
|
|
text += QLatin1Char('.');
|
|
|
|
}
|
2009-04-22 15:21:04 +02:00
|
|
|
|
2009-09-16 13:56:06 +02:00
|
|
|
return text;
|
|
|
|
}
|
2009-04-30 17:23:32 +02:00
|
|
|
|
2009-09-16 13:56:06 +02:00
|
|
|
void accept(AST::Node *node)
|
|
|
|
{ AST::Node::acceptChild(node, this); }
|
|
|
|
|
|
|
|
void init(Declaration *decl, AST::UiObjectMember *member)
|
|
|
|
{
|
|
|
|
const SourceLocation first = member->firstSourceLocation();
|
|
|
|
const SourceLocation last = member->lastSourceLocation();
|
|
|
|
decl->startLine = first.startLine;
|
|
|
|
decl->startColumn = first.startColumn;
|
|
|
|
decl->endLine = last.startLine;
|
|
|
|
decl->endColumn = last.startColumn + last.length;
|
|
|
|
}
|
2009-04-30 17:23:32 +02:00
|
|
|
|
2009-09-16 13:56:06 +02:00
|
|
|
virtual bool visit(AST::UiObjectDefinition *node)
|
|
|
|
{
|
|
|
|
++_depth;
|
2009-04-30 17:23:32 +02:00
|
|
|
|
2009-09-16 13:56:06 +02:00
|
|
|
Declaration decl;
|
|
|
|
init(&decl, node);
|
2009-04-30 17:23:32 +02:00
|
|
|
|
2009-09-16 13:56:06 +02:00
|
|
|
decl.text.fill(QLatin1Char(' '), _depth);
|
|
|
|
if (node->qualifiedTypeNameId)
|
|
|
|
decl.text.append(asString(node->qualifiedTypeNameId));
|
|
|
|
else
|
|
|
|
decl.text.append(QLatin1Char('?'));
|
2009-04-30 17:23:32 +02:00
|
|
|
|
2009-09-16 13:56:06 +02:00
|
|
|
_declarations.append(decl);
|
2009-04-30 17:23:32 +02:00
|
|
|
|
2009-09-16 13:56:06 +02:00
|
|
|
return true; // search for more bindings
|
|
|
|
}
|
2009-04-30 17:23:32 +02:00
|
|
|
|
2009-09-16 13:56:06 +02:00
|
|
|
virtual void endVisit(AST::UiObjectDefinition *)
|
|
|
|
{
|
|
|
|
--_depth;
|
|
|
|
}
|
2009-04-30 17:23:32 +02:00
|
|
|
|
2009-09-16 13:56:06 +02:00
|
|
|
virtual bool visit(AST::UiObjectBinding *node)
|
|
|
|
{
|
|
|
|
++_depth;
|
2009-04-30 17:23:32 +02:00
|
|
|
|
2009-09-16 13:56:06 +02:00
|
|
|
Declaration decl;
|
|
|
|
init(&decl, node);
|
2009-04-30 17:23:32 +02:00
|
|
|
|
2009-09-16 13:56:06 +02:00
|
|
|
decl.text.fill(QLatin1Char(' '), _depth);
|
2009-04-30 17:23:32 +02:00
|
|
|
|
2009-09-16 13:56:06 +02:00
|
|
|
decl.text.append(asString(node->qualifiedId));
|
|
|
|
decl.text.append(QLatin1String(": "));
|
2009-04-30 17:23:32 +02:00
|
|
|
|
2009-09-16 13:56:06 +02:00
|
|
|
if (node->qualifiedTypeNameId)
|
|
|
|
decl.text.append(asString(node->qualifiedTypeNameId));
|
|
|
|
else
|
|
|
|
decl.text.append(QLatin1Char('?'));
|
2009-04-30 17:23:32 +02:00
|
|
|
|
2009-09-16 13:56:06 +02:00
|
|
|
_declarations.append(decl);
|
2009-04-30 17:23:32 +02:00
|
|
|
|
2009-09-16 13:56:06 +02:00
|
|
|
return true; // search for more bindings
|
|
|
|
}
|
2009-04-30 17:23:32 +02:00
|
|
|
|
2009-09-16 13:56:06 +02:00
|
|
|
virtual void endVisit(AST::UiObjectBinding *)
|
|
|
|
{
|
|
|
|
--_depth;
|
|
|
|
}
|
2009-04-30 17:23:32 +02:00
|
|
|
|
2009-05-11 11:53:24 +02:00
|
|
|
#if 0 // ### ignore script bindings for now.
|
2009-09-16 13:56:06 +02:00
|
|
|
virtual bool visit(AST::UiScriptBinding *node)
|
|
|
|
{
|
|
|
|
++_depth;
|
2009-04-30 17:23:32 +02:00
|
|
|
|
2009-09-16 13:56:06 +02:00
|
|
|
Declaration decl;
|
|
|
|
init(&decl, node);
|
2009-04-30 17:23:32 +02:00
|
|
|
|
2009-09-16 13:56:06 +02:00
|
|
|
decl.text.fill(QLatin1Char(' '), _depth);
|
|
|
|
decl.text.append(asString(node->qualifiedId));
|
2009-04-30 17:23:32 +02:00
|
|
|
|
2009-09-16 13:56:06 +02:00
|
|
|
_declarations.append(decl);
|
2009-04-30 17:23:32 +02:00
|
|
|
|
2009-09-16 13:56:06 +02:00
|
|
|
return false; // more more bindings in this subtree.
|
|
|
|
}
|
2009-04-30 17:23:32 +02:00
|
|
|
|
2009-09-16 13:56:06 +02:00
|
|
|
virtual void endVisit(AST::UiScriptBinding *)
|
|
|
|
{
|
|
|
|
--_depth;
|
|
|
|
}
|
2009-05-11 11:53:24 +02:00
|
|
|
#endif
|
2009-04-22 15:21:04 +02:00
|
|
|
};
|
|
|
|
|
2010-01-15 17:20:03 +01:00
|
|
|
QmlJSEditorEditable::QmlJSEditorEditable(QmlJSTextEditor *editor)
|
2009-10-01 13:34:02 +02:00
|
|
|
: BaseTextEditorEditable(editor)
|
2009-04-22 15:21:04 +02:00
|
|
|
{
|
2009-10-01 13:34:02 +02:00
|
|
|
|
|
|
|
Core::UniqueIDManager *uidm = Core::UniqueIDManager::instance();
|
2010-01-15 17:20:03 +01:00
|
|
|
m_context << uidm->uniqueIdentifier(QmlJSEditor::Constants::C_QMLJSEDITOR_ID);
|
2009-10-01 13:34:02 +02:00
|
|
|
m_context << uidm->uniqueIdentifier(TextEditor::Constants::C_TEXTEDITOR);
|
2009-04-22 15:21:04 +02:00
|
|
|
}
|
|
|
|
|
2010-01-15 17:20:03 +01:00
|
|
|
QmlJSTextEditor::QmlJSTextEditor(QWidget *parent) :
|
2009-09-16 13:56:06 +02:00
|
|
|
TextEditor::BaseTextEditor(parent),
|
2009-09-04 16:51:11 +02:00
|
|
|
m_methodCombo(0),
|
2009-12-07 16:57:31 +01:00
|
|
|
m_modelManager(0),
|
|
|
|
m_typeSystem(0)
|
2009-04-22 15:21:04 +02:00
|
|
|
{
|
2010-01-14 16:30:48 +01:00
|
|
|
m_idsRevision = -1;
|
2009-09-16 13:56:06 +02:00
|
|
|
setParenthesesMatchingEnabled(true);
|
|
|
|
setMarksVisible(true);
|
|
|
|
setCodeFoldingSupported(true);
|
|
|
|
setCodeFoldingVisible(true);
|
2010-01-15 17:20:03 +01:00
|
|
|
setMimeType(QmlJSEditor::Constants::QMLJSEDITOR_MIMETYPE);
|
2009-04-22 15:21:04 +02:00
|
|
|
|
2009-09-16 13:56:06 +02:00
|
|
|
m_updateDocumentTimer = new QTimer(this);
|
|
|
|
m_updateDocumentTimer->setInterval(UPDATE_DOCUMENT_DEFAULT_INTERVAL);
|
|
|
|
m_updateDocumentTimer->setSingleShot(true);
|
|
|
|
connect(m_updateDocumentTimer, SIGNAL(timeout()), this, SLOT(updateDocumentNow()));
|
2009-04-22 15:21:04 +02:00
|
|
|
|
2010-01-14 16:30:48 +01:00
|
|
|
m_updateUsesTimer = new QTimer(this);
|
|
|
|
m_updateUsesTimer->setInterval(UPDATE_USES_DEFAULT_INTERVAL);
|
|
|
|
m_updateUsesTimer->setSingleShot(true);
|
|
|
|
connect(m_updateUsesTimer, SIGNAL(timeout()), this, SLOT(updateUsesNow()));
|
|
|
|
|
2009-09-16 13:56:06 +02:00
|
|
|
connect(this, SIGNAL(textChanged()), this, SLOT(updateDocument()));
|
2010-01-14 16:30:48 +01:00
|
|
|
connect(this, SIGNAL(textChanged()), this, SLOT(updateUses()));
|
2009-04-22 15:21:04 +02:00
|
|
|
|
2009-09-30 17:43:21 +02:00
|
|
|
baseTextDocument()->setSyntaxHighlighter(new QmlHighlighter);
|
2009-09-04 16:51:11 +02:00
|
|
|
|
2009-09-30 17:43:21 +02:00
|
|
|
m_modelManager = ExtensionSystem::PluginManager::instance()->getObject<QmlModelManagerInterface>();
|
2009-12-08 11:52:28 +01:00
|
|
|
m_typeSystem = ExtensionSystem::PluginManager::instance()->getObject<Qml::QmlTypeSystem>();
|
2009-09-04 16:51:11 +02:00
|
|
|
|
|
|
|
if (m_modelManager) {
|
2010-01-14 16:22:43 +01:00
|
|
|
connect(m_modelManager, SIGNAL(documentUpdated(Qml::QmlDocument::Ptr)),
|
|
|
|
this, SLOT(onDocumentUpdated(Qml::QmlDocument::Ptr)));
|
2009-09-04 16:51:11 +02:00
|
|
|
}
|
2009-04-22 15:21:04 +02:00
|
|
|
}
|
|
|
|
|
2010-01-15 17:20:03 +01:00
|
|
|
QmlJSTextEditor::~QmlJSTextEditor()
|
2009-04-22 15:21:04 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-01-15 17:20:03 +01:00
|
|
|
QList<Declaration> QmlJSTextEditor::declarations() const
|
2009-04-22 15:21:04 +02:00
|
|
|
{ return m_declarations; }
|
|
|
|
|
2010-01-15 17:20:03 +01:00
|
|
|
Core::IEditor *QmlJSEditorEditable::duplicate(QWidget *parent)
|
2009-04-22 15:21:04 +02:00
|
|
|
{
|
2010-01-15 17:20:03 +01:00
|
|
|
QmlJSTextEditor *newEditor = new QmlJSTextEditor(parent);
|
2009-09-16 13:56:06 +02:00
|
|
|
newEditor->duplicateFrom(editor());
|
2010-01-15 17:20:03 +01:00
|
|
|
QmlJSEditorPlugin::instance()->initializeEditor(newEditor);
|
2009-09-16 13:56:06 +02:00
|
|
|
return newEditor->editableInterface();
|
2009-04-22 15:21:04 +02:00
|
|
|
}
|
|
|
|
|
2010-01-15 17:20:03 +01:00
|
|
|
QString QmlJSEditorEditable::id() const
|
2009-04-22 15:21:04 +02:00
|
|
|
{
|
2010-01-15 17:20:03 +01:00
|
|
|
return QLatin1String(QmlJSEditor::Constants::C_QMLJSEDITOR_ID);
|
2009-04-22 15:21:04 +02:00
|
|
|
}
|
|
|
|
|
2010-01-15 17:20:03 +01:00
|
|
|
QmlJSTextEditor::Context QmlJSEditorEditable::context() const
|
2009-04-22 15:21:04 +02:00
|
|
|
{
|
2009-09-16 13:56:06 +02:00
|
|
|
return m_context;
|
2009-04-22 15:21:04 +02:00
|
|
|
}
|
|
|
|
|
2010-01-15 17:20:03 +01:00
|
|
|
void QmlJSTextEditor::updateDocument()
|
2009-04-22 15:21:04 +02:00
|
|
|
{
|
2009-09-16 13:56:06 +02:00
|
|
|
m_updateDocumentTimer->start(UPDATE_DOCUMENT_DEFAULT_INTERVAL);
|
2009-04-22 15:21:04 +02:00
|
|
|
}
|
|
|
|
|
2010-01-15 17:20:03 +01:00
|
|
|
void QmlJSTextEditor::updateDocumentNow()
|
2009-04-22 15:21:04 +02:00
|
|
|
{
|
2009-09-16 13:56:06 +02:00
|
|
|
// ### move in the parser thread.
|
2009-04-22 15:21:04 +02:00
|
|
|
|
2009-09-16 13:56:06 +02:00
|
|
|
m_updateDocumentTimer->stop();
|
2009-04-22 15:21:04 +02:00
|
|
|
|
2009-09-16 13:56:06 +02:00
|
|
|
const QString fileName = file()->fileName();
|
2009-04-22 15:21:04 +02:00
|
|
|
|
2009-09-04 16:51:11 +02:00
|
|
|
m_modelManager->updateSourceFiles(QStringList() << fileName);
|
|
|
|
}
|
|
|
|
|
2010-01-15 17:20:03 +01:00
|
|
|
void QmlJSTextEditor::onDocumentUpdated(Qml::QmlDocument::Ptr doc)
|
2009-09-04 16:51:11 +02:00
|
|
|
{
|
|
|
|
if (file()->fileName() != doc->fileName())
|
|
|
|
return;
|
|
|
|
|
|
|
|
m_document = doc;
|
2009-04-30 16:57:27 +02:00
|
|
|
|
2009-09-16 13:56:06 +02:00
|
|
|
FindIdDeclarations updateIds;
|
2010-01-14 16:30:48 +01:00
|
|
|
m_idsRevision = document()->revision();
|
2009-09-16 13:56:06 +02:00
|
|
|
m_ids = updateIds(doc->program());
|
2009-05-04 18:58:36 +02:00
|
|
|
|
2009-09-04 16:51:11 +02:00
|
|
|
if (doc->isParsedCorrectly()) {
|
2009-09-16 13:56:06 +02:00
|
|
|
FindDeclarations findDeclarations;
|
|
|
|
m_declarations = findDeclarations(doc->program());
|
2009-04-30 17:23:32 +02:00
|
|
|
|
2009-09-16 13:56:06 +02:00
|
|
|
QStringList items;
|
|
|
|
items.append(tr("<Select Symbol>"));
|
2009-04-22 15:21:04 +02:00
|
|
|
|
2009-09-16 13:56:06 +02:00
|
|
|
foreach (Declaration decl, m_declarations)
|
|
|
|
items.append(decl.text);
|
2009-04-22 15:21:04 +02:00
|
|
|
|
2009-09-16 13:56:06 +02:00
|
|
|
m_methodCombo->clear();
|
|
|
|
m_methodCombo->addItems(items);
|
|
|
|
updateMethodBoxIndex();
|
2009-09-04 16:51:11 +02:00
|
|
|
}
|
2009-04-22 15:21:04 +02:00
|
|
|
|
2009-09-16 13:56:06 +02:00
|
|
|
QList<QTextEdit::ExtraSelection> selections;
|
2009-04-22 15:21:04 +02:00
|
|
|
|
2009-09-16 13:56:06 +02:00
|
|
|
QTextCharFormat errorFormat;
|
|
|
|
errorFormat.setUnderlineColor(Qt::red);
|
|
|
|
errorFormat.setUnderlineStyle(QTextCharFormat::WaveUnderline);
|
2009-04-22 15:21:04 +02:00
|
|
|
|
2009-09-16 13:56:06 +02:00
|
|
|
QTextEdit::ExtraSelection sel;
|
2009-04-22 15:21:04 +02:00
|
|
|
|
2009-09-16 13:56:06 +02:00
|
|
|
m_diagnosticMessages = doc->diagnosticMessages();
|
2009-04-22 15:21:04 +02:00
|
|
|
|
2009-09-16 13:56:06 +02:00
|
|
|
foreach (const DiagnosticMessage &d, m_diagnosticMessages) {
|
|
|
|
int line = d.loc.startLine;
|
|
|
|
int column = d.loc.startColumn;
|
2009-04-22 15:21:04 +02:00
|
|
|
|
2009-09-16 13:56:06 +02:00
|
|
|
if (column == 0)
|
|
|
|
column = 1;
|
2009-04-22 15:21:04 +02:00
|
|
|
|
2009-09-16 13:56:06 +02:00
|
|
|
QTextCursor c(document()->findBlockByNumber(line - 1));
|
|
|
|
sel.cursor = c;
|
2009-05-06 15:45:19 +02:00
|
|
|
|
2009-09-16 13:56:06 +02:00
|
|
|
sel.cursor.setPosition(c.position() + column - 1);
|
|
|
|
if (sel.cursor.atBlockEnd())
|
|
|
|
sel.cursor.movePosition(QTextCursor::StartOfWord, QTextCursor::KeepAnchor);
|
|
|
|
else
|
|
|
|
sel.cursor.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor);
|
2009-05-06 15:45:19 +02:00
|
|
|
|
2009-09-16 13:56:06 +02:00
|
|
|
sel.format = errorFormat;
|
2009-04-22 15:21:04 +02:00
|
|
|
|
2009-09-16 13:56:06 +02:00
|
|
|
selections.append(sel);
|
|
|
|
}
|
2009-04-22 15:21:04 +02:00
|
|
|
|
2009-09-16 13:56:06 +02:00
|
|
|
setExtraSelections(CodeWarningsSelection, selections);
|
2009-04-22 15:21:04 +02:00
|
|
|
}
|
|
|
|
|
2010-01-15 17:20:03 +01:00
|
|
|
void QmlJSTextEditor::jumpToMethod(int index)
|
2009-04-22 15:21:04 +02:00
|
|
|
{
|
2009-09-16 13:56:06 +02:00
|
|
|
if (index) {
|
|
|
|
Declaration d = m_declarations.at(index - 1);
|
|
|
|
gotoLine(d.startLine, d.startColumn - 1);
|
|
|
|
setFocus();
|
|
|
|
}
|
2009-04-22 15:21:04 +02:00
|
|
|
}
|
|
|
|
|
2010-01-15 17:20:03 +01:00
|
|
|
void QmlJSTextEditor::updateMethodBoxIndex()
|
2009-04-22 15:21:04 +02:00
|
|
|
{
|
2009-09-16 13:56:06 +02:00
|
|
|
int line = 0, column = 0;
|
|
|
|
convertPosition(position(), &line, &column);
|
2009-04-22 15:21:04 +02:00
|
|
|
|
2009-09-16 13:56:06 +02:00
|
|
|
int currentSymbolIndex = 0;
|
2009-04-22 15:21:04 +02:00
|
|
|
|
2009-09-16 13:56:06 +02:00
|
|
|
int index = 0;
|
|
|
|
while (index < m_declarations.size()) {
|
|
|
|
const Declaration &d = m_declarations.at(index++);
|
2009-04-22 15:21:04 +02:00
|
|
|
|
2009-09-16 13:56:06 +02:00
|
|
|
if (line < d.startLine)
|
|
|
|
break;
|
|
|
|
else
|
|
|
|
currentSymbolIndex = index;
|
|
|
|
}
|
2009-04-22 15:21:04 +02:00
|
|
|
|
2009-09-16 13:56:06 +02:00
|
|
|
m_methodCombo->setCurrentIndex(currentSymbolIndex);
|
2010-01-14 16:30:48 +01:00
|
|
|
updateUses();
|
|
|
|
}
|
|
|
|
|
2010-01-15 17:20:03 +01:00
|
|
|
void QmlJSTextEditor::updateUses()
|
2010-01-14 16:30:48 +01:00
|
|
|
{
|
|
|
|
m_updateUsesTimer->start();
|
|
|
|
}
|
|
|
|
|
2010-01-15 17:20:03 +01:00
|
|
|
void QmlJSTextEditor::updateUsesNow()
|
2010-01-14 16:30:48 +01:00
|
|
|
{
|
|
|
|
if (document()->revision() != m_idsRevision) {
|
|
|
|
updateUses();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_updateUsesTimer->stop();
|
2009-05-04 18:58:36 +02:00
|
|
|
|
2009-09-16 13:56:06 +02:00
|
|
|
QList<QTextEdit::ExtraSelection> selections;
|
|
|
|
foreach (const AST::SourceLocation &loc, m_ids.value(wordUnderCursor())) {
|
|
|
|
if (! loc.isValid())
|
|
|
|
continue;
|
2009-05-05 10:46:58 +02:00
|
|
|
|
2009-09-16 13:56:06 +02:00
|
|
|
QTextEdit::ExtraSelection sel;
|
2010-01-14 16:30:48 +01:00
|
|
|
sel.format = m_occurrencesFormat;
|
2009-09-16 13:56:06 +02:00
|
|
|
sel.cursor = textCursor();
|
|
|
|
sel.cursor.setPosition(loc.begin());
|
|
|
|
sel.cursor.setPosition(loc.end(), QTextCursor::KeepAnchor);
|
|
|
|
selections.append(sel);
|
|
|
|
}
|
2009-05-04 18:58:36 +02:00
|
|
|
|
2009-09-16 13:56:06 +02:00
|
|
|
setExtraSelections(CodeSemanticsSelection, selections);
|
2009-04-22 15:21:04 +02:00
|
|
|
}
|
|
|
|
|
2010-01-15 17:20:03 +01:00
|
|
|
void QmlJSTextEditor::updateMethodBoxToolTip()
|
2009-04-22 15:21:04 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-01-15 17:20:03 +01:00
|
|
|
void QmlJSTextEditor::updateFileName()
|
2009-04-22 15:21:04 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-01-15 17:20:03 +01:00
|
|
|
void QmlJSTextEditor::renameIdUnderCursor()
|
2009-05-05 15:33:39 +02:00
|
|
|
{
|
2009-09-16 13:56:06 +02:00
|
|
|
const QString id = wordUnderCursor();
|
|
|
|
bool ok = false;
|
|
|
|
const QString newId = QInputDialog::getText(Core::ICore::instance()->mainWindow(),
|
|
|
|
tr("Rename..."),
|
|
|
|
tr("New id:"),
|
|
|
|
QLineEdit::Normal,
|
|
|
|
id, &ok);
|
|
|
|
if (ok) {
|
2009-12-02 17:59:27 +01:00
|
|
|
Utils::ChangeSet changeSet;
|
2009-09-16 13:56:06 +02:00
|
|
|
|
|
|
|
foreach (const AST::SourceLocation &loc, m_ids.value(id)) {
|
2009-12-02 17:59:27 +01:00
|
|
|
changeSet.replace(loc.offset, loc.length, newId);
|
2009-09-16 13:56:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QTextCursor tc = textCursor();
|
2009-12-02 17:59:27 +01:00
|
|
|
changeSet.apply(&tc);
|
2009-09-16 13:56:06 +02:00
|
|
|
}
|
2009-05-05 15:33:39 +02:00
|
|
|
}
|
|
|
|
|
2010-01-15 17:20:03 +01:00
|
|
|
QStringList QmlJSTextEditor::keywords() const
|
2009-05-06 18:22:42 +02:00
|
|
|
{
|
2009-09-16 13:56:06 +02:00
|
|
|
QStringList words;
|
2009-05-06 18:22:42 +02:00
|
|
|
|
2009-09-30 17:43:21 +02:00
|
|
|
if (QmlHighlighter *highlighter = qobject_cast<QmlHighlighter*>(baseTextDocument()->syntaxHighlighter()))
|
2009-09-16 13:56:06 +02:00
|
|
|
words = highlighter->keywords().toList();
|
2009-05-06 18:22:42 +02:00
|
|
|
|
2009-09-16 13:56:06 +02:00
|
|
|
return words;
|
2009-05-06 18:22:42 +02:00
|
|
|
}
|
|
|
|
|
2010-01-15 17:20:03 +01:00
|
|
|
void QmlJSTextEditor::setFontSettings(const TextEditor::FontSettings &fs)
|
2009-04-22 15:21:04 +02:00
|
|
|
{
|
2009-09-16 13:56:06 +02:00
|
|
|
TextEditor::BaseTextEditor::setFontSettings(fs);
|
2009-09-30 17:43:21 +02:00
|
|
|
QmlHighlighter *highlighter = qobject_cast<QmlHighlighter*>(baseTextDocument()->syntaxHighlighter());
|
2009-09-16 13:56:06 +02:00
|
|
|
if (!highlighter)
|
|
|
|
return;
|
|
|
|
|
|
|
|
static QVector<QString> categories;
|
|
|
|
if (categories.isEmpty()) {
|
|
|
|
categories << QLatin1String(TextEditor::Constants::C_NUMBER)
|
|
|
|
<< QLatin1String(TextEditor::Constants::C_STRING)
|
|
|
|
<< QLatin1String(TextEditor::Constants::C_TYPE)
|
|
|
|
<< QLatin1String(TextEditor::Constants::C_KEYWORD)
|
|
|
|
<< QLatin1String(TextEditor::Constants::C_PREPROCESSOR)
|
|
|
|
<< QLatin1String(TextEditor::Constants::C_LABEL)
|
2009-10-01 13:34:02 +02:00
|
|
|
<< QLatin1String(TextEditor::Constants::C_COMMENT)
|
|
|
|
<< QLatin1String(TextEditor::Constants::C_VISUAL_WHITESPACE);
|
2009-09-16 13:56:06 +02:00
|
|
|
}
|
|
|
|
|
2010-01-14 16:30:48 +01:00
|
|
|
m_occurrencesFormat = fs.toTextCharFormat(QLatin1String(TextEditor::Constants::C_OCCURRENCES));
|
|
|
|
m_occurrencesFormat.clearForeground();
|
|
|
|
|
2009-09-16 13:56:06 +02:00
|
|
|
highlighter->setFormats(fs.toTextCharFormats(categories));
|
|
|
|
highlighter->rehighlight();
|
2009-04-22 15:21:04 +02:00
|
|
|
}
|
|
|
|
|
2010-01-15 17:20:03 +01:00
|
|
|
QString QmlJSTextEditor::wordUnderCursor() const
|
2009-05-05 15:33:39 +02:00
|
|
|
{
|
2009-09-16 13:56:06 +02:00
|
|
|
QTextCursor tc = textCursor();
|
|
|
|
tc.movePosition(QTextCursor::StartOfWord);
|
|
|
|
tc.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor);
|
|
|
|
const QString word= tc.selectedText();
|
|
|
|
return word;
|
2009-05-05 15:33:39 +02:00
|
|
|
}
|
|
|
|
|
2010-01-15 17:20:03 +01:00
|
|
|
bool QmlJSTextEditor::isElectricCharacter(const QChar &ch) const
|
2009-04-22 15:21:04 +02:00
|
|
|
{
|
2009-10-02 15:06:50 +02:00
|
|
|
if (ch == QLatin1Char('}')
|
|
|
|
|| ch == QLatin1Char(']'))
|
2009-09-16 13:56:06 +02:00
|
|
|
return true;
|
|
|
|
return false;
|
2009-04-22 15:21:04 +02:00
|
|
|
}
|
|
|
|
|
2010-01-15 17:20:03 +01:00
|
|
|
bool QmlJSTextEditor::isClosingBrace(const QList<QScriptIncrementalScanner::Token> &tokens) const
|
2009-04-22 15:21:04 +02:00
|
|
|
{
|
2009-09-16 13:56:06 +02:00
|
|
|
|
2009-11-16 12:38:47 +01:00
|
|
|
if (tokens.size() == 1) {
|
|
|
|
const QScriptIncrementalScanner::Token firstToken = tokens.first();
|
2009-09-16 13:56:06 +02:00
|
|
|
|
2009-11-16 12:38:47 +01:00
|
|
|
return firstToken.is(QScriptIncrementalScanner::Token::RightBrace) || firstToken.is(QScriptIncrementalScanner::Token::RightBracket);
|
|
|
|
}
|
2009-09-16 13:56:06 +02:00
|
|
|
|
2009-11-16 12:38:47 +01:00
|
|
|
return false;
|
|
|
|
}
|
2009-09-16 13:56:06 +02:00
|
|
|
|
2010-01-15 17:20:03 +01:00
|
|
|
void QmlJSTextEditor::indentBlock(QTextDocument *doc, QTextBlock block, QChar typedChar)
|
2009-11-16 12:38:47 +01:00
|
|
|
{
|
|
|
|
TextEditor::TabSettings ts = tabSettings();
|
2010-01-12 18:01:39 +01:00
|
|
|
SharedTools::QScriptIndenter indenter;
|
|
|
|
indenter.setTabSize(ts.m_tabSize);
|
|
|
|
indenter.setIndentSize(ts.m_indentSize);
|
2009-11-16 12:38:47 +01:00
|
|
|
|
2010-01-12 18:01:39 +01:00
|
|
|
const int indent = indenter.indentForBottomLine(doc->begin(), block.next(), typedChar);
|
2009-11-16 12:38:47 +01:00
|
|
|
ts.indentLine(block, indent);
|
2009-04-22 15:21:04 +02:00
|
|
|
}
|
|
|
|
|
2010-01-15 17:20:03 +01:00
|
|
|
TextEditor::BaseTextEditorEditable *QmlJSTextEditor::createEditableInterface()
|
2009-04-22 15:21:04 +02:00
|
|
|
{
|
2010-01-15 17:20:03 +01:00
|
|
|
QmlJSEditorEditable *editable = new QmlJSEditorEditable(this);
|
2009-09-16 13:56:06 +02:00
|
|
|
createToolBar(editable);
|
|
|
|
return editable;
|
2009-04-22 15:21:04 +02:00
|
|
|
}
|
|
|
|
|
2010-01-15 17:20:03 +01:00
|
|
|
void QmlJSTextEditor::createToolBar(QmlJSEditorEditable *editable)
|
2009-04-22 15:21:04 +02:00
|
|
|
{
|
2009-09-16 13:56:06 +02:00
|
|
|
m_methodCombo = new QComboBox;
|
|
|
|
m_methodCombo->setMinimumContentsLength(22);
|
|
|
|
//m_methodCombo->setSizeAdjustPolicy(QComboBox::AdjustToContents);
|
2009-04-22 15:21:04 +02:00
|
|
|
|
2009-09-16 13:56:06 +02:00
|
|
|
// Make the combo box prefer to expand
|
|
|
|
QSizePolicy policy = m_methodCombo->sizePolicy();
|
|
|
|
policy.setHorizontalPolicy(QSizePolicy::Expanding);
|
|
|
|
m_methodCombo->setSizePolicy(policy);
|
2009-04-22 15:21:04 +02:00
|
|
|
|
2009-09-16 13:56:06 +02:00
|
|
|
connect(m_methodCombo, SIGNAL(activated(int)), this, SLOT(jumpToMethod(int)));
|
|
|
|
connect(this, SIGNAL(cursorPositionChanged()), this, SLOT(updateMethodBoxIndex()));
|
|
|
|
connect(m_methodCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(updateMethodBoxToolTip()));
|
2009-04-22 15:21:04 +02:00
|
|
|
|
2009-09-16 13:56:06 +02:00
|
|
|
connect(file(), SIGNAL(changed()), this, SLOT(updateFileName()));
|
2009-04-22 15:21:04 +02:00
|
|
|
|
2009-09-16 13:56:06 +02:00
|
|
|
QToolBar *toolBar = static_cast<QToolBar*>(editable->toolBar());
|
2009-04-22 15:21:04 +02:00
|
|
|
|
2009-09-16 13:56:06 +02:00
|
|
|
QList<QAction*> actions = toolBar->actions();
|
|
|
|
toolBar->insertWidget(actions.first(), m_methodCombo);
|
2009-04-22 15:21:04 +02:00
|
|
|
}
|
|
|
|
|
2010-01-15 17:20:03 +01:00
|
|
|
TextEditor::BaseTextEditor::Link QmlJSTextEditor::findLinkAt(const QTextCursor &cursor, bool /*resolveTarget*/)
|
2009-09-11 14:42:50 +02:00
|
|
|
{
|
|
|
|
Link link;
|
|
|
|
|
|
|
|
if (!m_modelManager)
|
|
|
|
return link;
|
|
|
|
|
|
|
|
const Snapshot snapshot = m_modelManager->snapshot();
|
2009-09-30 17:43:21 +02:00
|
|
|
QmlDocument::Ptr doc = snapshot.document(file()->fileName());
|
2009-09-11 14:42:50 +02:00
|
|
|
if (!doc)
|
|
|
|
return link;
|
|
|
|
|
2009-10-02 10:49:33 +02:00
|
|
|
QTextCursor expressionCursor(cursor);
|
|
|
|
{
|
|
|
|
// correct the position by moving to the end of an identifier (if we're hovering over one):
|
|
|
|
int pos = cursor.position();
|
|
|
|
forever {
|
|
|
|
const QChar ch = characterAt(pos);
|
|
|
|
|
|
|
|
if (ch.isLetterOrNumber() || ch == QLatin1Char('_'))
|
|
|
|
++pos;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
expressionCursor.setPosition(pos);
|
|
|
|
}
|
|
|
|
|
2009-09-23 12:21:43 +02:00
|
|
|
QmlExpressionUnderCursor expressionUnderCursor;
|
2009-10-02 10:49:33 +02:00
|
|
|
expressionUnderCursor(expressionCursor, doc);
|
2009-09-22 17:43:05 +02:00
|
|
|
|
2009-12-07 16:57:31 +01:00
|
|
|
QmlLookupContext context(expressionUnderCursor.expressionScopes(), doc, snapshot, m_typeSystem);
|
2009-10-02 10:49:33 +02:00
|
|
|
QmlResolveExpression resolver(context);
|
|
|
|
QmlSymbol *symbol = resolver.typeOf(expressionUnderCursor.expressionNode());
|
2009-09-23 12:21:43 +02:00
|
|
|
|
|
|
|
if (!symbol)
|
|
|
|
return link;
|
|
|
|
|
|
|
|
if (const QmlSymbolFromFile *target = symbol->asSymbolFromFile()) {
|
|
|
|
link.pos = expressionUnderCursor.expressionOffset();
|
|
|
|
link.length = expressionUnderCursor.expressionLength();
|
|
|
|
link.fileName = target->fileName();
|
|
|
|
link.line = target->line();
|
|
|
|
link.column = target->column();
|
2009-09-23 13:59:38 +02:00
|
|
|
if (link.column > 0)
|
|
|
|
--link.column;
|
2009-09-23 12:21:43 +02:00
|
|
|
}
|
2009-09-22 17:25:18 +02:00
|
|
|
|
2009-09-11 14:42:50 +02:00
|
|
|
return link;
|
|
|
|
}
|
|
|
|
|
2010-01-15 17:20:03 +01:00
|
|
|
void QmlJSTextEditor::contextMenuEvent(QContextMenuEvent *e)
|
2009-04-22 15:21:04 +02:00
|
|
|
{
|
2009-11-09 17:35:20 +01:00
|
|
|
QMenu *menu = new QMenu();
|
2009-09-16 13:56:06 +02:00
|
|
|
|
2010-01-15 17:20:03 +01:00
|
|
|
if (Core::ActionContainer *mcontext = Core::ICore::instance()->actionManager()->actionContainer(QmlJSEditor::Constants::M_CONTEXT)) {
|
2009-09-16 13:56:06 +02:00
|
|
|
QMenu *contextMenu = mcontext->menu();
|
|
|
|
foreach (QAction *action, contextMenu->actions())
|
|
|
|
menu->addAction(action);
|
|
|
|
}
|
|
|
|
|
|
|
|
const QString id = wordUnderCursor();
|
|
|
|
const QList<AST::SourceLocation> &locations = m_ids.value(id);
|
|
|
|
if (! locations.isEmpty()) {
|
|
|
|
menu->addSeparator();
|
|
|
|
QAction *a = menu->addAction(tr("Rename id '%1'...").arg(id));
|
|
|
|
connect(a, SIGNAL(triggered()), this, SLOT(renameIdUnderCursor()));
|
|
|
|
}
|
|
|
|
|
2009-11-09 17:35:20 +01:00
|
|
|
appendStandardContextMenuActions(menu);
|
|
|
|
|
2009-09-16 13:56:06 +02:00
|
|
|
menu->exec(e->globalPos());
|
|
|
|
menu->deleteLater();
|
2009-04-22 15:21:04 +02:00
|
|
|
}
|
|
|
|
|
2010-01-15 17:20:03 +01:00
|
|
|
void QmlJSTextEditor::unCommentSelection()
|
2009-05-14 16:37:17 +02:00
|
|
|
{
|
2009-10-05 11:06:05 +02:00
|
|
|
Utils::unCommentSelection(this);
|
2009-05-14 16:37:17 +02:00
|
|
|
}
|
|
|
|
|
2010-01-13 13:51:01 +01:00
|
|
|
static bool isCompleteStringLiteral(const QStringRef &text)
|
|
|
|
{
|
|
|
|
if (text.length() < 2)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const QChar quote = text.at(0);
|
|
|
|
|
|
|
|
if (text.at(text.length() - 1) == quote)
|
|
|
|
return text.at(text.length() - 2) != QLatin1Char('\\'); // ### not exactly.
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-01-15 17:20:03 +01:00
|
|
|
bool QmlJSTextEditor::contextAllowsAutoParentheses(const QTextCursor &cursor, const QString &textToInsert) const
|
2010-01-12 14:13:43 +01:00
|
|
|
{
|
2010-01-12 14:39:16 +01:00
|
|
|
QChar ch;
|
|
|
|
|
|
|
|
if (! textToInsert.isEmpty())
|
|
|
|
ch = textToInsert.at(0);
|
|
|
|
|
2010-01-12 15:59:22 +01:00
|
|
|
switch (ch.unicode()) {
|
|
|
|
case '\'':
|
|
|
|
case '"':
|
|
|
|
|
|
|
|
case '(':
|
|
|
|
case '[':
|
|
|
|
case '{':
|
|
|
|
|
|
|
|
case ')':
|
|
|
|
case ']':
|
|
|
|
case '}':
|
|
|
|
|
|
|
|
case ';':
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
if (ch.isNull())
|
|
|
|
break;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
} // end of switch
|
|
|
|
|
2010-01-12 14:30:54 +01:00
|
|
|
const QString blockText = cursor.block().text();
|
|
|
|
const int blockState = blockStartState(cursor.block());
|
|
|
|
|
|
|
|
QScriptIncrementalScanner tokenize;
|
|
|
|
const QList<QScriptIncrementalScanner::Token> tokens = tokenize(blockText, blockState);
|
|
|
|
const int pos = cursor.columnNumber();
|
|
|
|
|
2010-01-13 13:51:01 +01:00
|
|
|
int tokenIndex = 0;
|
|
|
|
for (; tokenIndex < tokens.size(); ++tokenIndex) {
|
2010-01-12 14:30:54 +01:00
|
|
|
const QScriptIncrementalScanner::Token &token = tokens.at(tokenIndex);
|
2010-01-13 13:51:01 +01:00
|
|
|
|
2010-01-12 15:59:22 +01:00
|
|
|
if (pos >= token.begin()) {
|
|
|
|
if (pos < token.end())
|
|
|
|
break;
|
2010-01-13 13:51:01 +01:00
|
|
|
|
|
|
|
else if (pos == token.end() && (token.is(QScriptIncrementalScanner::Token::Comment) ||
|
|
|
|
token.is(QScriptIncrementalScanner::Token::String)))
|
2010-01-12 15:59:22 +01:00
|
|
|
break;
|
|
|
|
}
|
2010-01-12 14:30:54 +01:00
|
|
|
}
|
|
|
|
|
2010-01-13 13:51:01 +01:00
|
|
|
if (tokenIndex != tokens.size()) {
|
2010-01-12 14:30:54 +01:00
|
|
|
const QScriptIncrementalScanner::Token &token = tokens.at(tokenIndex);
|
|
|
|
|
|
|
|
switch (token.kind) {
|
|
|
|
case QScriptIncrementalScanner::Token::Comment:
|
|
|
|
return false;
|
|
|
|
|
2010-01-12 14:39:16 +01:00
|
|
|
case QScriptIncrementalScanner::Token::String: {
|
2010-01-13 13:51:01 +01:00
|
|
|
const QStringRef tokenText = blockText.midRef(token.offset, token.length);
|
|
|
|
const QChar quote = tokenText.at(0);
|
|
|
|
|
|
|
|
if (ch == quote && isCompleteStringLiteral(tokenText))
|
|
|
|
break;
|
|
|
|
|
2010-01-12 14:39:16 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-01-12 14:30:54 +01:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
} // end of switch
|
|
|
|
}
|
2010-01-12 14:13:43 +01:00
|
|
|
|
2010-01-12 15:59:22 +01:00
|
|
|
return true;
|
2010-01-12 14:13:43 +01:00
|
|
|
}
|
|
|
|
|
2010-01-15 17:20:03 +01:00
|
|
|
bool QmlJSTextEditor::isInComment(const QTextCursor &) const
|
2010-01-12 14:13:43 +01:00
|
|
|
{
|
|
|
|
// ### implement me
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-01-15 17:20:03 +01:00
|
|
|
QString QmlJSTextEditor::insertMatchingBrace(const QTextCursor &tc, const QString &text, const QChar &, int *skippedChars) const
|
2010-01-12 14:13:43 +01:00
|
|
|
{
|
|
|
|
if (text.length() != 1)
|
|
|
|
return QString();
|
|
|
|
|
|
|
|
const QChar la = characterAt(tc.position());
|
|
|
|
|
|
|
|
const QChar ch = text.at(0);
|
|
|
|
switch (ch.unicode()) {
|
|
|
|
case '\'':
|
|
|
|
if (la != ch)
|
|
|
|
return QString(ch);
|
|
|
|
++*skippedChars;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '"':
|
|
|
|
if (la != ch)
|
|
|
|
return QString(ch);
|
|
|
|
++*skippedChars;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '(':
|
|
|
|
return QString(QLatin1Char(')'));
|
|
|
|
|
|
|
|
case '[':
|
|
|
|
return QString(QLatin1Char(']'));
|
|
|
|
|
|
|
|
case '{':
|
|
|
|
return QString(); // nothing to do.
|
|
|
|
|
|
|
|
case ')':
|
|
|
|
case ']':
|
|
|
|
case '}':
|
|
|
|
case ';':
|
|
|
|
if (la == ch)
|
|
|
|
++*skippedChars;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
} // end of switch
|
|
|
|
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
2010-01-15 17:20:03 +01:00
|
|
|
QString QmlJSTextEditor::insertParagraphSeparator(const QTextCursor &) const
|
2010-01-12 14:13:43 +01:00
|
|
|
{
|
|
|
|
return QLatin1String("}\n");
|
|
|
|
}
|
|
|
|
|
2009-04-22 15:21:04 +02:00
|
|
|
} // namespace Internal
|
2010-01-15 17:20:03 +01:00
|
|
|
} // namespace QmlJSEditor
|