@@ -1317,8 +1317,21 @@ class CodeCompletionCallbacksImpl : public CodeCompletionCallbacks {
1317
1317
// / \returns true on success, false on failure.
1318
1318
bool typecheckParsedType () {
1319
1319
assert (ParsedTypeLoc.getTypeRepr () && " should have a TypeRepr" );
1320
- return !performTypeLocChecking (P.Context , ParsedTypeLoc,
1321
- CurDeclContext, false );
1320
+ if (!performTypeLocChecking (P.Context , ParsedTypeLoc,
1321
+ CurDeclContext, false ))
1322
+ return true ;
1323
+
1324
+ // It doesn't type check as a type, so see if it's a qualifying module name.
1325
+ if (auto *ITR = dyn_cast<IdentTypeRepr>(ParsedTypeLoc.getTypeRepr ())) {
1326
+ SmallVector<ImportDecl::AccessPathElement, 4 > AccessPath;
1327
+ for (auto Component : ITR->getComponentRange ())
1328
+ AccessPath.push_back ({ Component->getIdentifier (),
1329
+ Component->getIdLoc () });
1330
+ if (auto Module = Context.getLoadedModule (AccessPath))
1331
+ ParsedTypeLoc.setType (ModuleType::get (Module));
1332
+ return true ;
1333
+ }
1334
+ return false ;
1322
1335
}
1323
1336
1324
1337
public:
@@ -1583,26 +1596,37 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
1583
1596
bool OnlyTypes;
1584
1597
bool OnlyPrecedenceGroups;
1585
1598
bool NeedLeadingDot;
1599
+ bool IncludeModuleQualifier;
1586
1600
1587
1601
static RequestedResultsTy fromModule (const ModuleDecl *TheModule) {
1588
- return { TheModule, false , false , false };
1602
+ return { TheModule, false , false , false , true };
1589
1603
}
1590
1604
1591
1605
RequestedResultsTy onlyTypes () const {
1592
- return { TheModule, true , false , NeedLeadingDot };
1606
+ return { TheModule, true , false , NeedLeadingDot, IncludeModuleQualifier };
1593
1607
}
1594
1608
1595
1609
RequestedResultsTy onlyPrecedenceGroups () const {
1596
1610
assert (!OnlyTypes && " onlyTypes() already includes precedence groups" );
1597
- return { TheModule, false , true , false };
1611
+ return { TheModule, false , true , false , true };
1598
1612
}
1599
1613
1600
1614
RequestedResultsTy needLeadingDot (bool NeedDot) const {
1601
- return { TheModule, OnlyTypes, OnlyPrecedenceGroups, NeedDot };
1615
+ return {
1616
+ TheModule, OnlyTypes, OnlyPrecedenceGroups, NeedDot,
1617
+ IncludeModuleQualifier
1618
+ };
1619
+ }
1620
+
1621
+ RequestedResultsTy withModuleQualifier (bool IncludeModule) const {
1622
+ return {
1623
+ TheModule, OnlyTypes, OnlyPrecedenceGroups, NeedLeadingDot,
1624
+ IncludeModule
1625
+ };
1602
1626
}
1603
1627
1604
1628
static RequestedResultsTy toplevelResults () {
1605
- return { nullptr , false , false , false };
1629
+ return { nullptr , false , false , false , true };
1606
1630
}
1607
1631
};
1608
1632
@@ -1741,6 +1765,21 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
1741
1765
}
1742
1766
}
1743
1767
1768
+ void addModuleName (
1769
+ const ModuleDecl *MD,
1770
+ Optional<CodeCompletionResult::NotRecommendedReason> R = None) {
1771
+ CodeCompletionResultBuilder Builder (
1772
+ Sink,
1773
+ CodeCompletionResult::ResultKind::Declaration,
1774
+ SemanticContextKind::OtherModule,
1775
+ expectedTypeContext);
1776
+ Builder.setAssociatedDecl (MD);
1777
+ Builder.addTextChunk (MD->getNameStr ());
1778
+ Builder.addTypeAnnotation (" Module" );
1779
+ if (R)
1780
+ Builder.setNotRecommended (*R);
1781
+ }
1782
+
1744
1783
void addImportModuleNames () {
1745
1784
SmallVector<Identifier, 0 > ModuleNames;
1746
1785
Ctx.getVisibleTopLevelModuleNames (ModuleNames);
@@ -1757,19 +1796,13 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
1757
1796
continue ;
1758
1797
1759
1798
auto MD = ModuleDecl::create (ModuleName, Ctx);
1760
- CodeCompletionResultBuilder Builder (
1761
- Sink,
1762
- CodeCompletionResult::ResultKind::Declaration,
1763
- SemanticContextKind::OtherModule,
1764
- expectedTypeContext);
1765
- Builder.setAssociatedDecl (MD);
1766
- Builder.addTextChunk (MD->getNameStr ());
1767
- Builder.addTypeAnnotation (" Module" );
1799
+ Optional<CodeCompletionResult::NotRecommendedReason> Reason = None;
1768
1800
1769
1801
// Imported modules are not recommended.
1770
1802
if (ImportedModules.count (MD->getNameStr ()) != 0 )
1771
- Builder.setNotRecommended (
1772
- CodeCompletionResult::NotRecommendedReason::Redundant);
1803
+ Reason = CodeCompletionResult::NotRecommendedReason::Redundant;
1804
+
1805
+ addModuleName (MD, Reason);
1773
1806
}
1774
1807
}
1775
1808
@@ -3199,13 +3232,17 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
3199
3232
return false ;
3200
3233
}
3201
3234
3202
- bool tryModuleCompletions (Type ExprType) {
3235
+ bool tryModuleCompletions (Type ExprType, bool TypesOnly = false ) {
3203
3236
if (auto MT = ExprType->getAs <ModuleType>()) {
3204
3237
ModuleDecl *M = MT->getModule ();
3205
3238
if (CurrModule != M) {
3206
3239
// Only use the cache if it is not the current module.
3207
- RequestedCachedResults.push_back (
3208
- RequestedResultsTy::fromModule (M).needLeadingDot (needDot ()));
3240
+ RequestedResultsTy Request = RequestedResultsTy::fromModule (M)
3241
+ .needLeadingDot (needDot ())
3242
+ .withModuleQualifier (false );
3243
+ if (TypesOnly)
3244
+ Request = Request.onlyTypes ();
3245
+ RequestedCachedResults.push_back (Request);
3209
3246
return true ;
3210
3247
}
3211
3248
}
@@ -3763,17 +3800,16 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
3763
3800
3764
3801
void getValueCompletionsInDeclContext (SourceLoc Loc,
3765
3802
DeclFilter Filter = DefaultFilter,
3766
- bool IncludeTopLevel = false ,
3767
- bool RequestCache = true ,
3768
- bool LiteralCompletions = true ) {
3803
+ bool LiteralCompletions = true ,
3804
+ bool ModuleQualifier = true ) {
3769
3805
ExprType = Type ();
3770
3806
Kind = LookupKind::ValueInDeclContext;
3771
3807
NeedLeadingDot = false ;
3772
3808
FilteredDeclConsumer Consumer (*this , Filter);
3773
3809
lookupVisibleDecls (Consumer, CurrDeclContext,
3774
- /* IncludeTopLevel=*/ IncludeTopLevel , Loc);
3775
- if (RequestCache )
3776
- RequestedCachedResults. push_back ( RequestedResultsTy::toplevelResults ( ));
3810
+ /* IncludeTopLevel=*/ false , Loc);
3811
+ RequestedCachedResults. push_back ( RequestedResultsTy::toplevelResults ( )
3812
+ . withModuleQualifier (ModuleQualifier ));
3777
3813
3778
3814
// Manually add any expected nominal types from imported modules so that
3779
3815
// they get their expected type relation. Don't include protocols, since
@@ -3871,6 +3907,8 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
3871
3907
}
3872
3908
3873
3909
void getTypeCompletions (Type BaseType) {
3910
+ if (tryModuleCompletions (BaseType, /* OnlyTypes=*/ true ))
3911
+ return ;
3874
3912
Kind = LookupKind::Type;
3875
3913
this ->BaseType = BaseType;
3876
3914
NeedLeadingDot = !HaveDot;
@@ -3880,7 +3918,7 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
3880
3918
if (BaseType->isAnyExistentialType ()) {
3881
3919
addKeyword (" Protocol" , MetatypeType::get (BaseType));
3882
3920
addKeyword (" Type" , ExistentialMetatypeType::get (BaseType));
3883
- } else {
3921
+ } else if (!BaseType-> is <ModuleType>()) {
3884
3922
addKeyword (" Type" , MetatypeType::get (BaseType));
3885
3923
}
3886
3924
}
@@ -3958,8 +3996,9 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
3958
3996
if (Module == CurrModule)
3959
3997
continue ;
3960
3998
3961
- RequestedCachedResults.push_back (
3962
- RequestedResultsTy::fromModule (Module).onlyPrecedenceGroups ());
3999
+ RequestedCachedResults.push_back (RequestedResultsTy::fromModule (Module)
4000
+ .onlyPrecedenceGroups ()
4001
+ .withModuleQualifier (false ));
3963
4002
}
3964
4003
}
3965
4004
@@ -3996,13 +4035,16 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
3996
4035
getAttributeDeclParamCompletions (DAK_Available, 0 );
3997
4036
}
3998
4037
3999
- void getTypeCompletionsInDeclContext (SourceLoc Loc) {
4038
+ void getTypeCompletionsInDeclContext (SourceLoc Loc,
4039
+ bool ModuleQualifier = true ) {
4000
4040
Kind = LookupKind::TypeInDeclContext;
4001
4041
lookupVisibleDecls (*this , CurrDeclContext,
4002
4042
/* IncludeTopLevel=*/ false , Loc);
4003
4043
4004
4044
RequestedCachedResults.push_back (
4005
- RequestedResultsTy::toplevelResults ().onlyTypes ());
4045
+ RequestedResultsTy::toplevelResults ()
4046
+ .onlyTypes ()
4047
+ .withModuleQualifier (ModuleQualifier));
4006
4048
}
4007
4049
4008
4050
void getToplevelCompletions (bool OnlyTypes) {
@@ -5116,20 +5158,8 @@ void CodeCompletionCallbacksImpl::doneParsing() {
5116
5158
return ;
5117
5159
}
5118
5160
5119
- if (!ParsedTypeLoc.isNull () && !typecheckParsedType ()) {
5120
- // If we think we parsed a type but it doesn't type check as a type, see if
5121
- // it's a qualifying module name and recover if so.
5122
- if (auto *ITR = dyn_cast<IdentTypeRepr>(ParsedTypeLoc.getTypeRepr ())) {
5123
- SmallVector<ImportDecl::AccessPathElement, 4 > AccessPath;
5124
- for (auto Component : ITR->getComponentRange ())
5125
- AccessPath.push_back ({ Component->getIdentifier (),
5126
- Component->getIdLoc () });
5127
- if (auto Module = Context.getLoadedModule (AccessPath))
5128
- ParsedTypeLoc.setType (ModuleType::get (Module));
5129
- }
5130
- if (ParsedTypeLoc.isNull ())
5131
- return ;
5132
- }
5161
+ if (!ParsedTypeLoc.isNull () && !typecheckParsedType ())
5162
+ return ;
5133
5163
5134
5164
CompletionLookup Lookup (CompletionContext.getResultSink (), P.Context ,
5135
5165
CurDeclContext, &CompletionContext);
@@ -5281,7 +5311,7 @@ void CodeCompletionCallbacksImpl::doneParsing() {
5281
5311
} else {
5282
5312
SourceLoc Loc = P.Context .SourceMgr .getCodeCompletionLoc ();
5283
5313
Lookup.getValueCompletionsInDeclContext (Loc, KeyPathFilter,
5284
- false , true , false );
5314
+ /* LiteralCompletions= */ false );
5285
5315
}
5286
5316
break ;
5287
5317
}
@@ -5516,6 +5546,8 @@ void CodeCompletionCallbacksImpl::doneParsing() {
5516
5546
return ; // already handled.
5517
5547
RequestedModules.push_back ({std::move (K), TheModule,
5518
5548
Request.OnlyTypes , Request.OnlyPrecedenceGroups });
5549
+ if (Request.IncludeModuleQualifier )
5550
+ Lookup.addModuleName (TheModule);
5519
5551
}
5520
5552
};
5521
5553
@@ -5528,6 +5560,10 @@ void CodeCompletionCallbacksImpl::doneParsing() {
5528
5560
// Add results from current module.
5529
5561
Lookup.getToplevelCompletions (Request.OnlyTypes );
5530
5562
5563
+ // Add the qualifying module name
5564
+ if (Request.IncludeModuleQualifier )
5565
+ Lookup.addModuleName (CurDeclContext->getParentModule ());
5566
+
5531
5567
// Add results for all imported modules.
5532
5568
ModuleDecl::ImportFilter ImportFilter;
5533
5569
ImportFilter |= ModuleDecl::ImportFilterKind::Public;
0 commit comments