2010-01-04 11:30:14 +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-22 16:32:49 +02:00
|
|
|
#include "qmlexpressionundercursor.h"
|
|
|
|
|
2010-01-15 13:39:54 +01:00
|
|
|
#include <qmljs/parser/qmljsast_p.h>
|
2010-01-27 12:40:02 +01:00
|
|
|
#include <qmljs/qmljsscanner.h>
|
|
|
|
|
|
|
|
#include <QtGui/QTextBlock>
|
2009-12-02 17:59:27 +01:00
|
|
|
|
|
|
|
#include <QDebug>
|
|
|
|
|
2009-09-22 16:32:49 +02:00
|
|
|
using namespace QmlJS;
|
|
|
|
using namespace QmlJS::AST;
|
|
|
|
|
2010-01-15 17:20:03 +01:00
|
|
|
namespace QmlJSEditor {
|
2009-10-02 10:49:33 +02:00
|
|
|
namespace Internal {
|
|
|
|
|
2010-01-27 12:40:02 +01:00
|
|
|
class ExpressionUnderCursor
|
|
|
|
{
|
|
|
|
QTextCursor _cursor;
|
2010-02-15 12:56:03 +01:00
|
|
|
Scanner scanner;
|
2009-10-02 10:49:33 +02:00
|
|
|
|
2010-01-27 12:40:02 +01:00
|
|
|
public:
|
|
|
|
ExpressionUnderCursor()
|
|
|
|
: start(0), end(0)
|
|
|
|
{}
|
2009-10-02 10:49:33 +02:00
|
|
|
|
2010-01-27 12:40:02 +01:00
|
|
|
int start, end;
|
2009-10-02 10:49:33 +02:00
|
|
|
|
2010-01-27 12:40:02 +01:00
|
|
|
QString operator()(const QTextCursor &cursor)
|
2009-10-02 10:49:33 +02:00
|
|
|
{
|
2010-01-27 12:40:02 +01:00
|
|
|
_cursor = cursor;
|
2009-10-02 10:49:33 +02:00
|
|
|
|
2010-01-27 12:40:02 +01:00
|
|
|
QTextBlock block = _cursor.block();
|
|
|
|
const QString blockText = block.text().left(cursor.columnNumber());
|
|
|
|
//qDebug() << "block text:" << blockText;
|
2009-10-02 10:49:33 +02:00
|
|
|
|
2010-01-27 12:40:02 +01:00
|
|
|
int startState = block.previous().userState();
|
|
|
|
if (startState == -1)
|
|
|
|
startState = 0;
|
|
|
|
else
|
|
|
|
startState = startState & 0xff;
|
2009-10-02 10:49:33 +02:00
|
|
|
|
2010-01-27 12:40:02 +01:00
|
|
|
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);
|
2009-10-02 10:49:33 +02:00
|
|
|
|
2010-01-27 12:40:02 +01:00
|
|
|
if (tk.is(Token::Comment) || tk.is(Token::String) || tk.is(Token::Number))
|
|
|
|
continue;
|
2009-10-02 10:49:33 +02:00
|
|
|
|
2010-01-27 12:40:02 +01:00
|
|
|
if (! skipping) {
|
|
|
|
tokens.append(tk);
|
2009-10-02 10:49:33 +02:00
|
|
|
|
2010-01-27 12:40:02 +01:00
|
|
|
if (tk.is(Token::Identifier)) {
|
|
|
|
if (index > 0 && originalTokens.at(index - 1).isNot(Token::Dot))
|
|
|
|
break;
|
2009-10-02 10:49:33 +02:00
|
|
|
}
|
2010-01-27 12:40:02 +01:00
|
|
|
} else {
|
|
|
|
//qDebug() << "skip:" << blockText.mid(tk.offset, tk.length);
|
2009-10-02 10:49:33 +02:00
|
|
|
}
|
|
|
|
|
2010-01-27 12:40:02 +01:00
|
|
|
if (tk.is(Token::RightParenthesis) || tk.is(Token::RightBracket))
|
|
|
|
++skipping;
|
2009-10-02 10:49:33 +02:00
|
|
|
|
2010-01-27 12:40:02 +01:00
|
|
|
else if (tk.is(Token::LeftParenthesis) || tk.is(Token::LeftBracket)) {
|
|
|
|
--skipping;
|
2009-10-02 10:49:33 +02:00
|
|
|
|
2010-01-27 12:40:02 +01:00
|
|
|
if (! skipping)
|
|
|
|
tokens.append(tk);
|
2009-10-02 10:49:33 +02:00
|
|
|
|
2010-01-27 12:40:02 +01:00
|
|
|
if (index > 0 && originalTokens.at(index - 1).isNot(Token::Identifier))
|
|
|
|
break;
|
|
|
|
}
|
2009-10-02 10:49:33 +02:00
|
|
|
}
|
|
|
|
|
2010-01-27 12:40:02 +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));
|
2009-10-02 10:49:33 +02:00
|
|
|
|
2010-01-27 12:40:02 +01:00
|
|
|
}
|
|
|
|
start = tokens.first().begin();
|
|
|
|
end = tokens.first().end();
|
|
|
|
//qDebug() << "expression under cursor:" << expr;
|
|
|
|
return expr;
|
2009-10-02 10:49:33 +02:00
|
|
|
}
|
|
|
|
|
2010-01-27 12:40:02 +01:00
|
|
|
//qDebug() << "no expression";
|
|
|
|
return QString();
|
2009-10-02 10:49:33 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-15 17:20:03 +01:00
|
|
|
using namespace QmlJSEditor;
|
|
|
|
using namespace QmlJSEditor::Internal;
|
2009-10-02 10:49:33 +02:00
|
|
|
|
2009-09-22 16:32:49 +02:00
|
|
|
QmlExpressionUnderCursor::QmlExpressionUnderCursor()
|
2010-01-27 12:40:02 +01:00
|
|
|
: _expressionNode(0), _expressionOffset(0), _expressionLength(0)
|
|
|
|
{}
|
2009-09-22 16:32:49 +02:00
|
|
|
|
2010-01-27 12:40:02 +01:00
|
|
|
QmlJS::AST::ExpressionNode *QmlExpressionUnderCursor::operator()(const QTextCursor &cursor)
|
2009-09-22 16:32:49 +02:00
|
|
|
{
|
2009-09-22 17:25:18 +02:00
|
|
|
_expressionNode = 0;
|
2009-09-23 12:21:43 +02:00
|
|
|
_expressionOffset = -1;
|
|
|
|
_expressionLength = -1;
|
|
|
|
|
2010-01-27 12:40:02 +01:00
|
|
|
ExpressionUnderCursor expressionUnderCursor;
|
|
|
|
_text = expressionUnderCursor(cursor);
|
2009-09-23 12:21:43 +02:00
|
|
|
|
2010-01-27 12:40:02 +01:00
|
|
|
exprDoc = Document::create(QLatin1String("<expression>"));
|
|
|
|
exprDoc->setSource(_text);
|
|
|
|
exprDoc->parseExpression();
|
2009-09-23 12:21:43 +02:00
|
|
|
|
2010-01-27 12:40:02 +01:00
|
|
|
_expressionNode = exprDoc->expression();
|
2009-09-22 16:32:49 +02:00
|
|
|
|
2010-01-27 12:40:02 +01:00
|
|
|
_expressionOffset = cursor.block().position() + expressionUnderCursor.start;
|
|
|
|
_expressionLength = expressionUnderCursor.end - expressionUnderCursor.start;
|
2009-09-23 13:59:38 +02:00
|
|
|
|
2010-01-27 12:40:02 +01:00
|
|
|
return _expressionNode;
|
2009-09-22 16:32:49 +02:00
|
|
|
}
|
|
|
|
|
2010-01-27 12:40:02 +01:00
|
|
|
ExpressionNode *QmlExpressionUnderCursor::expressionNode() const
|
2009-09-22 16:32:49 +02:00
|
|
|
{
|
2010-01-27 12:40:02 +01:00
|
|
|
return _expressionNode;
|
2009-09-22 16:32:49 +02:00
|
|
|
}
|
|
|
|
|