1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights. These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
32 **************************************************************************/
34 #include "qmlexpressionundercursor.h"
36 #include <qmljs/parser/qmljsast_p.h>
37 #include <qmljs/qmljsscanner.h>
39 #include <QtGui/QTextBlock>
43 using namespace QmlJS;
44 using namespace QmlJS::AST;
48 class ExpressionUnderCursor
54 ExpressionUnderCursor()
60 int startState(const QTextBlock &block) const
62 int state = block.previous().userState();
68 QString operator()(const QTextCursor &cursor)
70 return process(cursor);
73 int startOfExpression(const QList<Token> &tokens) const
75 return startOfExpression(tokens, tokens.size() - 1);
78 int startOfExpression(const QList<Token> &tokens, int index) const
81 const Token &tk = tokens.at(index);
83 if (tk.is(Token::Identifier)) {
84 if (index > 0 && tokens.at(index - 1).is(Token::Dot))
85 index = startOfExpression(tokens, index - 2);
87 } else if (tk.is(Token::RightParenthesis)) {
89 while (index != -1 && tokens.at(index).isNot(Token::LeftParenthesis));
90 if (index > 0 && tokens.at(index - 1).is(Token::Identifier))
91 index = startOfExpression(tokens, index - 1);
93 } else if (tk.is(Token::RightBracket)) {
95 while (index != -1 && tokens.at(index).isNot(Token::LeftBracket));
96 if (index > 0 && tokens.at(index - 1).is(Token::Identifier))
97 index = startOfExpression(tokens, index - 1);
104 QString process(const QTextCursor &cursor)
108 QTextBlock block = _cursor.block();
109 const QString blockText = block.text().left(cursor.positionInBlock());
111 scanner.setScanComments(false);
112 const QList<Token> tokens = scanner(blockText, startState(block));
113 int start = startOfExpression(tokens);
117 const Token &tk = tokens.at(start);
118 return blockText.mid(tk.begin(), tokens.last().end() - tk.begin());
122 } // enf of anonymous namespace
124 using namespace QmlJSEditor;
125 using namespace QmlJSEditor::Internal;
127 QmlExpressionUnderCursor::QmlExpressionUnderCursor()
128 : _expressionNode(0), _expressionOffset(0), _expressionLength(0)
131 QmlJS::AST::ExpressionNode *QmlExpressionUnderCursor::operator()(const QTextCursor &cursor)
134 _expressionOffset = -1;
135 _expressionLength = -1;
137 ExpressionUnderCursor expressionUnderCursor;
138 _text = expressionUnderCursor(cursor);
140 exprDoc = Document::create(QLatin1String("<expression>"));
141 exprDoc->setSource(_text);
142 exprDoc->parseExpression();
144 _expressionNode = exprDoc->expression();
146 _expressionOffset = cursor.block().position() + expressionUnderCursor.start;
147 _expressionLength = expressionUnderCursor.end - expressionUnderCursor.start;
149 return _expressionNode;
152 ExpressionNode *QmlExpressionUnderCursor::expressionNode() const
154 return _expressionNode;