Skip to content

[CodeCompletion] Add decl context dependent 'Flair' to decl keywords #37725

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

Conversation

rintaro
Copy link
Member

@rintaro rintaro commented Jun 1, 2021

Based on #37563

Introduce CommonKeywordAtCurrentPosition and RareKeywordAtCurrentPosition

  • super in a overriding decl is "common".
  • type decl introducers (e.g. struct, enum) at top-level in library files are "common"
  • type decl introducers in protocol are invalid, hence "rare"
  • top-level only decl introducer (e.g. import, extension) are invalid at non-top-level, hence "rare"
  • nested types in function bodies are "rare"
  • member only decls (e.g. subscript, deinit) are invalid in function body, hence "rare"
  • some modifiers (e.g. public, private, override) are invalid for local decls, hence "rare"

rdar://77934651

@rintaro rintaro force-pushed the ide-completion-prioritize-kw-rdar77934651 branch 3 times, most recently from 7b37745 to 0cdee7b Compare June 1, 2021 23:24
@rintaro
Copy link
Member Author

rintaro commented Jun 1, 2021

@swift-ci Please smoke test

@rintaro
Copy link
Member Author

rintaro commented Jun 2, 2021

@swift-ci Please smoke test macOS

@@ -1540,6 +1542,12 @@ class CodeCompletionCallbacksImpl : public CodeCompletionCallbacks {
CodeCompletionResult::ResultKind::Keyword,
SemanticContextKind::CurrentNominal,
{});
if (auto *AFD = dyn_cast<AbstractFunctionDecl>(CurDeclContext)) {
if (AFD->getOverriddenDecl() != nullptr) {
Builder.addFlair(CodeCompletionFlairBit::CommonKeywordAtCurrentPosition);
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice.

return false;
}

static bool isCompletionDeclContextLocalContext(DeclContext *DC) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This probably deserves a brief doc comment for why it's different from DC->isLocalContext(). Also, why do we need both isCompletionDeclContextLocalContext and isCodeCompletionAtTopLevel instead of using ! to invert one of them? It's not clear to me whether isCodeCompletionAtTopLevel(X) == !isCompletionDeclContextLocalContext(X)and the isLocalContext check is just an optimization, or if there is a subtle difference I don't understand.

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 will add a doc comment.
For example, for class C { <HERE> }, isCompletionDeclContextLocalContext and isCodeCompletionAtTopLevel are both false.
I don't think there's a true/true case though.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, of course, thanks for explaining!

@@ -5930,11 +5971,127 @@ addKeyword(CodeCompletionResultSink &Sink, StringRef Name,
Builder.setExpectedTypeRelation(TypeRelation);
}

static void addDeclKeywords(CodeCompletionResultSink &Sink,
static void
addKeyword(CodeCompletionResultSink &Sink, StringRef Name,
Copy link
Contributor

Choose a reason for hiding this comment

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

Could we add a default Flair parameter to the existing addKeyword(Sink, Name, Kind...) function?

rintaro added 3 commits June 7, 2021 17:25
To describe fine grained priorities.

Introduce 'CodeCompletionFlair' that is a set of more descriptive flags for
prioritizing completion items. This aims to replace '
SemanticContextKind::ExpressionSpecific' which was a "catch all"
prioritization flag.
* 'super' in a overriding decl is "common".
* type decl introducers (e.g. 'struct', 'enum') at top-level in library
  files are "common"
* type decl introducers in 'protocol' are invalid, hence "rare"
* top-level only decl introducer (e.g. 'import', 'extension') are invalid
  at non-top-level, hence "rare"
* nested types in function bodies are "rare"
* member only decls (e.g. 'subscript', 'deinit') are invalid in function
  body, hence "rare"
* some modifiers (e.g. 'public', 'private', 'override') are invalid for
  local decls, hence "rare"

rdar://77934651
@rintaro rintaro force-pushed the ide-completion-prioritize-kw-rdar77934651 branch 2 times, most recently from 725d91b to a2b5968 Compare June 8, 2021 00:54
@rintaro
Copy link
Member Author

rintaro commented Jun 8, 2021

@swift-ci Please smoke test

@rintaro rintaro merged commit 3602f1a into swiftlang:main Jun 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants