Skip to content

[SourceKit] Add an option to sort completion result #30653

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

Merged
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
30 changes: 19 additions & 11 deletions test/SourceKit/CodeComplete/complete_sort_order.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,31 @@ func test() {

// XFAIL: broken_std_regex
// RUN: %sourcekitd-test -req=complete -req-opts=hidelowpriority=0 -pos=7:1 %s -- %s > %t.orig
// RUN: %FileCheck -check-prefix=NAME %s < %t.orig
// Make sure the order is as below, foo(Int) should come before foo(String).
// RUN: %sourcekitd-test -req=complete -req-opts=hidelowpriority=0,sort.byname=0 -pos=7:1 %s -- %s > %t.orig.off
// RUN: %FileCheck -check-prefix=NAME_SORTED %s < %t.orig
// RUN: %FileCheck -check-prefix=NAME_UNSORTED %s < %t.orig.off
Comment on lines +13 to +14
Copy link
Contributor

Choose a reason for hiding this comment

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

How about adding a diff check for %t.orig vs %t.orig.off to ensure we got different behavior?
I suspect the test will still pass even if both invocations end up sorting the results.

Copy link
Member Author

Choose a reason for hiding this comment

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

I suspect the test will still pass even if both invocations end up sorting the results.

Right.

I will add the diff test.

// RUN: not diff -u %t.orig %t.orig.off

// NAME: key.description: "#column"
// NAME: key.description: "foo(a: Int)"
// NAME-NOT: key.description
// NAME: key.description: "foo(a: String)"
// NAME-NOT: key.description
// NAME: key.description: "foo(b: Int)"
// NAME: key.description: "test()"
// NAME: key.description: "x"
// Make sure the order is as below, foo(Int) should come before foo(String).
// NAME_SORTED: key.description: "#column"
// NAME_SORTED: key.description: "foo(a: Int)"
// NAME_SORTED-NOT: key.description
// NAME_SORTED: key.description: "foo(a: String)"
// NAME_SORTED-NOT: key.description
// NAME_SORTED: key.description: "foo(b: Int)"
// NAME_SORTED: key.description: "test()"
// NAME_SORTED: key.description: "x"

// NAME_UNSORTED-DAG: key.description: "x"
// NAME_UNSORTED-DAG: key.description: "foo(a: String)"
// NAME_UNSORTED-DAG: key.description: "foo(a: Int)"
// NAME_UNSORTED-DAG: key.description: "foo(b: Int)"

// RUN: %sourcekitd-test -req=complete.open -pos=7:1 -req-opts=hidelowpriority=0,hideunderscores=0 %s -- %s > %t.default
// RUN: %sourcekitd-test -req=complete.open -pos=7:1 -req-opts=sort.byname=0,hidelowpriority=0,hideunderscores=0 %s -- %s > %t.on
// RUN: %sourcekitd-test -req=complete.open -pos=7:1 -req-opts=sort.byname=1,hidelowpriority=0,hideunderscores=0 %s -- %s > %t.off
// RUN: %FileCheck -check-prefix=CONTEXT %s < %t.default
// RUN: %FileCheck -check-prefix=NAME %s < %t.off
// RUN: %FileCheck -check-prefix=NAME_SORTED %s < %t.off
// FIXME: rdar://problem/20109989 non-deterministic sort order
// RUN-disabled: diff %t.on %t.default
// RUN: %FileCheck -check-prefix=CONTEXT %s < %t.on
Expand Down
3 changes: 2 additions & 1 deletion tools/SourceKit/lib/SwiftLang/SwiftCompletion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,8 @@ void SwiftLangSupport::codeComplete(
SKConsumer.setCompletionKind(kind);

bool hasRequiredType = info.completionContext->typeContextKind == TypeContextKind::Required;
CodeCompletionContext::sortCompletionResults(Results);
if (CCOpts.sortByName)
CodeCompletionContext::sortCompletionResults(Results);
// FIXME: this adhoc filtering should be configurable like it is in the
// codeCompleteOpen path.
for (auto *Result : Results) {
Expand Down
2 changes: 2 additions & 0 deletions tools/SourceKit/tools/sourcekitd-test/sourcekitd-test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,8 @@ static int handleTestInvocation(TestOptions Opts, TestOptions &InitOpts) {
sourcekitd_request_dictionary_set_uid(Req, KeyRequest, RequestCodeComplete);
sourcekitd_request_dictionary_set_int64(Req, KeyOffset, ByteOffset);
sourcekitd_request_dictionary_set_string(Req, KeyName, SemaName.c_str());
// Default to sort by name.
Opts.RequestOptions.insert(Opts.RequestOptions.begin(), "sort.byname=1");
addCodeCompleteOptions(Req, Opts);
break;

Expand Down