aboutsummaryrefslogtreecommitdiffstats
path: root/src/qmlls/qqmllscompletion.cpp
blob: 8ecbcffc7020363813bf87148315556b403cd90d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
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
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
// Copyright (C) 2024 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#include "qqmllscompletion_p.h"

using namespace QLspSpecification;
using namespace QQmlJS::Dom;
using namespace Qt::StringLiterals;

QT_BEGIN_NAMESPACE

Q_LOGGING_CATEGORY(QQmlLSCompletionLog, "qt.languageserver.completions")

/*!
\class QQmlLSCompletion
\internal
\brief QQmlLSCompletion provides completions for all kinds of QML and JS constructs.

Use the \l{completions} method to obtain completions at a certain DomItem.

All the other methods in this class are helper methods: some compute completions for specific QML
and JS constructs and some are shared between multiple QML or JS constructs to avoid code
duplication. Most of the helper methods add their completion items via a BackInsertIterator.

Some helper methods are called "suggest*" and will try to suggest code that does not exist yet. For
example, any JS statement can be expected inside a Blockstatement so suggestJSStatementCompletion()
is used to suggest JS statements inside of BlockStatements. Another example might be
suggestReachableTypes() that will suggest Types for type annotations, attached types or Qml Object
hierarchies, or suggestCaseAndDefaultStatementCompletion() that will only suggest "case" and
"default" clauses for switch statements.

Some helper methods are called "inside*" and will try to suggest code inside an existing structure.
For example, insideForStatementCompletion() will try to suggest completion for the different code
pieces initializer, condition, increment and statement that exist inside of:
\badcode
for(initializer; condition; increment)
    statement
\endcode
*/

CompletionItem QQmlLSCompletion::makeSnippet(QUtf8StringView qualifier, QUtf8StringView label,
                                             QUtf8StringView insertText)
{
    CompletionItem res;
    if (!qualifier.isEmpty()) {
        res.label = qualifier.data();
        res.label += '.';
    }
    res.label += label.data();
    res.insertTextFormat = InsertTextFormat::Snippet;
    if (!qualifier.isEmpty()) {
        res.insertText = qualifier.data();
        *res.insertText += '.';
        *res.insertText += insertText.data();
    } else {
        res.insertText = insertText.data();
    }
    res.kind = int(CompletionItemKind::Snippet);
    res.insertTextMode = InsertTextMode::AdjustIndentation;
    return res;
}

CompletionItem QQmlLSCompletion::makeSnippet(QUtf8StringView label, QUtf8StringView insertText)
{
    return makeSnippet(QByteArray(), label, insertText);
}

/*!
\internal
\brief Compare left and right locations to the position denoted by ctx, see special cases below.

Statements and expressions need to provide different completions depending on where the cursor is.
For example, lets take following for-statement:
\badcode
for (let i = 0; <here> ; ++i) {}
\endcode
We want to provide script expression completion (method names, property names, available JS
variables names, QML objects ids, and so on) at the place denoted by \c{<here>}.
The question is: how do we know that the cursor is really at \c{<here>}? In the case of the
for-loop, we can compare the position of the cursor with the first and the second semicolon of the
for loop.

If the first semicolon does not exist, it has an invalid sourcelocation and the cursor is
definitively \e{not} at \c{<here>}. Therefore, return false when \c{left} is invalid.

If the second semicolon does not exist, then just ignore it: it might not have been written yet.
*/
bool QQmlLSCompletion::betweenLocations(QQmlJS::SourceLocation left,
                                        const QQmlLSCompletionPosition &positionInfo,
                                        QQmlJS::SourceLocation right) const
{
    if (!left.isValid())
        return false;
    // note: left.end() == ctx.offset() means that the cursor lies exactly after left
    if (!(left.end() <= positionInfo.offset()))
        return false;
    if (!right.isValid())
        return true;

    // note: ctx.offset() == right.begin() means that the cursor lies exactly before right
    return positionInfo.offset() <= right.begin();
}

/*!
\internal
Returns true if ctx denotes an offset lying behind left.end(), and false otherwise.
*/
bool QQmlLSCompletion::afterLocation(QQmlJS::SourceLocation left,
                                     const QQmlLSCompletionPosition &positionInfo) const
{
    return betweenLocations(left, positionInfo, QQmlJS::SourceLocation{});
}

/*!
\internal
Returns true if ctx denotes an offset lying before right.begin(), and false otherwise.
*/
bool QQmlLSCompletion::beforeLocation(const QQmlLSCompletionPosition &ctx,
                                      QQmlJS::SourceLocation right) const
{
    if (!right.isValid())
        return true;

    // note: ctx.offset() == right.begin() means that the cursor lies exactly before right
    if (ctx.offset() <= right.begin())
        return true;

    return false;
}

bool QQmlLSCompletion::ctxBeforeStatement(const QQmlLSCompletionPosition &positionInfo,
                                          const DomItem &parentForContext,
                                          FileLocationRegion firstRegion) const
{
    const auto regions = FileLocations::treeOf(parentForContext)->info().regions;
    const bool result = beforeLocation(positionInfo, regions[firstRegion]);
    return result;
}

void
QQmlLSCompletion::suggestBindingCompletion(const DomItem &itemAtPosition, BackInsertIterator it) const
{
    suggestReachableTypes(itemAtPosition, LocalSymbolsType::AttachedType, CompletionItemKind::Class,
                          it);

    const QQmlJSScope::ConstPtr scope = [&]() {
        if (!QQmlLSUtils::isFieldMemberAccess(itemAtPosition))
            return itemAtPosition.qmlObject().semanticScope();

        const DomItem owner = itemAtPosition.directParent().field(Fields::left);
        auto expressionType = QQmlLSUtils::resolveExpressionType(
                owner, ResolveActualTypeForFieldMemberExpression);
        return expressionType ? expressionType->semanticScope : QQmlJSScope::ConstPtr{};
    }();

    if (!scope)
        return;

    propertyCompletion(scope, nullptr, it);
    signalHandlerCompletion(scope, nullptr, it);
}

void QQmlLSCompletion::insideImportCompletionHelper(const DomItem &file,
                                                    const QQmlLSCompletionPosition &positionInfo,
                                                    BackInsertIterator it) const
{
    // returns completions for import statements, ctx is supposed to be in an import statement
    const CompletionContextStrings &ctx = positionInfo.cursorPosition;
    ImportCompletionType importCompletionType = ImportCompletionType::None;
    QRegularExpression spaceRe(uR"(\W+)"_s);
    QList<QStringView> linePieces = ctx.preLine().split(spaceRe, Qt::SkipEmptyParts);
    qsizetype effectiveLength = linePieces.size()
            + ((!ctx.preLine().isEmpty() && ctx.preLine().last().isSpace()) ? 1 : 0);
    if (effectiveLength < 2) {
        CompletionItem comp;
        comp.label = "import";
        comp.kind = int(CompletionItemKind::Keyword);
        it = comp;
    }
    if (linePieces.isEmpty() || linePieces.first() != u"import")
        return;
    if (effectiveLength == 2) {
        // the cursor is after the import, possibly in a partial module name
        importCompletionType = ImportCompletionType::Module;
    } else if (effectiveLength == 3) {
        if (linePieces.last() != u"as") {
            // the cursor is after the module, possibly in a partial version token (or partial as)
            CompletionItem comp;
            comp.label = "as";
            comp.kind = int(CompletionItemKind::Keyword);
            it = comp;
            importCompletionType = ImportCompletionType::Version;
        }
    }
    DomItem env = file.environment();
    if (std::shared_ptr<DomEnvironment> envPtr = env.ownerAs<DomEnvironment>()) {
        switch (importCompletionType) {
        case ImportCompletionType::None:
            break;
        case ImportCompletionType::Module: {
            QDuplicateTracker<QString> modulesSeen;
            for (const QString &uri : envPtr->moduleIndexUris(env)) {
                QStringView base = ctx.base(); // if we allow spaces we should get rid of them
                if (uri.startsWith(base)) {
                    QStringList rest = uri.mid(base.size()).split(u'.');
                    if (rest.isEmpty())
                        continue;

                    const QString label = rest.first();
                    if (!modulesSeen.hasSeen(label)) {
                        CompletionItem comp;
                        comp.label = label.toUtf8();
                        comp.kind = int(CompletionItemKind::Module);
                        it = comp;
                    }
                }
            }
            break;
        }
        case ImportCompletionType::Version:
            if (ctx.base().isEmpty()) {
                for (int majorV :
                     envPtr->moduleIndexMajorVersions(env, linePieces.at(1).toString())) {
                    CompletionItem comp;
                    comp.label = QString::number(majorV).toUtf8();
                    comp.kind = int(CompletionItemKind::Constant);
                    it = comp;
                }
            } else {
                bool hasMajorVersion = ctx.base().endsWith(u'.');
                int majorV = -1;
                if (hasMajorVersion)
                    majorV = ctx.base().mid(0, ctx.base().size() - 1).toInt(&hasMajorVersion);
                if (!hasMajorVersion)
                    break;
                if (std::shared_ptr<ModuleIndex> mIndex =
                            envPtr->moduleIndexWithUri(env, linePieces.at(1).toString(), majorV)) {
                    for (int minorV : mIndex->minorVersions()) {
                        CompletionItem comp;
                        comp.label = QString::number(minorV).toUtf8();
                        comp.kind = int(CompletionItemKind::Constant);
                        it = comp;
                    }
                }
            }
            break;
        }
    }
}

