7#include <QtCore/qdiriterator.h>
8#include <QtCore/qfile.h>
12Q_LOGGING_CATEGORY(QQmlLSCompletionLog,
"qt.languageserver.completions")
14using namespace QLspSpecification;
15using namespace QQmlJS::Dom;
16using namespace Qt::StringLiterals;
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
45CompletionItem
QQmlLSCompletion::makeSnippet(QUtf8StringView qualifier, QUtf8StringView label,
46 QUtf8StringView insertText)
49 if (!qualifier.isEmpty()) {
50 res.label = qualifier.data();
53 res.label += label.data();
54 res.insertTextFormat = InsertTextFormat::Snippet;
55 if (!qualifier.isEmpty()) {
56 res.insertText = qualifier.data();
57 *res.insertText +=
'.';
58 *res.insertText += insertText.data();
60 res.insertText = insertText.data();
62 res.kind = CompletionItemKind::Snippet;
63 res.insertTextMode = InsertTextMode::AdjustIndentation;
67CompletionItem
QQmlLSCompletion::makeSnippet(QUtf8StringView label, QUtf8StringView insertText)
69 return makeSnippet(QByteArray(), label, insertText);
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
93 const QQmlLSCompletionPosition &positionInfo,
94 QQmlJS::SourceLocation right)
const
99 if (!(left.end() <= positionInfo.offset()))
101 if (!right.isValid())
105 return positionInfo.offset() <= right.begin();
109
110
111
113 const QQmlLSCompletionPosition &positionInfo)
const
115 return betweenLocations(left, positionInfo, QQmlJS::SourceLocation{});
119
120
121
123 QQmlJS::SourceLocation right)
const
125 if (!right.isValid())
129 if (ctx.offset() <= right.begin())
135bool QQmlLSCompletion::ctxBeforeStatement(
const QQmlLSCompletionPosition &positionInfo,
136 const DomItem &parentForContext,
137 FileLocationRegion firstRegion)
const
139 const auto regions = FileLocations::treeOf(parentForContext)->info().regions;
140 const bool result = beforeLocation(positionInfo, regions[firstRegion]);
145QQmlLSCompletion::suggestBindingCompletion(
const DomItem &itemAtPosition, BackInsertIterator it)
const
147 suggestReachableTypes(itemAtPosition, LocalSymbolsType::AttachedType, CompletionItemKind::Class,
150 const auto scope = [&]() -> QQmlJSScope::ConstPtr {
151 const DomItem owner = ownerOfQualifiedExpression(itemAtPosition);
153 return itemAtPosition.qmlObject().semanticScope();
155 const auto expressionType = QQmlLSUtils::resolveExpressionType(
156 owner, QQmlLSUtils::ResolveActualTypeForFieldMemberExpression);
159 if (!expressionType || expressionType->type == QQmlLSUtils::QualifiedModuleIdentifier)
162 return expressionType->semanticScope;
168 propertyCompletion(scope,
nullptr, it);
169 signalHandlerCompletion(scope,
nullptr, it);
172void QQmlLSCompletion::idsCompletions(
const DomItem &component, BackInsertIterator it)
const
174 qCDebug(QQmlLSCompletionLog) <<
"adding ids completions";
175 for (
const QString &k : component.field(Fields::ids).keys()) {
177 comp.label = k.toUtf8();
178 comp.kind = CompletionItemKind::Value;
183static bool testScopeSymbol(
const QQmlJSScope::ConstPtr &scope, LocalSymbolsTypes options,
184 CompletionItemKind kind)
186 const bool currentIsSingleton = scope->isSingleton();
187 const bool currentIsAttached = !scope->attachedType().isNull();
188 if ((options & LocalSymbolsType::Singleton) && currentIsSingleton) {
191 if ((options & LocalSymbolsType::AttachedType) && currentIsAttached) {
194 const bool isObjectType = scope->isReferenceType();
195 if (options & LocalSymbolsType::ObjectType && !currentIsSingleton && isObjectType) {
196 return kind != CompletionItemKind::Constructor || scope->isCreatable();
198 if (options & LocalSymbolsType::ValueType && !currentIsSingleton && !isObjectType) {
205
206
207
208void QQmlLSCompletion::suggestReachableTypes(
const DomItem &el, LocalSymbolsTypes options,
209 CompletionItemKind kind, BackInsertIterator it)
const
211 auto file = el.containingFile().as<QmlFile>();
214 auto resolver = file->typeResolver();
218 const QString requiredQualifiers = QQmlLSUtils::qualifiersFrom(el);
219 const auto keyValueRange = resolver->importedTypes().asKeyValueRange();
220 for (
const auto &type : keyValueRange) {
222 const bool isMarkerType = type.first.contains(u"$internal$.")
223 || type.first.contains(u"$anonymous$.") || type.first.contains(u"$module$.");
224 if (isMarkerType || !type.first.startsWith(requiredQualifiers))
227 auto &scope = type.second.scope;
231 if (!testScopeSymbol(scope, options, kind))
234 CompletionItem completion;
235 completion.label = QStringView(type.first).sliced(requiredQualifiers.size()).toUtf8();
236 completion.kind = kind;
241void QQmlLSCompletion::jsIdentifierCompletion(
const QQmlJSScope::ConstPtr &scope,
242 QDuplicateTracker<QString> *usedNames,
243 BackInsertIterator it)
const
245 for (
const auto &[name, jsIdentifier] : scope->ownJSIdentifiers().asKeyValueRange()) {
246 CompletionItem completion;
247 if (usedNames && usedNames->hasSeen(name)) {
250 completion.label = name.toUtf8();
251 completion.kind = CompletionItemKind::Variable;
252 QString detail = u"has type "_s;
253 if (jsIdentifier.typeName) {
254 if (jsIdentifier.isConst) {
255 detail.append(u"const ");
257 detail.append(*jsIdentifier.typeName);
259 detail.append(jsIdentifier.isConst ? u"const"_s : u"var"_s);
261 completion.detail = detail.toUtf8();
267 QDuplicateTracker<QString> *usedNames,
268 BackInsertIterator it)
const
271 for (
const auto &[name, method] : scope->methods().asKeyValueRange()) {
272 if (method.access() != QQmlJSMetaMethod::Public)
274 if (usedNames && usedNames->hasSeen(name)) {
277 CompletionItem completion;
278 completion.label = name.toUtf8();
279 completion.kind = CompletionItemKind::Method;
286void QQmlLSCompletion::propertyCompletion(
const QQmlJSScope::ConstPtr &scope,
287 QDuplicateTracker<QString> *usedNames,
288 BackInsertIterator it)
const
290 for (
const auto &[name, property] : scope->properties().asKeyValueRange()) {
291 if (usedNames && usedNames->hasSeen(name)) {
294 CompletionItem completion;
295 completion.label = name.toUtf8();
296 completion.kind = CompletionItemKind::Property;
297 QString detail{ u"has type "_s };
298 if (!property.isWritable())
299 detail.append(u"readonly "_s);
300 detail.append(property.typeName().isEmpty() ? u"var"_s : property.typeName());
301 completion.detail = detail.toUtf8();
306void QQmlLSCompletion::enumerationCompletion(
const QQmlJSScope::ConstPtr &scope,
307 QDuplicateTracker<QString> *usedNames,
308 BackInsertIterator it)
const
310 for (
const QQmlJSMetaEnum &enumerator : scope->enumerations()) {
311 if (usedNames && usedNames->hasSeen(enumerator.name())) {
314 CompletionItem completion;
315 completion.label = enumerator.name().toUtf8();
316 completion.kind = CompletionItemKind::Enum;
321void QQmlLSCompletion::enumerationValueCompletionHelper(
const QStringList &enumeratorKeys,
322 BackInsertIterator it)
const
324 for (
const QString &enumeratorKey : enumeratorKeys) {
325 CompletionItem completion;
326 completion.label = enumeratorKey.toUtf8();
327 completion.kind = CompletionItemKind::EnumMember;
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
352void QQmlLSCompletion::enumerationValueCompletion(
const QQmlJSScope::ConstPtr &scope,
353 const QString &enumeratorName,
354 BackInsertIterator result)
const
356 auto enumerator = scope->enumeration(enumeratorName);
357 if (enumerator.isValid()) {
358 enumerationValueCompletionHelper(enumerator.keys(), result);
362 const bool enforcesScopedEnums = scope->enforcesScopedEnums();
363 for (
const QQmlJSMetaEnum &enumerator : scope->enumerations()) {
364 if (enforcesScopedEnums && enumerator.isScoped())
366 enumerationValueCompletionHelper(enumerator.keys(), result);
371
372
373
374
375
376
377
378
379
380
381
382
386 for (QQmlJSScope::ConstPtr current = scope; current; current = current->parentScope()) {
388 if (current->scopeType() == QQmlSA::ScopeType::QMLScope)
394
395
396
397
398
399
400
401
402
404 const QQmlJSScope::ConstPtr &scope,
const QString &enumName,
405 QDuplicateTracker<QString> &usedNames, BackInsertIterator result)
const
407 enumerationValueCompletion(scope, enumName, result);
410 if (
auto enumerator = scope->enumeration(enumName); !enumerator.isValid()) {
411 enumerationCompletion(scope, &usedNames, result);
416
417
418
419
420
421
422
423
424
425DomItem
QQmlLSCompletion::ownerOfQualifiedExpression(
const DomItem &qualifiedExpression)
const
430 const bool askForCompletionOnDot = QQmlLSUtils::isFieldMemberExpression(qualifiedExpression);
431 const bool hasQualifier =
432 QQmlLSUtils::isFieldMemberAccess(qualifiedExpression) || askForCompletionOnDot;
437 const DomItem owner =
438 (askForCompletionOnDot ? qualifiedExpression : qualifiedExpression.directParent())
439 .field(Fields::left);
444
445
446
447
448
449
450
451
452void QQmlLSCompletion::suggestJSExpressionCompletion(
const DomItem &scriptIdentifier,
453 BackInsertIterator result)
const
455 QDuplicateTracker<QString> usedNames;
456 QQmlJSScope::ConstPtr nearestScope;
458 const DomItem owner = ownerOfQualifiedExpression(scriptIdentifier);
461 for (QUtf8StringView view : std::array<QUtf8StringView, 3>{
"null",
"false",
"true" }) {
462 CompletionItem completion;
463 completion.label = view.data();
464 completion.kind = CompletionItemKind::Value;
467 idsCompletions(scriptIdentifier.component(), result);
468 suggestReachableTypes(scriptIdentifier,
469 LocalSymbolsType::Singleton | LocalSymbolsType::AttachedType,
470 CompletionItemKind::Class, result);
472 auto scope = scriptIdentifier.nearestSemanticScope();
475 nearestScope = scope;
477 enumerationCompletion(nearestScope, &usedNames, result);
479 auto ownerExpressionType = QQmlLSUtils::resolveExpressionType(
480 owner, QQmlLSUtils::ResolveActualTypeForFieldMemberExpression);
481 if (!ownerExpressionType || !ownerExpressionType->semanticScope)
483 nearestScope = ownerExpressionType->semanticScope;
485 switch (ownerExpressionType->type) {
486 case QQmlLSUtils::EnumeratorValueIdentifier:
488 case QQmlLSUtils::EnumeratorIdentifier:
489 suggestEnumerationsAndEnumerationValues(nearestScope, *ownerExpressionType->name,
492 case QQmlLSUtils::QmlComponentIdentifier:
495 if (QQmlJSScope::ConstPtr attachedType =
496 ownerExpressionType->semanticScope->attachedType()) {
497 methodCompletion(attachedType, &usedNames, result);
498 propertyCompletion(attachedType, &usedNames, result);
499 suggestEnumerationsAndEnumerationValues(
500 attachedType, *ownerExpressionType->name, usedNames, result);
503 case QQmlLSUtils::SingletonIdentifier:
504 if (ownerExpressionType->name)
505 suggestEnumerationsAndEnumerationValues(nearestScope, *ownerExpressionType->name,
513 Q_ASSERT(nearestScope);
515 methodCompletion(nearestScope, &usedNames, result);
516 propertyCompletion(nearestScope, &usedNames, result);
520 collectFromAllJavaScriptParents(
521 [
this, &usedNames, result](
const QQmlJSScope::ConstPtr &scope) {
522 jsIdentifierCompletion(scope, &usedNames, result);
525 collectFromAllJavaScriptParents(
526 [
this, &usedNames, result](
const QQmlJSScope::ConstPtr &scope) {
527 methodCompletion(scope, &usedNames, result);
530 collectFromAllJavaScriptParents(
531 [
this, &usedNames, result](
const QQmlJSScope::ConstPtr &scope) {
532 propertyCompletion(scope, &usedNames, result);
536 auto file = scriptIdentifier.containingFile().as<QmlFile>();
539 auto resolver = file->typeResolver();
543 const auto globals = resolver->jsGlobalObject();
544 methodCompletion(globals, &usedNames, result);
545 propertyCompletion(globals, &usedNames, result);
551 for (
const QString &name : names) {
552 if (
auto property = current->property(name); property.isValid()) {
553 if (
auto propertyType = property.type().get()) {
554 current = propertyType;
564 const QQmlLSCompletionPosition &positionInfo)
566 auto fileLocations = FileLocations::treeOf(parentForContext)->info().fullRegion;
567 return positionInfo.offset() <= fileLocations.begin();
571 const QQmlLSCompletionPosition &positionInfo)
573 auto location = FileLocations::treeOf(currentItem)->info();
574 auto region = location.regions.constFind(ColonTokenRegion);
576 if (region == location.regions.constEnd())
579 if (region.value().isValid() && region.value().begin() < positionInfo.offset()) {
586
587
588
589
590
591
592
593
594
595
596static const QMap<QString, QList<QString>> valuesForPragmas{
597 { u"ComponentBehavior"_s, { u"Unbound"_s, u"Bound"_s } },
598 { u"NativeMethodBehavior"_s, { u"AcceptThisObject"_s, u"RejectThisObject"_s } },
599 { u"ListPropertyAssignBehavior"_s, { u"Append"_s, u"Replace"_s, u"ReplaceIfNotDefault"_s } },
600 { u"Singleton"_s, {} },
601 { u"ValueTypeBehavior"_s, { u"Addressable"_s, u"Inaddressable"_s } },
604void QQmlLSCompletion::insidePragmaCompletion(QQmlJS::Dom::DomItem currentItem,
605 const QQmlLSCompletionPosition &positionInfo,
606 BackInsertIterator result)
const
608 if (cursorAfterColon(currentItem, positionInfo)) {
609 const QString name = currentItem.field(Fields::name).value().toString();
610 auto values = valuesForPragmas.constFind(name);
611 if (values == valuesForPragmas.constEnd())
614 for (
const auto &value : *values) {
616 comp.label = value.toUtf8();
617 comp.kind = CompletionItemKind::Value;
623 for (
const auto &pragma : valuesForPragmas.asKeyValueRange()) {
625 comp.label = pragma.first.toUtf8();
626 if (!pragma.second.isEmpty()) {
627 comp.insertText = QString(pragma.first).append(u": ").toUtf8();
629 comp.kind = CompletionItemKind::Value;
634void QQmlLSCompletion::insideQmlObjectCompletion(
const DomItem &parentForContext,
635 const QQmlLSCompletionPosition &positionInfo,
636 BackInsertIterator result)
const
639 const auto regions = FileLocations::treeOf(parentForContext)->info().regions;
641 const QQmlJS::SourceLocation leftBrace = regions[LeftBraceRegion];
642 const QQmlJS::SourceLocation rightBrace = regions[RightBraceRegion];
644 if (beforeLocation(positionInfo, leftBrace)) {
645 LocalSymbolsTypes options;
646 options.setFlag(LocalSymbolsType::ObjectType);
647 suggestReachableTypes(positionInfo.itemAtPosition, options, CompletionItemKind::Constructor,
649 if (parentForContext.directParent().internalKind() == DomType::Binding)
650 suggestSnippetsForRightHandSideOfBinding(positionInfo.itemAtPosition, result);
652 suggestSnippetsForLeftHandSideOfBinding(positionInfo.itemAtPosition, result);
654 if (QQmlLSUtils::isFieldMemberExpression(positionInfo.itemAtPosition)) {
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676 suggestJSExpressionCompletion(positionInfo.itemAtPosition, result);
681 if (betweenLocations(leftBrace, positionInfo, rightBrace)) {
685 constexpr static std::array completions{
""_L1,
693 "default virtual "_L1,
694 "default override "_L1,
695 "default required "_L1,
696 "final required "_L1,
697 "final readonly "_L1,
698 "virtual required "_L1,
699 "virtual readonly "_L1,
700 "override required "_L1,
701 "override readonly "_L1,
702 "default final required "_L1,
703 "default virtual required "_L1,
704 "default override required "_L1 };
705 for (QLatin1StringView completion : completions) {
710 if (!completion.contains(QLatin1StringView(
"readonly"))) {
711 result = makeSnippet(
712 QByteArray(completion.data()).append(
"property type name;"),
713 QByteArray(completion.data()).append(
"property ${1:type} ${0:name};"));
716 if (!completion.contains(QLatin1StringView(
"required"))) {
717 result = makeSnippet(
718 QByteArray(completion.data()).append(
"property type name: value;"),
719 QByteArray(completion.data())
720 .append(
"property ${1:type} ${2:name}: ${0:value};"));
725 result = makeSnippet(
"signal name(arg1:type1, ...)",
"signal ${1:name}($0)");
728 result = makeSnippet(
"signal name;",
"signal ${0:name};");
731 result = makeSnippet(
"required name;",
"required ${0:name};");
734 result = makeSnippet(
"function name(args...): returnType { statements...}",
735 "function ${1:name}($2): ${3:returnType} {\n\t$0\n}");
738 result = makeSnippet(
"enum name { Values...}",
"enum ${1:name} {\n\t${0:values}\n}");
741 result = makeSnippet(
"component Name: BaseType { ... }",
742 "component ${1:name}: ${2:baseType} {\n\t$0\n}");
744 suggestBindingCompletion(positionInfo.itemAtPosition, result);
747 const DomItem containingFile = parentForContext.containingFile();
748 suggestReachableTypes(containingFile, LocalSymbolsType::ObjectType,
749 CompletionItemKind::Constructor, result);
750 suggestSnippetsForLeftHandSideOfBinding(positionInfo.itemAtPosition, result);
757 const DomItem ¤tItem,
const QQmlLSCompletionPosition &positionInfo,
758 BackInsertIterator result)
const
760 auto info = FileLocations::treeOf(currentItem)->info();
761 const QQmlJS::SourceLocation propertyKeyword = info.regions[PropertyKeywordRegion];
764 if (positionInfo.offset() < propertyKeyword.end()) {
765 auto addCompletionKeyword = [&result](QUtf8StringView view) {
767 item.label = view.data();
768 item.kind = CompletionItemKind::Keyword;
772 const auto alreadyTyped = [&info,
773 &positionInfo](
const FileLocationRegion &keywordRegion) ->
bool {
774 const auto &keywordSLoc = info.regions[keywordRegion];
775 return keywordSLoc.isValid() && keywordSLoc.begin() < positionInfo.offset();
778 const bool alreadyTypedReadonlyOrRequired =
779 alreadyTyped(ReadonlyKeywordRegion) || alreadyTyped(RequiredKeywordRegion);
780 if (!alreadyTypedReadonlyOrRequired) {
785 addCompletionKeyword(u8"readonly");
786 addCompletionKeyword(u8"required");
789 if (!alreadyTyped(DefaultKeywordRegion)) {
790 addCompletionKeyword(u8"default");
793 const bool alreadyTypedVirtualOrOverrideOrFinal = alreadyTyped(VirtualKeywordRegion)
794 || alreadyTyped(OverrideKeywordRegion) || alreadyTyped(FinalKeywordRegion);
795 if (!alreadyTypedVirtualOrOverrideOrFinal) {
800 addCompletionKeyword(u8"final");
801 addCompletionKeyword(u8"virtual");
802 addCompletionKeyword(u8"override");
805 addCompletionKeyword(u8"property");
810 const QQmlJS::SourceLocation propertyIdentifier = info.regions[IdentifierRegion];
811 if (propertyKeyword.end() <= positionInfo.offset()
812 && positionInfo.offset() < propertyIdentifier.begin()) {
813 suggestReachableTypes(currentItem,
814 LocalSymbolsType::ObjectType | LocalSymbolsType::ValueType,
815 CompletionItemKind::Class, result);
822 const QQmlLSCompletionPosition &positionInfo,
823 BackInsertIterator result)
const
825 const DomItem containingBinding = currentItem.filterUp(
826 [](DomType type,
const QQmlJS::Dom::DomItem &) {
return type == DomType::Binding; },
827 FilterUpOptions::ReturnOuter);
830 if (cursorAfterColon(containingBinding, positionInfo)) {
831 suggestJSExpressionCompletion(positionInfo.itemAtPosition, result);
833 if (
auto type = QQmlLSUtils::resolveExpressionType(currentItem,
834 QQmlLSUtils::ResolveOwnerType)) {
835 const QStringList names = currentItem.field(Fields::name).toString().split(u'.');
836 const QQmlJSScope *current = resolve(type->semanticScope.get(), names);
838 if (!current || current->accessSemantics() == QQmlSA::AccessSemantics::Reference) {
839 LocalSymbolsTypes options;
840 options.setFlag(LocalSymbolsType::ObjectType);
841 suggestReachableTypes(positionInfo.itemAtPosition, options,
842 CompletionItemKind::Constructor, result);
843 suggestSnippetsForRightHandSideOfBinding(positionInfo.itemAtPosition, result);
850 if (cursorInFrontOfItem(containingBinding, positionInfo)) {
851 insideQmlObjectCompletion(currentItem.containingObject(), positionInfo, result);
855 const DomItem containingObject = currentItem.qmlObject();
857 suggestBindingCompletion(positionInfo.itemAtPosition, result);
860 suggestReachableTypes(positionInfo.itemAtPosition, LocalSymbolsType::ObjectType,
861 CompletionItemKind::Constructor, result);
862 suggestSnippetsForLeftHandSideOfBinding(positionInfo.itemAtPosition, result);
866 const QQmlLSCompletionPosition &positionInfo,
867 BackInsertIterator result)
const
869 const DomItem containingFile = currentItem.containingFile();
872 if (cursorInFrontOfItem(currentItem, positionInfo)) {
873 suggestReachableTypes(containingFile, LocalSymbolsType::ObjectType,
874 CompletionItemKind::Constructor, result);
878 const auto regions = FileLocations::treeOf(currentItem)->info().regions;
879 const QQmlJS::SourceLocation importToken = regions.value(ImportTokenRegion);
880 const QQmlJS::SourceLocation uri = regions.value(ImportUriRegion);
882 if (importToken.isValid() && positionInfo.offset() <= importToken.end()) {
884 item.label =
"import";
885 item.kind = CompletionItemKind::Keyword;
890 if (!uri.isValid() || positionInfo.offset() < uri.begin())
893 if (positionInfo.offset() > uri.end()) {
894 const QQmlJS::SourceLocation asToken = regions.value(AsTokenRegion);
895 if (!asToken.isValid() || positionInfo.offset() <= asToken.end()) {
898 item.kind = CompletionItemKind::Keyword;
904 const auto environment = currentItem.environment().ownerAs<DomEnvironment>();
908 const auto qmlFile = containingFile.ownerAs<QmlFile>();
912 QStringView typedModulePrefix =
913 QStringView(qmlFile->code()).mid(uri.begin(), positionInfo.offset() - uri.begin());
914 const qsizetype lastDotIndex = typedModulePrefix.lastIndexOf(u'.');
915 if (lastDotIndex == -1) {
916 for (
const QString &moduleName : importableModules(environment->loadPaths())) {
918 item.label = moduleName.toUtf8();
919 item.kind = CompletionItemKind::Module;
924 typedModulePrefix.truncate(lastDotIndex + 1);
925 for (
const QString &moduleName : importableModules(environment->loadPaths())) {
926 if (!moduleName.startsWith(typedModulePrefix))
929 item.label = moduleName.mid(lastDotIndex + 1).toUtf8();
930 item.kind = CompletionItemKind::Module;
938 m_moduleExplorer.explore(importPaths);
941QStringList
QQmlLSCompletion::importableModules(
const QStringList &importPaths)
const
943 m_moduleExplorer.explore(importPaths);
944 return m_moduleExplorer.modulesIn(importPaths);
948 const QQmlLSCompletionPosition &positionInfo,
949 BackInsertIterator result)
const
951 const DomItem containingFile = currentItem.containingFile();
954 if (positionInfo.cursorPosition.atLineStart()) {
955 if (positionInfo.cursorPosition.base().isEmpty()) {
956 for (
const QStringView &s : std::array<QStringView, 2>({ u"pragma", u"import" })) {
958 comp.label = s.toUtf8();
959 comp.kind = CompletionItemKind::Keyword;
965 suggestReachableTypes(containingFile, LocalSymbolsType::ObjectType,
966 CompletionItemKind::Constructor, result);
970
971
972
977 for (
auto view : std::array<QUtf8StringView, 3>{
"let",
"var",
"const" }) {
978 auto snippet = makeSnippet(QByteArray(view.data()).append(
" variable = value"),
979 QByteArray(view.data()).append(
" ${1:variable} = $0"));
980 if (option == AppendSemicolon) {
981 snippet.insertText->append(
";");
982 snippet.label.append(
";");
989
990
991
992void QQmlLSCompletion::suggestCaseAndDefaultStatementCompletion(BackInsertIterator result)
const
995 result = makeSnippet(
"case value: statements...",
"case ${1:value}:\n\t$0");
997 result = makeSnippet(
"case value: { statements... }",
"case ${1:value}: {\n\t$0\n}");
1000 result = makeSnippet(
"default: statements...",
"default:\n\t$0");
1002 result = makeSnippet(
"default: { statements... }",
"default: {\n\t$0\n}");
1006
1007
1008
1009
1010
1011
1012
1013
1014void QQmlLSCompletion::suggestContinueAndBreakStatementIfNeeded(
const DomItem &itemAtPosition,
1015 BackInsertIterator result)
const
1017 bool alreadyInLabel =
false;
1018 bool alreadyInSwitch =
false;
1019 for (DomItem current = itemAtPosition; current; current = current.directParent()) {
1020 switch (current.internalKind()) {
1021 case DomType::ScriptExpression:
1025 case DomType::ScriptForStatement:
1026 case DomType::ScriptForEachStatement:
1027 case DomType::ScriptWhileStatement:
1028 case DomType::ScriptDoWhileStatement: {
1029 CompletionItem continueKeyword;
1030 continueKeyword.label =
"continue";
1031 continueKeyword.kind = CompletionItemKind::Keyword;
1032 result = continueKeyword;
1035 if (!alreadyInSwitch && !alreadyInLabel) {
1036 CompletionItem breakKeyword;
1037 breakKeyword.label =
"break";
1038 breakKeyword.kind = CompletionItemKind::Keyword;
1039 result = breakKeyword;
1044 case DomType::ScriptSwitchStatement: {
1046 if (alreadyInSwitch || alreadyInLabel)
1048 alreadyInSwitch =
true;
1050 CompletionItem breakKeyword;
1051 breakKeyword.label =
"break";
1052 breakKeyword.kind = CompletionItemKind::Keyword;
1053 result = breakKeyword;
1056 case DomType::ScriptLabelledStatement: {
1058 if (alreadyInSwitch || alreadyInLabel)
1060 alreadyInLabel =
true;
1062 CompletionItem breakKeyword;
1063 breakKeyword.label =
"break";
1064 breakKeyword.kind = CompletionItemKind::Keyword;
1065 result = breakKeyword;
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090void QQmlLSCompletion::suggestJSStatementCompletion(
const DomItem &itemAtPosition,
1091 BackInsertIterator result)
const
1093 suggestJSExpressionCompletion(itemAtPosition, result);
1095 if (QQmlLSUtils::isFieldMemberAccess(itemAtPosition)
1096 || QQmlLSUtils::isFieldMemberExpression(itemAtPosition))
1100 suggestVariableDeclarationStatementCompletion(result);
1102 result = makeSnippet(
"{ statements... }",
"{\n\t$0\n}");
1105 result = makeSnippet(
"if (condition) { statements }",
"if ($1) {\n\t$0\n}");
1108 result = makeSnippet(
"do { statements } while (condition);",
"do {\n\t$1\n} while ($0);");
1111 result = makeSnippet(
"while (condition) { statements...}",
"while ($1) {\n\t$0\n}");
1114 result = makeSnippet(
"for (initializer; condition; increment) { statements... }",
1115 "for ($1;$2;$3) {\n\t$0\n}");
1118 result = makeSnippet(
"for (property in object) { statements... }",
"for ($1 in $2) {\n\t$0\n}");
1121 result = makeSnippet(
"for (element of array) { statements... }",
"for ($1 of $2) {\n\t$0\n}");
1124 result = makeSnippet(
"try { statements... } catch(error) { statements... }",
1125 "try {\n\t$1\n} catch($2) {\n\t$0\n}");
1128 result = makeSnippet(
"try { statements... } finally { statements... }",
1129 "try {\n\t$1\n} finally {\n\t$0\n}");
1132 result = makeSnippet(
1133 "try { statements... } catch(error) { statements... } finally { statements... }",
1134 "try {\n\t$1\n} catch($2) {\n\t$3\n} finally {\n\t$0\n}");
1137 for (
const QByteArray &view : {
"return"_ba,
"throw"_ba }) {
1138 CompletionItem item;
1140 item.kind = CompletionItemKind::Keyword;
1158 const DomType currentKind = itemAtPosition.internalKind();
1159 const DomType parentKind = itemAtPosition.directParent().internalKind();
1160 if (currentKind == DomType::ScriptCaseBlock || currentKind == DomType::ScriptCaseClause
1161 || currentKind == DomType::ScriptDefaultClause
1162 || (currentKind == DomType::List
1163 && (parentKind == DomType::ScriptCaseClause
1164 || parentKind == DomType::ScriptDefaultClause))) {
1165 suggestCaseAndDefaultStatementCompletion(result);
1167 suggestContinueAndBreakStatementIfNeeded(itemAtPosition, result);
1170void QQmlLSCompletion::insideForStatementCompletion(
const DomItem &parentForContext,
1171 const QQmlLSCompletionPosition &positionInfo,
1172 BackInsertIterator result)
const
1174 const auto regions = FileLocations::treeOf(parentForContext)->info().regions;
1176 const QQmlJS::SourceLocation leftParenthesis = regions[LeftParenthesisRegion];
1177 const QQmlJS::SourceLocation firstSemicolon = regions[FirstSemicolonTokenRegion];
1178 const QQmlJS::SourceLocation secondSemicolon = regions[SecondSemicolonRegion];
1179 const QQmlJS::SourceLocation rightParenthesis = regions[RightParenthesisRegion];
1181 if (betweenLocations(leftParenthesis, positionInfo, firstSemicolon)) {
1182 suggestJSExpressionCompletion(positionInfo.itemAtPosition, result);
1183 suggestVariableDeclarationStatementCompletion(result,
1184 AppendOption::AppendNothing);
1187 if (betweenLocations(firstSemicolon, positionInfo, secondSemicolon)
1188 || betweenLocations(secondSemicolon, positionInfo, rightParenthesis)) {
1189 suggestJSExpressionCompletion(positionInfo.itemAtPosition, result);
1193 if (afterLocation(rightParenthesis, positionInfo)) {
1194 suggestJSStatementCompletion(positionInfo.itemAtPosition, result);
1199void QQmlLSCompletion::insideScriptLiteralCompletion(
const DomItem ¤tItem,
1200 const QQmlLSCompletionPosition &positionInfo,
1201 BackInsertIterator result)
const
1203 Q_UNUSED(currentItem);
1204 if (positionInfo.cursorPosition.base().isEmpty()) {
1205 suggestJSExpressionCompletion(positionInfo.itemAtPosition, result);
1211 const QQmlLSCompletionPosition &positionInfo,
1212 BackInsertIterator result)
const
1214 const auto regions = FileLocations::treeOf(currentItem)->info().regions;
1215 const QQmlJS::SourceLocation leftParenthesis = regions[LeftParenthesisRegion];
1216 const QQmlJS::SourceLocation rightParenthesis = regions[RightParenthesisRegion];
1217 if (beforeLocation(positionInfo, leftParenthesis)) {
1218 suggestJSExpressionCompletion(positionInfo.itemAtPosition, result);
1221 if (betweenLocations(leftParenthesis, positionInfo, rightParenthesis)) {
1222 suggestJSExpressionCompletion(positionInfo.itemAtPosition, result);
1228 const QQmlLSCompletionPosition &positionInfo,
1229 BackInsertIterator result)
const
1231 const auto regions = FileLocations::treeOf(currentItem)->info().regions;
1232 const QQmlJS::SourceLocation leftParenthesis = regions[LeftParenthesisRegion];
1233 const QQmlJS::SourceLocation rightParenthesis = regions[RightParenthesisRegion];
1234 const QQmlJS::SourceLocation elseKeyword = regions[ElseKeywordRegion];
1236 if (betweenLocations(leftParenthesis, positionInfo, rightParenthesis)) {
1237 suggestJSExpressionCompletion(positionInfo.itemAtPosition, result);
1240 if (betweenLocations(rightParenthesis, positionInfo, elseKeyword)) {
1241 suggestJSStatementCompletion(positionInfo.itemAtPosition, result);
1244 if (afterLocation(elseKeyword, positionInfo)) {
1245 suggestJSStatementCompletion(positionInfo.itemAtPosition, result);
1251 const QQmlLSCompletionPosition &positionInfo,
1252 BackInsertIterator result)
const
1254 const auto regions = FileLocations::treeOf(currentItem)->info().regions;
1255 const QQmlJS::SourceLocation returnKeyword = regions[ReturnKeywordRegion];
1257 if (afterLocation(returnKeyword, positionInfo)) {
1258 suggestJSExpressionCompletion(positionInfo.itemAtPosition, result);
1264 const QQmlLSCompletionPosition &positionInfo,
1265 BackInsertIterator result)
const
1267 const auto regions = FileLocations::treeOf(currentItem)->info().regions;
1268 const QQmlJS::SourceLocation leftParenthesis = regions[LeftParenthesisRegion];
1269 const QQmlJS::SourceLocation rightParenthesis = regions[RightParenthesisRegion];
1271 if (betweenLocations(leftParenthesis, positionInfo, rightParenthesis)) {
1272 suggestJSExpressionCompletion(positionInfo.itemAtPosition, result);
1275 if (afterLocation(rightParenthesis, positionInfo)) {
1276 suggestJSStatementCompletion(positionInfo.itemAtPosition, result);
1281void QQmlLSCompletion::insideDoWhileStatement(
const DomItem &parentForContext,
1282 const QQmlLSCompletionPosition &positionInfo,
1283 BackInsertIterator result)
const
1285 const auto regions = FileLocations::treeOf(parentForContext)->info().regions;
1286 const QQmlJS::SourceLocation doKeyword = regions[DoKeywordRegion];
1287 const QQmlJS::SourceLocation whileKeyword = regions[WhileKeywordRegion];
1288 const QQmlJS::SourceLocation leftParenthesis = regions[LeftParenthesisRegion];
1289 const QQmlJS::SourceLocation rightParenthesis = regions[RightParenthesisRegion];
1291 if (betweenLocations(doKeyword, positionInfo, whileKeyword)) {
1292 suggestJSStatementCompletion(positionInfo.itemAtPosition, result);
1295 if (betweenLocations(leftParenthesis, positionInfo, rightParenthesis)) {
1296 suggestJSExpressionCompletion(positionInfo.itemAtPosition, result);
1301void QQmlLSCompletion::insideForEachStatement(
const DomItem &parentForContext,
1302 const QQmlLSCompletionPosition &positionInfo,
1303 BackInsertIterator result)
const
1305 const auto regions = FileLocations::treeOf(parentForContext)->info().regions;
1307 const QQmlJS::SourceLocation inOf = regions[InOfTokenRegion];
1308 const QQmlJS::SourceLocation leftParenthesis = regions[LeftParenthesisRegion];
1309 const QQmlJS::SourceLocation rightParenthesis = regions[RightParenthesisRegion];
1311 if (betweenLocations(leftParenthesis, positionInfo, inOf)) {
1312 suggestJSExpressionCompletion(positionInfo.itemAtPosition, result);
1313 suggestVariableDeclarationStatementCompletion(result);
1316 if (betweenLocations(inOf, positionInfo, rightParenthesis)) {
1317 suggestJSExpressionCompletion(positionInfo.itemAtPosition, result);
1321 if (afterLocation(rightParenthesis, positionInfo)) {
1322 suggestJSStatementCompletion(positionInfo.itemAtPosition, result);
1327void QQmlLSCompletion::insideSwitchStatement(
const DomItem &parentForContext,
1328 const QQmlLSCompletionPosition positionInfo,
1329 BackInsertIterator result)
const
1331 const auto regions = FileLocations::treeOf(parentForContext)->info().regions;
1333 const QQmlJS::SourceLocation leftParenthesis = regions[LeftParenthesisRegion];
1334 const QQmlJS::SourceLocation rightParenthesis = regions[RightParenthesisRegion];
1336 if (betweenLocations(leftParenthesis, positionInfo, rightParenthesis)) {
1337 suggestJSExpressionCompletion(positionInfo.itemAtPosition, result);
1343 const QQmlLSCompletionPosition &positionInfo,
1344 BackInsertIterator result)
const
1346 const auto regions = FileLocations::treeOf(parentForContext)->info().regions;
1348 const QQmlJS::SourceLocation caseKeyword = regions[CaseKeywordRegion];
1349 const QQmlJS::SourceLocation colonToken = regions[ColonTokenRegion];
1351 if (betweenLocations(caseKeyword, positionInfo, colonToken)) {
1352 suggestJSExpressionCompletion(positionInfo.itemAtPosition, result);
1355 if (afterLocation(colonToken, positionInfo)) {
1356 suggestJSStatementCompletion(positionInfo.itemAtPosition, result);
1363
1364
1365
1366bool QQmlLSCompletion::isCaseOrDefaultBeforeCtx(
const DomItem ¤tClause,
1367 const QQmlLSCompletionPosition &positionInfo,
1368 FileLocationRegion keywordRegion)
const
1370 Q_ASSERT(keywordRegion == QQmlJS::Dom::CaseKeywordRegion
1371 || keywordRegion == QQmlJS::Dom::DefaultKeywordRegion);
1376 const auto token = FileLocations::treeOf(currentClause)->info().regions[keywordRegion];
1377 if (afterLocation(token, positionInfo))
1384
1385
1386
1387
1388
1389
1390
1393 const QQmlLSCompletionPosition &positionInfo)
const
1395 const DomItem caseClauses = parentForContext.field(Fields::caseClauses);
1396 for (
int i = 0; i < caseClauses.indexes(); ++i) {
1397 const DomItem currentClause = caseClauses.index(i);
1398 if (isCaseOrDefaultBeforeCtx(currentClause, positionInfo, QQmlJS::Dom::CaseKeywordRegion)) {
1399 return currentClause;
1403 const DomItem defaultClause = parentForContext.field(Fields::defaultClause);
1404 if (isCaseOrDefaultBeforeCtx(defaultClause, positionInfo, QQmlJS::Dom::DefaultKeywordRegion))
1405 return parentForContext.field(Fields::defaultClause);
1407 const DomItem moreCaseClauses = parentForContext.field(Fields::moreCaseClauses);
1408 for (
int i = 0; i < moreCaseClauses.indexes(); ++i) {
1409 const DomItem currentClause = moreCaseClauses.index(i);
1410 if (isCaseOrDefaultBeforeCtx(currentClause, positionInfo, QQmlJS::Dom::CaseKeywordRegion)) {
1411 return currentClause;
1419 const QQmlLSCompletionPosition &positionInfo,
1420 BackInsertIterator result)
const
1422 const auto regions = FileLocations::treeOf(parentForContext)->info().regions;
1424 const QQmlJS::SourceLocation leftBrace = regions[LeftBraceRegion];
1425 const QQmlJS::SourceLocation rightBrace = regions[RightBraceRegion];
1427 if (!betweenLocations(leftBrace, positionInfo, rightBrace))
1432 if (
const auto previousCase = previousCaseOfCaseBlock(parentForContext, positionInfo)) {
1433 suggestJSStatementCompletion(previousCase, result);
1438 suggestCaseAndDefaultStatementCompletion(result);
1442 const QQmlLSCompletionPosition &positionInfo,
1443 BackInsertIterator result)
const
1445 const auto regions = FileLocations::treeOf(parentForContext)->info().regions;
1447 const QQmlJS::SourceLocation colonToken = regions[ColonTokenRegion];
1449 if (afterLocation(colonToken, positionInfo)) {
1450 suggestJSStatementCompletion(positionInfo.itemAtPosition, result);
1456 const DomItem &parentForContext,
const QQmlLSCompletionPosition &positionInfo,
1457 BackInsertIterator result)
const
1459 const auto regions = FileLocations::treeOf(parentForContext)->info().regions;
1461 const QQmlJS::SourceLocation operatorLocation = regions[OperatorTokenRegion];
1463 if (beforeLocation(positionInfo, operatorLocation)) {
1464 suggestJSExpressionCompletion(positionInfo.itemAtPosition, result);
1467 if (afterLocation(operatorLocation, positionInfo)) {
1468 suggestJSExpressionCompletion(positionInfo.itemAtPosition, result);
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1514 const QQmlLSCompletionPosition &positionInfo,
1515 BackInsertIterator result)
const
1517 const auto regions = FileLocations::treeOf(parentForContext)->info().regions;
1519 const QQmlJS::SourceLocation equal = regions[EqualTokenRegion];
1521 if (!afterLocation(equal, positionInfo))
1525 suggestJSExpressionCompletion(positionInfo.itemAtPosition, result);
1529
1530
1531
1532void QQmlLSCompletion::insideVariableDeclarationEntry(
const DomItem &parentForContext,
1533 const QQmlLSCompletionPosition &positionInfo,
1534 BackInsertIterator result)
const
1536 insideScriptPattern(parentForContext, positionInfo, result);
1539void QQmlLSCompletion::insideThrowStatement(
const DomItem &parentForContext,
1540 const QQmlLSCompletionPosition &positionInfo,
1541 BackInsertIterator result)
const
1543 const auto regions = FileLocations::treeOf(parentForContext)->info().regions;
1545 const QQmlJS::SourceLocation throwKeyword = regions[ThrowKeywordRegion];
1547 if (afterLocation(throwKeyword, positionInfo)) {
1548 suggestJSExpressionCompletion(positionInfo.itemAtPosition, result);
1553void QQmlLSCompletion::insideLabelledStatement(
const DomItem &parentForContext,
1554 const QQmlLSCompletionPosition &positionInfo,
1555 BackInsertIterator result)
const
1557 const auto regions = FileLocations::treeOf(parentForContext)->info().regions;
1559 const QQmlJS::SourceLocation colon = regions[ColonTokenRegion];
1561 if (afterLocation(colon, positionInfo)) {
1562 suggestJSStatementCompletion(positionInfo.itemAtPosition, result);
1573
1574
1575
1576static void collectLabels(
const DomItem &context, QQmlLSCompletion::BackInsertIterator result)
1578 for (DomItem current = context; current; current = current.directParent()) {
1579 if (current.internalKind() == DomType::ScriptLabelledStatement) {
1580 const QString label = current.field(Fields::label).value().toString();
1581 if (label.isEmpty())
1583 CompletionItem item;
1584 item.label = label.toUtf8();
1585 item.kind = CompletionItemKind::Value;
1588 }
else if (current.internalKind() == DomType::ScriptExpression) {
1596void QQmlLSCompletion::insideContinueStatement(
const DomItem &parentForContext,
1597 const QQmlLSCompletionPosition &positionInfo,
1598 BackInsertIterator result)
const
1600 const auto regions = FileLocations::treeOf(parentForContext)->info().regions;
1602 const QQmlJS::SourceLocation continueKeyword = regions[ContinueKeywordRegion];
1604 if (afterLocation(continueKeyword, positionInfo)) {
1605 collectLabels(parentForContext, result);
1610void QQmlLSCompletion::insideBreakStatement(
const DomItem &parentForContext,
1611 const QQmlLSCompletionPosition &positionInfo,
1612 BackInsertIterator result)
const
1614 const auto regions = FileLocations::treeOf(parentForContext)->info().regions;
1616 const QQmlJS::SourceLocation breakKeyword = regions[BreakKeywordRegion];
1618 if (afterLocation(breakKeyword, positionInfo)) {
1619 collectLabels(parentForContext, result);
1624void QQmlLSCompletion::insideConditionalExpression(
const DomItem &parentForContext,
1625 const QQmlLSCompletionPosition &positionInfo,
1626 BackInsertIterator result)
const
1628 const auto regions = FileLocations::treeOf(parentForContext)->info().regions;
1630 const QQmlJS::SourceLocation questionMark = regions[QuestionMarkTokenRegion];
1631 const QQmlJS::SourceLocation colon = regions[ColonTokenRegion];
1633 if (beforeLocation(positionInfo, questionMark)) {
1634 suggestJSExpressionCompletion(positionInfo.itemAtPosition, result);
1637 if (betweenLocations(questionMark, positionInfo, colon)) {
1638 suggestJSExpressionCompletion(positionInfo.itemAtPosition, result);
1641 if (afterLocation(colon, positionInfo)) {
1642 suggestJSExpressionCompletion(positionInfo.itemAtPosition, result);
1647void QQmlLSCompletion::insideUnaryExpression(
const DomItem &parentForContext,
1648 const QQmlLSCompletionPosition &positionInfo,
1649 BackInsertIterator result)
const
1651 const auto regions = FileLocations::treeOf(parentForContext)->info().regions;
1653 const QQmlJS::SourceLocation operatorToken = regions[OperatorTokenRegion];
1655 if (afterLocation(operatorToken, positionInfo)) {
1656 suggestJSExpressionCompletion(positionInfo.itemAtPosition, result);
1661void QQmlLSCompletion::insidePostExpression(
const DomItem &parentForContext,
1662 const QQmlLSCompletionPosition &positionInfo,
1663 BackInsertIterator result)
const
1665 const auto regions = FileLocations::treeOf(parentForContext)->info().regions;
1667 const QQmlJS::SourceLocation operatorToken = regions[OperatorTokenRegion];
1669 if (beforeLocation(positionInfo, operatorToken)) {
1670 suggestJSExpressionCompletion(positionInfo.itemAtPosition, result);
1675void QQmlLSCompletion::insideParenthesizedExpression(
const DomItem &parentForContext,
1676 const QQmlLSCompletionPosition &positionInfo,
1677 BackInsertIterator result)
const
1679 const auto regions = FileLocations::treeOf(parentForContext)->info().regions;
1681 const QQmlJS::SourceLocation leftParenthesis = regions[LeftParenthesisRegion];
1682 const QQmlJS::SourceLocation rightParenthesis = regions[RightParenthesisRegion];
1684 if (betweenLocations(leftParenthesis, positionInfo, rightParenthesis)) {
1685 suggestJSExpressionCompletion(positionInfo.itemAtPosition, result);
1690void QQmlLSCompletion::insideTemplateLiteral(
const DomItem &parentForContext,
1691 const QQmlLSCompletionPosition &positionInfo,
1692 BackInsertIterator result)
const
1694 Q_UNUSED(parentForContext);
1695 suggestJSExpressionCompletion(positionInfo.itemAtPosition, result);
1699 const QQmlLSCompletionPosition &positionInfo,
1700 BackInsertIterator result)
const
1702 const auto regions = FileLocations::treeOf(parentForContext)->info().regions;
1703 const QQmlJS::SourceLocation newKeyword = regions[NewKeywordRegion];
1705 if (afterLocation(newKeyword, positionInfo)) {
1706 suggestJSExpressionCompletion(positionInfo.itemAtPosition, result);
1711void QQmlLSCompletion::insideNewMemberExpression(
const DomItem &parentForContext,
1712 const QQmlLSCompletionPosition &positionInfo,
1713 BackInsertIterator result)
const
1715 const auto regions = FileLocations::treeOf(parentForContext)->info().regions;
1716 const QQmlJS::SourceLocation newKeyword = regions[NewKeywordRegion];
1717 const QQmlJS::SourceLocation leftParenthesis = regions[LeftParenthesisRegion];
1718 const QQmlJS::SourceLocation rightParenthesis = regions[RightParenthesisRegion];
1720 if (betweenLocations(newKeyword, positionInfo, leftParenthesis)
1721 || betweenLocations(leftParenthesis, positionInfo, rightParenthesis)) {
1722 suggestJSExpressionCompletion(positionInfo.itemAtPosition, result);
1727void QQmlLSCompletion::signalHandlerCompletion(
const QQmlJSScope::ConstPtr &scope,
1728 QDuplicateTracker<QString> *usedNames,
1729 BackInsertIterator result)
const
1731 const auto keyValues = scope->methods().asKeyValueRange();
1732 for (
const auto &[name, method] : keyValues) {
1733 if (method.access() != QQmlJSMetaMethod::Public
1734 || method.methodType() != QQmlJSMetaMethodType::Signal) {
1737 if (usedNames && usedNames->hasSeen(name)) {
1741 CompletionItem completion;
1742 completion.label = QQmlSignalNames::signalNameToHandlerName(name).toUtf8();
1743 completion.kind = CompletionItemKind::Method;
1744 result = completion;
1749
1750
1751
1754 const CompletionContextStrings &contextStrings)
const
1756 QList<CompletionItem> result;
1757 collectCompletions(currentItem, contextStrings, std::back_inserter(result));
1762 const CompletionContextStrings &contextStrings,
1763 BackInsertIterator result)
const
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786 const QQmlLSCompletionPosition positionInfo{ currentItem, contextStrings };
1787 for (DomItem currentParent = currentItem; currentParent;
1788 currentParent = currentParent.directParent()) {
1789 const DomType currentType = currentParent.internalKind();
1791 switch (currentType) {
1795 case DomType::Pragma:
1796 insidePragmaCompletion(currentParent, positionInfo, result);
1798 case DomType::ScriptType: {
1799 if (currentParent.directParent().internalKind() == DomType::QmlObject) {
1800 insideQmlObjectCompletion(currentParent.directParent(), positionInfo, result);
1804 LocalSymbolsTypes options;
1805 options.setFlag(LocalSymbolsType::ObjectType);
1806 options.setFlag(LocalSymbolsType::ValueType);
1807 suggestReachableTypes(currentItem, options, CompletionItemKind::Class, result);
1810 case DomType::ScriptFormalParameter:
1813 case DomType::Binding:
1814 insideBindingCompletion(currentParent, positionInfo, result);
1816 case DomType::Import:
1817 insideImportCompletion(currentParent, positionInfo, result);
1819 case DomType::ScriptForStatement:
1820 insideForStatementCompletion(currentParent, positionInfo, result);
1822 case DomType::ScriptBlockStatement:
1823 suggestJSStatementCompletion(positionInfo.itemAtPosition, result);
1825 case DomType::QmlFile:
1826 insideQmlFileCompletion(currentParent, positionInfo, result);
1828 case DomType::QmlObject:
1829 insideQmlObjectCompletion(currentParent, positionInfo, result);
1831 case DomType::MethodInfo:
1834 case DomType::PropertyDefinition:
1835 insidePropertyDefinitionCompletion(currentParent, positionInfo, result);
1837 case DomType::ScriptBinaryExpression:
1839 if (QQmlLSUtils::isFieldMemberExpression(currentParent))
1841 insideBinaryExpressionCompletion(currentParent, positionInfo, result);
1843 case DomType::ScriptLiteral:
1844 insideScriptLiteralCompletion(currentParent, positionInfo, result);
1846 case DomType::ScriptRegExpLiteral:
1849 case DomType::ScriptCallExpression:
1850 insideCallExpression(currentParent, positionInfo, result);
1852 case DomType::ScriptIfStatement:
1853 insideIfStatement(currentParent, positionInfo, result);
1855 case DomType::ScriptReturnStatement:
1856 insideReturnStatement(currentParent, positionInfo, result);
1858 case DomType::ScriptWhileStatement:
1859 insideWhileStatement(currentParent, positionInfo, result);
1861 case DomType::ScriptDoWhileStatement:
1862 insideDoWhileStatement(currentParent, positionInfo, result);
1864 case DomType::ScriptForEachStatement:
1865 insideForEachStatement(currentParent, positionInfo, result);
1867 case DomType::ScriptTryCatchStatement:
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1887 case DomType::ScriptSwitchStatement:
1888 insideSwitchStatement(currentParent, positionInfo, result);
1890 case DomType::ScriptCaseClause:
1891 insideCaseClause(currentParent, positionInfo, result);
1893 case DomType::ScriptDefaultClause:
1894 if (ctxBeforeStatement(positionInfo, currentParent, QQmlJS::Dom::DefaultKeywordRegion))
1896 insideDefaultClause(currentParent, positionInfo, result);
1898 case DomType::ScriptCaseBlock:
1899 insideCaseBlock(currentParent, positionInfo, result);
1901 case DomType::ScriptVariableDeclaration:
1906 case DomType::ScriptVariableDeclarationEntry:
1907 insideVariableDeclarationEntry(currentParent, positionInfo, result);
1909 case DomType::ScriptProperty:
1912 case DomType::ScriptPattern:
1913 insideScriptPattern(currentParent, positionInfo, result);
1915 case DomType::ScriptThrowStatement:
1916 insideThrowStatement(currentParent, positionInfo, result);
1918 case DomType::ScriptLabelledStatement:
1919 insideLabelledStatement(currentParent, positionInfo, result);
1921 case DomType::ScriptContinueStatement:
1922 insideContinueStatement(currentParent, positionInfo, result);
1924 case DomType::ScriptBreakStatement:
1925 insideBreakStatement(currentParent, positionInfo, result);
1927 case DomType::ScriptConditionalExpression:
1928 insideConditionalExpression(currentParent, positionInfo, result);
1930 case DomType::ScriptUnaryExpression:
1931 insideUnaryExpression(currentParent, positionInfo, result);
1933 case DomType::ScriptPostExpression:
1934 insidePostExpression(currentParent, positionInfo, result);
1936 case DomType::ScriptParenthesizedExpression:
1937 insideParenthesizedExpression(currentParent, positionInfo, result);
1939 case DomType::ScriptTemplateLiteral:
1940 insideTemplateLiteral(currentParent, positionInfo, result);
1942 case DomType::ScriptTemplateStringPart:
1945 case DomType::ScriptNewExpression:
1946 insideNewExpression(currentParent, positionInfo, result);
1948 case DomType::ScriptNewMemberExpression:
1949 insideNewMemberExpression(currentParent, positionInfo, result);
1951 case DomType::ScriptThisExpression:
1954 case DomType::ScriptSuperLiteral:
1957 case DomType::Comment:
1963 case DomType::ScriptArray:
1964 case DomType::ScriptObject:
1965 case DomType::ScriptElision:
1966 case DomType::ScriptArrayEntry:
1976 qCDebug(QQmlLSUtilsLog) <<
"No completion was found for current request.";
1982 const auto keys = pluginLoader.metaDataKeys();
1983 for (qsizetype i = 0; i < keys.size(); ++i) {
1984 auto instance = std::unique_ptr<QQmlLSPlugin>(
1985 qobject_cast<QQmlLSPlugin *>(pluginLoader.instance(i)));
1988 if (
auto completionInstance = instance->createCompletionPlugin())
1989 m_plugins.push_back(std::move(completionInstance));
1994
1995
1996
1997void QQmlLSCompletion::collectFromPlugins(qxp::function_ref<CompletionFromPluginFunction> f,
1998 BackInsertIterator result)
const
2000 for (
const auto &plugin : m_plugins) {
2002 f(plugin.get(), result);
2006void QQmlLSCompletion::suggestSnippetsForLeftHandSideOfBinding(
const DomItem &itemAtPosition,
2007 BackInsertIterator result)
const
2010 [&itemAtPosition](QQmlLSCompletionPlugin *p, BackInsertIterator result) {
2011 p->suggestSnippetsForLeftHandSideOfBinding(itemAtPosition, result);
2016void QQmlLSCompletion::suggestSnippetsForRightHandSideOfBinding(
const DomItem &itemAtPosition,
2017 BackInsertIterator result)
const
2020 [&itemAtPosition](QQmlLSCompletionPlugin *p, BackInsertIterator result) {
2021 p->suggestSnippetsForRightHandSideOfBinding(itemAtPosition, result);
QQmlLSCompletion provides completions for all kinds of QML and JS constructs.
void exploreImportPaths(const QStringList &importPaths)
QList< CompletionItem > completions(const DomItem ¤tItem, const CompletionContextStrings &ctx) const
QQmlLSCompletion(const QFactoryLoader &pluginLoader)
Combined button and popup list for selecting options.
void collectFromAllJavaScriptParents(const F &&f, const QQmlJSScope::ConstPtr &scope)
static const QQmlJSScope * resolve(const QQmlJSScope *current, const QStringList &names)
static void collectLabels(const DomItem &context, QQmlLSCompletion::BackInsertIterator result)
static bool testScopeSymbol(const QQmlJSScope::ConstPtr &scope, LocalSymbolsTypes options, CompletionItemKind kind)