@@ -1467,6 +1467,7 @@ class CodeCompletionCallbacksImpl : public CodeCompletionCallbacks {
1467
1467
void completeReturnStmt (CodeCompletionExpr *E) override ;
1468
1468
void completeAfterPound (CodeCompletionExpr *E, StmtKind ParentKind) override ;
1469
1469
void completeGenericParams (TypeLoc TL) override ;
1470
+ void completeAfterIfStmt (bool hasElse) override ;
1470
1471
void addKeywords (CodeCompletionResultSink &Sink, bool MaybeFuncBody);
1471
1472
1472
1473
void doneParsing () override ;
@@ -4745,6 +4746,15 @@ void CodeCompletionCallbacksImpl::completeAfterPound(CodeCompletionExpr *E,
4745
4746
ParentStmtKind = ParentKind;
4746
4747
}
4747
4748
4749
+ void CodeCompletionCallbacksImpl::completeAfterIfStmt (bool hasElse) {
4750
+ CurDeclContext = P.CurDeclContext ;
4751
+ if (hasElse) {
4752
+ Kind = CompletionKind::AfterIfStmtElse;
4753
+ } else {
4754
+ Kind = CompletionKind::StmtOrExpr;
4755
+ }
4756
+ }
4757
+
4748
4758
void CodeCompletionCallbacksImpl::completeGenericParams (TypeLoc TL) {
4749
4759
CurDeclContext = P.CurDeclContext ;
4750
4760
Kind = CompletionKind::GenericParams;
@@ -4770,8 +4780,20 @@ static bool isClangSubModule(ModuleDecl *TheModule) {
4770
4780
return false ;
4771
4781
}
4772
4782
4783
+ static void addKeyword (CodeCompletionResultSink &Sink, StringRef Name,
4784
+ CodeCompletionKeywordKind Kind,
4785
+ StringRef TypeAnnotation = " " ) {
4786
+ CodeCompletionResultBuilder Builder (
4787
+ Sink, CodeCompletionResult::ResultKind::Keyword,
4788
+ SemanticContextKind::None, {});
4789
+ Builder.setKeywordKind (Kind);
4790
+ Builder.addTextChunk (Name);
4791
+ if (!TypeAnnotation.empty ())
4792
+ Builder.addTypeAnnotation (TypeAnnotation);
4793
+ }
4794
+
4773
4795
static void addDeclKeywords (CodeCompletionResultSink &Sink) {
4774
- auto AddKeyword = [&](StringRef Name, CodeCompletionKeywordKind Kind,
4796
+ auto AddDeclKeyword = [&](StringRef Name, CodeCompletionKeywordKind Kind,
4775
4797
Optional<DeclAttrKind> DAK) {
4776
4798
if (Name == " let" || Name == " var" ) {
4777
4799
// Treat keywords that could be the start of a pattern specially.
@@ -4781,19 +4803,15 @@ static void addDeclKeywords(CodeCompletionResultSink &Sink) {
4781
4803
// Remove user inaccessible keywords.
4782
4804
if (DAK.hasValue () && DeclAttribute::isUserInaccessible (*DAK)) return ;
4783
4805
4784
- CodeCompletionResultBuilder Builder (
4785
- Sink, CodeCompletionResult::ResultKind::Keyword,
4786
- SemanticContextKind::None, {});
4787
- Builder.setKeywordKind (Kind);
4788
- Builder.addTextChunk (Name);
4806
+ addKeyword (Sink, Name, Kind);
4789
4807
};
4790
4808
4791
- #define DECL_KEYWORD (kw ) AddKeyword (#kw, CodeCompletionKeywordKind::kw_##kw, None);
4809
+ #define DECL_KEYWORD (kw ) AddDeclKeyword (#kw, CodeCompletionKeywordKind::kw_##kw, None);
4792
4810
#include " swift/Syntax/TokenKinds.def"
4793
4811
4794
4812
// Context-sensitive keywords.
4795
4813
auto AddCSKeyword = [&](StringRef Name, DeclAttrKind Kind) {
4796
- AddKeyword (Name, CodeCompletionKeywordKind::None, Kind);
4814
+ AddDeclKeyword (Name, CodeCompletionKeywordKind::None, Kind);
4797
4815
};
4798
4816
4799
4817
#define CONTEXTUAL_CASE (KW, CLASS ) AddCSKeyword(#KW, DAK_##CLASS);
@@ -4806,65 +4824,37 @@ static void addDeclKeywords(CodeCompletionResultSink &Sink) {
4806
4824
}
4807
4825
4808
4826
static void addStmtKeywords (CodeCompletionResultSink &Sink, bool MaybeFuncBody) {
4809
- auto AddKeyword = [&](StringRef Name, CodeCompletionKeywordKind Kind) {
4827
+ auto AddStmtKeyword = [&](StringRef Name, CodeCompletionKeywordKind Kind) {
4810
4828
if (!MaybeFuncBody && Kind == CodeCompletionKeywordKind::kw_return)
4811
4829
return ;
4812
-
4813
- CodeCompletionResultBuilder Builder (
4814
- Sink, CodeCompletionResult::ResultKind::Keyword,
4815
- SemanticContextKind::None, {});
4816
- Builder.setKeywordKind (Kind);
4817
- Builder.addTextChunk (Name);
4830
+ addKeyword (Sink, Name, Kind);
4818
4831
};
4819
- #define STMT_KEYWORD (kw ) AddKeyword (#kw, CodeCompletionKeywordKind::kw_##kw);
4832
+ #define STMT_KEYWORD (kw ) AddStmtKeyword (#kw, CodeCompletionKeywordKind::kw_##kw);
4820
4833
#include " swift/Syntax/TokenKinds.def"
4821
4834
}
4822
4835
4823
4836
static void addLetVarKeywords (CodeCompletionResultSink &Sink) {
4824
- auto AddKeyword = [&](StringRef Name, CodeCompletionKeywordKind Kind) {
4825
- CodeCompletionResultBuilder Builder (
4826
- Sink, CodeCompletionResult::ResultKind::Keyword,
4827
- SemanticContextKind::None, {});
4828
- Builder.setKeywordKind (Kind);
4829
- Builder.addTextChunk (Name);
4830
- };
4831
-
4832
- AddKeyword (" let" , CodeCompletionKeywordKind::kw_let);
4833
- AddKeyword (" var" , CodeCompletionKeywordKind::kw_var);
4837
+ addKeyword (Sink, " let" , CodeCompletionKeywordKind::kw_let);
4838
+ addKeyword (Sink, " var" , CodeCompletionKeywordKind::kw_var);
4834
4839
}
4835
4840
4836
4841
static void addExprKeywords (CodeCompletionResultSink &Sink) {
4837
- auto AddKeyword = [&](StringRef Name, StringRef TypeAnnotation, CodeCompletionKeywordKind Kind) {
4838
- CodeCompletionResultBuilder Builder (
4839
- Sink, CodeCompletionResult::ResultKind::Keyword,
4840
- SemanticContextKind::None, {});
4841
- Builder.setKeywordKind (Kind);
4842
- Builder.addTextChunk (Name);
4843
- if (!TypeAnnotation.empty ())
4844
- Builder.addTypeAnnotation (TypeAnnotation);
4845
- };
4846
-
4847
4842
// Expr keywords.
4848
- AddKeyword ( " try" , StringRef () , CodeCompletionKeywordKind::kw_try);
4849
- AddKeyword ( " try!" , StringRef () , CodeCompletionKeywordKind::kw_try);
4850
- AddKeyword ( " try?" , StringRef () , CodeCompletionKeywordKind::kw_try);
4843
+ addKeyword (Sink, " try" , CodeCompletionKeywordKind::kw_try);
4844
+ addKeyword (Sink, " try!" , CodeCompletionKeywordKind::kw_try);
4845
+ addKeyword (Sink, " try?" , CodeCompletionKeywordKind::kw_try);
4851
4846
// FIXME: The pedantically correct way to find the type is to resolve the
4852
4847
// Swift.StringLiteralType type.
4853
- AddKeyword ( " #function" , " String " , CodeCompletionKeywordKind::pound_function);
4854
- AddKeyword ( " #file" , " String " , CodeCompletionKeywordKind::pound_file);
4848
+ addKeyword (Sink, " #function" , CodeCompletionKeywordKind::pound_function, " String " );
4849
+ addKeyword (Sink, " #file" , CodeCompletionKeywordKind::pound_file, " String " );
4855
4850
// Same: Swift.IntegerLiteralType.
4856
- AddKeyword ( " #line" , " Int " , CodeCompletionKeywordKind::pound_line);
4857
- AddKeyword ( " #column" , " Int " , CodeCompletionKeywordKind::pound_column);
4858
- AddKeyword ( " #dsohandle" , " UnsafeMutableRawPointer " , CodeCompletionKeywordKind::pound_dsohandle);
4851
+ addKeyword (Sink, " #line" , CodeCompletionKeywordKind::pound_line, " Int " );
4852
+ addKeyword (Sink, " #column" , CodeCompletionKeywordKind::pound_column, " Int " );
4853
+ addKeyword (Sink, " #dsohandle" , CodeCompletionKeywordKind::pound_dsohandle, " UnsafeMutableRawPointer " );
4859
4854
}
4860
4855
4861
4856
static void addAnyTypeKeyword (CodeCompletionResultSink &Sink) {
4862
- CodeCompletionResultBuilder Builder (
4863
- Sink, CodeCompletionResult::ResultKind::Keyword,
4864
- SemanticContextKind::None, {});
4865
- Builder.setKeywordKind (CodeCompletionKeywordKind::None);
4866
- Builder.addTextChunk (" Any" );
4867
- Builder.addTypeAnnotation (" Any" );
4857
+ addKeyword (Sink, " Any" , CodeCompletionKeywordKind::None, " Any" );
4868
4858
}
4869
4859
4870
4860
@@ -4918,6 +4908,10 @@ void CodeCompletionCallbacksImpl::addKeywords(CodeCompletionResultSink &Sink,
4918
4908
addDeclKeywords (Sink);
4919
4909
addLetVarKeywords (Sink);
4920
4910
break ;
4911
+
4912
+ case CompletionKind::AfterIfStmtElse:
4913
+ addKeyword (Sink, " if" , CodeCompletionKeywordKind::kw_if);
4914
+ break ;
4921
4915
}
4922
4916
}
4923
4917
@@ -5494,6 +5488,9 @@ void CodeCompletionCallbacksImpl::doneParsing() {
5494
5488
}
5495
5489
}
5496
5490
break ;
5491
+ case CompletionKind::AfterIfStmtElse:
5492
+ // Handled earlier by keyword completions.
5493
+ break ;
5497
5494
}
5498
5495
5499
5496
if (Lookup.RequestedCachedResults ) {
0 commit comments