Skip to content

Commit

Permalink
ToLower, Test, Icons
Browse files Browse the repository at this point in the history
  • Loading branch information
StandaSojka committed Nov 19, 2016
1 parent 28a09e2 commit 87d0772
Show file tree
Hide file tree
Showing 27 changed files with 541 additions and 186 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<AssemblyName>CSafeRefactoring.Vsix</AssemblyName>
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
<GeneratePkgDefFile>false</GeneratePkgDefFile>
<IncludeAssemblyInVSIXContainer>false</IncludeAssemblyInVSIXContainer>
<IncludeAssemblyInVSIXContainer>true</IncludeAssemblyInVSIXContainer>
<IncludeDebugSymbolsInVSIXContainer>false</IncludeDebugSymbolsInVSIXContainer>
<IncludeDebugSymbolsInLocalVSIXDeployment>false</IncludeDebugSymbolsInLocalVSIXDeployment>
<CopyBuildOutputToOutputDirectory>false</CopyBuildOutputToOutputDirectory>
Expand All @@ -41,6 +41,8 @@
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<CopyVsixExtensionFiles>True</CopyVsixExtensionFiles>
<CopyVsixExtensionLocation>C:/temp</CopyVsixExtensionLocation>
</PropertyGroup>
<PropertyGroup>
<StartAction>Program</StartAction>
Expand All @@ -58,6 +60,18 @@
<Name>CSafeRefactoring</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Content Include="Icons\CSafe.png">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
<IncludeInVSIX>true</IncludeInVSIX>
</Content>
</ItemGroup>
<ItemGroup>
<Content Include="Icons\CSafe.ico">
<IncludeInVSIX>true</IncludeInVSIX>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="$(VSToolsPath)\VSSDK\Microsoft.VsSDK.targets" Condition="'$(VSToolsPath)' != ''" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Expand Down
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<PackageManifest Version="2.0.0" xmlns="http://schemas.microsoft.com/developer/vsx-schema/2011" xmlns:d="http://schemas.microsoft.com/developer/vsx-schema-design/2011">
<Metadata>
<Identity Id="CSafeRefactoring.Kentico software.c8750bdf-1661-4112-95e5-3fe2bbf4ebf3" Version="1.2.1" Language="en-US" Publisher="StanislavS"/>
<Identity Id="CSafeRefactoring.c8750bdf-1661-4112-95e5-3fe2bbf4ebf3" Version="2.1.4" Language="en-US" Publisher="StanislavS"/>
<DisplayName>CSafeRefactoring</DisplayName>
<Description xml:space="preserve">Kentico CSafe methods refactoring extension.</Description>
<Icon>Icons\CSafe.ico</Icon>
<PreviewImage>Icons\CSafe.png</PreviewImage>
</Metadata>
<Installation>
<Installation AllUsers="true">
<InstallationTarget Version="[14.0,]" Id="Microsoft.VisualStudio.Pro" />
</Installation>
<Dependencies>
Expand Down
6 changes: 6 additions & 0 deletions CSafeRefactoring/CSafeRefactoring/CSafeRefactoring.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Compile Include="CodeRefactoringProviderBase.cs" />
<Compile Include="EndsWithIgnoreCaseRefactoringProvider.cs" />
<Compile Include="StartsEndsWithRefactoringBase.cs" />
<Compile Include="EqualsRefactoringBase.cs" />
Expand All @@ -42,7 +43,12 @@
<Compile Include="StartsWithRefactoringProvider.cs" />
<Compile Include="EqualsRefactoringProvider.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Substituters\EqualsSubstituter.cs" />
<Compile Include="Substituters\StartsWithEndsWithSubstituter.cs" />
<Compile Include="Substituters\ToLowerSubstituter.cs" />
<Compile Include="SyntaxTreeCreators\EqualsSyntaxTreeCreator.cs" />
<Compile Include="SyntaxTreeCreators\ToLowerSyntaxTreeCreator.cs" />
<Compile Include="ToLowerrefactoringProvider.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
Expand Down
18 changes: 18 additions & 0 deletions CSafeRefactoring/CSafeRefactoring/CSafeRefactoring.nuspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0"?>
<package >
<metadata>
<id>$id$</id>
<version>$version$</version>
<title>$title$</title>
<authors>$author$</authors>
<owners>$author$</owners>
<licenseUrl>http://LICENSE_URL_HERE_OR_DELETE_THIS_LINE</licenseUrl>
<projectUrl>http://PROJECT_URL_HERE_OR_DELETE_THIS_LINE</projectUrl>
<iconUrl>http://ICON_URL_HERE_OR_DELETE_THIS_LINE</iconUrl>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>$description$</description>
<releaseNotes>Summary of changes made in this release of the package.</releaseNotes>
<copyright>Copyright 2016</copyright>
<tags>Tag1 Tag2</tags>
</metadata>
</package>
33 changes: 33 additions & 0 deletions CSafeRefactoring/CSafeRefactoring/CodeRefactoringProviderBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeRefactorings;

