Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Open
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
5 changes: 4 additions & 1 deletion 5 CodeCracker.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26430.14
VisualStudioVersion = 15.0.27130.2026
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CodeCracker.Common", "src\Common\CodeCracker.Common\CodeCracker.Common.csproj", "{753D4757-FCBA-43BA-B1BE-89201ACDA192}"
EndProject
Expand Down Expand Up @@ -154,4 +154,7 @@ Global
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {EF50C6AB-1421-41AE-8CB3-927AB24A69EA}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {15B937E4-F880-478B-B387-3438FDD895F7}
EndGlobalSection
Copy link
Contributor

@sharwell sharwell Sep 9, 2018

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.

EndGlobal
6 changes: 4 additions & 2 deletions 6 runTestsCS.ps1
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
param([String]$testClass)
$testDllDirPath = "$PSScriptRoot\test\CSharp\CodeCracker.Test\bin\Debug\"
# $testDllDirPath = "$PSScriptRoot\test\CSharp\CodeCracker.Test\bin\Debug\"
$testDllDirPath = "test\CSharp\CodeCracker.Test\bin\Debug\"
$testDllFileName = "CodeCracker.Test.CSharp.dll"
$testDllFullFileName = "$testDllDirPath$testDllFileName"
$xunitConsole = "$PSScriptRoot\packages\xunit.runner.console.2.2.0\tools\xunit.console.x86.exe"
# $xunitConsole = "$PSScriptRoot\packages\xunit.runner.console.2.2.0\tools\xunit.console.x86.exe"
$xunitConsole = "packages\xunit.runner.console.2.2.0\tools\xunit.console.x86.exe"

if (!(gcm nodemon -ErrorAction Ignore)) {
Write-Host -ForegroundColor DarkRed 'Nodemon not found, install it with npm: `npm i -g nodemon`'
Expand Down
2 changes: 2 additions & 0 deletions 2 src/CSharp/CodeCracker/CodeCracker.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@
<Compile Include="Refactoring\ReplaceWithGetterOnlyAutoPropertyCodeFixProvider.cs" />
<Compile Include="Refactoring\PropertyChangedEventArgsUnnecessaryAllocationAnalyzer.cs" />
<Compile Include="Refactoring\PropertyChangedEventArgsUnnecessaryAllocationCodeFixProvider.cs" />
<Compile Include="Refactoring\SortUsingsAnalyzer.cs" />
<Compile Include="Refactoring\SortUsingsCodeFixProvider.cs" />
<Compile Include="Refactoring\SplitIntoNestedIfFixAllProvider.cs" />
<Compile Include="Refactoring\SplitIntoNestedIfCodeFixProvider.cs" />
<Compile Include="Refactoring\SplitIntoNestedIfAnalyzer.cs" />
Expand Down
37 changes: 37 additions & 0 deletions 37 src/CSharp/CodeCracker/Refactoring/SortUsingsAnalyzer.cs
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
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);
}
}
}
1 change: 1 addition & 0 deletions 1 src/Common/CodeCracker.Common/DiagnosticId.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,6 @@ public enum DiagnosticId
SwitchCaseWithoutDefault = 120,
ReadOnlyComplexTypes = 121,
ReplaceWithGetterOnlyAutoProperty = 125,
SortUsings = 126,
}
}
1 change: 1 addition & 0 deletions 1 test/CSharp/CodeCracker.Test/CodeCracker.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@
<Compile Include="Refactoring\ComputeExpressionTests.cs" />
<Compile Include="Refactoring\ReplaceWithGetterOnlyAutoPropertyTests.cs" />
<Compile Include="Refactoring\PropertyChangedEventArgsUnnecessaryAllocationTests.cs" />
<Compile Include="Refactoring\SortUsingsTest.cs" />
<Compile Include="Refactoring\SplitIntoNestedIfTests.cs" />
<Compile Include="Refactoring\ParameterRefectoryTests.cs" />
<Compile Include="Refactoring\AllowMembersOrderingCodeFixProviderTests.StyleCop.cs" />
Expand Down
65 changes: 65 additions & 0 deletions 65 test/CSharp/CodeCracker.Test/Refactoring/SortUsingsTest.cs
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);
}

}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.