void QQmlLSCompletion::idsCompletions(const DomItem &component, BackInsertIterator it) const
{
    qCDebug(QQmlLSCompletionLog) << "adding ids completions";
    for (const QString &k : component.field(Fields::ids).keys()) {
        CompletionItem comp;
        comp.label = k.toUtf8();
        comp.kind = int(CompletionItemKind::Value);
        it = comp;
    }
}

static bool testScopeSymbol(const QQmlJSScope::ConstPtr &scope, LocalSymbolsTypes options,
                            CompletionItemKind kind)
{
    const bool currentIsSingleton = scope->isSingleton();
    const bool currentIsAttached = !scope->attachedType().isNull();
    if ((options & LocalSymbolsType::Singleton) && currentIsSingleton) {
        return true;
    }
    if ((options & LocalSymbolsType::AttachedType) && currentIsAttached) {
        return true;
    }
    const bool isObjectType = scope->isReferenceType();
    if (options & LocalSymbolsType::ObjectType && !currentIsSingleton && isObjectType) {
        return kind != CompletionItemKind::Constructor || scope->isCreatable();
    }
    if (options & LocalSymbolsType::ValueType && !currentIsSingleton && !isObjectType) {
        return true;
    }
    return false;
}

/*!
\internal
Obtain the types reachable from \c{el} as a CompletionItems.
*/
void QQmlLSCompletion::suggestReachableTypes(const DomItem &el, LocalSymbolsTypes options,
                                             CompletionItemKind kind, BackInsertIterator it) const
{
    auto file = el.containingFile().as<QmlFile>();
    if (!file)
        return;
    auto resolver = file->typeResolver();
    if (!resolver)
        return;

    const QString requiredQualifiers = QQmlLSUtils::qualifiersFrom(el);
    const auto keyValueRange = resolver->importedTypes().asKeyValueRange();
    for (const auto &type : keyValueRange) {
        // ignore special QQmlJSImporterMarkers
        const bool isMarkerType = type.first.contains(u"$internal$.")
                || type.first.contains(u"$anonymous$.") || type.first.contains(u"$module$.");
        if (isMarkerType || !type.first.startsWith(requiredQualifiers))
            continue;

        auto &scope = type.second.scope;
        if (!scope)
            continue;

        if (!testScopeSymbol(scope, options, kind))
            continue;

        CompletionItem completion;
        completion.label = QStringView(type.first).sliced(requiredQualifiers.size()).toUtf8();
        completion.kind = int(kind);
        it = completion;
    }
}

void QQmlLSCompletion::jsIdentifierCompletion(const QQmlJSScope::ConstPtr &scope,
                                              QDuplicateTracker<QString> *usedNames,
                                              BackInsertIterator it) const
{
    for (const auto &[name, jsIdentifier] : scope->ownJSIdentifiers().asKeyValueRange()) {
        CompletionItem completion;
        if (usedNames && usedNames->hasSeen(name)) {
            continue;
        }
        completion.label = name.toUtf8();
        completion.kind = int(CompletionItemKind::Variable);
        QString detail = u"has type "_s;
        if (jsIdentifier.typeName) {
            if (jsIdentifier.isConst) {
                detail.append(u"const ");
            }
            detail.append(*jsIdentifier.typeName);
        } else {
            detail.append(jsIdentifier.isConst ? u"const"_s : u"var"_s);
        }
        completion.detail = detail.toUtf8();
        it = completion;
    }
}

void QQmlLSCompletion::methodCompletion(const QQmlJSScope::ConstPtr &scope,
                                        QDuplicateTracker<QString> *usedNames,
                                        BackInsertIterator it) const
{
    // JS functions in current and base scopes
    for (const auto &[name, method] : scope->methods().asKeyValueRange()) {
        if (method.access() != QQmlJSMetaMethod::Public)
            continue;
        if (usedNames && usedNames->hasSeen(name)) {
            continue;
        }
        CompletionItem completion;
        completion.label = name.toUtf8();
        completion.kind = int(CompletionItemKind::Method);
        it = completion;
        // TODO: QQmlLSUtils::reachableSymbols seems to be able to do documentation and detail
        // and co, it should also be done here if possible.
    }
}

void QQmlLSCompletion::propertyCompletion(const QQmlJSScope::ConstPtr &scope,
                                          QDuplicateTracker<QString> *usedNames,
                                          BackInsertIterator it) const
{
    for (const auto &[name, property] : scope->properties().asKeyValueRange()) {
        if (usedNames && usedNames->hasSeen(name)) {
            continue;
        }
        CompletionItem completion;
        completion.label = name.toUtf8();
        completion.kind = int(CompletionItemKind::Property);
        QString detail{ u"has type "_s };
        if (!property.isWritable())
            detail.append(u"readonly "_s);
        detail.append(property.typeName().isEmpty() ? u"var"_s : property.typeName());
        completion.detail = detail.toUtf8();
        it = completion;
    }
}

void QQmlLSCompletion::enumerationCompletion(const QQmlJSScope::ConstPtr &scope,
                                             QDuplicateTracker<QString> *usedNames,
                                             BackInsertIterator it) const
{
    for (const QQmlJSMetaEnum &enumerator : scope->enumerations()) {
        if (usedNames && usedNames->hasSeen(enumerator.name())) {
            continue;
        }
        CompletionItem completion;
        completion.label = enumerator.name().toUtf8();
        completion.kind = static_cast<int>(CompletionItemKind::Enum);
        it = completion;
    }
}

void QQmlLSCompletion::enumerationValueCompletionHelper(const QStringList &enumeratorKeys,
                                                        BackInsertIterator it) const
{
    for (const QString &enumeratorKey : enumeratorKeys) {
        CompletionItem completion;
        completion.label = enumeratorKey.toUtf8();
        completion.kind = static_cast<int>(CompletionItemKind::EnumMember);
        it = completion;
    }
}

/*!
\internal
Creates completion items for enumerationvalues.
If enumeratorName is a valid enumerator then only do completion for the requested enumerator, and
otherwise do completion for \b{all other possible} enumerators.

For example:
```
id: someItem
enum Hello { World }
enum MyEnum { ValueOne, ValueTwo }

// Hello does refer to a enumerator:
property var a: Hello.<complete only World here>

// someItem does not refer to a enumerator:
property var b: someItem.<complete World, ValueOne and ValueTwo here>
```
*/

void QQmlLSCompletion::enumerationValueCompletion(const QQmlJSScope::ConstPtr &scope,
                                                  const QString &enumeratorName,
                                                  BackInsertIterator result) const
{
    auto enumerator = scope->enumeration(enumeratorName);
    if (enumerator.isValid()) {
        enumerationValueCompletionHelper(enumerator.keys(), result);
        return;
    }

    for (const QQmlJSMetaEnum &enumerator : scope->enumerations()) {
        enumerationValueCompletionHelper(enumerator.keys(), result);
    }
}

/*!
\internal
Calls F on all JavaScript-parents of scope. For example, you can use this method to
collect all the JavaScript Identifiers from following code:
```
{ // this block statement contains only 'x'
    let x = 3;
    { // this block statement contains only 'y', and 'x' has to be retrieved via its parent.
        let y = 4;
    }
}
```
*/
template<typename F>
void collectFromAllJavaScriptParents(const F &&f, const QQmlJSScope::ConstPtr &scope)
{
    for (QQmlJSScope::ConstPtr current = scope; current; current = current->parentScope()) {
        f(current);
        if (current->scopeType() == QQmlSA::ScopeType::QMLScope)
            return;
    }
}