namespace CSafeRefactoring
{
public abstract class CodeRefactoringProviderBase : CodeRefactoringProvider
{
private const string ORDINAL_IGNORECASE = "Use StringComparison.OrdinalIgnoreCase";
private const string ORDINAL = "Use StringComparison.Ordinal";

private const string INVARIANTCULTURE_IGNORECASE = "Use StringComparison.InvariantCultureIgnoreCase";
private const string INVARIANTCULTURE = "Use StringComparison.InvariantCulture";

protected abstract string MethodToReplaceName { get; }

protected abstract bool IgnoreCase { get; }

protected string GetOrdinalMessage()
{
return IgnoreCase ? ORDINAL_IGNORECASE : ORDINAL;
}

protected string GetInvariantCultureMessage()
{
return IgnoreCase ? INVARIANTCULTURE_IGNORECASE : INVARIANTCULTURE;
}

protected bool IsCurrentNodeReplacedMethod(SyntaxNode node)
{
return node.GetText().ToString() == MethodToReplaceName;
}
}
}
74 changes: 27 additions & 47 deletions CSafeRefactoring/CSafeRefactoring/EqualsRefactoringBase.cs
Original file line number Diff line number Diff line change
@@ -1,81 +1,61 @@
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using CSafeRefactoring.SyntaxTreeCreators;
using Microsoft.CodeAnalysis;
using CSafeRefactoring.Substituters;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeRefactorings;
using Microsoft.CodeAnalysis.CSharp.Syntax;

namespace CSafeRefactoring
{
public abstract class EqualsRefactoringBase : CodeRefactoringProvider
public abstract class EqualsRefactoringBase : CodeRefactoringProviderBase
{
protected abstract string MethodToReplaceName { get; }

protected abstract bool IgnoreCase { get; }

private const string TYPE_ORDINAL = "Ordinal";
private const string TYPE_INVARIANTCULTURE = "InvariantCulture";

public override async Task ComputeRefactoringsAsync(CodeRefactoringContext context)
{
var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);
var node = root.FindNode(context.Span);
var root = await context.Document
.GetSyntaxRootAsync(context.CancellationToken)
.ConfigureAwait(false);

var invocationExpr = node;
var node = root.FindNode(context.Span);

if (invocationExpr.GetText().ToString() != MethodToReplaceName)
if (!IsCurrentNodeReplacedMethod(node))
{
return;
}

var declaration = root.FindNode(context.Span).Parent.AncestorsAndSelf().OfType<InvocationExpressionSyntax>().First();
var declaration = root
.FindNode(context.Span)
.Parent
.AncestorsAndSelf()
.OfType<InvocationExpressionSyntax>()
.First();

if (!AnalyzeAdditionalRestrictions(declaration))
{
return;
}

var messageInvariantCulture = IgnoreCase ? "Use StringComparison.InvariantCultureIgnoreCase" : "Use StringComparison.InvariantCulture";
var messageOrdinal = IgnoreCase ? "Use StringComparison.OrdinalIgnoreCase" : "Use StringComparison.Ordinal";

var actionIvariantCulture = CodeAction.Create(messageInvariantCulture, c => FixDocument(context.Document, declaration, c, "InvariantCulture"));
var actionOrdinal = CodeAction.Create(messageOrdinal, c => FixDocument(context.Document, declaration, c, "Ordinal"));

context.RegisterRefactoring(actionIvariantCulture);
context.RegisterRefactoring(actionOrdinal);
CreateAndRegisterCodeActions(context, declaration);
}


protected async Task<Document> FixDocument(Document document, InvocationExpressionSyntax invocationExpressionSyntax, CancellationToken cancellationToken, string type)
private void CreateAndRegisterCodeActions(CodeRefactoringContext context, InvocationExpressionSyntax declaration)
{
var root = await document.GetSyntaxRootAsync(cancellationToken);
var messageInvariantCulture = GetInvariantCultureMessage();
var messageOrdinal = GetOrdinalMessage();

var memberAccessExpressionSyntax = invocationExpressionSyntax.Expression as MemberAccessExpressionSyntax;
if (memberAccessExpressionSyntax?.Expression == null)
{
return document;
}
var substituter = new EqualsSubstituter(context.Document, declaration, IgnoreCase);

var leadingTrivia = memberAccessExpressionSyntax.Expression.GetLeadingTrivia();
var leftString = memberAccessExpressionSyntax.Expression.WithoutLeadingTrivia();
var rightString = invocationExpressionSyntax.ArgumentList.Arguments[0].Expression;
var actionIvariantCulture = CodeAction.Create(messageInvariantCulture,
cancellationToken => substituter.FixDocument(TYPE_INVARIANTCULTURE, cancellationToken));
var actionOrdinal = CodeAction.Create(messageOrdinal,
cancellationToken => substituter.FixDocument(TYPE_ORDINAL, cancellationToken));

var newSyntaxTree = CreateNewOrdinalSyntaxTree(leftString, rightString, leadingTrivia, type);
var newRoot = root.ReplaceNode(invocationExpressionSyntax, newSyntaxTree);
return document.WithSyntaxRoot(newRoot);
context.RegisterRefactoring(actionIvariantCulture);
context.RegisterRefactoring(actionOrdinal);
}

private InvocationExpressionSyntax CreateNewOrdinalSyntaxTree(ExpressionSyntax leftString, ExpressionSyntax rightString, SyntaxTriviaList leadingTrivia, string type)
{
var syntaxTreeCreator = new EqualsSyntaxTreeCreator(leftString, rightString);
if (IgnoreCase)
{
return syntaxTreeCreator.Create($"{type}IgnoreCase")
.WithLeadingTrivia(leadingTrivia);
}

return syntaxTreeCreator.Create(type)
.WithLeadingTrivia(leadingTrivia);
}

protected abstract bool AnalyzeAdditionalRestrictions(InvocationExpressionSyntax invocationExpr);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@ public class EqualsRefactoringProvider : EqualsRefactoringBase

protected override bool IgnoreCase => false;


protected override bool AnalyzeAdditionalRestrictions(InvocationExpressionSyntax invocationExpr)
{
return invocationExpr.ArgumentList.Arguments.Count == 1;
}

}
}
Loading

0 comments on commit 87d0772

Please sign in to comment.