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 **************************************************************************/
33 #include "BackwardsScanner.h"
35 #include <QtGui/QTextCursor>
36 #include <QTextDocument>
38 using namespace CPlusPlus;
40 BackwardsScanner::BackwardsScanner(const QTextCursor &cursor,
42 const QString &suffix,
46 , _block(cursor.block())
47 , _maxBlockCount(maxBlockCount)
49 _tokenize.setQtMocRunEnabled(true);
50 _tokenize.setSkipComments(skipComments);
51 _tokenize.setObjCEnabled(true);
52 _text = _block.text().left(cursor.position() - cursor.block().position());
54 if (! suffix.isEmpty())
57 _tokens.append(_tokenize(_text, previousBlockState(_block)));
59 _startToken = _tokens.size();
62 Token BackwardsScanner::LA(int index) const
63 { return const_cast<BackwardsScanner *>(this)->fetchToken(_startToken - index); }
65 Token BackwardsScanner::operator[](int index) const
66 { return const_cast<BackwardsScanner *>(this)->fetchToken(index); }
68 const Token &BackwardsScanner::fetchToken(int tokenIndex)
70 while (_offset + tokenIndex < 0) {
71 _block = _block.previous();
72 if (_blocksTokenized == _maxBlockCount || !_block.isValid()) {
74 _tokens.prepend(Token()); // sentinel
79 QString blockText = _block.text();
80 _text.prepend(QLatin1Char('\n'));
81 _text.prepend(blockText);
83 QList<Token> adaptedTokens;
84 for (int i = 0; i < _tokens.size(); ++i) {
85 Token t = _tokens.at(i);
86 t.offset += + blockText.length() + 1;
87 adaptedTokens.append(t);
90 _tokens = _tokenize(blockText, previousBlockState(_block));
91 _offset += _tokens.size();
92 _tokens += adaptedTokens;
96 return _tokens.at(_offset + tokenIndex);
99 int BackwardsScanner::startToken() const
100 { return _startToken; }
102 int BackwardsScanner::startPosition() const
103 { return _block.position(); }
105 QString BackwardsScanner::text() const
108 QString BackwardsScanner::mid(int index) const
110 const Token &firstToken = _tokens.at(index + _offset);
111 return _text.mid(firstToken.begin());
114 QString BackwardsScanner::text(int index) const
116 const Token &firstToken = _tokens.at(index + _offset);
117 return _text.mid(firstToken.begin(), firstToken.length());
120 QStringRef BackwardsScanner::textRef(int index) const
122 const Token &firstToken = _tokens.at(index + _offset);
123 return _text.midRef(firstToken.begin(), firstToken.length());
126 int BackwardsScanner::size() const
128 return _tokens.size();
131 int BackwardsScanner::startOfMatchingBrace(int index) const
133 const BackwardsScanner &tk = *this;
135 if (tk[index - 1].is(T_RPAREN)) {
139 if (tk[i].is(T_LPAREN)) {
142 } else if (tk[i].is(T_RPAREN))
145 } while (count != 0 && tk[i].isNot(T_EOF_SYMBOL));
146 } else if (tk[index - 1].is(T_RBRACKET)) {
150 if (tk[i].is(T_LBRACKET)) {
153 } else if (tk[i].is(T_RBRACKET))
156 } while (count != 0 && tk[i].isNot(T_EOF_SYMBOL));
157 } else if (tk[index - 1].is(T_RBRACE)) {
161 if (tk[i].is(T_LBRACE)) {
164 } else if (tk[i].is(T_RBRACE))
167 } while (count != 0 && tk[i].isNot(T_EOF_SYMBOL));
168 } else if (tk[index - 1].is(T_GREATER)) {
172 if (tk[i].is(T_LESS)) {
175 } else if (tk[i].is(T_GREATER))
178 } while (count != 0 && tk[i].isNot(T_EOF_SYMBOL));
186 int BackwardsScanner::startOfLine(int index) const
188 const BackwardsScanner tk(*this);
191 const Token &tok = tk[index - 1];
193 if (tok.is(T_EOF_SYMBOL))
195 else if (tok.newline())
204 int BackwardsScanner::startOfBlock(int index) const
206 const BackwardsScanner tk(*this);
208 const int start = index;
211 Token token = tk[index - 1];
213 if (token.is(T_EOF_SYMBOL)) {
216 } else if (token.is(T_GREATER)) {
217 const int matchingBrace = startOfMatchingBrace(index);
219 if (matchingBrace != index && tk[matchingBrace - 1].is(T_TEMPLATE))
220 index = matchingBrace;
222 } else if (token.is(T_RPAREN) || token.is(T_RBRACKET) || token.is(T_RBRACE)) {
223 const int matchingBrace = startOfMatchingBrace(index);
225 if (matchingBrace != index)
226 index = matchingBrace;
228 } else if (token.is(T_LPAREN) || token.is(T_LBRACKET)) {
229 break; // unmatched brace
231 } else if (token.is(T_LBRACE)) {
242 QString BackwardsScanner::indentationString(int index) const
244 const Token tokenAfterNewline = operator[](startOfLine(index + 1));
245 const int newlinePos = qMax(0, _text.lastIndexOf(QLatin1Char('\n'),
246 tokenAfterNewline.begin()));
247 return _text.mid(newlinePos, tokenAfterNewline.begin() - newlinePos);
251 int BackwardsScanner::previousBlockState(const QTextBlock &block)
253 const QTextBlock prevBlock = block.previous();
255 if (prevBlock.isValid()) {
256 int state = prevBlock.userState();