/*!
\internal
Generate autocompletions for JS expressions, suggest possible properties, methods, etc.

If scriptIdentifier is inside a Field Member Expression, like \c{onCompleted} in
\c{Component.onCompleted} for example, then this method will only suggest properties, methods, etc
from the correct type. For the previous example that would be properties, methods, etc. from the
Component attached type.
*/
void QQmlLSCompletion::suggestJSExpressionCompletion(const DomItem &scriptIdentifier,
                                                     BackInsertIterator result) const
{
    QDuplicateTracker<QString> usedNames;
    QQmlJSScope::ConstPtr nearestScope;

    // note: there is an edge case, where the user asks for completion right after the dot
    // of some qualified expression like `root.hello`. In this case, scriptIdentifier is actually
    // the BinaryExpression instead of the left-hand-side that has not be written down yet.
    const bool askForCompletionOnDot = QQmlLSUtils::isFieldMemberExpression(scriptIdentifier);
    const bool hasQualifier =
            QQmlLSUtils::isFieldMemberAccess(scriptIdentifier) || askForCompletionOnDot;

    if (!hasQualifier) {
        for (QUtf8StringView view : std::array<QUtf8StringView, 3>{ "null", "false", "true" }) {
            CompletionItem completion;
            completion.label = view.data();
            completion.kind = int(CompletionItemKind::Value);
            result = completion;
        }
        idsCompletions(scriptIdentifier.component(), result);
        suggestReachableTypes(scriptIdentifier,
                              LocalSymbolsType::Singleton | LocalSymbolsType::AttachedType,
                              CompletionItemKind::Class, result);

        auto scope = scriptIdentifier.nearestSemanticScope();
        if (!scope)
            return;
        nearestScope = scope;

        enumerationCompletion(nearestScope, &usedNames, result);
    } else {
        const DomItem owner =
                (askForCompletionOnDot ? scriptIdentifier : scriptIdentifier.directParent())
                        .field(Fields::left);
        auto expressionType = QQmlLSUtils::resolveExpressionType(
                owner, ResolveActualTypeForFieldMemberExpression);
        if (!expressionType || !expressionType->semanticScope)
            return;
        nearestScope = expressionType->semanticScope;
        // Use root element scope to use find the enumerations
        // This should be changed when we support usages in external files
        if (expressionType->type == QmlComponentIdentifier)
            nearestScope = owner.rootQmlObject(GoTo::MostLikely).semanticScope();
        if (expressionType->name) {
            // note: you only get enumeration values in qualified expressions, never alone
            enumerationValueCompletion(nearestScope, *expressionType->name, result);

            // skip enumeration types if already inside an enumeration type
            if (auto enumerator = nearestScope->enumeration(*expressionType->name);
                !enumerator.isValid()) {
                enumerationCompletion(nearestScope, &usedNames, result);
            }

            if (expressionType->type == EnumeratorIdentifier)
                return;
        }
    }

    Q_ASSERT(nearestScope);

    methodCompletion(nearestScope, &usedNames, result);
    propertyCompletion(nearestScope, &usedNames, result);

    if (!hasQualifier) {
        // collect all of the stuff from parents
        collectFromAllJavaScriptParents(
                [this, &usedNames, result](const QQmlJSScope::ConstPtr &scope) {
                    jsIdentifierCompletion(scope, &usedNames, result);
                },
                nearestScope);
        collectFromAllJavaScriptParents(
                [this, &usedNames, result](const QQmlJSScope::ConstPtr &scope) {
                    methodCompletion(scope, &usedNames, result);
                },
                nearestScope);
        collectFromAllJavaScriptParents(
                [this, &usedNames, result](const QQmlJSScope::ConstPtr &scope) {
                    propertyCompletion(scope, &usedNames, result);
                },
                nearestScope);

        auto file = scriptIdentifier.containingFile().as<QmlFile>();
        if (!file)
            return;
        auto resolver = file->typeResolver();
        if (!resolver)
            return;

        const auto globals = resolver->jsGlobalObject();
        methodCompletion(globals, &usedNames, result);
        propertyCompletion(globals, &usedNames, result);
    }
}

static const QQmlJSScope *resolve(const QQmlJSScope *current, const QStringList &names)
{
    for (const QString &name : names) {
        if (auto property = current->property(name); property.isValid()) {
            if (auto propertyType = property.type().get()) {
                current = propertyType;
                continue;
            }
        }
        return {};
    }
    return current;
}

bool QQmlLSCompletion::cursorInFrontOfItem(const DomItem &parentForContext,
                                           const QQmlLSCompletionPosition &positionInfo)
{
    auto fileLocations = FileLocations::treeOf(parentForContext)->info().fullRegion;
    return positionInfo.offset() <= fileLocations.offset;
}

bool QQmlLSCompletion::cursorAfterColon(const DomItem &currentItem,
                                        const QQmlLSCompletionPosition &positionInfo)
{
    auto location = FileLocations::treeOf(currentItem)->info();
    auto region = location.regions.constFind(ColonTokenRegion);

    if (region == location.regions.constEnd())
        return false;

    if (region.value().isValid() && region.value().offset < positionInfo.offset()) {
        return true;
    }
    return false;
}

/*!
\internal
\brief Mapping from pragma names to allowed pragma values.

This mapping of pragma names to pragma values is not complete. In fact, it only contains the
pragma names and values that one should see autocompletion for.
Some pragmas like FunctionSignatureBehavior or Strict or the Reference/Value of ValueTypeBehavior,
for example, should currently not be proposed as completion items by qmlls.

An empty QList-value in the QMap means that the pragma does not accept pragma values.
*/
static const QMap<QString, QList<QString>> valuesForPragmas{
    { u"ComponentBehavior"_s, { u"Unbound"_s, u"Bound"_s } },
    { u"NativeMethodBehavior"_s, { u"AcceptThisObject"_s, u"RejectThisObject"_s } },
    { u"ListPropertyAssignBehavior"_s, { u"Append"_s, u"Replace"_s, u"ReplaceIfNotDefault"_s } },
    { u"Singleton"_s, {} },
    { u"ValueTypeBehavior"_s, { u"Addressable"_s, u"Inaddressable"_s } },
};

void QQmlLSCompletion::insidePragmaCompletion(QQmlJS::Dom::DomItem currentItem,
                                              const QQmlLSCompletionPosition &positionInfo,
                                              BackInsertIterator result) const
{
    if (cursorAfterColon(currentItem, positionInfo)) {
        const QString name = currentItem.field(Fields::name).value().toString();
        auto values = valuesForPragmas.constFind(name);
        if (values == valuesForPragmas.constEnd())
            return;

        for (const auto &value : *values) {
            CompletionItem comp;
            comp.label = value.toUtf8();
            comp.kind = static_cast<int>(CompletionItemKind::Value);
            result = comp;
        }
        return;
    }

    for (const auto &pragma : valuesForPragmas.asKeyValueRange()) {
        CompletionItem comp;
        comp.label = pragma.first.toUtf8();
        if (!pragma.second.isEmpty()) {
            comp.insertText = QString(pragma.first).append(u": ").toUtf8();
        }
        comp.kind = static_cast<int>(CompletionItemKind::Value);
        result = comp;
    }
}

void QQmlLSCompletion::insideQmlObjectCompletion(const DomItem &parentForContext,
                                                 const QQmlLSCompletionPosition &positionInfo,
                                                 BackInsertIterator result) const
{

    const auto regions = FileLocations::treeOf(parentForContext)->info().regions;

    const QQmlJS::SourceLocation leftBrace = regions[LeftBraceRegion];
    const QQmlJS::SourceLocation rightBrace = regions[RightBraceRegion];

    if (beforeLocation(positionInfo, leftBrace)) {
        LocalSymbolsTypes options;
        options.setFlag(LocalSymbolsType::ObjectType);
        suggestReachableTypes(positionInfo.itemAtPosition, options, CompletionItemKind::Constructor,
                              result);
        suggestSnippetsForLeftHandSideOfBinding(positionInfo.itemAtPosition, result);

        if (QQmlLSUtils::isFieldMemberExpression(positionInfo.itemAtPosition)) {
            /*!
                \internal
                In the case that a missing identifier is followed by an assignment to the default
                property, the parser will create a QmlObject out of both binding and default
               binding. For example, in \code property int x: root. Item {} \endcode the parser will
               create one binding containing one QmlObject of type `root.Item`, instead of two
               bindings (one for `x` and one for the default property). For this special case, if
               completion is requested inside `root.Item`, then try to also suggest JS expressions.

                Note: suggestJSExpressionCompletion() will suggest nothing if the
               fieldMemberExpression starts with the name of a qualified module or a filename, so
               this only adds invalid suggestions in the case that there is something shadowing the
               qualified module name or filename, like a property name for example.

                Note 2: This does not happen for field member accesses. For example, in
                \code
                property int x: root.x
                Item {}
                \endcode
                The parser will create both bindings correctly.
            */
            suggestJSExpressionCompletion(positionInfo.itemAtPosition, result);
        }
        return;
    }

    if (betweenLocations(leftBrace, positionInfo, rightBrace)) {
        // default/required property completion
        for (QUtf8StringView view :
             std::array<QUtf8StringView, 6>{ "", "readonly ", "default ", "default required ",
                                             "required default ", "required " }) {
            // readonly properties require an initializer
            if (view != QUtf8StringView("readonly ")) {
                result = makeSnippet(
                        QByteArray(view.data()).append("property type name;"),
                        QByteArray(view.data()).append("property ${1:type} ${0:name};"));
            }

            result = makeSnippet(
                    QByteArray(view.data()).append("property type name: value;"),
                    QByteArray(view.data()).append("property ${1:type} ${2:name}: ${0:value};"));
        }

        // signal
        result = makeSnippet("signal name(arg1:type1, ...)", "signal ${1:name}($0)");

        // signal without parameters
        result = makeSnippet("signal name;", "signal ${0:name};");

        // make already existing property required
        result = makeSnippet("required name;", "required ${0:name};");

        // function
        result = makeSnippet("function name(args...): returnType { statements...}",
                             "function ${1:name}($2): ${3:returnType} {\n\t$0\n}");

        // enum
        result = makeSnippet("enum name { Values...}", "enum ${1:name} {\n\t${0:values}\n}");

        // inline component
        result = makeSnippet("component Name: BaseType { ... }",
                             "component ${1:name}: ${2:baseType} {\n\t$0\n}");

        // add bindings
        const DomItem containingObject = parentForContext.qmlObject();
        suggestBindingCompletion(containingObject, result);

        // add Qml Types for default binding
        const DomItem containingFile = parentForContext.containingFile();
        suggestReachableTypes(containingFile, LocalSymbolsType::ObjectType,
                              CompletionItemKind::Constructor, result);
        suggestSnippetsForLeftHandSideOfBinding(positionInfo.itemAtPosition, result);
        return;
    }
}

