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
Closed
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
73 changes: 28 additions & 45 deletions include/swift/IDE/CodeCompletion.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "swift/AST/Identifier.h"
#include "swift/Basic/Debug.h"
#include "swift/Basic/LLVM.h"
#include "swift/Basic/OptionSet.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/ADT/StringRef.h"
Expand Down Expand Up @@ -375,33 +376,6 @@ enum class SemanticContextKind {
/// Used in cases when the concept of semantic context is not applicable.
None,

/// This is a highly-likely expression-context-specific completion
/// result. This description is intentionally vague: this is a catch-all
/// category for all heuristics for highly-likely results.
///
/// For example, the name of an overridden superclass member inside a nominal
/// member function has ExpressionSpecific context:
/// \code
/// class Base {
/// init() {}
/// init(a: Int) {}
/// func foo() {}
/// func bar() {}
/// }
/// class Derived {
/// init() {
/// super. // init() -- ExpressionSpecific
/// // init(a: Int) -- Super
/// }
///
/// func foo() {
/// super. // foo() -- ExpressionSpecific
/// // bar() -- Super
/// }
/// }
/// \endcode
ExpressionSpecific,

/// A declaration from the same function.
Local,

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

enum class CodeCompletionFlairBit: uint8_t {
/// **Deprecated**. Old style catch-all prioritization.
ExpressionSpecific = 1 << 0,

/// E.g. override func foo() { super.foo() ...
SuperChain = 1 << 1,

/// Argument label and type. i.e. 'label: <#Ty#>'.
ArgumentLabels = 1 << 2,
};

using CodeCompletionFlair = OptionSet<CodeCompletionFlairBit>;

/// The declaration kind of a code completion result, if it is a declaration.
enum class CodeCompletionDeclKind {
Module,
Expand Down Expand Up @@ -615,7 +602,7 @@ class CodeCompletionResult {
unsigned AssociatedKind : 8;
unsigned KnownOperatorKind : 6;
unsigned SemanticContext : 3;
unsigned IsArgumentLabels : 1;
unsigned Flair: 8;
unsigned NotRecommended : 4;
unsigned IsSystem : 1;

Expand All @@ -640,15 +627,14 @@ class CodeCompletionResult {
///
/// \note The caller must ensure \c CodeCompletionString outlives this result.
CodeCompletionResult(ResultKind Kind, SemanticContextKind SemanticContext,
bool IsArgumentLabels, unsigned NumBytesToErase,
CodeCompletionFlair Flair, unsigned NumBytesToErase,
CodeCompletionString *CompletionString,
ExpectedTypeRelation TypeDistance,
CodeCompletionOperatorKind KnownOperatorKind =
CodeCompletionOperatorKind::None,
StringRef BriefDocComment = StringRef())
: Kind(Kind), KnownOperatorKind(unsigned(KnownOperatorKind)),
SemanticContext(unsigned(SemanticContext)),
IsArgumentLabels(unsigned(IsArgumentLabels)),
SemanticContext(unsigned(SemanticContext)), Flair(unsigned(Flair.toRaw())),
NotRecommended(unsigned(NotRecommendedReason::None)),
NumBytesToErase(NumBytesToErase), CompletionString(CompletionString),
BriefDocComment(BriefDocComment), TypeDistance(TypeDistance) {
Expand All @@ -668,13 +654,13 @@ class CodeCompletionResult {
/// \note The caller must ensure \c CodeCompletionString outlives this result.
CodeCompletionResult(CodeCompletionKeywordKind Kind,
SemanticContextKind SemanticContext,
bool IsArgumentLabels, unsigned NumBytesToErase,
CodeCompletionFlair Flair,
unsigned NumBytesToErase,
CodeCompletionString *CompletionString,
ExpectedTypeRelation TypeDistance,
StringRef BriefDocComment = StringRef())
: Kind(Keyword), KnownOperatorKind(0),
SemanticContext(unsigned(SemanticContext)),
IsArgumentLabels(unsigned(IsArgumentLabels)),
SemanticContext(unsigned(SemanticContext)), Flair(unsigned(Flair.toRaw())),
NotRecommended(unsigned(NotRecommendedReason::None)),
NumBytesToErase(NumBytesToErase), CompletionString(CompletionString),
BriefDocComment(BriefDocComment), TypeDistance(TypeDistance) {
Expand All @@ -688,12 +674,11 @@ class CodeCompletionResult {
/// \note The caller must ensure \c CodeCompletionString outlives this result.
CodeCompletionResult(CodeCompletionLiteralKind LiteralKind,
SemanticContextKind SemanticContext,
bool IsArgumentLabels, unsigned NumBytesToErase,
CodeCompletionFlair Flair, unsigned NumBytesToErase,
CodeCompletionString *CompletionString,
ExpectedTypeRelation TypeDistance)
: Kind(Literal), KnownOperatorKind(0),
SemanticContext(unsigned(SemanticContext)),
IsArgumentLabels(unsigned(IsArgumentLabels)),
SemanticContext(unsigned(SemanticContext)), Flair(unsigned(Flair.toRaw())),
NotRecommended(unsigned(NotRecommendedReason::None)),
NumBytesToErase(NumBytesToErase), CompletionString(CompletionString),
TypeDistance(TypeDistance) {
Expand All @@ -708,7 +693,7 @@ class CodeCompletionResult {
/// arguments outlive this result, typically by storing them in the same
/// \c CodeCompletionResultSink as the result itself.
CodeCompletionResult(SemanticContextKind SemanticContext,
bool IsArgumentLabels, unsigned NumBytesToErase,
CodeCompletionFlair Flair, unsigned NumBytesToErase,
CodeCompletionString *CompletionString,
const Decl *AssociatedDecl, StringRef ModuleName,
CodeCompletionResult::NotRecommendedReason NotRecReason,
Expand All @@ -717,8 +702,7 @@ class CodeCompletionResult {
ArrayRef<std::pair<StringRef, StringRef>> DocWords,
enum ExpectedTypeRelation TypeDistance)
: Kind(ResultKind::Declaration), KnownOperatorKind(0),
SemanticContext(unsigned(SemanticContext)),
IsArgumentLabels(unsigned(IsArgumentLabels)),
SemanticContext(unsigned(SemanticContext)), Flair(unsigned(Flair.toRaw())),
NotRecommended(unsigned(NotRecReason)),
NumBytesToErase(NumBytesToErase), CompletionString(CompletionString),
ModuleName(ModuleName), BriefDocComment(BriefDocComment),
Expand All @@ -737,7 +721,7 @@ class CodeCompletionResult {

// Used by deserialization.
CodeCompletionResult(SemanticContextKind SemanticContext,
bool IsArgumentLabels, unsigned NumBytesToErase,
CodeCompletionFlair Flair, unsigned NumBytesToErase,
CodeCompletionString *CompletionString,
CodeCompletionDeclKind DeclKind, bool IsSystem,
StringRef ModuleName,
Expand All @@ -749,8 +733,7 @@ class CodeCompletionResult {
CodeCompletionOperatorKind KnownOperatorKind)
: Kind(ResultKind::Declaration),
KnownOperatorKind(unsigned(KnownOperatorKind)),
SemanticContext(unsigned(SemanticContext)),
IsArgumentLabels(unsigned(IsArgumentLabels)),
SemanticContext(unsigned(SemanticContext)), Flair(unsigned(Flair.toRaw())),
NotRecommended(unsigned(NotRecReason)), IsSystem(IsSystem),
NumBytesToErase(NumBytesToErase), CompletionString(CompletionString),
ModuleName(ModuleName), BriefDocComment(BriefDocComment),
Expand Down Expand Up @@ -813,8 +796,8 @@ class CodeCompletionResult {
return static_cast<SemanticContextKind>(SemanticContext);
}

bool isArgumentLabels() const {
return static_cast<bool>(IsArgumentLabels);
CodeCompletionFlair getFlair() const {
return static_cast<CodeCompletionFlair>(Flair);
}

bool isNotRecommended() const {
Expand Down
Loading