Don't activate the completion when the token under cursor is a comment or a string literal.

This commit is contained in:
Roberto Raggi
2010-02-01 11:55:19 +01:00
parent d514949ad5
commit 4880ae5b94
2 changed files with 35 additions and 1 deletions

View File

@@ -518,13 +518,46 @@ static bool isIdentifierChar(QChar ch)
}
bool QmlCodeCompletion::triggersCompletion(TextEditor::ITextEditable *editor)
{
if (maybeTriggersCompletion(editor)) {
// check the token under cursor
if (QmlJSTextEditor *ed = qobject_cast<QmlJSTextEditor *>(editor->widget())) {
QTextCursor tc = ed->textCursor();
QTextBlock block = tc.block();
const int column = tc.columnNumber();
const int blockState = qMax(0, block.previous().userState()) & 0xff;
const QString blockText = block.text();
QmlJSScanner scanner;
const QList<Token> tokens = scanner(blockText, blockState);
foreach (const Token &tk, tokens) {
if (column >= tk.begin() && column <= tk.end()) {
if (tk.is(Token::Comment) || tk.is(Token::String))
return false;
else
break;
}
}
}
return true;
}
return false;
}
bool QmlCodeCompletion::maybeTriggersCompletion(TextEditor::ITextEditable *editor)
{
const int cursorPosition = editor->position();
const QChar characterUnderCursor = editor->characterAt(cursorPosition);
const QChar ch = editor->characterAt(cursorPosition - 1);
if (ch == QLatin1Char('(') || ch == QLatin1Char('.'))
return true;
else if (isIdentifierChar(ch)) {
else if (isIdentifierChar(ch) && (characterUnderCursor.isSpace() ||
characterUnderCursor.isNull() ||
isDelimiter(characterUnderCursor))) {
int pos = editor->position() - 1;
for (; pos != -1; --pos) {
if (! isIdentifierChar(editor->characterAt(pos)))

View File

@@ -74,6 +74,7 @@ public:
private:
void updateSnippets();
bool maybeTriggersCompletion(TextEditor::ITextEditable *editor);
bool isDelimiter(const QChar &ch) const;
QmlModelManagerInterface *m_modelManager;