Commit e77544cc authored by Denis Steckelmacher's avatar Denis Steckelmacher

Disable the code-completion in comments

parent d1fef743
......@@ -44,10 +44,33 @@ namespace QmlJS {
CodeCompletionContext::CodeCompletionContext(const DUContextPointer& context, const QString& text,
const CursorInRevision& position, int depth)
: KDevelop::CodeCompletionContext(context, extractLastLine(text), position, depth)
: KDevelop::CodeCompletionContext(context, extractLastLine(text), position, depth),
m_completionKind(NormalCompletion)
{
// Determine which kind of completion should be offered
// ...
// Detect whether the cursor is in a comment
bool isLastLine = true;
for (int index = text.size()-1; index > 0; --index) {
const QChar c = text.at(index);
const QChar prev = text.at(index - 1);
if (c == QLatin1Char('\n')) {
isLastLine = false;
} else if (isLastLine && prev == QLatin1Char('/') && c == QLatin1Char('/')) {
// Single-line comment on the current line, we are in a comment
m_completionKind = CommentCompletion;
break;
} else if (prev == QLatin1Char('/') && c == QLatin1Char('*')) {
// Start of a multi-line comment encountered
m_completionKind = CommentCompletion;
break;
} else if (prev == QLatin1Char('*') && c == QLatin1Char('/')) {
// End of a multi-line comment. Because /* and */ cannot be nested,
// encountering a */ is enough to know that the cursor is outside a
// comment
break;
}
}
}
QList<CompletionTreeItemPointer> CodeCompletionContext::completionItems(bool& abort, bool fullCompletion)
......@@ -59,7 +82,7 @@ QList<CompletionTreeItemPointer> CodeCompletionContext::completionItems(bool& ab
QList<CompletionTreeItemPointer> items;
if (abort) {
if (abort || m_completionKind == CommentCompletion) {
return items;
}
......
......@@ -52,6 +52,14 @@ private:
CompletionItem::Decoration decoration);
bool containsOnlySpaces(const QString &str);
private:
enum CompletionKind {
NormalCompletion, /*!< @brief Completion in a code context */
CommentCompletion, /*!< @brief Completion in comments */
};
CompletionKind m_completionKind;
};
Q_DECLARE_OPERATORS_FOR_FLAGS(CodeCompletionContext::CompletionInContextFlags)
......
......@@ -174,6 +174,10 @@ void QmlCompletionTest::testContainsDeclaration_data()
QTest::addColumn<QString>("expectedItem");
QTest::addColumn<bool>("qml");
// Comments
QTest::newRow("js_outside_single_line_comment") << "var a // this is a comment;\n%INVOKE" << "%CURSOR" << "a" << false;
QTest::newRow("js_outside_multi_line_comment") << "var a;\n%INVOKE" << "/* comment */ %CURSOR" << "a" << false;
// Basic JS tests
QTest::newRow("js_basic_variable") << "var a;\n %INVOKE" << "%CURSOR" << "a" << false;
QTest::newRow("js_basic_function") << "function f();\n %INVOKE" << "%CURSOR" << "f" << false;
......@@ -245,6 +249,10 @@ void QmlCompletionTest::testDoesNotContainDeclaration_data()
QTest::addColumn<QString>("item");
QTest::addColumn<bool>("qml");
// Comments
QTest::newRow("js_in_single_line_comment") << "var a;\n%INVOKE" << "// %CURSOR" << "a" << false;
QTest::newRow("js_in_multi_line_comment") << "var a;\n%INVOKE" << "/* %CURSOR" << "a" << false;
// Don't show unreachable declarations when providing code-completions for object members
QTest::newRow("js_object_member_not_surrounding") << "var a; var b = {c: 0};%INVOKE" << "b.%CURSOR" << "a" << false;
QTest::newRow("js_object_member_local") << "var a = {b: 0};%INVOKE" << "%CURSOR" << "b" << false;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment