@@ -1461,6 +1461,7 @@ class CodeCompletionCallbacksImpl : public CodeCompletionCallbacks {
1461
1461
void completeReturnStmt (CodeCompletionExpr *E) override ;
1462
1462
void completeAfterPound (CodeCompletionExpr *E, StmtKind ParentKind) override ;
1463
1463
void completeGenericParams (TypeLoc TL) override ;
1464
+ void completeAfterIfStmt (bool hasElse) override ;
1464
1465
void addKeywords (CodeCompletionResultSink &Sink, bool MaybeFuncBody);
1465
1466
1466
1467
void doneParsing () override ;
@@ -4844,6 +4845,15 @@ void CodeCompletionCallbacksImpl::completeAfterPound(CodeCompletionExpr *E,
4844
4845
ParentStmtKind = ParentKind;
4845
4846
}
4846
4847
4848
+ void CodeCompletionCallbacksImpl::completeAfterIfStmt (bool hasElse) {
4849
+ CurDeclContext = P.CurDeclContext ;
4850
+ if (hasElse) {
4851
+ Kind = CompletionKind::AfterIfStmtElse;
4852
+ } else {
4853
+ Kind = CompletionKind::StmtOrExpr;
4854
+ }
4855
+ }
4856
+
4847
4857
void CodeCompletionCallbacksImpl::completeGenericParams (TypeLoc TL) {
4848
4858
CurDeclContext = P.CurDeclContext ;
4849
4859
Kind = CompletionKind::GenericParams;
@@ -4869,8 +4879,20 @@ static bool isClangSubModule(ModuleDecl *TheModule) {
4869
4879
return false ;
4870
4880
}
4871
4881
4882
+ static void addKeyword (CodeCompletionResultSink &Sink, StringRef Name,
4883
+ CodeCompletionKeywordKind Kind,
4884
+ StringRef TypeAnnotation = " " ) {
4885
+ CodeCompletionResultBuilder Builder (
4886
+ Sink, CodeCompletionResult::ResultKind::Keyword,
4887
+ SemanticContextKind::None, {});
4888
+ Builder.setKeywordKind (Kind);
4889
+ Builder.addTextChunk (Name);
4890
+ if (!TypeAnnotation.empty ())
4891
+ Builder.addTypeAnnotation (TypeAnnotation);
4892
+ }
4893
+
4872
4894
static void addDeclKeywords (CodeCompletionResultSink &Sink) {
4873
- auto AddKeyword = [&](StringRef Name, CodeCompletionKeywordKind Kind,
4895
+ auto AddDeclKeyword = [&](StringRef Name, CodeCompletionKeywordKind Kind,
4874
4896
Optional<DeclAttrKind> DAK) {
4875
4897
if (Name == " let" || Name == " var" ) {
4876
4898
// Treat keywords that could be the start of a pattern specially.
@@ -4880,19 +4902,15 @@ static void addDeclKeywords(CodeCompletionResultSink &Sink) {
4880
4902
// Remove user inaccessible keywords.
4881
4903
if (DAK.hasValue () && DeclAttribute::isUserInaccessible (*DAK)) return ;
4882
4904
4883
- CodeCompletionResultBuilder Builder (
4884
- Sink, CodeCompletionResult::ResultKind::Keyword,
4885
- SemanticContextKind::None, {});
4886
- Builder.setKeywordKind (Kind);
4887
- Builder.addTextChunk (Name);
4905
+ addKeyword (Sink, Name, Kind);
4888
4906
};
4889
4907
4890
- #define DECL_KEYWORD (kw ) AddKeyword (#kw, CodeCompletionKeywordKind::kw_##kw, None);
4908
+ #define DECL_KEYWORD (kw ) AddDeclKeyword (#kw, CodeCompletionKeywordKind::kw_##kw, None);
4891
4909
#include " swift/Syntax/TokenKinds.def"
4892
4910
4893
4911
// Context-sensitive keywords.
4894
4912
auto AddCSKeyword = [&](StringRef Name, DeclAttrKind Kind) {
4895
- AddKeyword (Name, CodeCompletionKeywordKind::None, Kind);
4913
+ AddDeclKeyword (Name, CodeCompletionKeywordKind::None, Kind);
4896
4914
};
4897
4915
4898
4916
#define CONTEXTUAL_CASE (KW, CLASS ) AddCSKeyword(#KW, DAK_##CLASS);
@@ -4905,68 +4923,40 @@ static void addDeclKeywords(CodeCompletionResultSink &Sink) {
4905
4923
}
4906
4924
4907
4925
static void addStmtKeywords (CodeCompletionResultSink &Sink, bool MaybeFuncBody) {
4908
- auto AddKeyword = [&](StringRef Name, CodeCompletionKeywordKind Kind) {
4926
+ auto AddStmtKeyword = [&](StringRef Name, CodeCompletionKeywordKind Kind) {
4909
4927
if (!MaybeFuncBody && Kind == CodeCompletionKeywordKind::kw_return)
4910
4928
return ;
4911
-
4912
- CodeCompletionResultBuilder Builder (
4913
- Sink, CodeCompletionResult::ResultKind::Keyword,
4914
- SemanticContextKind::None, {});
4915
- Builder.setKeywordKind (Kind);
4916
- Builder.addTextChunk (Name);
4929
+ addKeyword (Sink, Name, Kind);
4917
4930
};
4918
- #define STMT_KEYWORD (kw ) AddKeyword (#kw, CodeCompletionKeywordKind::kw_##kw);
4931
+ #define STMT_KEYWORD (kw ) AddStmtKeyword (#kw, CodeCompletionKeywordKind::kw_##kw);
4919
4932
#include " swift/Syntax/TokenKinds.def"
4920
4933
4921
4934
// Throw is not marked as a STMT_KEYWORD.
4922
- AddKeyword (" throw" , CodeCompletionKeywordKind::kw_throw);
4935
+ AddStmtKeyword (" throw" , CodeCompletionKeywordKind::kw_throw);
4923
4936
}
4924
4937
4925
4938
static void addLetVarKeywords (CodeCompletionResultSink &Sink) {
4926
- auto AddKeyword = [&](StringRef Name, CodeCompletionKeywordKind Kind) {
4927
- CodeCompletionResultBuilder Builder (
4928
- Sink, CodeCompletionResult::ResultKind::Keyword,
4929
- SemanticContextKind::None, {});
4930
- Builder.setKeywordKind (Kind);
4931
- Builder.addTextChunk (Name);
4932
- };
4933
-
4934
- AddKeyword (" let" , CodeCompletionKeywordKind::kw_let);
4935
- AddKeyword (" var" , CodeCompletionKeywordKind::kw_var);
4939
+ addKeyword (Sink, " let" , CodeCompletionKeywordKind::kw_let);
4940
+ addKeyword (Sink, " var" , CodeCompletionKeywordKind::kw_var);
4936
4941
}
4937
4942
4938
4943
static void addExprKeywords (CodeCompletionResultSink &Sink) {
4939
- auto AddKeyword = [&](StringRef Name, StringRef TypeAnnotation, CodeCompletionKeywordKind Kind) {
4940
- CodeCompletionResultBuilder Builder (
4941
- Sink, CodeCompletionResult::ResultKind::Keyword,
4942
- SemanticContextKind::None, {});
4943
- Builder.setKeywordKind (Kind);
4944
- Builder.addTextChunk (Name);
4945
- if (!TypeAnnotation.empty ())
4946
- Builder.addTypeAnnotation (TypeAnnotation);
4947
- };
4948
-
4949
4944
// Expr keywords.
4950
- AddKeyword ( " try" , StringRef () , CodeCompletionKeywordKind::kw_try);
4951
- AddKeyword ( " try!" , StringRef () , CodeCompletionKeywordKind::kw_try);
4952
- AddKeyword ( " try?" , StringRef () , CodeCompletionKeywordKind::kw_try);
4945
+ addKeyword (Sink, " try" , CodeCompletionKeywordKind::kw_try);
4946
+ addKeyword (Sink, " try!" , CodeCompletionKeywordKind::kw_try);
4947
+ addKeyword (Sink, " try?" , CodeCompletionKeywordKind::kw_try);
4953
4948
// FIXME: The pedantically correct way to find the type is to resolve the
4954
4949
// Swift.StringLiteralType type.
4955
- AddKeyword ( " #function" , " String " , CodeCompletionKeywordKind::pound_function);
4956
- AddKeyword ( " #file" , " String " , CodeCompletionKeywordKind::pound_file);
4950
+ addKeyword (Sink, " #function" , CodeCompletionKeywordKind::pound_function, " String " );
4951
+ addKeyword (Sink, " #file" , CodeCompletionKeywordKind::pound_file, " String " );
4957
4952
// Same: Swift.IntegerLiteralType.
4958
- AddKeyword ( " #line" , " Int " , CodeCompletionKeywordKind::pound_line);
4959
- AddKeyword ( " #column" , " Int " , CodeCompletionKeywordKind::pound_column);
4960
- AddKeyword ( " #dsohandle" , " UnsafeMutableRawPointer " , CodeCompletionKeywordKind::pound_dsohandle);
4953
+ addKeyword (Sink, " #line" , CodeCompletionKeywordKind::pound_line, " Int " );
4954
+ addKeyword (Sink, " #column" , CodeCompletionKeywordKind::pound_column, " Int " );
4955
+ addKeyword (Sink, " #dsohandle" , CodeCompletionKeywordKind::pound_dsohandle, " UnsafeMutableRawPointer " );
4961
4956
}
4962
4957
4963
4958
static void addAnyTypeKeyword (CodeCompletionResultSink &Sink) {
4964
- CodeCompletionResultBuilder Builder (
4965
- Sink, CodeCompletionResult::ResultKind::Keyword,
4966
- SemanticContextKind::None, {});
4967
- Builder.setKeywordKind (CodeCompletionKeywordKind::None);
4968
- Builder.addTextChunk (" Any" );
4969
- Builder.addTypeAnnotation (" Any" );
4959
+ addKeyword (Sink, " Any" , CodeCompletionKeywordKind::None, " Any" );
4970
4960
}
4971
4961
4972
4962
@@ -5020,6 +5010,10 @@ void CodeCompletionCallbacksImpl::addKeywords(CodeCompletionResultSink &Sink,
5020
5010
addDeclKeywords (Sink);
5021
5011
addLetVarKeywords (Sink);
5022
5012
break ;
5013
+
5014
+ case CompletionKind::AfterIfStmtElse:
5015
+ addKeyword (Sink, " if" , CodeCompletionKeywordKind::kw_if);
5016
+ break ;
5023
5017
}
5024
5018
}
5025
5019
@@ -5578,6 +5572,9 @@ void CodeCompletionCallbacksImpl::doneParsing() {
5578
5572
}
5579
5573
}
5580
5574
break ;
5575
+ case CompletionKind::AfterIfStmtElse:
5576
+ // Handled earlier by keyword completions.
5577
+ break ;
5581
5578
}
5582
5579
5583
5580
if (Lookup.RequestedCachedResults ) {
0 commit comments