Skip to content

[CodeCompletion] Suggest 'actor' decl introducer keyword #58698

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions lib/IDE/CodeCompletion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -671,11 +671,6 @@ static void addDeclKeywords(CodeCompletionResultSink &Sink, DeclContext *DC,
case CodeCompletionKeywordKind::kw_enum:
case CodeCompletionKeywordKind::kw_extension:
return true;
case CodeCompletionKeywordKind::None:
if (DAK && *DAK == DeclAttrKind::DAK_Actor) {
return true;
}
break;
default:
break;
}
Expand Down Expand Up @@ -782,12 +777,20 @@ static void addDeclKeywords(CodeCompletionResultSink &Sink, DeclContext *DC,
DeclAttribute::isConcurrencyOnly(*DAK))
return;

addKeyword(Sink, Name, Kind, /*TypeAnnotation=*/"", getFlair(Kind, DAK));
CodeCompletionFlair flair = getFlair(Kind, DAK);

// Special case for 'actor'. Get the same flair with 'kw_class'.
if (Kind == CodeCompletionKeywordKind::None && Name == "actor")
flair = getFlair(CodeCompletionKeywordKind::kw_class, None);

addKeyword(Sink, Name, Kind, /*TypeAnnotation=*/"", flair);
};

#define DECL_KEYWORD(kw) \
AddDeclKeyword(#kw, CodeCompletionKeywordKind::kw_##kw, None);
#include "swift/Syntax/TokenKinds.def"
// Manually add "actor" because it's a contextual keyword.
AddDeclKeyword("actor", CodeCompletionKeywordKind::None, None);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn’t it be simpler to remove the special case in AddDeclKeyword and change this

Suggested change
AddDeclKeyword("actor", CodeCompletionKeywordKind::None, None);
AddDeclKeyword("actor", CodeCompletionKeywordKind::kw_class, None);

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't 100% sure we can emit actor as CodeCompletionKeywordKind::kw_class. WDYT?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn’t that exactly what you’re doing in line 783-784? Or am I missing something?

Copy link
Member Author

@rintaro rintaro May 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I'm not rewriting the Kind, but I'm just using kw_class to get the flair for actor.
Using kw_class results

Keyword[class]/None: actor

instead of

Keyword/None: actor

I admit CodeCompletionKeywordKind is not used in many place, but for example, CodeCompletion::LimitedResultView has an ability to filter the results by the kind.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I see. I that case forget what I said.


// Context-sensitive keywords.
auto AddCSKeyword = [&](StringRef Name, DeclAttrKind Kind) {
Expand Down
4 changes: 4 additions & 0 deletions test/IDE/complete_keywords.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

// KW_DECL: Begin completions
// KW_DECL-DAG: Keyword[class]/None: class{{; name=.+$}}
// KW_DECL-DAG: Keyword/None: actor{{; name=.+$}}
// KW_DECL-DAG: Keyword/None: convenience{{; name=.+$}}
// KW_DECL-DAG: Keyword[deinit]/None: deinit{{; name=.+$}}
// KW_DECL-DAG: Keyword/None: dynamic{{; name=.+$}}
Expand Down Expand Up @@ -42,6 +43,7 @@

// KW_DECL_PROTOCOL: Begin completions
// KW_DECL_PROTOCOL-DAG: Keyword[class]/None/Flair[RareKeyword]: class{{; name=.+$}}
// KW_DECL_PROTOCOL-DAG: Keyword/None/Flair[RareKeyword]: actor{{; name=.+$}}
// KW_DECL_PROTOCOL-DAG: Keyword/None: convenience{{; name=.+$}}
// KW_DECL_PROTOCOL-DAG: Keyword[deinit]/None: deinit{{; name=.+$}}
// KW_DECL_PROTOCOL-DAG: Keyword/None: dynamic{{; name=.+$}}
Expand Down Expand Up @@ -78,6 +80,7 @@

// KW_DECL_TYPECONTEXT: Begin completions
// KW_DECL_TYPECONTEXT-DAG: Keyword[class]/None: class{{; name=.+$}}
// KW_DECL_TYPECONTEXT-DAG: Keyword/None: actor{{; name=.+$}}
// KW_DECL_TYPECONTEXT-DAG: Keyword/None: convenience{{; name=.+$}}
// KW_DECL_TYPECONTEXT-DAG: Keyword[deinit]/None: deinit{{; name=.+$}}
// KW_DECL_TYPECONTEXT-DAG: Keyword/None: dynamic{{; name=.+$}}
Expand Down Expand Up @@ -118,6 +121,7 @@
// Declaration keywords.
//
// KW_DECL_STMT_TOPLEVEL-DAG: Keyword[class]/None: class{{; name=.+$}}
// KW_DECL_STMT_TOPLEVEL-DAG: Keyword/None: actor{{; name=.+$}}
// KW_DECL_STMT_TOPLEVEL-DAG: Keyword/None: convenience{{; name=.+$}}
// KW_DECL_STMT_TOPLEVEL-DAG: Keyword[deinit]/None: deinit{{; name=.+$}}
// KW_DECL_STMT_TOPLEVEL-DAG: Keyword/None: dynamic{{; name=.+$}}
Expand Down
10 changes: 10 additions & 0 deletions test/SourceKit/CodeComplete/complete_override.swift.response
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@
key.typerelation: source.codecompletion.typerelation.notapplicable,
key.num_bytes_to_erase: 0
},
{
key.kind: source.lang.swift.keyword,
key.name: "actor",
key.sourcetext: "actor",
key.description: "actor",
key.typename: "",
key.context: source.codecompletion.context.none,
key.typerelation: source.codecompletion.typerelation.notapplicable,
key.num_bytes_to_erase: 0
},
{
key.kind: source.lang.swift.keyword,
key.name: "associatedtype",
Expand Down