Skip to content

Commit 3602f1a

Browse files
authored
Merge pull request #37725 from rintaro/ide-completion-prioritize-kw-rdar77934651
[CodeCompletion] Add decl context dependent 'Flair' to decl keywords
2 parents 85891fd + a2b5968 commit 3602f1a

File tree

58 files changed

+1107
-625
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+1107
-625
lines changed

include/swift/IDE/CodeCompletion.h

Lines changed: 35 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "swift/AST/Identifier.h"
1717
#include "swift/Basic/Debug.h"
1818
#include "swift/Basic/LLVM.h"
19+
#include "swift/Basic/OptionSet.h"
1920
#include "llvm/ADT/ArrayRef.h"
2021
#include "llvm/ADT/StringMap.h"
2122
#include "llvm/ADT/StringRef.h"
@@ -375,33 +376,6 @@ enum class SemanticContextKind {
375376
/// Used in cases when the concept of semantic context is not applicable.
376377
None,
377378

378-
/// This is a highly-likely expression-context-specific completion
379-
/// result. This description is intentionally vague: this is a catch-all
380-
/// category for all heuristics for highly-likely results.
381-
///
382-
/// For example, the name of an overridden superclass member inside a nominal
383-
/// member function has ExpressionSpecific context:
384-
/// \code
385-
/// class Base {
386-
/// init() {}
387-
/// init(a: Int) {}
388-
/// func foo() {}
389-
/// func bar() {}
390-
/// }
391-
/// class Derived {
392-
/// init() {
393-
/// super. // init() -- ExpressionSpecific
394-
/// // init(a: Int) -- Super
395-
/// }
396-
///
397-
/// func foo() {
398-
/// super. // foo() -- ExpressionSpecific
399-
/// // bar() -- Super
400-
/// }
401-
/// }
402-
/// \endcode
403-
ExpressionSpecific,
404-
405379
/// A declaration from the same function.
406380
Local,
407381

@@ -434,6 +408,26 @@ enum class SemanticContextKind {
434408
OtherModule,
435409
};
436410

411+
enum class CodeCompletionFlairBit: uint8_t {
412+
/// **Deprecated**. Old style catch-all prioritization.
413+
ExpressionSpecific = 1 << 0,
414+
415+
/// E.g. override func foo() { super.foo() ...
416+
SuperChain = 1 << 1,
417+
418+
/// Argument label and type. i.e. 'label: <#Ty#>'.
419+
ArgumentLabels = 1 << 2,
420+
421+
/// E.g. decl introducer or modifiers ('enum', 'protocol', 'public', etc.) at
422+
/// top-level.
423+
CommonKeywordAtCurrentPosition = 1 << 3,
424+
425+
/// E.g. type decl introducer ('enum', 'class', etc.) in a function body.
426+
RareKeywordAtCurrentPosition = 1 << 4,
427+
};
428+
429+
using CodeCompletionFlair = OptionSet<CodeCompletionFlairBit>;
430+
437431
/// The declaration kind of a code completion result, if it is a declaration.
438432
enum class CodeCompletionDeclKind {
439433
Module,
@@ -615,7 +609,7 @@ class CodeCompletionResult {
615609
unsigned AssociatedKind : 8;
616610
unsigned KnownOperatorKind : 6;
617611
unsigned SemanticContext : 3;
618-
unsigned IsArgumentLabels : 1;
612+
unsigned Flair: 8;
619613
unsigned NotRecommended : 4;
620614
unsigned IsSystem : 1;
621615

@@ -640,15 +634,14 @@ class CodeCompletionResult {
640634
///
641635
/// \note The caller must ensure \c CodeCompletionString outlives this result.
642636
CodeCompletionResult(ResultKind Kind, SemanticContextKind SemanticContext,
643-
bool IsArgumentLabels, unsigned NumBytesToErase,
637+
CodeCompletionFlair Flair, unsigned NumBytesToErase,
644638
CodeCompletionString *CompletionString,
645639
ExpectedTypeRelation TypeDistance,
646640
CodeCompletionOperatorKind KnownOperatorKind =
647641
CodeCompletionOperatorKind::None,
648642
StringRef BriefDocComment = StringRef())
649643
: Kind(Kind), KnownOperatorKind(unsigned(KnownOperatorKind)),
650-
SemanticContext(unsigned(SemanticContext)),
651-
IsArgumentLabels(unsigned(IsArgumentLabels)),
644+
SemanticContext(unsigned(SemanticContext)), Flair(unsigned(Flair.toRaw())),
652645
NotRecommended(unsigned(NotRecommendedReason::None)),
653646
NumBytesToErase(NumBytesToErase), CompletionString(CompletionString),
654647
BriefDocComment(BriefDocComment), TypeDistance(TypeDistance) {
@@ -668,13 +661,13 @@ class CodeCompletionResult {
668661
/// \note The caller must ensure \c CodeCompletionString outlives this result.
669662
CodeCompletionResult(CodeCompletionKeywordKind Kind,
670663
SemanticContextKind SemanticContext,
671-
bool IsArgumentLabels, unsigned NumBytesToErase,
664+
CodeCompletionFlair Flair,
665+
unsigned NumBytesToErase,
672666
CodeCompletionString *CompletionString,
673667
ExpectedTypeRelation TypeDistance,
674668
StringRef BriefDocComment = StringRef())
675669
: Kind(Keyword), KnownOperatorKind(0),
676-
SemanticContext(unsigned(SemanticContext)),
677-
IsArgumentLabels(unsigned(IsArgumentLabels)),
670+
SemanticContext(unsigned(SemanticContext)), Flair(unsigned(Flair.toRaw())),
678671
NotRecommended(unsigned(NotRecommendedReason::None)),
679672
NumBytesToErase(NumBytesToErase), CompletionString(CompletionString),
680673
BriefDocComment(BriefDocComment), TypeDistance(TypeDistance) {
@@ -688,12 +681,11 @@ class CodeCompletionResult {
688681
/// \note The caller must ensure \c CodeCompletionString outlives this result.
689682
CodeCompletionResult(CodeCompletionLiteralKind LiteralKind,
690683
SemanticContextKind SemanticContext,
691-
bool IsArgumentLabels, unsigned NumBytesToErase,
684+
CodeCompletionFlair Flair, unsigned NumBytesToErase,
692685
CodeCompletionString *CompletionString,
693686
ExpectedTypeRelation TypeDistance)
694687
: Kind(Literal), KnownOperatorKind(0),
695-
SemanticContext(unsigned(SemanticContext)),
696-
IsArgumentLabels(unsigned(IsArgumentLabels)),
688+
SemanticContext(unsigned(SemanticContext)), Flair(unsigned(Flair.toRaw())),
697689
NotRecommended(unsigned(NotRecommendedReason::None)),
698690
NumBytesToErase(NumBytesToErase), CompletionString(CompletionString),
699691
TypeDistance(TypeDistance) {
@@ -708,7 +700,7 @@ class CodeCompletionResult {
708700
/// arguments outlive this result, typically by storing them in the same
709701
/// \c CodeCompletionResultSink as the result itself.
710702
CodeCompletionResult(SemanticContextKind SemanticContext,
711-
bool IsArgumentLabels, unsigned NumBytesToErase,
703+
CodeCompletionFlair Flair, unsigned NumBytesToErase,
712704
CodeCompletionString *CompletionString,
713705
const Decl *AssociatedDecl, StringRef ModuleName,
714706
CodeCompletionResult::NotRecommendedReason NotRecReason,
@@ -717,8 +709,7 @@ class CodeCompletionResult {
717709
ArrayRef<std::pair<StringRef, StringRef>> DocWords,
718710
enum ExpectedTypeRelation TypeDistance)
719711
: Kind(ResultKind::Declaration), KnownOperatorKind(0),
720-
SemanticContext(unsigned(SemanticContext)),
721-
IsArgumentLabels(unsigned(IsArgumentLabels)),
712+
SemanticContext(unsigned(SemanticContext)), Flair(unsigned(Flair.toRaw())),
722713
NotRecommended(unsigned(NotRecReason)),
723714
NumBytesToErase(NumBytesToErase), CompletionString(CompletionString),
724715
ModuleName(ModuleName), BriefDocComment(BriefDocComment),
@@ -737,7 +728,7 @@ class CodeCompletionResult {
737728

738729
// Used by deserialization.
739730
CodeCompletionResult(SemanticContextKind SemanticContext,
740-
bool IsArgumentLabels, unsigned NumBytesToErase,
731+
CodeCompletionFlair Flair, unsigned NumBytesToErase,
741732
CodeCompletionString *CompletionString,
742733
CodeCompletionDeclKind DeclKind, bool IsSystem,
743734
StringRef ModuleName,
@@ -749,8 +740,7 @@ class CodeCompletionResult {
749740
CodeCompletionOperatorKind KnownOperatorKind)
750741
: Kind(ResultKind::Declaration),
751742
KnownOperatorKind(unsigned(KnownOperatorKind)),
752-
SemanticContext(unsigned(SemanticContext)),
753-
IsArgumentLabels(unsigned(IsArgumentLabels)),
743+
SemanticContext(unsigned(SemanticContext)), Flair(unsigned(Flair.toRaw())),
754744
NotRecommended(unsigned(NotRecReason)), IsSystem(IsSystem),
755745
NumBytesToErase(NumBytesToErase), CompletionString(CompletionString),
756746
ModuleName(ModuleName), BriefDocComment(BriefDocComment),
@@ -813,8 +803,8 @@ class CodeCompletionResult {
813803
return static_cast<SemanticContextKind>(SemanticContext);
814804
}
815805

816-
bool isArgumentLabels() const {
817-
return static_cast<bool>(IsArgumentLabels);
806+
CodeCompletionFlair getFlair() const {
807+
return static_cast<CodeCompletionFlair>(Flair);
818808
}
819809

820810
bool isNotRecommended() const {

0 commit comments

Comments
 (0)