Skip to content
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

Sort unimported items from System.* ahead of other unimported items #67083

Merged
merged 1 commit into from
Mar 1, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -7904,6 +7904,78 @@ namespace NS
End Using
End Function

<WorkItem(67081, "https://github.com/dotnet/roslyn/issues/67081")>
<WpfTheory>
<InlineData("System", True)>
<InlineData("System.Collections", True)>
<InlineData("SystemNamespace", False)>
<InlineData("MyNamespace1", True)>
<InlineData("MyNamespace3", False)>
Public Async Function SortUnimportedItemFromSystemNamespacesFirst(containingNamespace As String, sortedAhead As Boolean) As Task
Using state = TestStateFactory.CreateCSharpTestState(
<Document>
namespace NS1
{
class C
{
void M()
{
$$
}
}
}

namespace MyNamespace2
{
public class UnimportedType { }
}

namespace <%= containingNamespace %>
{
public class UnimportedType { }
}
</Document>)

state.Workspace.GlobalOptions.SetGlobalOption(CompletionOptionsStorage.ForceExpandedCompletionIndexCreation, True)
state.Workspace.GlobalOptions.SetGlobalOption(CompletionOptionsStorage.ShowItemsFromUnimportedNamespaces, LanguageNames.CSharp, True)

Await state.SendInvokeCompletionListAndWaitForUiRenderAsync()

' make sure expander is selected
state.AssertCompletionItemExpander(isAvailable:=True, isSelected:=True)

state.SendEscape()
Await state.AssertNoCompletionSession()

state.SendTypeChars("unimportedtype")
Await state.WaitForAsynchronousOperationsAsync()

' make sure expander is selected
state.AssertCompletionItemExpander(isAvailable:=True, isSelected:=True)

Dim expectedOrder As (String, String)()

If sortedAhead Then
Await state.AssertSelectedCompletionItem(displayText:="UnimportedType", inlineDescription:=containingNamespace)
expectedOrder =
{
("UnimportedType", containingNamespace),
("UnimportedType", "MyNamespace2")
}
Else
Await state.AssertSelectedCompletionItem(displayText:="UnimportedType", inlineDescription:="MyNamespace2")
expectedOrder =
{
("UnimportedType", "MyNamespace2"),
("UnimportedType", containingNamespace)
}
End If

state.AssertItemsInOrder(expectedOrder)

End Using
End Function

<WorkItem(41601, "https://github.com/dotnet/roslyn/issues/41601")>
<WpfTheory, CombinatorialData>
Public Async Function SortItemsByExpandedFlag(showCompletionInArgumentLists As Boolean) As Task
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ namespace Microsoft.CodeAnalysis.Completion.Providers
{
internal static class ImportCompletionItem
{
private const string SortTextFormat = "{0} {1}";
// Note the additional space as prefix to the System namespace,
// to make sure items from System.* get sorted ahead.
private const string OtherNamespaceSortTextFormat = "{0} {1}";
private const string SystemNamespaceSortTextFormat = "{0} {1}";
Copy link
Member

Choose a reason for hiding this comment

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

lol


private const string TypeAritySuffixName = nameof(TypeAritySuffixName);
private const string AttributeFullName = nameof(AttributeFullName);
Expand Down Expand Up @@ -63,7 +66,7 @@ public static CompletionItem Create(
// but from different namespace all show up in the list, it also makes sure item with shorter name shows first,
// e.g. 'SomeType` before 'SomeTypeWithLongerName'.
var sortTextBuilder = PooledStringBuilder.GetInstance();
sortTextBuilder.Builder.AppendFormat(SortTextFormat, name, containingNamespace);
sortTextBuilder.Builder.AppendFormat(GetSortTextFormatString(containingNamespace), name, containingNamespace);

var item = CompletionItem.Create(
displayText: name,
Expand Down Expand Up @@ -93,7 +96,7 @@ public static CompletionItem CreateAttributeItemWithoutSuffix(CompletionItem att
var newProperties = attributeItem.Properties.Add(AttributeFullName, attributeItem.DisplayText);

var sortTextBuilder = PooledStringBuilder.GetInstance();
sortTextBuilder.Builder.AppendFormat(SortTextFormat, attributeNameWithoutSuffix, attributeItem.InlineDescription);
sortTextBuilder.Builder.AppendFormat(GetSortTextFormatString(attributeItem.InlineDescription), attributeNameWithoutSuffix, attributeItem.InlineDescription);

var item = CompletionItem.Create(
displayText: attributeNameWithoutSuffix,
Expand All @@ -110,6 +113,14 @@ public static CompletionItem CreateAttributeItemWithoutSuffix(CompletionItem att
return item;
}

private static string GetSortTextFormatString(string containingNamespace)
{
if (containingNamespace == "System" || containingNamespace.StartsWith("System."))
return SystemNamespaceSortTextFormat;

return OtherNamespaceSortTextFormat;
}

public static CompletionItem CreateItemWithGenericDisplaySuffix(CompletionItem item, string genericTypeSuffix)
=> item.WithDisplayTextSuffix(genericTypeSuffix);

Expand Down