Skip to content

Commit

Permalink
Order APIs case-insensitive with case-sensitive fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
sharwell committed Oct 26, 2020
1 parent 9f68083 commit c19a6ac
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/PublicApiAnalyzers/Core/CodeFixes/DeclarePublicApiFix.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ static void insertInList(List<string> list, string name)
{
for (int i = 0; i < list.Count; i++)
{
if (string.Compare(name, list[i], StringComparison.Ordinal) < 0)
if (IgnoreCaseWhenPossibleComparer.Instance.Compare(name, list[i]) < 0)
{
list.Insert(i, name);
return;
Expand Down Expand Up @@ -231,7 +231,7 @@ protected override async Task<Solution> GetChangedSolutionAsync(CancellationToke
.Where(d => d.Location.IsInSource)
.GroupBy(d => d.Location.SourceTree);

var newSymbolNames = new SortedSet<string>();
var newSymbolNames = new SortedSet<string>(IgnoreCaseWhenPossibleComparer.Instance);
var symbolNamesToRemoveBuilder = PooledHashSet<string>.GetInstance();

foreach (IGrouping<SyntaxTree, Diagnostic> grouping in groupedDiagnostics)
Expand Down Expand Up @@ -358,5 +358,23 @@ private class PublicSurfaceAreaFixAllProvider : FixAllProvider
return new FixAllAdditionalDocumentChangeAction(title, fixAllContext.Solution, diagnosticsToFix);
}
}

private sealed class IgnoreCaseWhenPossibleComparer : IComparer<string>
{
public static readonly IgnoreCaseWhenPossibleComparer Instance = new();

private IgnoreCaseWhenPossibleComparer()
{
}

public int Compare(string x, string y)
{
var result = StringComparer.OrdinalIgnoreCase.Compare(x, y);
if (result == 0)
result = StringComparer.Ordinal.Compare(x, y);

return result;
}
}
}
}
29 changes: 29 additions & 0 deletions src/PublicApiAnalyzers/UnitTests/DeclarePublicApiAnalyzerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1757,6 +1757,35 @@ public class {|RS0016:{|RS0016:C2|}|} { }
await VerifyCSharpAdditionalFileFixAsync(source, shippedText, unshippedText, fixedUnshippedText);
}

[Fact]
public async Task TestMultipleMissingTypeAndMember_CaseSensitiveFix()
{
var source = @"
public class {|RS0016:C|}
{
private C() { }
public int {|RS0016:Field_A|};
public int {|RS0016:Field_b|};
public int {|RS0016:Field_C|};
public int {|RS0016:Field_d|};
}
public class {|RS0016:{|RS0016:C2|}|} { }
";

var shippedText = @"";
var unshippedText = @"";
var fixedUnshippedText = @"C
C.Field_A -> int
C.Field_b -> int
C.Field_C -> int
C.Field_d -> int
C2
C2.C2() -> void";

await VerifyCSharpAdditionalFileFixAsync(source, shippedText, unshippedText, fixedUnshippedText);
}

[Fact]
public async Task TestChangingMethodSignatureForAnUnshippedMethod_Fix()
{
Expand Down

0 comments on commit c19a6ac

Please sign in to comment.