From 3dcad6981056d176dc55b6fc396bbb923ca14444 Mon Sep 17 00:00:00 2001 From: Ciprian Khlud Date: Sun, 28 Apr 2013 13:19:06 +0300 Subject: [PATCH] Remove "Remove Field Refactory" as it is dangerous. --- .../ICSharpCode.NRefactory.CSharp.csproj | 12 +- ...veFieldRefactoryActionRefactoringAction.cs | 110 ------------------ .../RemoveFieldRefactoryActionTests.cs | 96 --------------- .../ICSharpCode.NRefactory.Tests.csproj | 15 +-- NRefactory.suo | Bin 0 -> 3072 bytes 5 files changed, 3 insertions(+), 230 deletions(-) delete mode 100644 ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/RemoveFieldRefactoryActionRefactoringAction.cs delete mode 100644 ICSharpCode.NRefactory.Tests/CSharp/CodeActions/RemoveFieldRefactoryActionTests.cs create mode 100644 NRefactory.suo diff --git a/ICSharpCode.NRefactory.CSharp/ICSharpCode.NRefactory.CSharp.csproj b/ICSharpCode.NRefactory.CSharp/ICSharpCode.NRefactory.CSharp.csproj index bf5e6b4bc3..2a1aa591af 100644 --- a/ICSharpCode.NRefactory.CSharp/ICSharpCode.NRefactory.CSharp.csproj +++ b/ICSharpCode.NRefactory.CSharp/ICSharpCode.NRefactory.CSharp.csproj @@ -534,7 +534,6 @@ - @@ -546,16 +545,7 @@ - - - - - - - - - - + diff --git a/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/RemoveFieldRefactoryActionRefactoringAction.cs b/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/RemoveFieldRefactoryActionRefactoringAction.cs deleted file mode 100644 index 7f44799942..0000000000 --- a/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/RemoveFieldRefactoryActionRefactoringAction.cs +++ /dev/null @@ -1,110 +0,0 @@ -// -// RemoveField.cs -// -// Author: -// Ciprian Khlud -// -// Copyright (c) 2013 Ciprian Khlud -// -// 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 System; -using ICSharpCode.NRefactory.CSharp.Refactoring; -using System.Collections.Generic; -using ICSharpCode.NRefactory.CSharp.Resolver; -using ICSharpCode.NRefactory.Semantics; - -namespace ICSharpCode.NRefactory.CSharp -{ - [ContextAction("Removes a field from a class", Description = "It removes also the empty assingments and the usages")] - public class RemoveFieldRefactoryAction : ICodeActionProvider - { - public IEnumerable GetActions(RefactoringContext context) - { - var fieldDeclaration = GetFieldDeclaration(context); - if(fieldDeclaration==null) - yield break; - - - yield return new CodeAction(string.Format(context.TranslateString("Remove field '{0}'"), fieldDeclaration.Name) - , script => GenerateNewScript( - script, fieldDeclaration, context), fieldDeclaration); - } - - - void GenerateNewScript(Script script, FieldDeclaration fieldDeclaration, RefactoringContext context) - { - var firstOrNullObject = fieldDeclaration.Variables.FirstOrNullObject(); - if(firstOrNullObject==null) - return; - var matchedNodes = ComputeMatchNodes(context, firstOrNullObject); - - foreach (var matchNode in matchedNodes) - { - var parent = matchNode.Parent; - if (matchNode is VariableInitializer) - { - script.Remove(parent); - } - else - if (matchNode is IdentifierExpression) - { - if(parent is AssignmentExpression) - { - script.Remove(parent.Parent); - } - else - { - var clone = (IdentifierExpression)matchNode.Clone(); - clone.Identifier = "TODO"; - script.Replace(matchNode, clone); - } - } - } - } - - private static List ComputeMatchNodes(RefactoringContext context, VariableInitializer firstOrNullObject) - { - var referenceFinder = new FindReferences(); - var matchedNodes = new List(); - - var resolveResult = context.Resolver.Resolve(firstOrNullObject); - var member = resolveResult as MemberResolveResult; - if (member == null)//not a member is unexpected case, so is better to return no match than to break the code - return matchedNodes; - - FoundReferenceCallback callback = (matchNode, result) => matchedNodes.Add(matchNode); - - var searchScopes = referenceFinder.GetSearchScopes(member.Member); - referenceFinder.FindReferencesInFile(searchScopes, - context.UnresolvedFile, - context.RootNode as SyntaxTree, - context.Compilation, callback, - context.CancellationToken); - return matchedNodes; - } - - FieldDeclaration GetFieldDeclaration(RefactoringContext context) - { - var result = context.GetNode(); - - return result; - } - } -} - diff --git a/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/RemoveFieldRefactoryActionTests.cs b/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/RemoveFieldRefactoryActionTests.cs deleted file mode 100644 index 3b2023a10c..0000000000 --- a/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/RemoveFieldRefactoryActionTests.cs +++ /dev/null @@ -1,96 +0,0 @@ -// -// RemoveFieldRefactoryActionTests.cs -// -// Author: -// Ciprian Khlud -// -// Copyright (c) 2013 Ciprian Khlud -// -// 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; - -namespace ICSharpCode.NRefactory.CSharp.CodeActions -{ - [TestFixture] - public class RemoveFieldRefactoryActionTests : ContextActionTestBase - { - [Test] - public void RemoveOneField() - { - Test( - @" -class A { - int $field; -} -", - @" -class A { -} -" - );} - - [Test] - public void RemoveOneFieldAndAssignmentValue() - { - Test( - @" -class A { - int $field; - A() { - field = 3; - } -} -", - @" -class A { - A() { - } -} -" - ); - } - - [Test] - public void RemoveOneFieldAndAssignmentValueAndMethodCall() - { - Test( - @" -class A { - int $field; - A() { - field = 3; -if(field!=0) - Method(field); - } -} -", - @" -class A { - A() { -if(TODO!=0) - Method(TODO); - } -} -" - ); - } - } -} - diff --git a/ICSharpCode.NRefactory.Tests/ICSharpCode.NRefactory.Tests.csproj b/ICSharpCode.NRefactory.Tests/ICSharpCode.NRefactory.Tests.csproj index 08aa57084b..04902be570 100644 --- a/ICSharpCode.NRefactory.Tests/ICSharpCode.NRefactory.Tests.csproj +++ b/ICSharpCode.NRefactory.Tests/ICSharpCode.NRefactory.Tests.csproj @@ -1,4 +1,4 @@ - + {63D3B27A-D966-4902-90B3-30290E1692F1} @@ -409,7 +409,6 @@ - @@ -435,17 +434,7 @@ False - - - - - - - - - - - + diff --git a/NRefactory.suo b/NRefactory.suo new file mode 100644 index 0000000000000000000000000000000000000000..cb425d482667ddc550fd81d6ade9dc6610b411db GIT binary patch literal 3072 zcmca`Uhu)fjZzO8(10BSGsD0CoD6J8;*3Bx2!nwD0|OI~0pkDr|NlQkkbwcn90fxt z1pWfu3W`4vW`V}#5OV&gQz;CAAclM(EMZV!aAn8?(nSoFu&hiJEylnK&-2U#bS!9I zz2&F#IYyAW1E9hK2m*jM<}+k5cmi!t1F~EhDj*U>l48J82ssFd)W!fTzQDkrp%m!r z9EMDw{UDsq5Dere0&yzXEhYpER3XEDP?>25El3I3Z%o*JMvzgU-~@#O0p(z6P?;_T zwI7u4L3%-P0?PlaKnxON2htos%n8I?P%)7CqhNT20QVF@*|d+0ZcQJwlUoaee*>eM zBrt=?nZb%7h9Q)p7?@*47sx9E(r!Q(rvgiY6bw@;8Ibj%yBT5!vI;$*EyWDUyH!Gr)$SH-TM|8W{-a_E zP;Np`I}8k87zZ}|LV$iQ28KQ;-jKtcx?zT}1rj!L8@VoiI(+n2`8D%v^0~VtK&1|5 N*pQSEM-`J50sw)RZxjFk literal 0 HcmV?d00001