void QQmlLSCompletion::insidePropertyDefinitionCompletion(
        const DomItem &currentItem, const QQmlLSCompletionPosition &positionInfo,
        BackInsertIterator result) const
{
    auto info = FileLocations::treeOf(currentItem)->info();
    const QQmlJS::SourceLocation propertyKeyword = info.regions[PropertyKeywordRegion];

    // do completions for the keywords
    if (positionInfo.offset() < propertyKeyword.offset + propertyKeyword.length) {
        const QQmlJS::SourceLocation readonlyKeyword = info.regions[ReadonlyKeywordRegion];
        const QQmlJS::SourceLocation defaultKeyword = info.regions[DefaultKeywordRegion];
        const QQmlJS::SourceLocation requiredKeyword = info.regions[RequiredKeywordRegion];

        bool completeReadonly = true;
        bool completeRequired = true;
        bool completeDefault = true;

        // if there is already a readonly keyword before the cursor: do not auto complete it again
        if (readonlyKeyword.isValid() && readonlyKeyword.offset < positionInfo.offset()) {
            completeReadonly = false;
            // also, required keywords do not like readonly keywords
            completeRequired = false;
        }

        // same for required
        if (requiredKeyword.isValid() && requiredKeyword.offset < positionInfo.offset()) {
            completeRequired = false;
            // also, required keywords do not like readonly keywords
            completeReadonly = false;
        }

        // same for default
        if (defaultKeyword.isValid() && defaultKeyword.offset < positionInfo.offset()) {
            completeDefault = false;
        }
        auto addCompletionKeyword = [&result](QUtf8StringView view, bool complete) {
            if (!complete)
                return;
            CompletionItem item;
            item.label = view.data();
            item.kind = int(CompletionItemKind::Keyword);
            result = item;
        };
        addCompletionKeyword(u8"readonly", completeReadonly);
        addCompletionKeyword(u8"required", completeRequired);
        addCompletionKeyword(u8"default", completeDefault);
        addCompletionKeyword(u8"property", true);

        return;
    }

    const QQmlJS::SourceLocation propertyIdentifier = info.regions[IdentifierRegion];
    if (propertyKeyword.end() <= positionInfo.offset()
        && positionInfo.offset() < propertyIdentifier.offset) {
        suggestReachableTypes(currentItem,
                              LocalSymbolsType::ObjectType | LocalSymbolsType::ValueType,
                              CompletionItemKind::Class, result);
    }
    // do not autocomplete the rest
    return;
}

void QQmlLSCompletion::insideBindingCompletion(const DomItem &currentItem,
                                               const QQmlLSCompletionPosition &positionInfo,
                                               BackInsertIterator result) const
{
    const DomItem containingBinding = currentItem.filterUp(
            [](DomType type, const QQmlJS::Dom::DomItem &) { return type == DomType::Binding; },
            FilterUpOptions::ReturnOuter);

    // do scriptidentifiercompletion after the ':' of a binding
    if (cursorAfterColon(containingBinding, positionInfo)) {
        suggestJSExpressionCompletion(positionInfo.itemAtPosition, result);

        if (auto type = QQmlLSUtils::resolveExpressionType(currentItem, ResolveOwnerType)) {
            const QStringList names = currentItem.field(Fields::name).toString().split(u'.');
            const QQmlJSScope *current = resolve(type->semanticScope.get(), names);
            // add type names when binding to an object type or a property with var type
            if (!current || current->accessSemantics() == QQmlSA::AccessSemantics::Reference) {
                LocalSymbolsTypes options;
                options.setFlag(LocalSymbolsType::ObjectType);
                suggestReachableTypes(positionInfo.itemAtPosition, options,
                                      CompletionItemKind::Constructor, result);
                suggestSnippetsForRightHandSideOfBinding(positionInfo.itemAtPosition, result);
            }
        }
        return;
    }

    // ignore the binding if asking for completion in front of the binding
    if (cursorInFrontOfItem(containingBinding, positionInfo)) {
        insideQmlObjectCompletion(currentItem.containingObject(), positionInfo, result);
        return;
    }

    const DomItem containingObject = currentItem.qmlObject();

    suggestBindingCompletion(positionInfo.itemAtPosition, result);

    // add Qml Types for default binding
    suggestReachableTypes(positionInfo.itemAtPosition, LocalSymbolsType::ObjectType,
                          CompletionItemKind::Constructor, result);
    suggestSnippetsForLeftHandSideOfBinding(positionInfo.itemAtPosition, result);
}

void QQmlLSCompletion::insideImportCompletion(const DomItem &currentItem,
                                              const QQmlLSCompletionPosition &positionInfo,
                                              BackInsertIterator result) const
{
    const DomItem containingFile = currentItem.containingFile();
    insideImportCompletionHelper(containingFile, positionInfo, result);

    // when in front of the import statement: propose types for root Qml Object completion
    if (cursorInFrontOfItem(currentItem, positionInfo)) {
        suggestReachableTypes(containingFile, LocalSymbolsType::ObjectType,
                              CompletionItemKind::Constructor, result);
    }
}

void QQmlLSCompletion::insideQmlFileCompletion(const DomItem &currentItem,
                                               const QQmlLSCompletionPosition &positionInfo,
                                               BackInsertIterator result) const
{
    const DomItem containingFile = currentItem.containingFile();
    // completions for code outside the root Qml Object
    // global completions
    if (positionInfo.cursorPosition.atLineStart()) {
        if (positionInfo.cursorPosition.base().isEmpty()) {
            for (const QStringView &s : std::array<QStringView, 2>({ u"pragma", u"import" })) {
                CompletionItem comp;
                comp.label = s.toUtf8();
                comp.kind = int(CompletionItemKind::Keyword);
                result = comp;
            }
        }
    }
    // Types for root Qml Object completion
    suggestReachableTypes(containingFile, LocalSymbolsType::ObjectType,
                          CompletionItemKind::Constructor, result);
}

/*!
\internal
Generate the snippets for let, var and const variable declarations.
*/
void QQmlLSCompletion::suggestVariableDeclarationStatementCompletion(
        BackInsertIterator result, QQmlLSUtilsAppendOption option) const
{
    // let/var/const statement
    for (auto view : std::array<QUtf8StringView, 3>{ "let", "var", "const" }) {
        auto snippet = makeSnippet(QByteArray(view.data()).append(" variable = value"),
                             QByteArray(view.data()).append(" ${1:variable} = $0"));
        if (option == AppendSemicolon) {
            snippet.insertText->append(";");
            snippet.label.append(";");
        }
        result = snippet;
    }
}

/*!
\internal
Generate the snippets for case and default statements.
*/
void QQmlLSCompletion::suggestCaseAndDefaultStatementCompletion(BackInsertIterator result) const
{
    // case snippet
    result = makeSnippet("case value: statements...", "case ${1:value}:\n\t$0");
    // case + brackets snippet
    result = makeSnippet("case value: { statements... }", "case ${1:value}: {\n\t$0\n}");

    // default snippet
    result = makeSnippet("default: statements...", "default:\n\t$0");
    // default + brackets snippet
    result = makeSnippet("default: { statements... }", "default: {\n\t$0\n}");
}

/*!
\internal
Break and continue can be inserted only in following situations:
\list
    \li Break and continue inside a loop.
    \li Break inside a (nested) LabelledStatement
    \li Break inside a (nested) SwitchStatement
\endlist
*/
void QQmlLSCompletion::suggestContinueAndBreakStatementIfNeeded(const DomItem &itemAtPosition,
                                                                BackInsertIterator result) const
{
    bool alreadyInLabel = false;
    bool alreadyInSwitch = false;
    for (DomItem current = itemAtPosition; current; current = current.directParent()) {
        switch (current.internalKind()) {
        case DomType::ScriptExpression:
            // reached end of script expression
            return;

        case DomType::ScriptForStatement:
        case DomType::ScriptForEachStatement:
        case DomType::ScriptWhileStatement:
        case DomType::ScriptDoWhileStatement: {
            CompletionItem continueKeyword;
            continueKeyword.label = "continue";
            continueKeyword.kind = int(CompletionItemKind::Keyword);
            result = continueKeyword;

            // do not add break twice
            if (!alreadyInSwitch && !alreadyInLabel) {
                CompletionItem breakKeyword;
                breakKeyword.label = "break";
                breakKeyword.kind = int(CompletionItemKind::Keyword);
                result = breakKeyword;
            }
            // early exit: cannot suggest more completions
            return;
        }
        case DomType::ScriptSwitchStatement: {
            // check if break was already inserted
            if (alreadyInSwitch || alreadyInLabel)
                break;
            alreadyInSwitch = true;

            CompletionItem breakKeyword;
            breakKeyword.label = "break";
            breakKeyword.kind = int(CompletionItemKind::Keyword);
            result = breakKeyword;
            break;
        }
        case DomType::ScriptLabelledStatement: {
            // check if break was already inserted because of switch or loop
            if (alreadyInSwitch || alreadyInLabel)
                break;
            alreadyInLabel = true;

            CompletionItem breakKeyword;
            breakKeyword.label = "break";
            breakKeyword.kind = int(CompletionItemKind::Keyword);
            result = breakKeyword;
            break;
        }
        default:
            break;
        }
    }
}

/*!
\internal
Generates snippets or keywords for all possible JS statements where it makes sense. To use whenever
any JS statement can be expected, but when no JS statement is there yet.

Only generates JS expression completions when itemAtPosition is a qualified name.

Here is a list of statements that do \e{not} get any snippets:
\list
    \li BlockStatement does not need a code snippet, editors automatically include the closing
bracket anyway. \li EmptyStatement completion would only generate a single \c{;} \li
ExpressionStatement completion cannot generate any snippet, only identifiers \li WithStatement
completion is not recommended: qmllint will warn about usage of with statements \li
LabelledStatement completion might need to propose labels (TODO?) \li DebuggerStatement completion
does not strike as being very useful \endlist
*/
void QQmlLSCompletion::suggestJSStatementCompletion(const DomItem &itemAtPosition,
                                                    BackInsertIterator result) const
{
    suggestJSExpressionCompletion(itemAtPosition, result);
    
    if (QQmlLSUtils::isFieldMemberAccess(itemAtPosition))
        return;

    // expression statements
    suggestVariableDeclarationStatementCompletion(result);
    // block statement
    result = makeSnippet("{ statements... }", "{\n\t$0\n}");

    // if + brackets statement
    result = makeSnippet("if (condition) { statements }", "if ($1) {\n\t$0\n}");

    // do statement
    result = makeSnippet("do { statements } while (condition);", "do {\n\t$1\n} while ($0);");

    // while + brackets statement
    result = makeSnippet("while (condition) { statements...}", "while ($1) {\n\t$0\n}");

    // for + brackets loop statement
    result = makeSnippet("for (initializer; condition; increment) { statements... }",
                         "for ($1;$2;$3) {\n\t$0\n}");

    // for ... in + brackets loop statement
    result = makeSnippet("for (property in object) { statements... }", "for ($1 in $2) {\n\t$0\n}");

    // for ... of + brackets loop statement
    result = makeSnippet("for (element of array) { statements... }", "for ($1 of $2) {\n\t$0\n}");

    // try + catch statement
    result = makeSnippet("try { statements... } catch(error) { statements... }",
                         "try {\n\t$1\n} catch($2) {\n\t$0\n}");

    // try + finally statement
    result = makeSnippet("try { statements... } finally { statements... }",
                         "try {\n\t$1\n} finally {\n\t$0\n}");

    // try + catch + finally statement
    result = makeSnippet(
            "try { statements... } catch(error) { statements... } finally { statements... }",
            "try {\n\t$1\n} catch($2) {\n\t$3\n} finally {\n\t$0\n}");

    // one can always assume that JS code in QML is inside a function, so always propose `return`
    for (auto &&view : { "return"_ba, "throw"_ba }) {
        CompletionItem item;
        item.label = std::move(view);
        item.kind = int(CompletionItemKind::Keyword);
        result = item;
    }

    // rules for case+default statements:
    // 1) when inside a CaseBlock, or
    // 2) inside a CaseClause, as an (non-nested) element of the CaseClause statementlist.
    // 3) inside a DefaultClause, as an (non-nested) element of the DefaultClause statementlist,
    //
    // switch (x) {
    // // (1)
    // case 1:
    //      myProperty = 5;
    //      // (2) -> could be another statement of current case, but also a new case or default!
    // default:
    //      myProperty = 5;
    //      // (3) -> could be another statement of current default, but also a new case or default!
    // }
    const DomType currentKind = itemAtPosition.internalKind();
    const DomType parentKind = itemAtPosition.directParent().internalKind();
    if (currentKind == DomType::ScriptCaseBlock || currentKind == DomType::ScriptCaseClause
        || currentKind == DomType::ScriptDefaultClause
        || (currentKind == DomType::List
            && (parentKind == DomType::ScriptCaseClause
                || parentKind == DomType::ScriptDefaultClause))) {
        suggestCaseAndDefaultStatementCompletion(result);
    }
    suggestContinueAndBreakStatementIfNeeded(itemAtPosition, result);
}

void QQmlLSCompletion::insideForStatementCompletion(const DomItem &parentForContext,
                                                    const QQmlLSCompletionPosition &positionInfo,
                                                    BackInsertIterator result) const
{
    const auto regions = FileLocations::treeOf(parentForContext)->info().regions;

    const QQmlJS::SourceLocation leftParenthesis = regions[LeftParenthesisRegion];
    const QQmlJS::SourceLocation firstSemicolon = regions[FirstSemicolonTokenRegion];
    const QQmlJS::SourceLocation secondSemicolon = regions[SecondSemicolonRegion];
    const QQmlJS::SourceLocation rightParenthesis = regions[RightParenthesisRegion];

    if (betweenLocations(leftParenthesis, positionInfo, firstSemicolon)) {
        suggestJSExpressionCompletion(positionInfo.itemAtPosition, result);
        suggestVariableDeclarationStatementCompletion(result,
                                                      QQmlLSUtilsAppendOption::AppendNothing);
        return;
    }
    if (betweenLocations(firstSemicolon, positionInfo, secondSemicolon)
        || betweenLocations(secondSemicolon, positionInfo, rightParenthesis)) {
        suggestJSExpressionCompletion(positionInfo.itemAtPosition, result);
        return;
    }

    if (afterLocation(rightParenthesis, positionInfo)) {
        suggestJSStatementCompletion(positionInfo.itemAtPosition, result);
        return;
    }
}

void QQmlLSCompletion::insideScriptLiteralCompletion(const DomItem &currentItem,
                                                     const QQmlLSCompletionPosition &positionInfo,
                                                     BackInsertIterator result) const
{
    Q_UNUSED(currentItem);
    if (positionInfo.cursorPosition.base().isEmpty()) {
        suggestJSExpressionCompletion(positionInfo.itemAtPosition, result);
        return;
    }
}

void QQmlLSCompletion::insideCallExpression(const DomItem &currentItem,
                                            const QQmlLSCompletionPosition &positionInfo,
                                            BackInsertIterator result) const
{
    const auto regions = FileLocations::treeOf(currentItem)->info().regions;
    const QQmlJS::SourceLocation leftParenthesis = regions[LeftParenthesisRegion];
    const QQmlJS::SourceLocation rightParenthesis = regions[RightParenthesisRegion];
    if (beforeLocation(positionInfo, leftParenthesis)) {
        suggestJSExpressionCompletion(positionInfo.itemAtPosition, result);
        return;
    }
    if (betweenLocations(leftParenthesis, positionInfo, rightParenthesis)) {
        suggestJSExpressionCompletion(positionInfo.itemAtPosition, result);
        return;
    }
}

void QQmlLSCompletion::insideIfStatement(const DomItem &currentItem,
                                         const QQmlLSCompletionPosition &positionInfo,
                                         BackInsertIterator result) const
{
    const auto regions = FileLocations::treeOf(currentItem)->info().regions;
    const QQmlJS::SourceLocation leftParenthesis = regions[LeftParenthesisRegion];
    const QQmlJS::SourceLocation rightParenthesis = regions[RightParenthesisRegion];
    const QQmlJS::SourceLocation elseKeyword = regions[ElseKeywordRegion];

    if (betweenLocations(leftParenthesis, positionInfo, rightParenthesis)) {
        suggestJSExpressionCompletion(positionInfo.itemAtPosition, result);
        return;
    }
    if (betweenLocations(rightParenthesis, positionInfo, elseKeyword)) {
        suggestJSStatementCompletion(positionInfo.itemAtPosition, result);
        return;
    }
    if (afterLocation(elseKeyword, positionInfo)) {
        suggestJSStatementCompletion(positionInfo.itemAtPosition, result);
        return;
    }
}

void QQmlLSCompletion::insideReturnStatement(const DomItem &currentItem,
                                             const QQmlLSCompletionPosition &positionInfo,
                                             BackInsertIterator result) const
{
    const auto regions = FileLocations::treeOf(currentItem)->info().regions;
    const QQmlJS::SourceLocation returnKeyword = regions[ReturnKeywordRegion];

    if (afterLocation(returnKeyword, positionInfo)) {
        suggestJSExpressionCompletion(positionInfo.itemAtPosition, result);
        return;
    }
}

