Browse Source

Add RemoveRedundantCatchTypeAction.

newNRvisualizers
Simon Lindgren 14 years ago
parent
commit
44eff13a62
  1. 1
      ICSharpCode.NRefactory.CSharp/ICSharpCode.NRefactory.CSharp.csproj
  2. 92
      ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/RemoveRedundantCatchTypeAction.cs
  3. 108
      ICSharpCode.NRefactory.Tests/CSharp/CodeActions/RemoveRedundantCatchTypeTests.cs
  4. 1
      ICSharpCode.NRefactory.Tests/ICSharpCode.NRefactory.Tests.csproj

1
ICSharpCode.NRefactory.CSharp/ICSharpCode.NRefactory.CSharp.csproj

@ -373,6 +373,7 @@ @@ -373,6 +373,7 @@
<Compile Include="Refactoring\CodeActions\ImplementInterfaceAction.cs" />
<Compile Include="Refactoring\CodeActions\ImplementInterfaceExplicitAction.cs" />
<Compile Include="Refactoring\CodeActions\ImplementAbstractMembersAction.cs" />
<Compile Include="Refactoring\CodeActions\RemoveRedundantCatchTypeAction.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ICSharpCode.NRefactory\ICSharpCode.NRefactory.csproj">

92
ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/RemoveRedundantCatchTypeAction.cs

@ -0,0 +1,92 @@ @@ -0,0 +1,92 @@
//
// RemoveRedundantCatchTypeAction.cs
//
// Author:
// Simon Lindgren <simon.n.lindgren@gmail.com>
//
// Copyright (c) 2012 Simon Lindgren
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using ICSharpCode.NRefactory.CSharp.Refactoring;
using ICSharpCode.NRefactory.TypeSystem;
using System;
using ICSharpCode.NRefactory.Semantics;
namespace ICSharpCode.NRefactory.CSharp.Refactoring
{
[ContextAction("Remove redundant type",
Description = "Removes a redundant exception type specifier from catch clauses.")]
public class RemoveRedundantCatchTypeAction : ICodeActionProvider
{
#region ICodeActionProvider implementation
public System.Collections.Generic.IEnumerable<CodeAction> GetActions(RefactoringContext context)
{
var catchClause = context.GetNode<CatchClause>();
if (catchClause == null)
yield break;
if (catchClause.Type.IsNull)
yield break;
var exceptionType = context.ResolveType(catchClause.Type);
if (exceptionType != context.Compilation.FindType(typeof(Exception)))
yield break;
var exceptionIdentifierRR = context.Resolve(catchClause.VariableNameToken) as LocalResolveResult;
if (exceptionIdentifierRR != null &&
IsReferenced(exceptionIdentifierRR.Variable, catchClause.Body, context))
yield break;
yield return new CodeAction(context.TranslateString("Remove type specifier"), script => {
script.Replace(catchClause, new CatchClause() {
Body = catchClause.Body.Clone() as BlockStatement
});
});
}
bool IsReferenced(IVariable variable, AstNode node, RefactoringContext context)
{
var visitor = new ReferenceFinderVisitor(context, variable);
node.AcceptVisitor(visitor);
return visitor.FoundReference;
}
class ReferenceFinderVisitor: DepthFirstAstVisitor
{
RefactoringContext context;
IVariable variable;
public ReferenceFinderVisitor(RefactoringContext context, IVariable variable)
{
this.context = context;
this.variable = variable;
}
public bool FoundReference { get; private set; }
public override void VisitIdentifierExpression(IdentifierExpression identifierExpression)
{
var resolvedIdentifier = context.Resolve(identifierExpression) as LocalResolveResult;
if (resolvedIdentifier != null)
FoundReference |= resolvedIdentifier.Variable == variable;
base.VisitIdentifierExpression(identifierExpression);
}
}
#endregion
}
}

108
ICSharpCode.NRefactory.Tests/CSharp/CodeActions/RemoveRedundantCatchTypeTests.cs

@ -0,0 +1,108 @@ @@ -0,0 +1,108 @@
//
// RemoveRedundantCatchTypeTests.cs
//
// Author:
// Simon Lindgren <simon.n.lindgren@gmail.com>
//
// Copyright (c) 2012 Simon Lindgren
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using NUnit.Framework;
using ICSharpCode.NRefactory.CSharp.Refactoring;
namespace ICSharpCode.NRefactory.CSharp.CodeActions
{
[TestFixture]
public class RemoveRedundantCatchTypeTests : ContextActionTestBase
{
[Test]
public void RemovesSimpleExceptionMatch()
{
Test<RemoveRedundantCatchTypeAction>(@"
class TestClass
{
public void F()
{
try {
}
catch $(System.Exception) {
}
}
}", @"
class TestClass
{
public void F()
{
try {
}
catch {
}
}
}");
}
[Test]
public void PreservesBody()
{
Test<RemoveRedundantCatchTypeAction>(@"
class TestClass
{
public void F()
{
try {
}
catch $(System.Exception e) {
System.Console.WriteLine (""Hi"");
}
}
}", @"
class TestClass
{
public void F()
{
try {
}
catch {
System.Console.WriteLine (""Hi"");
}
}
}");
}
[Test]
public void IgnoresReferencedExceptionMatch()
{
TestWrongContext<RemoveRedundantCatchTypeAction>(@"
class TestClass
{
public void F()
{
try {
}
catch $(System.Exception e) {
System.Console.WriteLine (e);
}
}
}");
}
}
}

1
ICSharpCode.NRefactory.Tests/ICSharpCode.NRefactory.Tests.csproj

@ -269,6 +269,7 @@ @@ -269,6 +269,7 @@
<Compile Include="CSharp\CodeActions\ImplementInterfaceTests.cs" />
<Compile Include="CSharp\CodeActions\ImplementInterfaceExplicitTests.cs" />
<Compile Include="CSharp\CodeActions\ImplementAbstractMembersTest.cs" />
<Compile Include="CSharp\CodeActions\RemoveRedundantCatchTypeTests.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\Mono.Cecil\Mono.Cecil.csproj">

Loading…
Cancel
Save