[CodeCompletion] Prioritize call argument pattern

struct Foo {
    init(_ arg1: String, arg2: Int) {}
    init(label: Int) {}
  }
  func test(strVal: String) {
    _ = Foo(<HERE>)
  }

In this case, 'strVal' was prioritized because it can use as an argument
for 'init(_:arg2:)'. However, argument labels are almost always
preferable, and if the user actually want 'strVal', they can input a few
characters to get it at the top. So we should always prioritize call
argument patterns.

rdar://77188260
This commit is contained in:
Rintaro Ishizaki
2021-04-29 13:23:15 -07:00
parent fe9a290823
commit 3c2433c942
7 changed files with 65 additions and 19 deletions

View File

@@ -158,7 +158,7 @@ bool SourceKit::CodeCompletion::addCustomCompletions(
CodeCompletion::SwiftResult swiftResult(
CodeCompletion::SwiftResult::ResultKind::Pattern,
SemanticContextKind::ExpressionSpecific,
/*NumBytesToErase=*/0, completionString,
/*IsArgumentLabels=*/false, /*NumBytesToErase=*/0, completionString,
CodeCompletionResult::ExpectedTypeRelation::Unknown);
CompletionBuilder builder(sink, swiftResult);
@@ -666,6 +666,9 @@ static ResultBucket getResultBucket(Item &item, bool hasRequiredTypes,
!skipMetaGroups)
return ResultBucket::ExpressionSpecific;
if (completion->isArgumentLabels() && !skipMetaGroups)
return ResultBucket::ExpressionSpecific;
if (completion->isOperator())
return ResultBucket::Operator;
@@ -1167,13 +1170,15 @@ Completion *CompletionBuilder::finish() {
if (current.getKind() == SwiftResult::Declaration) {
base = SwiftResult(
semanticContext, current.getNumBytesToErase(), completionString,
semanticContext, current.isArgumentLabels(),
current.getNumBytesToErase(), completionString,
current.getAssociatedDeclKind(), current.isSystem(),
current.getModuleName(), current.getNotRecommendedReason(),
current.getBriefDocComment(), current.getAssociatedUSRs(),
current.getDeclKeywords(), typeRelation, opKind);
} else {
base = SwiftResult(current.getKind(), semanticContext,
current.isArgumentLabels(),
current.getNumBytesToErase(), completionString,
typeRelation, opKind);
}