From 4880ae5b9456a78d456e66fb0eb805e88a526c92 Mon Sep 17 00:00:00 2001
From: Roberto Raggi <roberto.raggi@nokia.com>
Date: Mon, 1 Feb 2010 11:55:19 +0100
Subject: [PATCH] Don't activate the completion when the token under cursor is
 a comment or a string literal.

---
 src/plugins/qmljseditor/qmlcodecompletion.cpp | 35 ++++++++++++++++++-
 src/plugins/qmljseditor/qmlcodecompletion.h   |  1 +
 2 files changed, 35 insertions(+), 1 deletion(-)

diff --git a/src/plugins/qmljseditor/qmlcodecompletion.cpp b/src/plugins/qmljseditor/qmlcodecompletion.cpp
index dbee0df295f..ef67358972e 100644
--- a/src/plugins/qmljseditor/qmlcodecompletion.cpp
+++ b/src/plugins/qmljseditor/qmlcodecompletion.cpp
@@ -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)))
diff --git a/src/plugins/qmljseditor/qmlcodecompletion.h b/src/plugins/qmljseditor/qmlcodecompletion.h
index bc4b5ae3923..e5e5d95962e 100644
--- a/src/plugins/qmljseditor/qmlcodecompletion.h
+++ b/src/plugins/qmljseditor/qmlcodecompletion.h
@@ -74,6 +74,7 @@ public:
 private:
     void updateSnippets();
 
+    bool maybeTriggersCompletion(TextEditor::ITextEditable *editor);
     bool isDelimiter(const QChar &ch) const;
 
     QmlModelManagerInterface *m_modelManager;
-- 
GitLab