2009-12-02 17:59:27 +01:00
|
|
|
/**************************************************************************
|
|
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator
|
|
|
|
|
**
|
|
|
|
|
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
|
|
|
**
|
|
|
|
|
** Contact: Nokia Corporation (qt-info@nokia.com)
|
|
|
|
|
**
|
|
|
|
|
** 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
|
|
|
|
|
** contact the sales department at http://qt.nokia.com/contact.
|
|
|
|
|
**
|
|
|
|
|
**************************************************************************/
|
|
|
|
|
|
2009-09-30 17:43:21 +02:00
|
|
|
#include "qmlcodecompletion.h"
|
2010-01-15 17:20:03 +01:00
|
|
|
#include "qmljseditor.h"
|
2009-10-02 10:49:33 +02:00
|
|
|
#include "qmlmodelmanagerinterface.h"
|
|
|
|
|
#include "qmllookupcontext.h"
|
|
|
|
|
|
2010-01-21 17:08:19 +01:00
|
|
|
#include <qmljs/parser/qmljsast_p.h>
|
2010-01-26 14:50:52 +01:00
|
|
|
#include <qmljs/qmljsbind.h>
|
2010-01-21 17:08:19 +01:00
|
|
|
#include <qmljs/qmljsinterpreter.h>
|
2010-01-18 16:15:23 +01:00
|
|
|
#include <qmljs/qmljssymbol.h>
|
2010-01-22 14:42:15 +01:00
|
|
|
#include <qmljs/qmljsscanner.h>
|
2010-01-26 14:53:11 +01:00
|
|
|
#include <qmljs/qmljscheck.h>
|
2010-01-22 14:42:15 +01:00
|
|
|
|
2009-04-22 15:21:04 +02:00
|
|
|
#include <texteditor/basetexteditor.h>
|
2009-12-02 17:59:27 +01:00
|
|
|
|
2010-01-19 18:50:50 +01:00
|
|
|
#include <coreplugin/icore.h>
|
2010-01-22 10:57:48 +01:00
|
|
|
#include <coreplugin/editormanager/editormanager.h>
|
2010-01-19 18:50:50 +01:00
|
|
|
|
|
|
|
|
#include <QtCore/QFile>
|
|
|
|
|
#include <QtCore/QFileInfo>
|
2010-01-25 17:13:04 +01:00
|
|
|
#include <QtCore/QDir>
|
2010-01-19 18:50:50 +01:00
|
|
|
#include <QtCore/QXmlStreamReader>
|
2010-01-22 10:57:48 +01:00
|
|
|
#include <QtCore/QDebug>
|
2010-01-19 18:50:50 +01:00
|
|
|
|
2010-01-22 10:57:48 +01:00
|
|
|
#include <QtGui/QPainter>
|
|
|
|
|
#include <QtGui/QLabel>
|
|
|
|
|
#include <QtGui/QStylePainter>
|
|
|
|
|
#include <QtGui/QStyleOption>
|
|
|
|
|
#include <QtGui/QToolButton>
|
|
|
|
|
#include <QtGui/QHBoxLayout>
|
|
|
|
|
#include <QtGui/QApplication>
|
|
|
|
|
#include <QtGui/QDesktopWidget>
|
2009-04-22 15:21:04 +02:00
|
|
|
|
2010-01-15 17:20:03 +01:00
|
|
|
using namespace QmlJSEditor;
|
|
|
|
|
using namespace QmlJSEditor::Internal;
|
2010-01-22 10:26:25 +01:00
|
|
|
using namespace QmlJS;
|
2009-04-22 15:21:04 +02:00
|
|
|
|
2010-01-21 10:56:58 +01:00
|
|
|
|
|
|
|
|
// Temporary workaround until we have proper icons for QML completion items
|
|
|
|
|
static QIcon iconForColor(const QColor &color)
|
|
|
|
|
{
|
|
|
|
|
QPixmap pix(6, 6);
|
|
|
|
|
|
|
|
|
|
int pixSize = 20;
|
|
|
|
|
QBrush br(color);
|
|
|
|
|
|
|
|
|
|
QPixmap pm(2 * pixSize, 2 * pixSize);
|
|
|
|
|
QPainter pmp(&pm);
|
|
|
|
|
pmp.fillRect(0, 0, pixSize, pixSize, Qt::lightGray);
|
|
|
|
|
pmp.fillRect(pixSize, pixSize, pixSize, pixSize, Qt::lightGray);
|
|
|
|
|
pmp.fillRect(0, pixSize, pixSize, pixSize, Qt::darkGray);
|
|
|
|
|
pmp.fillRect(pixSize, 0, pixSize, pixSize, Qt::darkGray);
|
|
|
|
|
pmp.fillRect(0, 0, 2 * pixSize, 2 * pixSize, color);
|
|
|
|
|
br = QBrush(pm);
|
|
|
|
|
|
|
|
|
|
QPainter p(&pix);
|
|
|
|
|
int corr = 1;
|
|
|
|
|
QRect r = pix.rect().adjusted(corr, corr, -corr, -corr);
|
|
|
|
|
p.setBrushOrigin((r.width() % pixSize + pixSize) / 2 + corr, (r.height() % pixSize + pixSize) / 2 + corr);
|
|
|
|
|
p.fillRect(r, br);
|
|
|
|
|
|
|
|
|
|
p.fillRect(r.width() / 4 + corr, r.height() / 4 + corr,
|
|
|
|
|
r.width() / 2, r.height() / 2,
|
|
|
|
|
QColor(color.rgb()));
|
|
|
|
|
p.drawRect(pix.rect().adjusted(0, 0, -1, -1));
|
|
|
|
|
|
|
|
|
|
return pix;
|
|
|
|
|
}
|
|
|
|
|
|
2010-01-25 15:49:21 +01:00
|
|
|
|
|
|
|
|
static QString qualifiedNameId(AST::UiQualifiedId *it)
|
|
|
|
|
{
|
|
|
|
|
QString text;
|
|
|
|
|
|
|
|
|
|
for (; it; it = it->next) {
|
|
|
|
|
if (! it->name)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
text += it->name->asString();
|
|
|
|
|
|
|
|
|
|
if (it->next)
|
|
|
|
|
text += QLatin1Char('.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return text;
|
|
|
|
|
}
|
|
|
|
|
|
2010-01-26 14:50:52 +01:00
|
|
|
#if 0
|
2010-01-25 15:49:21 +01:00
|
|
|
static Interpreter::ObjectValue *newComponent(Interpreter::Engine *engine, const QString &name,
|
|
|
|
|
const QHash<QString, Document::Ptr> &userComponents,
|
|
|
|
|
QSet<QString> *processed)
|
|
|
|
|
{
|
|
|
|
|
if (Interpreter::ObjectValue *object = engine->newQmlObject(name))
|
|
|
|
|
return object;
|
|
|
|
|
|
|
|
|
|
else if (! processed->contains(name)) {
|
|
|
|
|
processed->insert(name);
|
|
|
|
|
|
|
|
|
|
if (Document::Ptr doc = userComponents.value(name)) {
|
|
|
|
|
if (AST::UiProgram *program = doc->qmlProgram()) {
|
|
|
|
|
if (program->members) {
|
|
|
|
|
if (AST::UiObjectDefinition *def = AST::cast<AST::UiObjectDefinition *>(program->members->member)) {
|
|
|
|
|
const QString component = qualifiedNameId(def->qualifiedTypeNameId);
|
|
|
|
|
Interpreter::ObjectValue *object = newComponent(engine, component, userComponents, processed);
|
|
|
|
|
if (def->initializer) {
|
|
|
|
|
for (AST::UiObjectMemberList *it = def->initializer->members; it; it = it->next) {
|
|
|
|
|
if (AST::UiPublicMember *prop = AST::cast<AST::UiPublicMember *>(it->member)) {
|
|
|
|
|
if (prop->name && prop->memberType) {
|
2010-01-25 17:13:04 +01:00
|
|
|
const QString propName = prop->name->asString();
|
|
|
|
|
const QString propType = prop->memberType->asString();
|
|
|
|
|
|
2010-01-26 11:15:21 +01:00
|
|
|
object->setProperty(propName, engine->defaultValueForBuiltinType(propType));
|
2010-01-25 15:49:21 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return object;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static Interpreter::ObjectValue *newComponent(Interpreter::Engine *engine,
|
|
|
|
|
const QString &name,
|
|
|
|
|
const QHash<QString, Document::Ptr> &userComponents)
|
|
|
|
|
{
|
|
|
|
|
QSet<QString> processed;
|
|
|
|
|
return newComponent(engine, name, userComponents, &processed);
|
|
|
|
|
}
|
2010-01-26 14:50:52 +01:00
|
|
|
#endif
|
2010-01-25 15:49:21 +01:00
|
|
|
|
2010-01-22 10:26:25 +01:00
|
|
|
namespace {
|
2010-01-21 10:56:58 +01:00
|
|
|
|
2010-01-22 14:42:15 +01:00
|
|
|
class ExpressionUnderCursor
|
|
|
|
|
{
|
|
|
|
|
QTextCursor _cursor;
|
|
|
|
|
QmlJSScanner scanner;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
QString operator()(const QTextCursor &cursor)
|
|
|
|
|
{
|
|
|
|
|
_cursor = cursor;
|
|
|
|
|
|
|
|
|
|
QTextBlock block = _cursor.block();
|
|
|
|
|
const QString blockText = block.text().left(cursor.columnNumber());
|
|
|
|
|
//qDebug() << "block text:" << blockText;
|
|
|
|
|
|
|
|
|
|
int startState = block.previous().userState();
|
|
|
|
|
if (startState == -1)
|
|
|
|
|
startState = 0;
|
|
|
|
|
else
|
|
|
|
|
startState = startState & 0xff;
|
|
|
|
|
|
|
|
|
|
const QList<Token> originalTokens = scanner(blockText, startState);
|
|
|
|
|
QList<Token> tokens;
|
|
|
|
|
int skipping = 0;
|
|
|
|
|
for (int index = originalTokens.size() - 1; index != -1; --index) {
|
|
|
|
|
const Token &tk = originalTokens.at(index);
|
|
|
|
|
|
|
|
|
|
if (tk.is(Token::Comment) || tk.is(Token::String) || tk.is(Token::Number))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if (! skipping) {
|
|
|
|
|
tokens.append(tk);
|
|
|
|
|
|
|
|
|
|
if (tk.is(Token::Identifier)) {
|
|
|
|
|
if (index > 0 && originalTokens.at(index - 1).isNot(Token::Dot))
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
//qDebug() << "skip:" << blockText.mid(tk.offset, tk.length);
|
|
|
|
|
}
|
|
|
|
|
|
2010-01-26 15:09:04 +01:00
|
|
|
if (tk.is(Token::RightParenthesis) || tk.is(Token::RightBracket))
|
2010-01-22 14:42:15 +01:00
|
|
|
++skipping;
|
|
|
|
|
|
2010-01-26 15:09:04 +01:00
|
|
|
else if (tk.is(Token::LeftParenthesis) || tk.is(Token::LeftBracket)) {
|
2010-01-22 14:42:15 +01:00
|
|
|
--skipping;
|
|
|
|
|
|
|
|
|
|
if (! skipping)
|
|
|
|
|
tokens.append(tk);
|
2010-01-26 15:09:04 +01:00
|
|
|
|
|
|
|
|
if (index > 0 && originalTokens.at(index - 1).isNot(Token::Identifier))
|
|
|
|
|
break;
|
2010-01-22 14:42:15 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (! tokens.isEmpty()) {
|
|
|
|
|
QString expr;
|
|
|
|
|
for (int index = tokens.size() - 1; index >= 0; --index) {
|
|
|
|
|
Token tk = tokens.at(index);
|
|
|
|
|
expr.append(QLatin1Char(' '));
|
|
|
|
|
expr.append(blockText.midRef(tk.offset, tk.length));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//qDebug() << "expression under cursor:" << expr;
|
|
|
|
|
return expr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//qDebug() << "no expression";
|
|
|
|
|
return QString();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class SearchPropertyDefinitions: protected AST::Visitor
|
|
|
|
|
{
|
|
|
|
|
QList<AST::UiPublicMember *> _properties;
|
|
|
|
|
|
|
|
|
|
public:
|
2010-01-24 17:33:07 +01:00
|
|
|
QList<AST::UiPublicMember *> operator()(Document::Ptr doc)
|
2010-01-22 14:42:15 +01:00
|
|
|
{
|
|
|
|
|
_properties.clear();
|
2010-01-24 17:33:07 +01:00
|
|
|
if (doc && doc->qmlProgram())
|
|
|
|
|
doc->qmlProgram()->accept(this);
|
2010-01-22 14:42:15 +01:00
|
|
|
return _properties;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
using AST::Visitor::visit;
|
|
|
|
|
|
|
|
|
|
virtual bool visit(AST::UiPublicMember *member)
|
|
|
|
|
{
|
|
|
|
|
if (member->propertyToken.isValid()) {
|
|
|
|
|
_properties.append(member);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2010-01-24 11:10:01 +01:00
|
|
|
class EnumerateProperties: private Interpreter::MemberProcessor
|
2010-01-22 10:26:25 +01:00
|
|
|
{
|
|
|
|
|
QSet<const Interpreter::ObjectValue *> _processed;
|
|
|
|
|
QHash<QString, const Interpreter::Value *> _properties;
|
2010-01-26 10:10:11 +01:00
|
|
|
bool _globalCompletion;
|
2010-01-22 10:26:25 +01:00
|
|
|
|
|
|
|
|
public:
|
2010-01-26 10:10:11 +01:00
|
|
|
EnumerateProperties()
|
|
|
|
|
: _globalCompletion(false)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void setGlobalCompletion(bool globalCompletion)
|
|
|
|
|
{
|
|
|
|
|
_globalCompletion = globalCompletion;
|
|
|
|
|
}
|
|
|
|
|
|
2010-01-24 18:11:25 +01:00
|
|
|
QHash<QString, const Interpreter::Value *> operator()(const Interpreter::Value *value,
|
|
|
|
|
bool lookAtScope = false)
|
2010-01-22 10:26:25 +01:00
|
|
|
{
|
|
|
|
|
_processed.clear();
|
|
|
|
|
_properties.clear();
|
2010-01-24 18:11:25 +01:00
|
|
|
enumerateProperties(value, lookAtScope);
|
2010-01-22 10:26:25 +01:00
|
|
|
return _properties;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
2010-01-26 10:10:11 +01:00
|
|
|
virtual bool processProperty(const QString &name, const Interpreter::Value *value)
|
2010-01-24 11:10:01 +01:00
|
|
|
{
|
|
|
|
|
_properties.insert(name, value);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2010-01-26 15:19:19 +01:00
|
|
|
virtual bool processSignal(const QString &, const Interpreter::Value *)
|
2010-01-26 10:19:42 +01:00
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2010-01-26 10:10:11 +01:00
|
|
|
virtual bool processSlot(const QString &name, const Interpreter::Value *value)
|
2010-01-26 10:19:42 +01:00
|
|
|
{
|
|
|
|
|
if (! _globalCompletion)
|
|
|
|
|
_properties.insert(name, value);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual bool processGeneratedSlot(const QString &name, const Interpreter::Value *value)
|
2010-01-26 10:10:11 +01:00
|
|
|
{
|
|
|
|
|
if (_globalCompletion)
|
|
|
|
|
_properties.insert(name, value);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2010-01-24 18:11:25 +01:00
|
|
|
void enumerateProperties(const Interpreter::Value *value, bool lookAtScope)
|
2010-01-22 10:26:25 +01:00
|
|
|
{
|
|
|
|
|
if (! value)
|
|
|
|
|
return;
|
|
|
|
|
else if (const Interpreter::ObjectValue *object = value->asObjectValue()) {
|
2010-01-24 18:11:25 +01:00
|
|
|
enumerateProperties(object, lookAtScope);
|
2010-01-22 10:26:25 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-01-24 18:11:25 +01:00
|
|
|
void enumerateProperties(const Interpreter::ObjectValue *object, bool lookAtScope)
|
2010-01-22 10:26:25 +01:00
|
|
|
{
|
|
|
|
|
if (! object || _processed.contains(object))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
_processed.insert(object);
|
2010-01-24 18:11:25 +01:00
|
|
|
enumerateProperties(object->prototype(), /* lookAtScope = */ false);
|
|
|
|
|
|
|
|
|
|
if (lookAtScope)
|
|
|
|
|
enumerateProperties(object->scope(), /* lookAtScope = */ true);
|
2010-01-22 10:26:25 +01:00
|
|
|
|
2010-01-24 11:10:01 +01:00
|
|
|
object->processMembers(this);
|
2010-01-22 10:26:25 +01:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // end of anonymous namespace
|
|
|
|
|
|
2010-01-22 10:57:48 +01:00
|
|
|
namespace QmlJSEditor {
|
|
|
|
|
namespace Internal {
|
|
|
|
|
|
|
|
|
|
class FakeToolTipFrame : public QWidget
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
FakeToolTipFrame(QWidget *parent = 0) :
|
|
|
|
|
QWidget(parent, Qt::ToolTip | Qt::WindowStaysOnTopHint)
|
|
|
|
|
{
|
|
|
|
|
setFocusPolicy(Qt::NoFocus);
|
|
|
|
|
setAttribute(Qt::WA_DeleteOnClose);
|
|
|
|
|
|
|
|
|
|
// Set the window and button text to the tooltip text color, since this
|
|
|
|
|
// widget draws the background as a tooltip.
|
|
|
|
|
QPalette p = palette();
|
|
|
|
|
const QColor toolTipTextColor = p.color(QPalette::Inactive, QPalette::ToolTipText);
|
|
|
|
|
p.setColor(QPalette::Inactive, QPalette::WindowText, toolTipTextColor);
|
|
|
|
|
p.setColor(QPalette::Inactive, QPalette::ButtonText, toolTipTextColor);
|
|
|
|
|
setPalette(p);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
void paintEvent(QPaintEvent *e);
|
|
|
|
|
void resizeEvent(QResizeEvent *e);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class FunctionArgumentWidget : public QLabel
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
FunctionArgumentWidget();
|
2010-01-26 10:50:30 +01:00
|
|
|
void showFunctionHint(const QString &functionName,
|
|
|
|
|
const QStringList &signature,
|
|
|
|
|
int startPosition);
|
2010-01-22 10:57:48 +01:00
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
bool eventFilter(QObject *obj, QEvent *e);
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
void updateArgumentHighlight();
|
|
|
|
|
void updateHintText();
|
|
|
|
|
|
|
|
|
|
QString m_functionName;
|
2010-01-26 10:50:30 +01:00
|
|
|
QStringList m_signature;
|
2010-01-22 10:57:48 +01:00
|
|
|
int m_minimumArgumentCount;
|
|
|
|
|
int m_startpos;
|
|
|
|
|
int m_currentarg;
|
|
|
|
|
int m_current;
|
|
|
|
|
bool m_escapePressed;
|
|
|
|
|
|
|
|
|
|
TextEditor::ITextEditor *m_editor;
|
|
|
|
|
|
|
|
|
|
QWidget *m_pager;
|
|
|
|
|
QLabel *m_numberLabel;
|
|
|
|
|
FakeToolTipFrame *m_popupFrame;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
void FakeToolTipFrame::paintEvent(QPaintEvent *)
|
|
|
|
|
{
|
|
|
|
|
QStylePainter p(this);
|
|
|
|
|
QStyleOptionFrame opt;
|
|
|
|
|
opt.init(this);
|
|
|
|
|
p.drawPrimitive(QStyle::PE_PanelTipLabel, opt);
|
|
|
|
|
p.end();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FakeToolTipFrame::resizeEvent(QResizeEvent *)
|
|
|
|
|
{
|
|
|
|
|
QStyleHintReturnMask frameMask;
|
|
|
|
|
QStyleOption option;
|
|
|
|
|
option.init(this);
|
|
|
|
|
if (style()->styleHint(QStyle::SH_ToolTip_Mask, &option, this, &frameMask))
|
|
|
|
|
setMask(frameMask.region);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
FunctionArgumentWidget::FunctionArgumentWidget():
|
|
|
|
|
m_minimumArgumentCount(0),
|
|
|
|
|
m_startpos(-1),
|
|
|
|
|
m_current(0),
|
|
|
|
|
m_escapePressed(false)
|
|
|
|
|
{
|
|
|
|
|
QObject *editorObject = Core::EditorManager::instance()->currentEditor();
|
|
|
|
|
m_editor = qobject_cast<TextEditor::ITextEditor *>(editorObject);
|
|
|
|
|
|
|
|
|
|
m_popupFrame = new FakeToolTipFrame(m_editor->widget());
|
|
|
|
|
|
|
|
|
|
setParent(m_popupFrame);
|
|
|
|
|
setFocusPolicy(Qt::NoFocus);
|
|
|
|
|
|
|
|
|
|
m_pager = new QWidget;
|
|
|
|
|
QHBoxLayout *hbox = new QHBoxLayout(m_pager);
|
|
|
|
|
hbox->setMargin(0);
|
|
|
|
|
hbox->setSpacing(0);
|
|
|
|
|
m_numberLabel = new QLabel;
|
|
|
|
|
hbox->addWidget(m_numberLabel);
|
|
|
|
|
|
|
|
|
|
QHBoxLayout *layout = new QHBoxLayout;
|
|
|
|
|
layout->setMargin(0);
|
|
|
|
|
layout->setSpacing(0);
|
|
|
|
|
layout->addWidget(m_pager);
|
|
|
|
|
layout->addWidget(this);
|
|
|
|
|
m_popupFrame->setLayout(layout);
|
|
|
|
|
|
|
|
|
|
setTextFormat(Qt::RichText);
|
|
|
|
|
setMargin(1);
|
|
|
|
|
|
|
|
|
|
qApp->installEventFilter(this);
|
|
|
|
|
}
|
|
|
|
|
|
2010-01-26 10:50:30 +01:00
|
|
|
void FunctionArgumentWidget::showFunctionHint(const QString &functionName, const QStringList &signature, int startPosition)
|
2010-01-22 10:57:48 +01:00
|
|
|
{
|
|
|
|
|
if (m_startpos == startPosition)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
m_functionName = functionName;
|
2010-01-26 10:50:30 +01:00
|
|
|
m_signature = signature;
|
|
|
|
|
m_minimumArgumentCount = signature.size();
|
2010-01-22 10:57:48 +01:00
|
|
|
m_startpos = startPosition;
|
|
|
|
|
m_current = 0;
|
|
|
|
|
m_escapePressed = false;
|
|
|
|
|
|
|
|
|
|
// update the text
|
|
|
|
|
m_currentarg = -1;
|
|
|
|
|
updateArgumentHighlight();
|
|
|
|
|
|
|
|
|
|
m_popupFrame->show();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FunctionArgumentWidget::updateArgumentHighlight()
|
|
|
|
|
{
|
|
|
|
|
int curpos = m_editor->position();
|
|
|
|
|
if (curpos < m_startpos) {
|
|
|
|
|
m_popupFrame->close();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
updateHintText();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
QString str = m_editor->textAt(m_startpos, curpos - m_startpos);
|
|
|
|
|
int argnr = 0;
|
|
|
|
|
int parcount = 0;
|
2010-01-22 14:42:15 +01:00
|
|
|
QmlJSScanner tokenize;
|
|
|
|
|
const QList<Token> tokens = tokenize(str);
|
2010-01-22 10:57:48 +01:00
|
|
|
for (int i = 0; i < tokens.count(); ++i) {
|
2010-01-22 14:42:15 +01:00
|
|
|
const Token &tk = tokens.at(i);
|
|
|
|
|
if (tk.is(Token::LeftParenthesis))
|
2010-01-22 10:57:48 +01:00
|
|
|
++parcount;
|
2010-01-22 14:42:15 +01:00
|
|
|
else if (tk.is(Token::RightParenthesis))
|
2010-01-22 10:57:48 +01:00
|
|
|
--parcount;
|
2010-01-22 14:42:15 +01:00
|
|
|
else if (! parcount && tk.is(Token::Colon))
|
2010-01-22 10:57:48 +01:00
|
|
|
++argnr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (m_currentarg != argnr) {
|
2010-01-22 14:42:15 +01:00
|
|
|
// m_currentarg = argnr;
|
2010-01-22 10:57:48 +01:00
|
|
|
updateHintText();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (parcount < 0)
|
|
|
|
|
m_popupFrame->close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool FunctionArgumentWidget::eventFilter(QObject *obj, QEvent *e)
|
|
|
|
|
{
|
|
|
|
|
switch (e->type()) {
|
|
|
|
|
case QEvent::ShortcutOverride:
|
|
|
|
|
if (static_cast<QKeyEvent*>(e)->key() == Qt::Key_Escape) {
|
|
|
|
|
m_escapePressed = true;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case QEvent::KeyPress:
|
|
|
|
|
if (static_cast<QKeyEvent*>(e)->key() == Qt::Key_Escape) {
|
|
|
|
|
m_escapePressed = true;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case QEvent::KeyRelease:
|
|
|
|
|
if (static_cast<QKeyEvent*>(e)->key() == Qt::Key_Escape && m_escapePressed) {
|
|
|
|
|
m_popupFrame->close();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
updateArgumentHighlight();
|
|
|
|
|
break;
|
|
|
|
|
case QEvent::WindowDeactivate:
|
|
|
|
|
case QEvent::FocusOut:
|
|
|
|
|
if (obj != m_editor->widget())
|
|
|
|
|
break;
|
|
|
|
|
m_popupFrame->close();
|
|
|
|
|
break;
|
|
|
|
|
case QEvent::MouseButtonPress:
|
|
|
|
|
case QEvent::MouseButtonRelease:
|
|
|
|
|
case QEvent::MouseButtonDblClick:
|
|
|
|
|
case QEvent::Wheel: {
|
|
|
|
|
QWidget *widget = qobject_cast<QWidget *>(obj);
|
|
|
|
|
if (! (widget == this || m_popupFrame->isAncestorOf(widget))) {
|
|
|
|
|
m_popupFrame->close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FunctionArgumentWidget::updateHintText()
|
|
|
|
|
{
|
|
|
|
|
QString prettyMethod;
|
|
|
|
|
prettyMethod += QString::fromLatin1("function ");
|
|
|
|
|
prettyMethod += m_functionName;
|
2010-01-26 10:50:30 +01:00
|
|
|
prettyMethod += QLatin1Char('(');
|
|
|
|
|
for (int i = 0; i < m_minimumArgumentCount; ++i) {
|
|
|
|
|
if (i != 0)
|
|
|
|
|
prettyMethod += QLatin1String(", ");
|
|
|
|
|
|
|
|
|
|
prettyMethod += QLatin1String("arg");
|
|
|
|
|
|
|
|
|
|
if (m_minimumArgumentCount != 1)
|
|
|
|
|
prettyMethod += QString::number(i + 1);
|
|
|
|
|
}
|
|
|
|
|
prettyMethod += QLatin1Char(')');
|
2010-01-22 10:57:48 +01:00
|
|
|
|
|
|
|
|
m_numberLabel->setText(prettyMethod);
|
|
|
|
|
|
|
|
|
|
m_popupFrame->setFixedWidth(m_popupFrame->minimumSizeHint().width());
|
|
|
|
|
|
|
|
|
|
const QDesktopWidget *desktop = QApplication::desktop();
|
|
|
|
|
#ifdef Q_WS_MAC
|
|
|
|
|
const QRect screen = desktop->availableGeometry(desktop->screenNumber(m_editor->widget()));
|
|
|
|
|
#else
|
|
|
|
|
const QRect screen = desktop->screenGeometry(desktop->screenNumber(m_editor->widget()));
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
const QSize sz = m_popupFrame->sizeHint();
|
|
|
|
|
QPoint pos = m_editor->cursorRect(m_startpos).topLeft();
|
|
|
|
|
pos.setY(pos.y() - sz.height() - 1);
|
|
|
|
|
|
|
|
|
|
if (pos.x() + sz.width() > screen.right())
|
|
|
|
|
pos.setX(screen.right() - sz.width());
|
|
|
|
|
|
|
|
|
|
m_popupFrame->move(pos);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} } // end of namespace QmlJSEditor::Internal
|
|
|
|
|
|
2010-01-18 16:15:23 +01:00
|
|
|
QmlCodeCompletion::QmlCodeCompletion(QmlModelManagerInterface *modelManager, QmlJS::TypeSystem *typeSystem, QObject *parent)
|
2009-04-22 15:21:04 +02:00
|
|
|
: TextEditor::ICompletionCollector(parent),
|
2009-10-02 10:49:33 +02:00
|
|
|
m_modelManager(modelManager),
|
2009-04-22 15:21:04 +02:00
|
|
|
m_editor(0),
|
|
|
|
|
m_startPosition(0),
|
2009-12-07 16:57:31 +01:00
|
|
|
m_caseSensitivity(Qt::CaseSensitive),
|
|
|
|
|
m_typeSystem(typeSystem)
|
2009-10-02 10:49:33 +02:00
|
|
|
{
|
|
|
|
|
Q_ASSERT(modelManager);
|
2009-12-07 16:57:31 +01:00
|
|
|
Q_ASSERT(typeSystem);
|
2009-10-02 10:49:33 +02:00
|
|
|
}
|
2009-04-22 15:21:04 +02:00
|
|
|
|
2009-09-30 17:43:21 +02:00
|
|
|
QmlCodeCompletion::~QmlCodeCompletion()
|
2009-04-22 15:21:04 +02:00
|
|
|
{ }
|
|
|
|
|
|
2009-09-30 17:43:21 +02:00
|
|
|
Qt::CaseSensitivity QmlCodeCompletion::caseSensitivity() const
|
2009-04-22 15:21:04 +02:00
|
|
|
{ return m_caseSensitivity; }
|
|
|
|
|
|
2009-09-30 17:43:21 +02:00
|
|
|
void QmlCodeCompletion::setCaseSensitivity(Qt::CaseSensitivity caseSensitivity)
|
2009-04-22 15:21:04 +02:00
|
|
|
{ m_caseSensitivity = caseSensitivity; }
|
|
|
|
|
|
2010-01-26 11:44:45 +01:00
|
|
|
TextEditor::ITextEditable *QmlCodeCompletion::editor() const
|
|
|
|
|
{ return m_editor; }
|
|
|
|
|
|
|
|
|
|
int QmlCodeCompletion::startPosition() const
|
|
|
|
|
{ return m_startPosition; }
|
|
|
|
|
|
2009-09-30 17:43:21 +02:00
|
|
|
bool QmlCodeCompletion::supportsEditor(TextEditor::ITextEditable *editor)
|
2009-04-22 15:21:04 +02:00
|
|
|
{
|
2010-01-15 17:20:03 +01:00
|
|
|
if (qobject_cast<QmlJSTextEditor *>(editor->widget()))
|
2009-04-22 15:21:04 +02:00
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2010-01-22 10:57:48 +01:00
|
|
|
bool QmlCodeCompletion::triggersCompletion(TextEditor::ITextEditable *editor)
|
|
|
|
|
{
|
|
|
|
|
const QChar ch = editor->characterAt(editor->position() - 1);
|
|
|
|
|
|
|
|
|
|
if (ch == QLatin1Char('(') || ch == QLatin1Char('.'))
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2009-04-22 15:21:04 +02:00
|
|
|
|
2010-01-25 17:13:04 +01:00
|
|
|
bool QmlCodeCompletion::isImported(Document::Ptr doc, const QString ¤tFilePath) const
|
|
|
|
|
{
|
|
|
|
|
if (! (doc && doc->qmlProgram()))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
QFileInfo fileInfo(doc->fileName());
|
|
|
|
|
const QString absolutePath = fileInfo.absolutePath();
|
|
|
|
|
|
|
|
|
|
for (AST::UiImportList *it = doc->qmlProgram()->imports; it; it = it->next) {
|
|
|
|
|
if (! (it->import && it->import->fileName))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
QString path = absolutePath;
|
|
|
|
|
path += QLatin1Char('/');
|
|
|
|
|
path += it->import->fileName->asString();
|
|
|
|
|
path = QDir::cleanPath(path);
|
|
|
|
|
if (path == currentFilePath)
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2010-01-26 12:36:04 +01:00
|
|
|
bool QmlCodeCompletion::isDelimiter(const QChar &ch) const
|
|
|
|
|
{
|
|
|
|
|
switch (ch.unicode()) {
|
|
|
|
|
case '{':
|
|
|
|
|
case '}':
|
|
|
|
|
case '[':
|
|
|
|
|
case ']':
|
|
|
|
|
case '?':
|
|
|
|
|
case ':':
|
|
|
|
|
case ';':
|
|
|
|
|
case ',':
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-09-30 17:43:21 +02:00
|
|
|
int QmlCodeCompletion::startCompletion(TextEditor::ITextEditable *editor)
|
2009-04-22 15:21:04 +02:00
|
|
|
{
|
|
|
|
|
m_editor = editor;
|
|
|
|
|
|
2010-01-15 17:20:03 +01:00
|
|
|
QmlJSTextEditor *edit = qobject_cast<QmlJSTextEditor *>(m_editor->widget());
|
2009-04-22 15:21:04 +02:00
|
|
|
if (! edit)
|
|
|
|
|
return -1;
|
|
|
|
|
|
2010-01-22 14:42:15 +01:00
|
|
|
m_startPosition = editor->position();
|
2010-01-24 18:11:25 +01:00
|
|
|
const QString fileName = editor->file()->fileName();
|
2009-04-22 15:21:04 +02:00
|
|
|
|
2010-01-24 18:11:25 +01:00
|
|
|
while (editor->characterAt(m_startPosition - 1).isLetterOrNumber() ||
|
|
|
|
|
editor->characterAt(m_startPosition - 1) == QLatin1Char('_'))
|
2010-01-22 14:42:15 +01:00
|
|
|
--m_startPosition;
|
2009-04-22 15:21:04 +02:00
|
|
|
|
|
|
|
|
m_completions.clear();
|
|
|
|
|
|
2010-01-24 18:11:25 +01:00
|
|
|
QmlJS::Snapshot snapshot = m_modelManager->snapshot();
|
2010-01-21 11:36:36 +01:00
|
|
|
|
2010-01-25 14:18:53 +01:00
|
|
|
SemanticInfo semanticInfo = edit->semanticInfo();
|
|
|
|
|
Document::Ptr qmlDocument = semanticInfo.document;
|
2010-01-26 14:50:52 +01:00
|
|
|
if (qmlDocument.isNull())
|
|
|
|
|
return -1;
|
2010-01-25 14:18:53 +01:00
|
|
|
|
|
|
|
|
const QFileInfo currentFileInfo(fileName);
|
2010-01-22 09:37:44 +01:00
|
|
|
const QString currentFilePath = currentFileInfo.absolutePath();
|
|
|
|
|
|
2010-01-26 11:51:44 +01:00
|
|
|
bool isQmlFile = false;
|
|
|
|
|
if (currentFileInfo.suffix() == QLatin1String("qml"))
|
|
|
|
|
isQmlFile = true;
|
|
|
|
|
|
2010-01-24 18:11:25 +01:00
|
|
|
const QIcon componentIcon = iconForColor(Qt::yellow);
|
2010-01-24 17:33:07 +01:00
|
|
|
const QIcon symbolIcon = iconForColor(Qt::darkCyan);
|
|
|
|
|
|
|
|
|
|
Interpreter::Engine interp;
|
|
|
|
|
|
2010-01-26 14:50:52 +01:00
|
|
|
QHash<QString, Document::Ptr> userComponents; // ####
|
2010-01-25 15:49:21 +01:00
|
|
|
|
2010-01-24 17:33:07 +01:00
|
|
|
foreach (Document::Ptr doc, snapshot) {
|
|
|
|
|
const QFileInfo fileInfo(doc->fileName());
|
2010-01-25 17:13:04 +01:00
|
|
|
const QString absolutePath = fileInfo.absolutePath();
|
2010-01-24 17:33:07 +01:00
|
|
|
|
2010-01-25 17:13:04 +01:00
|
|
|
// ### generalize
|
2010-01-24 17:33:07 +01:00
|
|
|
if (fileInfo.suffix() != QLatin1String("qml"))
|
|
|
|
|
continue;
|
2010-01-25 17:13:04 +01:00
|
|
|
else if (absolutePath != currentFilePath && ! isImported(qmlDocument, absolutePath))
|
2010-01-24 17:33:07 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
const QString typeName = fileInfo.baseName();
|
2010-01-25 17:13:04 +01:00
|
|
|
if (typeName.isEmpty() || ! typeName.at(0).isUpper())
|
2010-01-24 17:33:07 +01:00
|
|
|
continue;
|
|
|
|
|
|
2010-01-25 15:49:21 +01:00
|
|
|
userComponents.insert(typeName, doc);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (Document::Ptr doc, snapshot) {
|
|
|
|
|
const QFileInfo fileInfo(doc->fileName());
|
2010-01-25 17:13:04 +01:00
|
|
|
const QString absolutePath = fileInfo.absolutePath();
|
2010-01-25 15:49:21 +01:00
|
|
|
|
2010-01-25 17:13:04 +01:00
|
|
|
// ### generalize
|
2010-01-25 15:49:21 +01:00
|
|
|
if (fileInfo.suffix() != QLatin1String("qml"))
|
|
|
|
|
continue;
|
2010-01-25 17:13:04 +01:00
|
|
|
else if (absolutePath != currentFilePath && ! isImported(qmlDocument, absolutePath))
|
2010-01-25 15:49:21 +01:00
|
|
|
continue;
|
|
|
|
|
|
2010-01-26 14:50:52 +01:00
|
|
|
#if 0
|
2010-01-24 17:33:07 +01:00
|
|
|
QMapIterator<QString, IdSymbol *> it(doc->ids());
|
|
|
|
|
while (it.hasNext()) {
|
|
|
|
|
it.next();
|
|
|
|
|
|
|
|
|
|
if (IdSymbol *symbol = it.value()) {
|
|
|
|
|
const QString id = it.key();
|
|
|
|
|
|
|
|
|
|
if (symbol->parentNode()) {
|
|
|
|
|
const QString component = symbol->parentNode()->name();
|
2010-01-24 18:11:25 +01:00
|
|
|
|
2010-01-25 15:49:21 +01:00
|
|
|
if (const Interpreter::ObjectValue *object = newComponent(&interp, component, userComponents)) {
|
2010-01-24 17:33:07 +01:00
|
|
|
interp.globalObject()->setProperty(id, object);
|
2010-01-25 15:49:21 +01:00
|
|
|
}
|
2010-01-24 17:33:07 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-01-26 14:50:52 +01:00
|
|
|
#endif
|
2010-01-24 17:33:07 +01:00
|
|
|
}
|
2010-01-21 11:36:36 +01:00
|
|
|
|
2010-01-24 18:11:25 +01:00
|
|
|
// Set up the current scope chain.
|
2010-01-26 11:51:44 +01:00
|
|
|
Interpreter::ObjectValue *scope = interp.globalObject();
|
|
|
|
|
|
|
|
|
|
if (isQmlFile) {
|
|
|
|
|
scope = interp.newObject(/* prototype = */ 0);
|
2010-01-24 18:11:25 +01:00
|
|
|
|
2010-01-26 11:51:44 +01:00
|
|
|
AST::UiObjectMember *declaringMember = 0;
|
|
|
|
|
AST::UiObjectMember *parentMember = 0;
|
2010-01-24 18:11:25 +01:00
|
|
|
|
2010-01-26 11:51:44 +01:00
|
|
|
const int cursorPosition = editor->position();
|
|
|
|
|
foreach (const Range &range, semanticInfo.ranges) {
|
|
|
|
|
if (cursorPosition >= range.begin.position() && cursorPosition <= range.end.position()) {
|
|
|
|
|
parentMember = declaringMember;
|
|
|
|
|
declaringMember = range.ast;
|
|
|
|
|
}
|
2010-01-24 19:31:38 +01:00
|
|
|
}
|
2010-01-24 18:11:25 +01:00
|
|
|
|
2010-01-26 14:50:52 +01:00
|
|
|
#if 0
|
2010-01-26 11:51:44 +01:00
|
|
|
// ### TODO: remove me. This is just a quick and dirty hack to get some completion
|
|
|
|
|
// for the property definitions.
|
|
|
|
|
SearchPropertyDefinitions searchPropertyDefinitions;
|
2010-01-24 19:31:38 +01:00
|
|
|
|
2010-01-26 11:51:44 +01:00
|
|
|
const QList<AST::UiPublicMember *> properties = searchPropertyDefinitions(qmlDocument);
|
|
|
|
|
foreach (AST::UiPublicMember *prop, properties) {
|
|
|
|
|
if (! (prop->name && prop->memberType))
|
|
|
|
|
continue;
|
2010-01-24 19:31:38 +01:00
|
|
|
|
2010-01-26 11:51:44 +01:00
|
|
|
const QString propName = prop->name->asString();
|
|
|
|
|
const QString propType = prop->memberType->asString();
|
2010-01-24 19:31:38 +01:00
|
|
|
|
2010-01-26 11:51:44 +01:00
|
|
|
interp.globalObject()->setProperty(propName, interp.defaultValueForBuiltinType(propType));
|
|
|
|
|
}
|
2010-01-24 19:31:38 +01:00
|
|
|
|
2010-01-26 11:51:44 +01:00
|
|
|
// Get the name of the declaring item.
|
|
|
|
|
QString declaringItemName = QLatin1String("Item");
|
2010-01-24 19:31:38 +01:00
|
|
|
|
2010-01-26 11:51:44 +01:00
|
|
|
if (AST::UiObjectDefinition *binding = AST::cast<AST::UiObjectDefinition *>(declaringMember))
|
|
|
|
|
declaringItemName = qualifiedNameId(binding->qualifiedTypeNameId);
|
|
|
|
|
else if (AST::UiObjectBinding *binding = AST::cast<AST::UiObjectBinding *>(declaringMember))
|
|
|
|
|
declaringItemName = qualifiedNameId(binding->qualifiedTypeNameId);
|
2010-01-24 19:31:38 +01:00
|
|
|
|
2010-01-26 11:51:44 +01:00
|
|
|
Interpreter::ObjectValue *declaringItem = newComponent(&interp, declaringItemName, userComponents);
|
|
|
|
|
if (! declaringItem)
|
|
|
|
|
declaringItem = interp.newQmlObject(QLatin1String("Item"));
|
2010-01-24 19:31:38 +01:00
|
|
|
|
2010-01-26 11:51:44 +01:00
|
|
|
if (declaringItem) {
|
|
|
|
|
scope->setScope(declaringItem);
|
|
|
|
|
declaringItem->setScope(interp.globalObject());
|
|
|
|
|
}
|
2010-01-24 19:31:38 +01:00
|
|
|
|
2010-01-26 11:51:44 +01:00
|
|
|
// Get the name of the parent of the declaring item.
|
|
|
|
|
QString parentItemName = QLatin1String("Item");
|
2010-01-24 19:31:38 +01:00
|
|
|
|
2010-01-26 11:51:44 +01:00
|
|
|
if (AST::UiObjectDefinition *binding = AST::cast<AST::UiObjectDefinition *>(parentMember))
|
|
|
|
|
parentItemName = qualifiedNameId(binding->qualifiedTypeNameId);
|
|
|
|
|
else if (AST::UiObjectBinding *binding = AST::cast<AST::UiObjectBinding *>(parentMember))
|
|
|
|
|
parentItemName = qualifiedNameId(binding->qualifiedTypeNameId);
|
2010-01-24 19:31:38 +01:00
|
|
|
|
2010-01-26 11:51:44 +01:00
|
|
|
Interpreter::ObjectValue *parentItem = newComponent(&interp, parentItemName, userComponents);
|
|
|
|
|
if (! parentItem)
|
|
|
|
|
parentItem = interp.newQmlObject(QLatin1String("Item"));
|
2010-01-24 19:31:38 +01:00
|
|
|
|
2010-01-26 11:51:44 +01:00
|
|
|
if (parentItem)
|
|
|
|
|
scope->setProperty(QLatin1String("parent"), parentItem);
|
2010-01-26 14:50:52 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
Bind bind;
|
|
|
|
|
scope = bind(qmlDocument, snapshot, declaringMember, interp);
|
2010-01-26 11:51:44 +01:00
|
|
|
}
|
2010-01-24 19:31:38 +01:00
|
|
|
|
2010-01-24 18:11:25 +01:00
|
|
|
// Search for the operator that triggered the completion.
|
2010-01-22 10:57:48 +01:00
|
|
|
QChar completionOperator;
|
2010-01-21 17:08:19 +01:00
|
|
|
if (m_startPosition > 0)
|
2010-01-22 10:57:48 +01:00
|
|
|
completionOperator = editor->characterAt(m_startPosition - 1);
|
2010-01-21 17:08:19 +01:00
|
|
|
|
2010-01-26 12:36:04 +01:00
|
|
|
if (completionOperator.isSpace() || completionOperator.isNull() || isDelimiter(completionOperator) ||
|
|
|
|
|
(completionOperator == QLatin1Char('(') && m_startPosition != editor->position())) {
|
2010-01-25 16:52:10 +01:00
|
|
|
// It's a global completion.
|
2010-01-24 18:11:25 +01:00
|
|
|
// Process the visible user defined components.
|
2010-01-26 09:54:02 +01:00
|
|
|
QHashIterator<QString, Document::Ptr> componentIt(userComponents);
|
|
|
|
|
while (componentIt.hasNext()) {
|
|
|
|
|
componentIt.next();
|
|
|
|
|
TextEditor::CompletionItem item(this);
|
|
|
|
|
item.text = componentIt.key();
|
|
|
|
|
item.icon = componentIcon;
|
|
|
|
|
m_completions.append(item);
|
2010-01-22 10:26:25 +01:00
|
|
|
}
|
|
|
|
|
|
2010-01-24 17:33:07 +01:00
|
|
|
EnumerateProperties enumerateProperties;
|
2010-01-26 10:10:11 +01:00
|
|
|
enumerateProperties.setGlobalCompletion(true);
|
2010-01-24 18:11:25 +01:00
|
|
|
QHashIterator<QString, const Interpreter::Value *> it(enumerateProperties(scope, /* lookAtScope = */ true));
|
2010-01-24 17:33:07 +01:00
|
|
|
while (it.hasNext()) {
|
|
|
|
|
it.next();
|
2010-01-21 11:21:55 +01:00
|
|
|
|
2010-01-21 17:08:19 +01:00
|
|
|
TextEditor::CompletionItem item(this);
|
2010-01-24 17:33:07 +01:00
|
|
|
item.text = it.key();
|
|
|
|
|
item.icon = symbolIcon;
|
2010-01-21 17:08:19 +01:00
|
|
|
m_completions.append(item);
|
|
|
|
|
}
|
2010-01-21 11:21:55 +01:00
|
|
|
}
|
|
|
|
|
|
2010-01-24 17:33:07 +01:00
|
|
|
else if (completionOperator == QLatin1Char('.') || completionOperator == QLatin1Char('(')) {
|
2010-01-24 18:11:25 +01:00
|
|
|
// Look at the expression under cursor.
|
2010-01-22 14:42:15 +01:00
|
|
|
QTextCursor tc = edit->textCursor();
|
|
|
|
|
tc.setPosition(m_startPosition - 1);
|
2010-01-22 10:26:25 +01:00
|
|
|
|
2010-01-24 18:11:25 +01:00
|
|
|
ExpressionUnderCursor expressionUnderCursor;
|
2010-01-22 14:42:15 +01:00
|
|
|
const QString expression = expressionUnderCursor(tc);
|
2010-01-22 10:26:25 +01:00
|
|
|
//qDebug() << "expression:" << expression;
|
|
|
|
|
|
2010-01-24 18:11:25 +01:00
|
|
|
// Wrap the expression in a QML document.
|
2010-01-24 17:33:07 +01:00
|
|
|
QmlJS::Document::Ptr exprDoc = Document::create(QLatin1String("<expression>"));
|
2010-01-22 10:26:25 +01:00
|
|
|
exprDoc->setSource(expression);
|
|
|
|
|
exprDoc->parseExpression();
|
2010-01-21 17:08:19 +01:00
|
|
|
|
2010-01-26 14:53:11 +01:00
|
|
|
if (exprDoc->expression() != 0) {
|
|
|
|
|
Check evaluate(&interp);
|
2010-01-21 11:21:55 +01:00
|
|
|
|
2010-01-24 18:11:25 +01:00
|
|
|
// Evaluate the expression under cursor.
|
2010-01-26 14:53:11 +01:00
|
|
|
const Interpreter::Value *value = interp.convertToObject(evaluate(exprDoc->expression(), scope));
|
2010-01-22 10:26:25 +01:00
|
|
|
//qDebug() << "type:" << interp.typeId(value);
|
|
|
|
|
|
2010-01-22 10:57:48 +01:00
|
|
|
if (value && completionOperator == QLatin1Char('.')) { // member completion
|
|
|
|
|
EnumerateProperties enumerateProperties;
|
|
|
|
|
QHashIterator<QString, const Interpreter::Value *> it(enumerateProperties(value));
|
|
|
|
|
while (it.hasNext()) {
|
|
|
|
|
it.next();
|
|
|
|
|
|
|
|
|
|
TextEditor::CompletionItem item(this);
|
|
|
|
|
item.text = it.key();
|
|
|
|
|
item.icon = symbolIcon;
|
|
|
|
|
m_completions.append(item);
|
|
|
|
|
}
|
2010-01-25 16:52:10 +01:00
|
|
|
} else if (value && completionOperator == QLatin1Char('(') && m_startPosition == editor->position()) {
|
|
|
|
|
// function completion
|
2010-01-22 10:57:48 +01:00
|
|
|
if (const Interpreter::FunctionValue *f = value->asFunctionValue()) {
|
|
|
|
|
QString functionName = expression;
|
|
|
|
|
int indexOfDot = expression.lastIndexOf(QLatin1Char('.'));
|
|
|
|
|
if (indexOfDot != -1)
|
|
|
|
|
functionName = expression.mid(indexOfDot + 1);
|
2010-01-22 14:42:15 +01:00
|
|
|
|
2010-01-22 10:57:48 +01:00
|
|
|
// Recreate if necessary
|
|
|
|
|
if (!m_functionArgumentWidget)
|
|
|
|
|
m_functionArgumentWidget = new QmlJSEditor::Internal::FunctionArgumentWidget;
|
|
|
|
|
|
2010-01-26 10:50:30 +01:00
|
|
|
QStringList signature;
|
|
|
|
|
for (int i = 0; i < f->argumentCount(); ++i)
|
|
|
|
|
signature.append(f->argumentName(i));
|
|
|
|
|
|
|
|
|
|
m_functionArgumentWidget->showFunctionHint(functionName.trimmed(),
|
|
|
|
|
signature,
|
2010-01-24 17:33:07 +01:00
|
|
|
m_startPosition);
|
2010-01-22 10:57:48 +01:00
|
|
|
}
|
|
|
|
|
|
2010-01-24 17:33:07 +01:00
|
|
|
return -1; // We always return -1 when completing function prototypes.
|
2009-07-06 16:29:44 +02:00
|
|
|
}
|
|
|
|
|
}
|
2010-01-22 10:26:25 +01:00
|
|
|
|
|
|
|
|
if (! m_completions.isEmpty())
|
|
|
|
|
return m_startPosition;
|
|
|
|
|
|
|
|
|
|
return -1;
|
2009-07-06 16:29:44 +02:00
|
|
|
}
|
|
|
|
|
|
2010-01-26 11:51:44 +01:00
|
|
|
if (isQmlFile) {
|
|
|
|
|
if (completionOperator.isNull()
|
|
|
|
|
|| completionOperator.isSpace()
|
|
|
|
|
|| completionOperator == QLatin1Char('{')
|
|
|
|
|
|| completionOperator == QLatin1Char('}')
|
|
|
|
|
|| completionOperator == QLatin1Char(':')
|
|
|
|
|
|| completionOperator == QLatin1Char(';')) {
|
|
|
|
|
updateSnippets();
|
|
|
|
|
m_completions.append(m_snippets);
|
|
|
|
|
}
|
2010-01-21 11:36:36 +01:00
|
|
|
}
|
2010-01-19 16:36:48 +01:00
|
|
|
|
2010-01-22 14:42:15 +01:00
|
|
|
if (! m_completions.isEmpty())
|
|
|
|
|
return m_startPosition;
|
|
|
|
|
|
|
|
|
|
return -1;
|
2009-04-22 15:21:04 +02:00
|
|
|
}
|
|
|
|
|
|
2009-09-30 17:43:21 +02:00
|
|
|
void QmlCodeCompletion::completions(QList<TextEditor::CompletionItem> *completions)
|
2009-04-22 15:21:04 +02:00
|
|
|
{
|
|
|
|
|
const int length = m_editor->position() - m_startPosition;
|
|
|
|
|
|
|
|
|
|
if (length == 0)
|
|
|
|
|
*completions = m_completions;
|
|
|
|
|
else if (length > 0) {
|
|
|
|
|
const QString key = m_editor->textAt(m_startPosition, length);
|
|
|
|
|
|
2010-01-26 11:44:45 +01:00
|
|
|
filter(m_completions, completions, key, FirstLetterCaseSensitive);
|
2009-04-22 15:21:04 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-09-30 17:43:21 +02:00
|
|
|
void QmlCodeCompletion::complete(const TextEditor::CompletionItem &item)
|
2009-04-22 15:21:04 +02:00
|
|
|
{
|
2010-01-19 16:36:48 +01:00
|
|
|
QString toInsert = item.text;
|
|
|
|
|
|
|
|
|
|
if (QmlJSTextEditor *edit = qobject_cast<QmlJSTextEditor *>(m_editor->widget())) {
|
|
|
|
|
if (item.data.isValid()) {
|
|
|
|
|
QTextCursor tc = edit->textCursor();
|
|
|
|
|
tc.beginEditBlock();
|
|
|
|
|
tc.setPosition(m_startPosition);
|
|
|
|
|
tc.setPosition(m_editor->position(), QTextCursor::KeepAnchor);
|
|
|
|
|
tc.removeSelectedText();
|
|
|
|
|
|
|
|
|
|
toInsert = item.data.toString();
|
|
|
|
|
edit->insertCodeSnippet(toInsert);
|
|
|
|
|
tc.endEditBlock();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-04-22 15:21:04 +02:00
|
|
|
const int length = m_editor->position() - m_startPosition;
|
|
|
|
|
m_editor->setCurPos(m_startPosition);
|
|
|
|
|
m_editor->replace(length, toInsert);
|
|
|
|
|
}
|
|
|
|
|
|
2009-09-30 17:43:21 +02:00
|
|
|
bool QmlCodeCompletion::partiallyComplete(const QList<TextEditor::CompletionItem> &completionItems)
|
2009-04-22 15:21:04 +02:00
|
|
|
{
|
|
|
|
|
if (completionItems.count() == 1) {
|
2010-01-21 10:56:58 +01:00
|
|
|
const TextEditor::CompletionItem item = completionItems.first();
|
2009-04-22 15:21:04 +02:00
|
|
|
|
2010-01-21 10:56:58 +01:00
|
|
|
if (!item.data.canConvert<QString>()) {
|
|
|
|
|
complete(item);
|
|
|
|
|
return true;
|
2009-04-22 15:21:04 +02:00
|
|
|
}
|
|
|
|
|
}
|
2010-01-21 10:56:58 +01:00
|
|
|
|
2010-01-26 11:44:45 +01:00
|
|
|
return TextEditor::ICompletionCollector::partiallyComplete(completionItems);
|
2009-04-22 15:21:04 +02:00
|
|
|
}
|
|
|
|
|
|
2009-09-30 17:43:21 +02:00
|
|
|
void QmlCodeCompletion::cleanup()
|
2009-04-22 15:21:04 +02:00
|
|
|
{
|
|
|
|
|
m_editor = 0;
|
|
|
|
|
m_startPosition = 0;
|
|
|
|
|
m_completions.clear();
|
|
|
|
|
}
|
|
|
|
|
|
2010-01-19 18:50:50 +01:00
|
|
|
|
|
|
|
|
void QmlCodeCompletion::updateSnippets()
|
|
|
|
|
{
|
|
|
|
|
QString qmlsnippets = Core::ICore::instance()->resourcePath() + QLatin1String("/snippets/qml.xml");
|
|
|
|
|
if (!QFile::exists(qmlsnippets))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
QDateTime lastModified = QFileInfo(qmlsnippets).lastModified();
|
|
|
|
|
if (!m_snippetFileLastModified.isNull() && lastModified == m_snippetFileLastModified)
|
|
|
|
|
return;
|
|
|
|
|
|
2010-01-21 10:56:58 +01:00
|
|
|
const QIcon icon = iconForColor(Qt::red);
|
|
|
|
|
|
2010-01-19 18:50:50 +01:00
|
|
|
m_snippetFileLastModified = lastModified;
|
|
|
|
|
QFile file(qmlsnippets);
|
|
|
|
|
file.open(QIODevice::ReadOnly);
|
|
|
|
|
QXmlStreamReader xml(&file);
|
2010-01-22 15:52:28 +01:00
|
|
|
if (xml.readNextStartElement()) {
|
2010-01-19 18:50:50 +01:00
|
|
|
if (xml.name() == QLatin1String("snippets")) {
|
|
|
|
|
while (xml.readNextStartElement()) {
|
|
|
|
|
if (xml.name() == QLatin1String("snippet")) {
|
|
|
|
|
TextEditor::CompletionItem item(this);
|
|
|
|
|
QString title, data;
|
|
|
|
|
QString description = xml.attributes().value("description").toString();
|
|
|
|
|
|
|
|
|
|
while (!xml.atEnd()) {
|
|
|
|
|
xml.readNext();
|
|
|
|
|
if (xml.isEndElement()) {
|
|
|
|
|
int i = 0;
|
|
|
|
|
while (i < data.size() && data.at(i).isLetterOrNumber())
|
|
|
|
|
++i;
|
|
|
|
|
title = data.left(i);
|
|
|
|
|
item.text = title;
|
|
|
|
|
if (!description.isEmpty()) {
|
|
|
|
|
item.text += QLatin1Char(' ');
|
|
|
|
|
item.text += description;
|
|
|
|
|
}
|
|
|
|
|
item.data = QVariant::fromValue(data);
|
2010-01-21 10:56:58 +01:00
|
|
|
item.icon = icon;
|
2010-01-19 18:50:50 +01:00
|
|
|
m_snippets.append(item);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (xml.isCharacters())
|
|
|
|
|
data += xml.text();
|
|
|
|
|
else if (xml.isStartElement()) {
|
|
|
|
|
if (xml.name() != QLatin1String("tab"))
|
|
|
|
|
xml.raiseError(QLatin1String("invalid snippets file"));
|
|
|
|
|
else {
|
|
|
|
|
data += QChar::ObjectReplacementCharacter;
|
|
|
|
|
data += xml.readElementText();
|
|
|
|
|
data += QChar::ObjectReplacementCharacter;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-01-22 15:52:28 +01:00
|
|
|
} else {
|
|
|
|
|
xml.skipCurrentElement();
|
2010-01-19 18:50:50 +01:00
|
|
|
}
|
|
|
|
|
}
|
2010-01-22 15:52:28 +01:00
|
|
|
} else {
|
|
|
|
|
xml.skipCurrentElement();
|
2010-01-19 18:50:50 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (xml.hasError())
|
2010-01-22 15:52:28 +01:00
|
|
|
qWarning() << qmlsnippets << xml.errorString() << xml.lineNumber() << xml.columnNumber();
|
2010-01-19 18:50:50 +01:00
|
|
|
file.close();
|
|
|
|
|
}
|
2010-01-21 12:15:18 +01:00
|
|
|
|
|
|
|
|
QList<TextEditor::CompletionItem> QmlCodeCompletion::getCompletions()
|
|
|
|
|
{
|
|
|
|
|
QList<TextEditor::CompletionItem> completionItems;
|
|
|
|
|
|
|
|
|
|
completions(&completionItems);
|
|
|
|
|
|
|
|
|
|
qStableSort(completionItems.begin(), completionItems.end(), completionItemLessThan);
|
|
|
|
|
|
|
|
|
|
// Remove duplicates
|
|
|
|
|
QString lastKey;
|
|
|
|
|
QList<TextEditor::CompletionItem> uniquelist;
|
|
|
|
|
|
|
|
|
|
foreach (const TextEditor::CompletionItem &item, completionItems) {
|
|
|
|
|
if (item.text != lastKey) {
|
|
|
|
|
uniquelist.append(item);
|
|
|
|
|
lastKey = item.text;
|
|
|
|
|
} else {
|
|
|
|
|
if (item.data.canConvert<QString>())
|
|
|
|
|
uniquelist.append(item);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return uniquelist;
|
|
|
|
|
}
|