Skip to content

Commit 5c8daca

Browse files
authored
Merge pull request #37878 from rintaro/5.5-ide-completion-flair
[5.5][CodeCompletion] Fine grained prioritization
2 parents 2193597 + 5869b40 commit 5c8daca

File tree

62 files changed

+1463
-681
lines changed

Some content is hidden

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

62 files changed

+1463
-681
lines changed

include/swift/IDE/CodeCompletion.h

Lines changed: 63 additions & 52 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"
@@ -32,12 +33,14 @@ class Decl;
3233
class DeclContext;
3334
class FrontendOptions;
3435
class ModuleDecl;
36+
class SourceFile;
3537

3638
namespace ide {
3739

3840
class CodeCompletionCache;
3941
class CodeCompletionContext;
4042
class CodeCompletionResultBuilder;
43+
struct CodeCompletionResultSink;
4144
struct RequestedCachedModule;
4245

4346
/// A routine to remove code completion tokens from code completion
@@ -375,33 +378,6 @@ enum class SemanticContextKind {
375378
/// Used in cases when the concept of semantic context is not applicable.
376379
None,
377380

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-
405381
/// A declaration from the same function.
406382
Local,
407383

@@ -434,6 +410,33 @@ enum class SemanticContextKind {
434410
OtherModule,
435411
};
436412

413+
enum class CodeCompletionFlairBit: uint8_t {
414+
/// **Deprecated**. Old style catch-all prioritization.
415+
ExpressionSpecific = 1 << 0,
416+
417+
/// E.g. override func foo() { super.foo() ...
418+
SuperChain = 1 << 1,
419+
420+
/// Argument label and type. i.e. 'label: <#Ty#>'.
421+
ArgumentLabels = 1 << 2,
422+
423+
/// E.g. decl introducer or modifiers ('enum', 'protocol', 'public', etc.) at
424+
/// top-level.
425+
CommonKeywordAtCurrentPosition = 1 << 3,
426+
427+
/// E.g. type decl introducer ('enum', 'class', etc.) in a function body.
428+
RareKeywordAtCurrentPosition = 1 << 4,
429+
430+
/// E.g. protocol names at an expression position.
431+
RareTypeAtCurrentPosition = 1 << 5,
432+
433+
/// E.g. referencing a type, function, etc… at top level position in a non
434+
/// script/main.swift file
435+
ExpressionAtNonScriptOrMainFileScope = 1 << 6,
436+
};
437+
438+
using CodeCompletionFlair = OptionSet<CodeCompletionFlairBit>;
439+
437440
/// The declaration kind of a code completion result, if it is a declaration.
438441
enum class CodeCompletionDeclKind {
439442
Module,
@@ -615,7 +618,7 @@ class CodeCompletionResult {
615618
unsigned AssociatedKind : 8;
616619
unsigned KnownOperatorKind : 6;
617620
unsigned SemanticContext : 3;
618-
unsigned IsArgumentLabels : 1;
621+
unsigned Flair: 8;
619622
unsigned NotRecommended : 4;
620623
unsigned IsSystem : 1;
621624

@@ -640,15 +643,14 @@ class CodeCompletionResult {
640643
///
641644
/// \note The caller must ensure \c CodeCompletionString outlives this result.
642645
CodeCompletionResult(ResultKind Kind, SemanticContextKind SemanticContext,
643-
bool IsArgumentLabels, unsigned NumBytesToErase,
646+
CodeCompletionFlair Flair, unsigned NumBytesToErase,
644647
CodeCompletionString *CompletionString,
645648
ExpectedTypeRelation TypeDistance,
646649
CodeCompletionOperatorKind KnownOperatorKind =
647650
CodeCompletionOperatorKind::None,
648651
StringRef BriefDocComment = StringRef())
649652
: Kind(Kind), KnownOperatorKind(unsigned(KnownOperatorKind)),
650-
SemanticContext(unsigned(SemanticContext)),
651-
IsArgumentLabels(unsigned(IsArgumentLabels)),
653+
SemanticContext(unsigned(SemanticContext)), Flair(unsigned(Flair.toRaw())),
652654
NotRecommended(unsigned(NotRecommendedReason::None)),
653655
NumBytesToErase(NumBytesToErase), CompletionString(CompletionString),
654656
BriefDocComment(BriefDocComment), TypeDistance(TypeDistance) {
@@ -668,13 +670,13 @@ class CodeCompletionResult {
668670
/// \note The caller must ensure \c CodeCompletionString outlives this result.
669671
CodeCompletionResult(CodeCompletionKeywordKind Kind,
670672
SemanticContextKind SemanticContext,
671-
bool IsArgumentLabels, unsigned NumBytesToErase,
673+
CodeCompletionFlair Flair,
674+
unsigned NumBytesToErase,
672675
CodeCompletionString *CompletionString,
673676
ExpectedTypeRelation TypeDistance,
674677
StringRef BriefDocComment = StringRef())
675678
: Kind(Keyword), KnownOperatorKind(0),
676-
SemanticContext(unsigned(SemanticContext)),
677-
IsArgumentLabels(unsigned(IsArgumentLabels)),
679+
SemanticContext(unsigned(SemanticContext)), Flair(unsigned(Flair.toRaw())),
678680
NotRecommended(unsigned(NotRecommendedReason::None)),
679681
NumBytesToErase(NumBytesToErase), CompletionString(CompletionString),
680682
BriefDocComment(BriefDocComment), TypeDistance(TypeDistance) {
@@ -688,12 +690,11 @@ class CodeCompletionResult {
688690
/// \note The caller must ensure \c CodeCompletionString outlives this result.
689691
CodeCompletionResult(CodeCompletionLiteralKind LiteralKind,
690692
SemanticContextKind SemanticContext,
691-
bool IsArgumentLabels, unsigned NumBytesToErase,
693+
CodeCompletionFlair Flair, unsigned NumBytesToErase,
692694
CodeCompletionString *CompletionString,
693695
ExpectedTypeRelation TypeDistance)
694696
: Kind(Literal), KnownOperatorKind(0),
695-
SemanticContext(unsigned(SemanticContext)),
696-
IsArgumentLabels(unsigned(IsArgumentLabels)),
697+
SemanticContext(unsigned(SemanticContext)), Flair(unsigned(Flair.toRaw())),
697698
NotRecommended(unsigned(NotRecommendedReason::None)),
698699
NumBytesToErase(NumBytesToErase), CompletionString(CompletionString),
699700
TypeDistance(TypeDistance) {
@@ -708,7 +709,7 @@ class CodeCompletionResult {
708709
/// arguments outlive this result, typically by storing them in the same
709710
/// \c CodeCompletionResultSink as the result itself.
710711
CodeCompletionResult(SemanticContextKind SemanticContext,
711-
bool IsArgumentLabels, unsigned NumBytesToErase,
712+
CodeCompletionFlair Flair, unsigned NumBytesToErase,
712713
CodeCompletionString *CompletionString,
713714
const Decl *AssociatedDecl, StringRef ModuleName,
714715
CodeCompletionResult::NotRecommendedReason NotRecReason,
@@ -717,8 +718,7 @@ class CodeCompletionResult {
717718
ArrayRef<std::pair<StringRef, StringRef>> DocWords,
718719
enum ExpectedTypeRelation TypeDistance)
719720
: Kind(ResultKind::Declaration), KnownOperatorKind(0),
720-
SemanticContext(unsigned(SemanticContext)),
721-
IsArgumentLabels(unsigned(IsArgumentLabels)),
721+
SemanticContext(unsigned(SemanticContext)), Flair(unsigned(Flair.toRaw())),
722722
NotRecommended(unsigned(NotRecReason)),
723723
NumBytesToErase(NumBytesToErase), CompletionString(CompletionString),
724724
ModuleName(ModuleName), BriefDocComment(BriefDocComment),
@@ -737,7 +737,7 @@ class CodeCompletionResult {
737737

738738
// Used by deserialization.
739739
CodeCompletionResult(SemanticContextKind SemanticContext,
740-
bool IsArgumentLabels, unsigned NumBytesToErase,
740+
CodeCompletionFlair Flair, unsigned NumBytesToErase,
741741
CodeCompletionString *CompletionString,
742742
CodeCompletionDeclKind DeclKind, bool IsSystem,
743743
StringRef ModuleName,
@@ -749,8 +749,7 @@ class CodeCompletionResult {
749749
CodeCompletionOperatorKind KnownOperatorKind)
750750
: Kind(ResultKind::Declaration),
751751
KnownOperatorKind(unsigned(KnownOperatorKind)),
752-
SemanticContext(unsigned(SemanticContext)),
753-
IsArgumentLabels(unsigned(IsArgumentLabels)),
752+
SemanticContext(unsigned(SemanticContext)), Flair(unsigned(Flair.toRaw())),
754753
NotRecommended(unsigned(NotRecReason)), IsSystem(IsSystem),
755754
NumBytesToErase(NumBytesToErase), CompletionString(CompletionString),
756755
ModuleName(ModuleName), BriefDocComment(BriefDocComment),
@@ -762,6 +761,12 @@ class CodeCompletionResult {
762761
getOperatorKind() != CodeCompletionOperatorKind::None);
763762
}
764763

764+
/// Copy this result to \p Sink with \p newFlair . Note that this does NOT
765+
/// copy the value of \c CompletionString , \c AssociatedUSRs etc. it only
766+
/// copies the pointers to them.
767+
CodeCompletionResult *withFlair(CodeCompletionFlair newFlair,
768+
CodeCompletionResultSink &Sink);
769+
765770
ResultKind getKind() const { return static_cast<ResultKind>(Kind); }
766771

767772
CodeCompletionDeclKind getAssociatedDeclKind() const {
@@ -813,8 +818,13 @@ class CodeCompletionResult {
813818
return static_cast<SemanticContextKind>(SemanticContext);
814819
}
815820

816-
bool isArgumentLabels() const {
817-
return static_cast<bool>(IsArgumentLabels);
821+
CodeCompletionFlair getFlair() const {
822+
return static_cast<CodeCompletionFlair>(Flair);
823+
}
824+
825+
/// Modify "flair" of this result *in place*.
826+
void setFlair(CodeCompletionFlair flair) {
827+
Flair = unsigned(flair.toRaw());
818828
}
819829

820830
bool isNotRecommended() const {
@@ -971,7 +981,7 @@ class CodeCompletionConsumer {
971981
virtual void
972982
handleResultsAndModules(CodeCompletionContext &context,
973983
ArrayRef<RequestedCachedModule> requestedModules,
974-
DeclContext *DCForModules) = 0;
984+
DeclContext *DC) = 0;
975985
};
976986

977987
/// A simplified code completion consumer interface that clients can use to get
@@ -1024,14 +1034,15 @@ void lookupCodeCompletionResultsFromModule(CodeCompletionResultSink &targetSink,
10241034
const ModuleDecl *module,
10251035
ArrayRef<std::string> accessPath,
10261036
bool needLeadingDot,
1027-
const DeclContext *currDeclContext);
1037+
const SourceFile *SF);
10281038

10291039
/// Copy code completion results from \p sourceSink to \p targetSink, possibly
1030-
/// restricting by \p onlyTypes.
1031-
void copyCodeCompletionResults(CodeCompletionResultSink &targetSink,
1032-
CodeCompletionResultSink &sourceSink,
1033-
bool onlyTypes,
1034-
bool onlyPrecedenceGroups);
1040+
/// restricting by \p onlyTypes. Returns copied results in \p targetSink.
1041+
MutableArrayRef<CodeCompletionResult *>
1042+
copyCodeCompletionResults(CodeCompletionResultSink &targetSink,
1043+
CodeCompletionResultSink &sourceSink,
1044+
bool onlyTypes,
1045+
bool onlyPrecedenceGroups);
10351046

10361047
} // end namespace ide
10371048
} // end namespace swift

0 commit comments

Comments
 (0)