-
Notifications
You must be signed in to change notification settings - Fork 277
Usings sort #977
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
Open
BugAgain
wants to merge
11
commits into
code-cracker:master
Choose a base branch
from
BugAgain:usings_sort
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Usings sort #977
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
cee5050
Not the cat
BugAgain 49692f9
Project update
BugAgain aaf0f29
xUnit corrections
BugAgain f1b3291
SortUsings
BugAgain 824d6b7
first commit
9f39716
Last Commit
03fc266
Fix Bug: CC0061: Implementing interface using async keyword should no…
1c2d5b5
Fix typo
pascalberger 74eefeb
Sort usings
BugAgain b04db16
Merge branch 'usings_sort' of https://github.com/BugAgain/code-cracke…
BugAgain 7e2f62c
Merge branch 'master' into usings_sort
BugAgain File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| using Microsoft.CodeAnalysis; | ||
| using System.Collections.Immutable; | ||
| using Microsoft.CodeAnalysis.CSharp; | ||
| using Microsoft.CodeAnalysis.Diagnostics; | ||
| using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
|
|
||
| namespace CodeCracker.CSharp.Refactoring | ||
| { | ||
| [DiagnosticAnalyzer(LanguageNames.CSharp)] | ||
| public class SortUsingsAnalyzer : DiagnosticAnalyzer | ||
| { | ||
| internal const string Title = "Sort by length."; | ||
| internal const string MessageFormat = "Sort Using directives by length"; | ||
| internal const string Category = SupportedCategories.Refactoring; | ||
|
|
||
| internal static readonly DiagnosticDescriptor Rule = new DiagnosticDescriptor( | ||
| DiagnosticId.SortUsings.ToDiagnosticId(), | ||
| Title, | ||
| MessageFormat, | ||
| Category, | ||
| DiagnosticSeverity.Hidden, | ||
| isEnabledByDefault: true, | ||
| helpLinkUri: HelpLink.ForDiagnostic(DiagnosticId.SortUsings)); | ||
|
|
||
| public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(Rule); | ||
|
|
||
| public override void Initialize(AnalysisContext context) => context.RegisterSyntaxNodeAction(AnalyzeNode, SyntaxKind.CompilationUnit); | ||
|
|
||
| private static void AnalyzeNode(SyntaxNodeAnalysisContext context) | ||
| { | ||
| if (context.IsGenerated()) return; | ||
| var root = (CompilationUnitSyntax)context.Node; | ||
| if (root.Usings.Count > 0) | ||
| context.ReportDiagnostic(Diagnostic.Create(Rule, root.GetLocation())); | ||
| } | ||
| } | ||
| } |
60 changes: 60 additions & 0 deletions
60
src/CSharp/CodeCracker/Refactoring/SortUsingsCodeFixProvider.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| using System.Linq; | ||
| using System.Threading; | ||
| using System.Composition; | ||
| using Microsoft.CodeAnalysis; | ||
| using System.Threading.Tasks; | ||
| using System.Collections.Generic; | ||
| using System.Collections.Immutable; | ||
| using Microsoft.CodeAnalysis.CSharp; | ||
| using Microsoft.CodeAnalysis.CodeFixes; | ||
| using Microsoft.CodeAnalysis.Formatting; | ||
| using Microsoft.CodeAnalysis.CodeActions; | ||
| using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
|
|
||
| namespace CodeCracker.CSharp.Refactoring | ||
| { | ||
| [ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(SortUsingsCodeFixProvider)), Shared] | ||
| public class SortUsingsCodeFixProvider : CodeFixProvider | ||
| { | ||
| public override ImmutableArray<string> FixableDiagnosticIds => ImmutableArray.Create(DiagnosticId.SortUsings.ToDiagnosticId()); | ||
|
|
||
| public sealed override FixAllProvider GetFixAllProvider() => WellKnownFixAllProviders.BatchFixer; | ||
|
|
||
| public override Task RegisterCodeFixesAsync(CodeFixContext context) | ||
| { | ||
| var diagnostic = context.Diagnostics.First(); | ||
| context.RegisterCodeFix(CodeAction.Create("Sort by length", ct => SortUsingsAsync(context.Document, diagnostic, ct), nameof(SortUsingsCodeFixProvider)), diagnostic); | ||
| return Task.FromResult(0); | ||
| } | ||
|
|
||
| private static async Task<Document> SortUsingsAsync(Document document, Diagnostic diagnostic, CancellationToken cancellationToken) | ||
| { | ||
| var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false); | ||
| var sourceSpan = (CompilationUnitSyntax)root.FindNode(diagnostic.Location.SourceSpan); | ||
|
|
||
| var collector = new UsingCollector(); | ||
| collector.Visit(root); | ||
|
|
||
| var sections = new List<UsingDirectiveSyntax>(); | ||
| sections.AddRange(collector.Usings | ||
| .OrderBy(u => u.ToString().Contains(" static ")) | ||
| .ThenBy(u => u.ToString().Length)); | ||
|
|
||
| var newUsings = sourceSpan.WithUsings(SyntaxFactory.List(sections)).WithAdditionalAnnotations(Formatter.Annotation); | ||
| var newRoot = root.ReplaceNode(sourceSpan, newUsings); | ||
| return document.WithSyntaxRoot(newRoot); | ||
| } | ||
| } | ||
|
|
||
| internal sealed class UsingCollector : CSharpSyntaxWalker | ||
| { | ||
| private readonly List<UsingDirectiveSyntax> usingCollection = new List<UsingDirectiveSyntax>(); | ||
|
|
||
| public IEnumerable<UsingDirectiveSyntax> Usings => usingCollection; | ||
|
|
||
| public override void VisitUsingDirective(UsingDirectiveSyntax node) | ||
| { | ||
| this.usingCollection.Add(node); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
test/CSharp/CodeCracker.Test/Refactoring/SortUsingsTest.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| using Xunit; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.CodeAnalysis; | ||
| using CodeCracker.CSharp.Refactoring; | ||
|
|
||
| namespace CodeCracker.Test.CSharp.Refactoring | ||
| { | ||
| public class SortUsingsTest : CodeFixVerifier<SortUsingsAnalyzer, SortUsingsCodeFixProvider> | ||
| { | ||
| [Fact] | ||
| public async Task CreateDiagnosticSortingUsings() | ||
| { | ||
| const string usings = @"using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using static System.Console; | ||
| using System.Text; | ||
| using static System.DateTime; | ||
| using System.Threading.Tasks;"; | ||
|
|
||
| const string source = @"static void Main(string[] args) | ||
| { | ||
| }"; | ||
|
|
||
| var diagnostic = new DiagnosticResult | ||
| { | ||
| Id = DiagnosticId.SortUsings.ToDiagnosticId(), | ||
| Message = "Sort Using directives by length", | ||
| Severity = DiagnosticSeverity.Hidden, | ||
| Locations = new[] { new DiagnosticResultLocation("Test0.cs", 2, 5) } | ||
| }; | ||
| await VerifyCSharpDiagnosticAsync(source.WrapInCSharpClass("Program", usings), diagnostic); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task FixSortingUsings() | ||
| { | ||
| const string actualUsings = @"using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using static System.Console; | ||
| using System.Text; | ||
| using static System.DateTime; | ||
| using System.Threading.Tasks;"; | ||
|
|
||
| const string expectedUsings = @"using System; | ||
| using System.Linq; | ||
| using System.Text; | ||
| using System.Threading.Tasks; | ||
| using System.Collections.Generic; | ||
| using static System.Console; | ||
| using static System.DateTime;"; | ||
|
|
||
| const string code = @"static void Main(string[] args) | ||
| { | ||
| }"; | ||
| const string typeName = "Program"; | ||
|
|
||
| var oldSource = code.WrapInCSharpClass(typeName, actualUsings); | ||
| var newSource = code.WrapInCSharpClass(typeName, expectedUsings); | ||
| await VerifyCSharpFixAsync(oldSource, newSource); | ||
| } | ||
|
|
||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📝 The changes in this file appear to be a mistake (possible merge conflict resolution?). It introduces an unexpected duplicate section.