OSDN Git Service

It's 2011 now.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / qmljseditor / qmlexpressionundercursor.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 **
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file.  Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights.  These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33
34 #include "qmlexpressionundercursor.h"
35
36 #include <qmljs/parser/qmljsast_p.h>
37 #include <qmljs/qmljsscanner.h>
38
39 #include <QtGui/QTextBlock>
40
41 #include <QDebug>
42
43 using namespace QmlJS;
44 using namespace QmlJS::AST;
45
46 namespace {
47
48 class ExpressionUnderCursor
49 {
50     QTextCursor _cursor;
51     Scanner scanner;
52
53 public:
54     ExpressionUnderCursor()
55         : start(0), end(0)
56     {}
57
58     int start, end;
59
60     int startState(const QTextBlock &block) const
61     {
62         int state = block.previous().userState();
63         if (state == -1)
64             return 0;
65         return state & 0xff;
66     }
67
68     QString operator()(const QTextCursor &cursor)
69     {
70         return process(cursor);
71     }
72
73     int startOfExpression(const QList<Token> &tokens) const
74     {
75         return startOfExpression(tokens, tokens.size() - 1);
76     }
77
78     int startOfExpression(const QList<Token> &tokens, int index) const
79     {
80         if (index != -1) {
81             const Token &tk = tokens.at(index);
82
83             if (tk.is(Token::Identifier)) {
84                 if (index > 0 && tokens.at(index - 1).is(Token::Dot))
85                     index = startOfExpression(tokens, index - 2);
86
87             } else if (tk.is(Token::RightParenthesis)) {
88                 do { --index; }
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);
92
93             } else if (tk.is(Token::RightBracket)) {
94                 do { --index; }
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);
98             }
99         }
100
101         return index;
102     }
103
104     QString process(const QTextCursor &cursor)
105     {
106         _cursor = cursor;
107
108         QTextBlock block = _cursor.block();
109         const QString blockText = block.text().left(cursor.positionInBlock());
110
111         scanner.setScanComments(false);
112         const QList<Token> tokens = scanner(blockText, startState(block));
113         int start = startOfExpression(tokens);
114         if (start == -1)
115             return QString();
116
117         const Token &tk = tokens.at(start);
118         return blockText.mid(tk.begin(), tokens.last().end() - tk.begin());
119     }
120 };
121
122 } // enf of anonymous namespace
123
124 using namespace QmlJSEditor;
125 using namespace QmlJSEditor::Internal;
126
127 QmlExpressionUnderCursor::QmlExpressionUnderCursor()
128     : _expressionNode(0), _expressionOffset(0), _expressionLength(0)
129 {}
130
131 QmlJS::AST::ExpressionNode *QmlExpressionUnderCursor::operator()(const QTextCursor &cursor)
132 {
133     _expressionNode = 0;
134     _expressionOffset = -1;
135     _expressionLength = -1;
136
137     ExpressionUnderCursor expressionUnderCursor;
138     _text = expressionUnderCursor(cursor);
139
140     exprDoc = Document::create(QLatin1String("<expression>"));
141     exprDoc->setSource(_text);
142     exprDoc->parseExpression();
143
144     _expressionNode = exprDoc->expression();
145
146     _expressionOffset = cursor.block().position() + expressionUnderCursor.start;
147     _expressionLength = expressionUnderCursor.end - expressionUnderCursor.start;
148
149     return _expressionNode;
150 }
151
152 ExpressionNode *QmlExpressionUnderCursor::expressionNode() const
153 {
154     return _expressionNode;
155 }
156