void QQmlLSCompletion::insideWhileStatement(const DomItem &currentItem,
                                            const QQmlLSCompletionPosition &positionInfo,
                                            BackInsertIterator result) const
{
    const auto regions = FileLocations::treeOf(currentItem)->info().regions;
    const QQmlJS::SourceLocation leftParenthesis = regions[LeftParenthesisRegion];
    const QQmlJS::SourceLocation rightParenthesis = regions[RightParenthesisRegion];

    if (betweenLocations(leftParenthesis, positionInfo, rightParenthesis)) {
        suggestJSExpressionCompletion(positionInfo.itemAtPosition, result);
        return;
    }
    if (afterLocation(rightParenthesis, positionInfo)) {
        suggestJSStatementCompletion(positionInfo.itemAtPosition, result);
        return;
    }
}

void QQmlLSCompletion::insideDoWhileStatement(const DomItem &parentForContext,
                                              const QQmlLSCompletionPosition &positionInfo,
                                              BackInsertIterator result) const
{
    const auto regions = FileLocations::treeOf(parentForContext)->info().regions;
    const QQmlJS::SourceLocation doKeyword = regions[DoKeywordRegion];
    const QQmlJS::SourceLocation whileKeyword = regions[WhileKeywordRegion];
    const QQmlJS::SourceLocation leftParenthesis = regions[LeftParenthesisRegion];
    const QQmlJS::SourceLocation rightParenthesis = regions[RightParenthesisRegion];

    if (betweenLocations(doKeyword, positionInfo, whileKeyword)) {
        suggestJSStatementCompletion(positionInfo.itemAtPosition, result);
        return;
    }
    if (betweenLocations(leftParenthesis, positionInfo, rightParenthesis)) {
        suggestJSExpressionCompletion(positionInfo.itemAtPosition, result);
        return;
    }
}

void QQmlLSCompletion::insideForEachStatement(const DomItem &parentForContext,
                                              const QQmlLSCompletionPosition &positionInfo,
                                              BackInsertIterator result) const
{
    const auto regions = FileLocations::treeOf(parentForContext)->info().regions;

    const QQmlJS::SourceLocation inOf = regions[InOfTokenRegion];
    const QQmlJS::SourceLocation leftParenthesis = regions[LeftParenthesisRegion];
    const QQmlJS::SourceLocation rightParenthesis = regions[RightParenthesisRegion];

    if (betweenLocations(leftParenthesis, positionInfo, inOf)) {
        suggestJSExpressionCompletion(positionInfo.itemAtPosition, result);
        suggestVariableDeclarationStatementCompletion(result);
        return;
    }
    if (betweenLocations(inOf, positionInfo, rightParenthesis)) {
        suggestJSExpressionCompletion(positionInfo.itemAtPosition, result);
        return;
    }

    if (afterLocation(rightParenthesis, positionInfo)) {
        suggestJSStatementCompletion(positionInfo.itemAtPosition, result);
        return;
    }
}

void QQmlLSCompletion::insideSwitchStatement(const DomItem &parentForContext,
                                             const QQmlLSCompletionPosition positionInfo,
                                             BackInsertIterator result) const
{
    const auto regions = FileLocations::treeOf(parentForContext)->info().regions;

    const QQmlJS::SourceLocation leftParenthesis = regions[LeftParenthesisRegion];
    const QQmlJS::SourceLocation rightParenthesis = regions[RightParenthesisRegion];

    if (betweenLocations(leftParenthesis, positionInfo, rightParenthesis)) {
        suggestJSExpressionCompletion(positionInfo.itemAtPosition, result);
        return;
    }
}

void QQmlLSCompletion::insideCaseClause(const DomItem &parentForContext,
                                        const QQmlLSCompletionPosition &positionInfo,
                                        BackInsertIterator result) const
{
    const auto regions = FileLocations::treeOf(parentForContext)->info().regions;

    const QQmlJS::SourceLocation caseKeyword = regions[CaseKeywordRegion];
    const QQmlJS::SourceLocation colonToken = regions[ColonTokenRegion];

    if (betweenLocations(caseKeyword, positionInfo, colonToken)) {
        suggestJSExpressionCompletion(positionInfo.itemAtPosition, result);
        return;
    }
    if (afterLocation(colonToken, positionInfo)) {
        suggestJSStatementCompletion(positionInfo.itemAtPosition, result);
        return;
    }

}

/*!
\internal
Checks if a case or default clause does happen before ctx in the code.
*/
bool QQmlLSCompletion::isCaseOrDefaultBeforeCtx(const DomItem &currentClause,
                                                const QQmlLSCompletionPosition &positionInfo,
                                                FileLocationRegion keywordRegion) const
{
    Q_ASSERT(keywordRegion == QQmlJS::Dom::CaseKeywordRegion
             || keywordRegion == QQmlJS::Dom::DefaultKeywordRegion);

    if (!currentClause)
        return false;

    const auto token = FileLocations::treeOf(currentClause)->info().regions[keywordRegion];
    if (afterLocation(token, positionInfo))
        return true;

    return false;
}

/*!
\internal

Search for a `case ...:` or a `default: ` clause happening before ctx, and return the
corresponding DomItem of type DomType::CaseClauses or DomType::DefaultClause.

Return an empty DomItem if neither case nor default was found.
*/
DomItem
QQmlLSCompletion::previousCaseOfCaseBlock(const DomItem &parentForContext,
                                          const QQmlLSCompletionPosition &positionInfo) const
{
    const DomItem caseClauses = parentForContext.field(Fields::caseClauses);
    for (int i = 0; i < caseClauses.indexes(); ++i) {
        const DomItem currentClause = caseClauses.index(i);
        if (isCaseOrDefaultBeforeCtx(currentClause, positionInfo, QQmlJS::Dom::CaseKeywordRegion)) {
            return currentClause;
        }
    }

    const DomItem defaultClause = parentForContext.field(Fields::defaultClause);
    if (isCaseOrDefaultBeforeCtx(defaultClause, positionInfo, QQmlJS::Dom::DefaultKeywordRegion))
        return parentForContext.field(Fields::defaultClause);

    const DomItem moreCaseClauses = parentForContext.field(Fields::moreCaseClauses);
    for (int i = 0; i < moreCaseClauses.indexes(); ++i) {
        const DomItem currentClause = moreCaseClauses.index(i);
        if (isCaseOrDefaultBeforeCtx(currentClause, positionInfo, QQmlJS::Dom::CaseKeywordRegion)) {
            return currentClause;
        }
    }

    return {};
}

void QQmlLSCompletion::insideCaseBlock(const DomItem &parentForContext,
                                       const QQmlLSCompletionPosition &positionInfo,
                                       BackInsertIterator result) const
{
    const auto regions = FileLocations::treeOf(parentForContext)->info().regions;

    const QQmlJS::SourceLocation leftBrace = regions[LeftBraceRegion];
    const QQmlJS::SourceLocation rightBrace = regions[RightBraceRegion];

    if (!betweenLocations(leftBrace, positionInfo, rightBrace))
        return;

    // TODO: looks fishy
    // if there is a previous case or default clause, you can still add statements to it
    if (const auto previousCase = previousCaseOfCaseBlock(parentForContext, positionInfo)) {
        suggestJSStatementCompletion(previousCase, result);
        return;
    }

    // otherwise, only complete case and default
    suggestCaseAndDefaultStatementCompletion(result);
}

void QQmlLSCompletion::insideDefaultClause(const DomItem &parentForContext,
                                           const QQmlLSCompletionPosition &positionInfo,
                                           BackInsertIterator result) const
{
    const auto regions = FileLocations::treeOf(parentForContext)->info().regions;

    const QQmlJS::SourceLocation colonToken = regions[ColonTokenRegion];

    if (afterLocation(colonToken, positionInfo)) {
        suggestJSStatementCompletion(positionInfo.itemAtPosition, result);
        return ;
    }
}

void QQmlLSCompletion::insideBinaryExpressionCompletion(
        const DomItem &parentForContext, const QQmlLSCompletionPosition &positionInfo,
        BackInsertIterator result) const
{
    const auto regions = FileLocations::treeOf(parentForContext)->info().regions;

    const QQmlJS::SourceLocation operatorLocation = regions[OperatorTokenRegion];

    if (beforeLocation(positionInfo, operatorLocation)) {
        suggestJSExpressionCompletion(positionInfo.itemAtPosition, result);
        return;
    }
    if (afterLocation(operatorLocation, positionInfo)) {
        suggestJSExpressionCompletion(positionInfo.itemAtPosition, result);
        return;
    }
}

