From 84e6cfcd52f73a8e5b545f8d8eab0869fcffdec0 Mon Sep 17 00:00:00 2001 From: Roberto Raggi Date: Tue, 12 Jan 2010 14:30:54 +0100 Subject: [PATCH] Look at the token under cursor when checking if the context allows auto parentheses. --- src/plugins/qmleditor/qmleditor.cpp | 28 +++++++++++++++++-- .../qscriptincrementalscanner.h | 1 + 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/plugins/qmleditor/qmleditor.cpp b/src/plugins/qmleditor/qmleditor.cpp index 2278b34caa5..cc32071f73a 100644 --- a/src/plugins/qmleditor/qmleditor.cpp +++ b/src/plugins/qmleditor/qmleditor.cpp @@ -721,8 +721,32 @@ void QmlTextEditor::unCommentSelection() bool QmlTextEditor::contextAllowsAutoParentheses(const QTextCursor &cursor, const QString &textToInsert) const { - if (isInComment(cursor)) - return false; + const QString blockText = cursor.block().text(); + const int blockState = blockStartState(cursor.block()); + + QScriptIncrementalScanner tokenize; + const QList tokens = tokenize(blockText, blockState); + const int pos = cursor.columnNumber(); + + int tokenIndex = tokens.size() - 1; + for (; tokenIndex != -1; --tokenIndex) { + const QScriptIncrementalScanner::Token &token = tokens.at(tokenIndex); + if (pos >= token.begin() && pos < token.end()) + break; + } + + if (tokenIndex != -1) { + const QScriptIncrementalScanner::Token &token = tokens.at(tokenIndex); + + switch (token.kind) { + case QScriptIncrementalScanner::Token::Comment: + case QScriptIncrementalScanner::Token::String: + return false; + + default: + break; + } // end of switch + } QChar ch; diff --git a/src/shared/qscripthighlighter/qscriptincrementalscanner.h b/src/shared/qscripthighlighter/qscriptincrementalscanner.h index bf1bec0de07..0c1bd41cae0 100644 --- a/src/shared/qscripthighlighter/qscriptincrementalscanner.h +++ b/src/shared/qscripthighlighter/qscriptincrementalscanner.h @@ -65,6 +65,7 @@ public: } kind; inline Token(int o, int l, Kind k): offset(o), length(l), kind(k) {} + inline int begin() const { return offset; } inline int end() const { return offset + length; } inline bool is(int k) const { return k == kind; } inline bool isNot(int k) const { return k != kind; }