Skip to content

[CodeCompletion] Fine grained prioritization #37563

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

Closed
wants to merge 2 commits into from

Conversation

rintaro
Copy link
Member

@rintaro rintaro commented May 21, 2021

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" flag. Also, use Flair to describe "is argument labels".

@rintaro
Copy link
Member Author

rintaro commented May 21, 2021

@swift-ci Please smoke test

@rintaro rintaro force-pushed the ide-completion-flair branch from 71ba965 to 9641e1c Compare May 21, 2021 16:09
@rintaro
Copy link
Member Author

rintaro commented May 21, 2021

@swift-ci Please smoke test

@rintaro rintaro requested a review from benlangmuir May 21, 2021 16:13
@rintaro rintaro force-pushed the ide-completion-flair branch from 9641e1c to bc3447a Compare May 22, 2021 06:25
@rintaro
Copy link
Member Author

rintaro commented May 22, 2021

@swift-ci Please smoke test

1 similar comment
@rintaro
Copy link
Member Author

rintaro commented May 22, 2021

@swift-ci Please smoke test

@@ -434,6 +408,19 @@ enum class SemanticContextKind {
OtherModule,
};

enum class CodeCompletionFlairBit: uint8_t {
/// **Deprecated**. Old style "catch all" prioritization.
Copy link
Contributor

Choose a reason for hiding this comment

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

Nitpick: it's usually spelled "catch-all".

@@ -2180,9 +2191,6 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
return SemanticContextKind::Local;

case DeclVisibilityKind::MemberOfCurrentNominal:
if (IsSuperRefExpr &&
CurrentMethod && CurrentMethod->getOverriddenDecl() == D)
return SemanticContextKind::ExpressionSpecific;
Copy link
Contributor

Choose a reason for hiding this comment

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

Doesn't really impact this patch, but do you know why this check was on MemberOfCurrentNominal and not MemberOfSuper?

Copy link
Member Author

@rintaro rintaro Jun 4, 2021

Choose a reason for hiding this comment

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

This is for prioritizing the overridden method after super.

class Base {
  func foo() {} 
}
class Sub: Base {
  override func foo() { super.#^HERE^# /* prioritize `foo()` */ }
}

In this case, the base type for lookupVisibleMemberDecls is Base not Sub, so Base.foo() is found as MemberOfCurrentNominal of Base.

But I just noticed this mechanism doesn't work for:

class Base {
  func foo() {} 
}
class Sub: Base { }
class Sub2: Sub {
  override func foo() { super.#^HERE^# } 
}

😅

Copy link
Member Author

Choose a reason for hiding this comment

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

So my change unintentionally fixed this issue.
I added a few cases to test/IDE/complete_after_super.swift

@@ -2778,7 +2786,8 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
if (ParentKind != StmtKind::If && ParentKind != StmtKind::Guard)
return;
CodeCompletionResultBuilder Builder(Sink, CodeCompletionResult::ResultKind::Keyword,
SemanticContextKind::ExpressionSpecific, expectedTypeContext);
SemanticContextKind::Local, expectedTypeContext);
Copy link
Contributor

Choose a reason for hiding this comment

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

Why Local? I would have expected None.

Copy link
Member Author

@rintaro rintaro Jun 4, 2021

Choose a reason for hiding this comment

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

No strong reason for Local or CurrentModule. But I couldn't just use None because None is rather deprioritized in CodeCompletionOrganizer. Maybe we need a new "neutral" semantic context kind.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm fine with leaving it the way you wrote for the current PR, but I think we should leave a comment about it and we should probably figure out a better solution. Either change the scoring to be more specific so that None is okay for these cases, or else introduce new context(s).

@@ -4575,13 +4599,14 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
continue;
CodeCompletionResultBuilder Builder(
Sink, CodeCompletionResult::ResultKind::Pattern,
SemanticContextKind::ExpressionSpecific, {});
SemanticContextKind::Local, {});
Copy link
Contributor

Choose a reason for hiding this comment

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

What's your reasoning for Local here? I wonder if we need a new semantic context for this, or maybe None.

@@ -6185,12 +6210,14 @@ static void addPlatformConditions(CodeCompletionResultSink &Sink) {
consumer) {
CodeCompletionResultBuilder Builder(
Sink, CodeCompletionResult::ResultKind::Pattern,
SemanticContextKind::ExpressionSpecific, {});
SemanticContextKind::CurrentModule, {});
Copy link
Contributor

Choose a reason for hiding this comment

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

Similar question here about why this is CurrentModule.

@@ -6231,7 +6258,8 @@ static void addConditionalCompilationFlags(ASTContext &Ctx,
// TODO: Should we filter out some flags?
CodeCompletionResultBuilder Builder(
Sink, CodeCompletionResult::ResultKind::Keyword,
SemanticContextKind::ExpressionSpecific, {});
SemanticContextKind::CurrentModule, {});
Copy link
Contributor

Choose a reason for hiding this comment

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

Similar question here about why this is CurrentModule.

@@ -1002,11 +1007,11 @@ static void transformAndForwardResults(
auto topResults = filterInnerResults(results, options.addInnerResults,
options.addInnerOperators, hasDot,
hasQDot, hasInit, rules);
// FIXME: Overriding the default to context "None" is a hack so that they
// won't overwhelm other results that also match the filter text.
// FIXME: Clearling the flair (and semantic context) is a hack so that
Copy link
Contributor

Choose a reason for hiding this comment

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

Typo: clearling -> clearing


if (Result->getFlair().contains(CodeCompletionFlairBit::ExpressionSpecific) ||
Result->getFlair().contains(CodeCompletionFlairBit::SuperChain)) {
Info.SemanticContext = CCCtxExpressionSpecific;
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe worth a comment that this is to maintain compatibility?

rintaro added 2 commits June 4, 2021 11:45
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.
@rintaro rintaro force-pushed the ide-completion-flair branch from bc3447a to 9f5756d Compare June 4, 2021 18:46
@rintaro
Copy link
Member Author

rintaro commented Jun 8, 2021

This is merged with #37725

@rintaro rintaro closed this 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