/*!
\internal
Doing completion in variable declarations requires taking a look at all different cases:

\list
    \li Normal variable names, like \c{let helloWorld = 123;}
        In this case, only autocomplete scriptexpressionidentifiers after the '=' token.
        Do not propose existing names for the variable name, because the variable name needs to be
        an identifier that is not used anywhere (to avoid shadowing and confusing code),

    \li Deconstructed arrays, like \c{let [ helloWorld, ] = [ 123, ];}
        In this case, only autocomplete scriptexpressionidentifiers after the '=' token.
        Do not propose already existing identifiers inside the left hand side array.

    \li Deconstructed arrays with initializers, like \c{let [ helloWorld = someVar, ] = [ 123, ];}
        Note: this assigns the value of someVar to helloWorld if the right hand side's first element
        is undefined or does not exist.

        In this case, only autocomplete scriptexpressionidentifiers after the '=' tokens.
        Only propose already existing identifiers inside the left hand side array when behind a '='
    token.

    \li Deconstructed Objects, like \c{let { helloWorld, } = { helloWorld: 123, };}
        In this case, only autocomplete scriptexpressionidentifiers after the '=' token.
        Do not propose already existing identifiers inside the left hand side object.

    \li Deconstructed Objects with initializers, like \c{let { helloWorld = someVar, } = {};}
        Note: this assigns the value of someVar to helloWorld if the right hand side's object does
        not have a property called 'helloWorld'.

        In this case, only autocomplete scriptexpressionidentifiers after the '=' token.
        Only propose already existing identifiers inside the left hand side object when behind a '='
        token.

    \li Finally, you are allowed to nest and combine all above possibilities together for all your
        deconstruction needs, so the exact same completion needs to be done for
        DomType::ScriptPatternElement too.

\endlist
*/
void QQmlLSCompletion::insideScriptPattern(const DomItem &parentForContext,
                                           const QQmlLSCompletionPosition &positionInfo,
                                           BackInsertIterator result) const
{
    const auto regions = FileLocations::treeOf(parentForContext)->info().regions;

    const QQmlJS::SourceLocation equal = regions[EqualTokenRegion];

    if (!afterLocation(equal, positionInfo))
        return;

    // otherwise, only complete case and default
    suggestJSExpressionCompletion(positionInfo.itemAtPosition, result);
}

/*!
\internal
See comment on insideScriptPattern().
*/
void QQmlLSCompletion::insideVariableDeclarationEntry(const DomItem &parentForContext,
                                                      const QQmlLSCompletionPosition &positionInfo,
                                                      BackInsertIterator result) const
{
    insideScriptPattern(parentForContext, positionInfo, result);
}

void QQmlLSCompletion::insideThrowStatement(const DomItem &parentForContext,
                                            const QQmlLSCompletionPosition &positionInfo,
                                            BackInsertIterator result) const
{
    const auto regions = FileLocations::treeOf(parentForContext)->info().regions;

    const QQmlJS::SourceLocation throwKeyword = regions[ThrowKeywordRegion];

    if (afterLocation(throwKeyword, positionInfo)) {
        suggestJSExpressionCompletion(positionInfo.itemAtPosition, result);
        return;
    }
}

void QQmlLSCompletion::insideLabelledStatement(const DomItem &parentForContext,
                                               const QQmlLSCompletionPosition &positionInfo,
                                               BackInsertIterator result) const
{
    const auto regions = FileLocations::treeOf(parentForContext)->info().regions;

    const QQmlJS::SourceLocation colon = regions[ColonTokenRegion];

    if (afterLocation(colon, positionInfo)) {
        suggestJSStatementCompletion(positionInfo.itemAtPosition, result);
        return;
    }
    // note: the case "beforeLocation(ctx, colon)" probably never happens:
    // this is because without the colon, the parser will probably not parse this as a
    // labelledstatement but as a normal expression statement.
    // So this case only happens when the colon already exists, and the user goes back to the
    // label name and requests completion for that label.
}

/*!
\internal
Collect the current set of labels that some DomItem can jump to.
*/
static void collectLabels(const DomItem &context, QQmlLSCompletion::BackInsertIterator result)
{
    for (DomItem current = context; current; current = current.directParent()) {
        if (current.internalKind() == DomType::ScriptLabelledStatement) {
            const QString label = current.field(Fields::label).value().toString();
            if (label.isEmpty())
                continue;
            CompletionItem item;
            item.label = label.toUtf8();
            item.kind = int(CompletionItemKind::Value); // variable?
            // TODO: more stuff here?
            result = item;
        } else if (current.internalKind() == DomType::ScriptExpression) {
            // quick exit when leaving the JS part
            return;
        }
    }
    return;
}

void QQmlLSCompletion::insideContinueStatement(const DomItem &parentForContext,
                                               const QQmlLSCompletionPosition &positionInfo,
                                               BackInsertIterator result) const
{
    const auto regions = FileLocations::treeOf(parentForContext)->info().regions;

    const QQmlJS::SourceLocation continueKeyword = regions[ContinueKeywordRegion];

    if (afterLocation(continueKeyword, positionInfo)) {
        collectLabels(parentForContext, result);
        return;
    }
}

void QQmlLSCompletion::insideBreakStatement(const DomItem &parentForContext,
                                            const QQmlLSCompletionPosition &positionInfo,
                                            BackInsertIterator result) const
{
    const auto regions = FileLocations::treeOf(parentForContext)->info().regions;

    const QQmlJS::SourceLocation breakKeyword = regions[BreakKeywordRegion];

    if (afterLocation(breakKeyword, positionInfo)) {
        collectLabels(parentForContext, result);
        return;
    }
}

void QQmlLSCompletion::insideConditionalExpression(const DomItem &parentForContext,
                                                   const QQmlLSCompletionPosition &positionInfo,
                                                   BackInsertIterator result) const
{
    const auto regions = FileLocations::treeOf(parentForContext)->info().regions;

    const QQmlJS::SourceLocation questionMark = regions[QuestionMarkTokenRegion];
    const QQmlJS::SourceLocation colon = regions[ColonTokenRegion];

    if (beforeLocation(positionInfo, questionMark)) {
        suggestJSExpressionCompletion(positionInfo.itemAtPosition, result);
        return;
    }
    if (betweenLocations(questionMark, positionInfo, colon)) {
        suggestJSExpressionCompletion(positionInfo.itemAtPosition, result);
        return;
    }
    if (afterLocation(colon, positionInfo)) {
        suggestJSExpressionCompletion(positionInfo.itemAtPosition, result);
        return;
    }
}

void QQmlLSCompletion::insideUnaryExpression(const DomItem &parentForContext,
                                             const QQmlLSCompletionPosition &positionInfo,
                                             BackInsertIterator result) const
{
    const auto regions = FileLocations::treeOf(parentForContext)->info().regions;

    const QQmlJS::SourceLocation operatorToken = regions[OperatorTokenRegion];

    if (afterLocation(operatorToken, positionInfo)) {
        suggestJSExpressionCompletion(positionInfo.itemAtPosition, result);
        return;
    }
}

void QQmlLSCompletion::insidePostExpression(const DomItem &parentForContext,
                                            const QQmlLSCompletionPosition &positionInfo,
                                            BackInsertIterator result) const
{
    const auto regions = FileLocations::treeOf(parentForContext)->info().regions;

    const QQmlJS::SourceLocation operatorToken = regions[OperatorTokenRegion];

    if (beforeLocation(positionInfo, operatorToken)) {
        suggestJSExpressionCompletion(positionInfo.itemAtPosition, result);
        return;
    }
}

void QQmlLSCompletion::insideParenthesizedExpression(const DomItem &parentForContext,
                                                     const QQmlLSCompletionPosition &positionInfo,
                                                     BackInsertIterator result) const
{
    const auto regions = FileLocations::treeOf(parentForContext)->info().regions;

    const QQmlJS::SourceLocation leftParenthesis = regions[LeftParenthesisRegion];
    const QQmlJS::SourceLocation rightParenthesis = regions[RightParenthesisRegion];

    if (betweenLocations(leftParenthesis, positionInfo, rightParenthesis)) {
        suggestJSExpressionCompletion(positionInfo.itemAtPosition, result);
        return;
    }
}

void QQmlLSCompletion::signalHandlerCompletion(const QQmlJSScope::ConstPtr &scope,
                                               QDuplicateTracker<QString> *usedNames,
                                               BackInsertIterator result) const
{
    const auto keyValues = scope->methods().asKeyValueRange();
    for (const auto &[name, method] : keyValues) {
        if (method.access() != QQmlJSMetaMethod::Public
            || method.methodType() != QQmlJSMetaMethodType::Signal) {
            continue;
        }
        if (usedNames && usedNames->hasSeen(name)) {
            continue;
        }

        CompletionItem completion;
        completion.label = QQmlSignalNames::signalNameToHandlerName(name).toUtf8();
        completion.kind = int(CompletionItemKind::Method);
        result = completion;
    }
}

/*!
\internal
Decide which completions can be used at currentItem and compute them.
*/
QList<CompletionItem>
QQmlLSCompletion::completions(const DomItem &currentItem,
                              const CompletionContextStrings &contextStrings) const
{
    QList<CompletionItem> result;
    collectCompletions(currentItem, contextStrings, std::back_inserter(result));
    return result;
}

void QQmlLSCompletion::collectCompletions(const DomItem &currentItem,
                                          const CompletionContextStrings &contextStrings,
                                          BackInsertIterator result) const
{
    /*!
    Completion is not provided on a script identifier expression because script identifier
    expressions lack context information. Instead, find the first parent that has enough
    context information and provide completion for this one.
    For example, a script identifier expression \c{le} in
    \badcode
        for (;le;) { ... }
    \endcode
    will get completion for a property called \c{leProperty}, while the same script identifier
    expression in
    \badcode
        for (le;;) { ... }
    \endcode
    will, in addition to \c{leProperty}, also get completion for the \c{let} statement snippet.
    In this example, the parent used for the completion is the for-statement, of type
    DomType::ScriptForStatement.

    In addition of the parent for the context, use positionInfo to have exact information on where
    the cursor is (to compare with the SourceLocations of tokens) and which item is at this position
    (required to provide completion at the correct position, for example for attached properties).
    */
    const QQmlLSCompletionPosition positionInfo{ currentItem, contextStrings };
    for (DomItem currentParent = currentItem; currentParent;
         currentParent = currentParent.directParent()) {
        const DomType currentType = currentParent.internalKind();

        switch (currentType) {
        case DomType::Id:
            // suppress completions for ids
            return;
        case DomType::Pragma:
            insidePragmaCompletion(currentParent, positionInfo, result);
            return;
        case DomType::ScriptType: {
            if (currentParent.directParent().internalKind() == DomType::QmlObject) {
                insideQmlObjectCompletion(currentParent.directParent(), positionInfo, result);
                return;
            }

            LocalSymbolsTypes options;
            options.setFlag(LocalSymbolsType::ObjectType);
            options.setFlag(LocalSymbolsType::ValueType);
            suggestReachableTypes(currentItem, options, CompletionItemKind::Class, result);
            return;
        }
        case DomType::ScriptFormalParameter:
            // no autocompletion inside of function parameter definition
            return;
        case DomType::Binding:
            insideBindingCompletion(currentParent, positionInfo, result);
            return;
        case DomType::Import:
            insideImportCompletion(currentParent, positionInfo, result);
            return;
        case DomType::ScriptForStatement:
            insideForStatementCompletion(currentParent, positionInfo, result);
            return;
        case DomType::ScriptBlockStatement:
            suggestJSStatementCompletion(positionInfo.itemAtPosition, result);
            return;
        case DomType::QmlFile:
            insideQmlFileCompletion(currentParent, positionInfo, result);
            return;
        case DomType::QmlObject:
            insideQmlObjectCompletion(currentParent, positionInfo, result);
            return;
        case DomType::MethodInfo:
            // suppress completions
            return;
        case DomType::PropertyDefinition:
            insidePropertyDefinitionCompletion(currentParent, positionInfo, result);
            return;
        case DomType::ScriptBinaryExpression:
            // ignore field member expressions: these need additional context from its parents
            if (QQmlLSUtils::isFieldMemberExpression(currentParent))
                continue;
            insideBinaryExpressionCompletion(currentParent, positionInfo, result);
            return;
        case DomType::ScriptLiteral:
            insideScriptLiteralCompletion(currentParent, positionInfo, result);
            return;
        case DomType::ScriptCallExpression:
            insideCallExpression(currentParent, positionInfo, result);
            return;
        case DomType::ScriptIfStatement:
            insideIfStatement(currentParent, positionInfo, result);
            return;
        case DomType::ScriptReturnStatement:
            insideReturnStatement(currentParent, positionInfo, result);
            return;
        case DomType::ScriptWhileStatement:
            insideWhileStatement(currentParent, positionInfo, result);
            return;
        case DomType::ScriptDoWhileStatement:
            insideDoWhileStatement(currentParent, positionInfo, result);
            return;
        case DomType::ScriptForEachStatement:
            insideForEachStatement(currentParent, positionInfo, result);
            return;
        case DomType::ScriptTryCatchStatement:
            /*!
            \internal
            The Ecmascript standard specifies that there can only be a block statement between \c
            try and \c catch(...), \c try and \c finally and \c catch(...) and \c finally, so all of
            these completions are already handled by the DomType::ScriptBlockStatement completion.
            The only place in the try statement where there is no BlockStatement and therefore needs
            its own completion is inside the catch parameter, but that is
            \quotation
            An optional identifier or pattern to hold the caught exception for the associated catch
            block.
            \endquotation
            citing
            https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/try...catch?retiredLocale=de#exceptionvar.
            This means that no completion is needed inside a catch-expression, as it should contain
            an identifier that is not yet used anywhere.
            Therefore, no completion is required at all when inside a try-statement but outside a
            block-statement.
            */
            return;
        case DomType::ScriptSwitchStatement:
            insideSwitchStatement(currentParent, positionInfo, result);
            return;
        case DomType::ScriptCaseClause:
            insideCaseClause(currentParent, positionInfo, result);
            return;
        case DomType::ScriptDefaultClause:
            if (ctxBeforeStatement(positionInfo, currentParent, QQmlJS::Dom::DefaultKeywordRegion))
                continue;
            insideDefaultClause(currentParent, positionInfo, result);
            return;
        case DomType::ScriptCaseBlock:
            insideCaseBlock(currentParent, positionInfo, result);
            return;
        case DomType::ScriptVariableDeclaration:
            // not needed: thats a list of ScriptVariableDeclarationEntry, and those entries cannot
            // be suggested because they all start with `{`, `[` or an identifier that should not be
            // in use yet.
            return;
        case DomType::ScriptVariableDeclarationEntry:
            insideVariableDeclarationEntry(currentParent, positionInfo, result);
            return;
        case DomType::ScriptProperty:
            // fallthrough: a ScriptProperty is a ScriptPattern but inside a JS Object. It gets the
            // same completions as a ScriptPattern.
        case DomType::ScriptPattern:
            insideScriptPattern(currentParent, positionInfo, result);
            return;
        case DomType::ScriptThrowStatement:
            insideThrowStatement(currentParent, positionInfo, result);
            return;
        case DomType::ScriptLabelledStatement:
            insideLabelledStatement(currentParent, positionInfo, result);
            return;
        case DomType::ScriptContinueStatement:
            insideContinueStatement(currentParent, positionInfo, result);
            return;
        case DomType::ScriptBreakStatement:
            insideBreakStatement(currentParent, positionInfo, result);
            return;
        case DomType::ScriptConditionalExpression:
            insideConditionalExpression(currentParent, positionInfo, result);
            return;
        case DomType::ScriptUnaryExpression:
            insideUnaryExpression(currentParent, positionInfo, result);
            return;
        case DomType::ScriptPostExpression:
            insidePostExpression(currentParent, positionInfo, result);
            return;
        case DomType::ScriptParenthesizedExpression:
            insideParenthesizedExpression(currentParent, positionInfo, result);
            return;

        // TODO: Implement those statements.
        // In the meanwhile, suppress completions to avoid weird behaviors.
        case DomType::ScriptArray:
        case DomType::ScriptObject:
        case DomType::ScriptElision:
        case DomType::ScriptArrayEntry:
            return;

        default:
            continue;
        }
        Q_UNREACHABLE();
    }

    // no completion could be found
    qCDebug(QQmlLSUtilsLog) << "No completion was found for current request.";
    return;
}

QQmlLSCompletion::QQmlLSCompletion(const QFactoryLoader &pluginLoader)
{
    const auto keys = pluginLoader.metaDataKeys();
    for (qsizetype i = 0; i < keys.size(); ++i) {
        auto instance = std::unique_ptr<QQmlLSPlugin>(
                qobject_cast<QQmlLSPlugin *>(pluginLoader.instance(i)));
        if (!instance)
            continue;
        if (auto completionInstance = instance->createCompletionPlugin())
            m_plugins.push_back(std::move(completionInstance));
    }
}

/*!
\internal
Helper method to call a method on all loaded plugins.
*/
void QQmlLSCompletion::collectFromPlugins(qxp::function_ref<CompletionFromPluginFunction> f,
                                          BackInsertIterator result) const
{
    for (const auto &plugin : m_plugins) {
        Q_ASSERT(plugin);
        f(plugin.get(), result);
    }
}

void QQmlLSCompletion::suggestSnippetsForLeftHandSideOfBinding(const DomItem &itemAtPosition,
                                                               BackInsertIterator result) const
{
    collectFromPlugins(
            [&itemAtPosition](QQmlLSCompletionPlugin *p, BackInsertIterator result) {
                p->suggestSnippetsForLeftHandSideOfBinding(itemAtPosition, result);
            },
            result);
}

void QQmlLSCompletion::suggestSnippetsForRightHandSideOfBinding(const DomItem &itemAtPosition,
                                                                BackInsertIterator result) const
{
    collectFromPlugins(
            [&itemAtPosition](QQmlLSCompletionPlugin *p, BackInsertIterator result) {
                p->suggestSnippetsForRightHandSideOfBinding(itemAtPosition, result);
            },
            result);
}

QT_END_NAMESPACE