Sort and remove (unused) using statements Roslyn script/code? I\'m looking for some .NET/Roslyn (compiler as service) code that can run through a project and sort and remov
I use this the following extension method to sort usings
internal static SyntaxList Sort(this SyntaxList usingDirectives, bool placeSystemNamespaceFirst = false) =>
SyntaxFactory.List(
usingDirectives
.OrderBy(x => x.StaticKeyword.IsKind(SyntaxKind.StaticKeyword) ? 1 : x.Alias == null ? 0 : 2)
.ThenBy(x => x.Alias?.ToString())
.ThenByDescending(x => placeSystemNamespaceFirst && x.Name.ToString().StartsWith(nameof(System) + "."))
.ThenBy(x => x.Name.ToString()));
and
compilationUnit = compilationUnit.WithUsings(SortUsings(compilationUnit.Usings))