aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorSami Shalayel <sami.shalayel@qt.io>2023-09-11 15:50:26 +0200
committerSami Shalayel <sami.shalayel@qt.io>2023-09-26 20:46:38 +0200
commit8237998bf4e22571a2dd6d80ea897171fed762af (patch)
tree61ad1e5e5e803184463aa53b77b71849415f32fe /tests
parente3ffea6d42191e5b2d00aab9cfbd628913af4754 (diff)
qmlls: autocomplete scriptexpressions
Add some support for autocompletion in script expressions. Add missing getter in QQmlJSScope to be able to list js identifiers of a semantic scope in qmlls. Add static helper methods XXXCompletion that collect some information JavaScript identifiers, methods and properties made available from a QQmlJSScope. Another helper method collectFromAllJavaScriptParents can collect the results of a XXXCompletion method through all JavaScript scopes of a DomItem. Avoid code duplication and remove the implementation searching for methods in the DOM: instead, use the already existing implementation in QQmlJSScope::methods. Finally, add the method scriptIdentifierCompletion() that computes all autocompletions for a scriptIdentifier, that is called in QQmlLSUtils::completions and in the tests. Fix some tests to flag property as properties, inside of "Fields", and add extra tests to see if the newly implemented completions work. Cleanup QQmlLSUtils::completions() by extracting its code into static helper methods reachableTypes() and reachableMethods(), replace two enums by one QFlag as both enums were always used together. Extend reachableTypes() to find also non-object types, like the value types int, date, for example. This is later used for property type or function parameter type completion, for example. Make QQmlLSUtils::completions() more readable by returning early and by separating the completion by QML language constructs, instead of grouping multiple unrelated constructs together. This became possible thanks to the new static helpers mentioned above. Suppress completion inside of function parameter definitions and inside of id definitions. Add some tests for property type completion and pragma completion with QEXPECT_FAIL, those features will be implemented in a later commit. Add 'import' completion for empty files + a test that test completions on an empty file. Fix tests for colon-checking: some completions insert '<propertyName>: ' for properties, e.g. for bindings, and some do not, e.g. for property usage in JS expressions. Also fix the test in tst_qmlls_modules to expect methods instead of functions. Add exception in tst_qmlformat for the empty qml file used to test if completions work in a completely empty file. Task-number: QTBUG-116899 Change-Id: I63de62c71d63aa4ab62ca6d83c6be157f4e6f96c Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Diffstat (limited to 'tests')
-rw-r--r--tests/auto/qml/qmlformat/tst_qmlformat.cpp1
-rw-r--r--tests/auto/qmlls/modules/tst_qmlls_modules.cpp10
-rw-r--r--tests/auto/qmlls/utils/data/Yyy.qml39
-rw-r--r--tests/auto/qmlls/utils/data/Zzz.qml2
-rw-r--r--tests/auto/qmlls/utils/data/emptyFile.qml0
-rw-r--r--tests/auto/qmlls/utils/tst_qmlls_utils.cpp236
6 files changed, 234 insertions, 54 deletions
diff --git a/tests/auto/qml/qmlformat/tst_qmlformat.cpp b/tests/auto/qml/qmlformat/tst_qmlformat.cpp
index a763e2b5cc..c5c51f178c 100644
--- a/tests/auto/qml/qmlformat/tst_qmlformat.cpp
+++ b/tests/auto/qml/qmlformat/tst_qmlformat.cpp
@@ -129,6 +129,7 @@ void TestQmlformat::initTestCase()
m_invalidFiles << "tests/auto/qml/qqmllanguage/data/nullishCoalescing_RHS_Or.qml";
m_invalidFiles << "tests/auto/qml/qqmllanguage/data/typeAnnotations.2.qml";
m_invalidFiles << "tests/auto/qml/qqmlparser/data/disallowedtypeannotations/qmlnestedfunction.qml";
+ m_invalidFiles << "tests/auto/qmlls/utils/data/emptyFile.qml";
// Files that get changed:
// rewrite of import "bla/bla/.." to import "bla"
diff --git a/tests/auto/qmlls/modules/tst_qmlls_modules.cpp b/tests/auto/qmlls/modules/tst_qmlls_modules.cpp
index 51fa1e6883..50a3078564 100644
--- a/tests/auto/qmlls/modules/tst_qmlls_modules.cpp
+++ b/tests/auto/qmlls/modules/tst_qmlls_modules.cpp
@@ -259,10 +259,10 @@ void tst_qmlls_modules::function_documentations_data()
QTest::newRow("longfunction")
<< filePath << 5 << 14
<< ExpectedDocumentations{
- std::make_tuple(u"lala()"_s, u"returns void"_s, u"lala()"_s),
- std::make_tuple(u"longfunction()"_s, u"returns string"_s,
+ std::make_tuple(u"lala"_s, u"returns void"_s, u"lala()"_s),
+ std::make_tuple(u"longfunction"_s, u"returns string"_s,
uR"(longfunction(a, b, c = "c", d = "d"))"_s),
- std::make_tuple(u"documentedFunction()"_s, u"returns string"_s,
+ std::make_tuple(u"documentedFunction"_s, u"returns string"_s,
uR"(// documentedFunction: is documented
// returns 'Good'
documentedFunction(arg1, arg2 = "Qt"))"_s),
@@ -301,7 +301,7 @@ void tst_qmlls_modules::function_documentations()
bool hasFoundExpected = false;
const auto expectedLabel = std::get<0>(exp);
for (const CompletionItem &c : *cItems) {
- if (c.kind->toInt() != int(CompletionItemKind::Function)) {
+ if (c.kind->toInt() != int(CompletionItemKind::Method)) {
// Only check functions.
continue;
}
@@ -348,7 +348,7 @@ void tst_qmlls_modules::function_documentations()
QVERIFY2(false, "error computing the completion");
clean();
});
- QTRY_VERIFY_WITH_TIMEOUT(*didFinish, 30000);
+ QTRY_VERIFY_WITH_TIMEOUT(*didFinish, 3000);
}
void tst_qmlls_modules::buildDir()
diff --git a/tests/auto/qmlls/utils/data/Yyy.qml b/tests/auto/qmlls/utils/data/Yyy.qml
index 23bbd057f7..eb4f290709 100644
--- a/tests/auto/qmlls/utils/data/Yyy.qml
+++ b/tests/auto/qmlls/utils/data/Yyy.qml
@@ -1,6 +1,6 @@
import QtQuick 2.0
import QtQuick as QQ
-
+pragma Singleton
Zzz {
id: root
width: height
@@ -26,4 +26,41 @@ Zzz {
QQ.Rectangle {
color:"red"
}
+
+ Item {
+ id: someItem
+ property int helloProperty
+ }
+
+ function parameterCompletion(helloWorld, helloMe: int) {
+ let helloVar = 42;
+ let result = someItem.helloProperty + helloWorld;
+ return result;
+ }
+
+ component Base: QtObject {
+ property int propertyInBase
+ function functionInBase(jsParameterInBase) {
+ let jsIdentifierInBase;
+ return jsIdentifierInBase;
+ }
+ }
+
+ Base {
+ property int propertyInDerived
+ function functionInDerived(jsParameterInDerived) {
+ let jsIdentifierInDerived;
+ return jsIdentifierInDerived;
+ }
+
+ property Base child: Base {
+ property int propertyInChild
+ function functionInChild(jsParameterInChild) {
+ let jsIdentifierInChild;
+ return someItem.helloProperty;
+ }
+ }
+ }
+ function test1() { for (myvar = 42; i<0; ++i){} console.log(myvar);}
+ function test2() { for (var myvar = 42; i<0; ++i){} console.log(myvar);}
}
diff --git a/tests/auto/qmlls/utils/data/Zzz.qml b/tests/auto/qmlls/utils/data/Zzz.qml
index 165ea46394..fa0edf69dc 100644
--- a/tests/auto/qmlls/utils/data/Zzz.qml
+++ b/tests/auto/qmlls/utils/data/Zzz.qml
@@ -7,4 +7,6 @@ Item {
Rectangle {
width: zzz.height
}
+
+ property int propertyOfZZZ
}
diff --git a/tests/auto/qmlls/utils/data/emptyFile.qml b/tests/auto/qmlls/utils/data/emptyFile.qml
new file mode 100644
index 0000000000..e69de29bb2
--- /dev/null
+++ b/tests/auto/qmlls/utils/data/emptyFile.qml
diff --git a/tests/auto/qmlls/utils/tst_qmlls_utils.cpp b/tests/auto/qmlls/utils/tst_qmlls_utils.cpp
index cff7ae9fac..f55eb0d3a3 100644
--- a/tests/auto/qmlls/utils/tst_qmlls_utils.cpp
+++ b/tests/auto/qmlls/utils/tst_qmlls_utils.cpp
@@ -1543,6 +1543,8 @@ void tst_qmlls_utils::isValidEcmaScriptIdentifier()
using namespace QLspSpecification;
+enum InsertOption { None, InsertColon };
+
void tst_qmlls_utils::completions_data()
{
QTest::addColumn<QString>("filePath");
@@ -1550,8 +1552,10 @@ void tst_qmlls_utils::completions_data()
QTest::addColumn<int>("character");
QTest::addColumn<ExpectedCompletions>("expected");
QTest::addColumn<QStringList>("notExpected");
+ QTest::addColumn<InsertOption>("insertOptions");
QString file = testFile(u"Yyy.qml"_s);
+ QString emptyFile = testFile(u"emptyFile.qml"_s);
QTest::newRow("objEmptyLine") << file << 9 << 1
<< ExpectedCompletions({
@@ -1560,52 +1564,51 @@ void tst_qmlls_utils::completions_data()
{ u"width"_s, CompletionItemKind::Property },
{ u"function"_s, CompletionItemKind::Keyword },
})
- << QStringList({ u"QtQuick"_s, u"vector4d"_s });
+ << QStringList({ u"QtQuick"_s, u"vector4d"_s }) << InsertColon;
- QTest::newRow("inBindingLabel") << file << 6 << 10
- << ExpectedCompletions({
- { u"Rectangle"_s, CompletionItemKind::Class },
- { u"property"_s, CompletionItemKind::Keyword },
- { u"width"_s, CompletionItemKind::Property },
- })
- << QStringList({ u"QtQuick"_s, u"vector4d"_s });
+ QTest::newRow("inBindingLabel")
+ << file << 6 << 10
+ << ExpectedCompletions({
+ { u"Rectangle"_s, CompletionItemKind::Class },
+ { u"width"_s, CompletionItemKind::Property },
+ })
+ << QStringList({ u"QtQuick"_s, u"vector4d"_s, u"property"_s }) << InsertColon;
QTest::newRow("afterBinding") << file << 6 << 11
<< ExpectedCompletions({
- { u"Rectangle"_s, CompletionItemKind::Field },
- { u"width"_s, CompletionItemKind::Field },
- { u"vector4d"_s, CompletionItemKind::Field },
+ { u"height"_s, CompletionItemKind::Property },
+ { u"width"_s, CompletionItemKind::Property },
+ { u"Rectangle"_s, CompletionItemKind::Class },
})
- << QStringList({ u"QtQuick"_s, u"property"_s });
+ << QStringList({ u"QtQuick"_s, u"property"_s, u"vector4d"_s })
+ << None;
- // suppress?
- QTest::newRow("afterId") << file << 5 << 8
- << ExpectedCompletions({
- { u"import"_s, CompletionItemKind::Keyword },
- })
+ QTest::newRow("afterId") << file << 5 << 8 << ExpectedCompletions({})
<< QStringList({ u"QtQuick"_s, u"property"_s, u"Rectangle"_s,
- u"width"_s, u"vector4d"_s });
+ u"width"_s, u"vector4d"_s, u"import"_s })
+ << None;
- QTest::newRow("fileStart") << file << 1 << 1
+ QTest::newRow("emptyFile") << emptyFile << 1 << 1
<< ExpectedCompletions({
- { u"Rectangle"_s, CompletionItemKind::Class },
{ u"import"_s, CompletionItemKind::Keyword },
+ { u"pragma"_s, CompletionItemKind::Keyword },
})
- << QStringList({ u"QtQuick"_s, u"vector4d"_s, u"width"_s });
+ << QStringList({ u"QtQuick"_s, u"vector4d"_s, u"width"_s }) << None;
QTest::newRow("importImport") << file << 1 << 4
<< ExpectedCompletions({
- { u"Rectangle"_s, CompletionItemKind::Class },
{ u"import"_s, CompletionItemKind::Keyword },
})
- << QStringList({ u"QtQuick"_s, u"vector4d"_s, u"width"_s });
+ << QStringList({ u"QtQuick"_s, u"vector4d"_s, u"width"_s,
+ u"Rectangle"_s })
+ << None;
QTest::newRow("importModuleStart")
<< file << 1 << 8
<< ExpectedCompletions({
{ u"QtQuick"_s, CompletionItemKind::Module },
})
- << QStringList({ u"vector4d"_s, u"width"_s, u"Rectangle"_s, u"import"_s });
+ << QStringList({ u"vector4d"_s, u"width"_s, u"Rectangle"_s, u"import"_s }) << None;
QTest::newRow("importVersionStart")
<< file << 1 << 16
@@ -1613,7 +1616,7 @@ void tst_qmlls_utils::completions_data()
{ u"2"_s, CompletionItemKind::Constant },
{ u"as"_s, CompletionItemKind::Keyword },
})
- << QStringList({ u"Rectangle"_s, u"import"_s, u"vector4d"_s, u"width"_s });
+ << QStringList({ u"Rectangle"_s, u"import"_s, u"vector4d"_s, u"width"_s }) << None;
// QTest::newRow("importVersionMinor")
// << uri << 1 << 18
@@ -1622,39 +1625,159 @@ void tst_qmlls_utils::completions_data()
// })
// << QStringList({ u"as"_s, u"Rectangle"_s, u"import"_s, u"vector4d"_s, u"width"_s });
- QTest::newRow("inScript") << file << 7 << 15
- << ExpectedCompletions({
- { u"Rectangle"_s, CompletionItemKind::Field },
- { u"vector4d"_s, CompletionItemKind::Field },
- { u"lala()"_s, CompletionItemKind::Function },
- { u"longfunction()"_s, CompletionItemKind::Function },
- { u"documentedFunction()"_s,
- CompletionItemKind::Function },
- { u"lala()"_s, CompletionItemKind{ 0 } },
- { u"width"_s, CompletionItemKind::Field },
- })
- << QStringList({ u"import"_s });
-
QTest::newRow("expandBase1") << file << 10 << 24
<< ExpectedCompletions({
- { u"width"_s, CompletionItemKind::Field },
- { u"foo"_s, CompletionItemKind::Field },
+ { u"width"_s, CompletionItemKind::Property },
+ { u"foo"_s, CompletionItemKind::Property },
})
- << QStringList({ u"import"_s, u"Rectangle"_s });
+ << QStringList({ u"import"_s, u"Rectangle"_s }) << None;
QTest::newRow("expandBase2") << file << 11 << 30
<< ExpectedCompletions({
- { u"width"_s, CompletionItemKind::Field },
- { u"color"_s, CompletionItemKind::Field },
+ { u"width"_s, CompletionItemKind::Property },
+ { u"color"_s, CompletionItemKind::Property },
})
- << QStringList({ u"foo"_s, u"import"_s, u"Rectangle"_s });
+ << QStringList({ u"foo"_s, u"import"_s, u"Rectangle"_s }) << None;
QTest::newRow("asCompletions")
<< file << 26 << 9
<< ExpectedCompletions({
{ u"Rectangle"_s, CompletionItemKind::Field },
})
- << QStringList({ u"foo"_s, u"import"_s, u"lala()"_s, u"width"_s });
+ << QStringList({ u"foo"_s, u"import"_s, u"lala()"_s, u"width"_s }) << None;
+
+ // TODO: disable completion inside of function arguments
+ // or only allow it for type completion
+ QTest::newRow("parameterCompletion")
+ << file << 36 << 24
+ << ExpectedCompletions({
+ { u"helloWorld"_s, CompletionItemKind::Variable },
+ { u"helloMe"_s, CompletionItemKind::Variable },
+ })
+ << QStringList() << None;
+
+ QTest::newRow("inParameterCompletion")
+ << file << 35 << 39 << ExpectedCompletions({})
+ << QStringList{
+ u"helloWorld"_s,
+ u"helloMe"_s,
+ } << None;
+
+ QTest::newRow("propertyTypeCompletion") << file << 16 << 14 << ExpectedCompletions({
+ {u"Zzz"_s, CompletionItemKind::Class },
+ {u"Item"_s, CompletionItemKind::Class },
+ {u"int"_s, CompletionItemKind::Class },
+ {u"date"_s, CompletionItemKind::Class },
+ })
+ << QStringList{
+ u"helloWorld"_s,
+ u"helloMe"_s,
+ } << None;
+
+ QTest::newRow("parameterTypeCompletion") << file << 35 << 55 << ExpectedCompletions({
+ {u"Zzz"_s, CompletionItemKind::Class },
+ {u"Item"_s, CompletionItemKind::Class },
+ {u"int"_s, CompletionItemKind::Class },
+ {u"date"_s, CompletionItemKind::Class },
+ })
+ << QStringList{
+ u"helloWorld"_s,
+ u"helloMe"_s,
+ } << None;
+
+ QTest::newRow("qualifiedIdentifierCompletion")
+ << file << 37 << 36
+ << ExpectedCompletions({
+ { u"helloProperty"_s, CompletionItemKind::Property },
+ { u"childAt"_s, CompletionItemKind::Method },
+ })
+ << QStringList{ u"helloVar"_s, u"someItem"_s, u"color"_s, u"helloWorld"_s,
+ u"propertyOfZZZ"_s }
+ << None;
+
+ QTest::newRow("scriptExpressionCompletion")
+ << file << 60 << 16
+ << ExpectedCompletions({
+ // parameters
+ { u"jsParameterInChild"_s, CompletionItemKind::Variable },
+ // own properties
+ { u"jsIdentifierInChild"_s, CompletionItemKind::Variable },
+ { u"functionInChild"_s, CompletionItemKind::Method },
+ { u"propertyInChild"_s, CompletionItemKind::Property },
+ // inherited properties from QML
+ { u"functionInBase"_s, CompletionItemKind::Method },
+ { u"propertyInBase"_s, CompletionItemKind::Property },
+ // inherited properties (transitive) from C++
+ { u"objectName"_s, CompletionItemKind::Property },
+ { u"someItem"_s, CompletionItemKind::Value },
+ })
+ << QStringList{
+ u"helloVar"_s,
+ u"color"_s,
+ u"helloWorld"_s,
+ u"propertyOfZZZ"_s,
+ u"propertyInDerived"_s,
+ u"functionInDerived"_s,
+ u"jsIdentifierInDerived"_s,
+ u"jsIdentifierInBase"_s,
+ u"lala"_s,
+ u"foo"_s,
+ u"jsParameterInBase"_s,
+ u"jsParameterInDerived"_s,
+ } << None;
+
+ QTest::newRow("qualifiedScriptExpressionCompletion")
+ << file << 60 << 34
+ << ExpectedCompletions({
+ // own properties
+ { u"helloProperty"_s, CompletionItemKind::Property },
+ // inherited properties (transitive) from C++
+ { u"width"_s, CompletionItemKind::Property },
+ })
+ << QStringList{
+ u"helloVar"_s,
+ u"color"_s,
+ u"helloWorld"_s,
+ u"propertyOfZZZ"_s,
+ u"propertyInDerived"_s,
+ u"functionInDerived"_s,
+ u"jsIdentifierInDerived"_s,
+ u"jsIdentifierInBase"_s,
+ u"jsIdentifierInChild"_s,
+ u"lala"_s,
+ u"foo"_s,
+ u"jsParameterInBase"_s,
+ u"jsParameterInDerived"_s,
+ u"jsParameterInChild"_s,
+ u"functionInChild"_s,
+ } << None;
+
+ QTest::newRow("pragma")
+ << file << 3 << 8
+ << ExpectedCompletions({
+ { u"NativeMethodBehavior"_s, CompletionItemKind::Value },
+ { u"ComponentBehavior"_s, CompletionItemKind::Value },
+ { u"ListPropertyAssignBehavior"_s, CompletionItemKind::Value },
+ { u"Singleton"_s, CompletionItemKind::Value },
+ // note: only complete the Addressible/Inaddressible part of ValueTypeBehavior!
+ { u"ValueTypeBehavior"_s, CompletionItemKind::Value },
+ })
+ << QStringList{
+ u"int"_s,
+ u"Rectangle"_s,
+ u"FunctionSignatureBehavior"_s,
+ u"Strict"_s,
+ } << None;
+
+ QTest::newRow("var-variable") << file << 64 << 67
+ << ExpectedCompletions({
+ { u"myvar"_s, CompletionItemKind::Value },
+ })
+ << QStringList{} << None;
+ QTest::newRow("let-variable")
+ << file << 3 << 8
+ << ExpectedCompletions({})
+ << QStringList{ u"myvar"_s, } << None;
}
void tst_qmlls_utils::completions()
@@ -1664,6 +1787,7 @@ void tst_qmlls_utils::completions()
QFETCH(int, character);
QFETCH(ExpectedCompletions, expected);
QFETCH(QStringList, notExpected);
+ QFETCH(InsertOption, insertOptions);
QQmlJS::Dom::DomCreationOptions options;
options.setFlag(QQmlJS::Dom::DomCreationOption::WithSemanticAnalysis);
@@ -1706,14 +1830,30 @@ void tst_qmlls_utils::completions()
QVERIFY2(!fieldsTracker.hasSeen(c.label), "Duplicate field: " + c.label);
} else if (c.kind->toInt() == int(CompletionItemKind::Property)) {
QVERIFY2(!propertiesTracker.hasSeen(c.label), "Duplicate property: " + c.label);
- QVERIFY2(c.insertText == c.label + u": "_s,
- "a property should end with a colon with a space for "
- "'insertText', for better coding experience");
+ if (insertOptions & InsertColon) {
+ QVERIFY2(c.insertText == c.label + u": "_s,
+ "a property should end with a colon with a space for "
+ "'insertText', for better coding experience");
+ } else {
+ QCOMPARE(c.insertText, std::nullopt);
+ }
}
labels << c.label;
}
for (const ExpectedCompletion &exp : expected) {
+ QEXPECT_FAIL(
+ "asCompletions",
+ "Cannot complete after 'QQ.': either there is already a type behind and then "
+ "there is nothing to complete, or there is nothing behind 'QQ.' and the parser "
+ "fails because of the unexpected '.'",
+ Abort);
+ QEXPECT_FAIL("pragma", "Pragma completion not supported yet", Abort);
+ QEXPECT_FAIL("propertyTypeCompletion", "No completion for property types supported yet",
+ Abort);
+ QEXPECT_FAIL("var-variable",
+ "Completion for var-variables currently acts the same as for let-variables.",
+ Abort);
QVERIFY2(labels.contains(exp.first),
u"no %1 in %2"_s
.arg(exp.first, QStringList(labels.begin(), labels.end()).join(u", "_s))