1 | // |
---|
2 | // RemoveField.cs |
---|
3 | // |
---|
4 | // Author: |
---|
5 | // Ciprian Khlud <ciprian.mustiata@yahoo.com> |
---|
6 | // |
---|
7 | // Copyright (c) 2013 Ciprian Khlud |
---|
8 | // |
---|
9 | // Permission is hereby granted, free of charge, to any person obtaining a copy |
---|
10 | // of this software and associated documentation files (the "Software"), to deal |
---|
11 | // in the Software without restriction, including without limitation the rights |
---|
12 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
---|
13 | // copies of the Software, and to permit persons to whom the Software is |
---|
14 | // furnished to do so, subject to the following conditions: |
---|
15 | // |
---|
16 | // The above copyright notice and this permission notice shall be included in |
---|
17 | // all copies or substantial portions of the Software. |
---|
18 | // |
---|
19 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
20 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
21 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
22 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
23 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
---|
24 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
---|
25 | // THE SOFTWARE. |
---|
26 | using System; |
---|
27 | using ICSharpCode.NRefactory.CSharp.Refactoring; |
---|
28 | using System.Collections.Generic; |
---|
29 | using ICSharpCode.NRefactory.CSharp.Resolver; |
---|
30 | using ICSharpCode.NRefactory.Semantics; |
---|
31 | |
---|
32 | namespace ICSharpCode.NRefactory.CSharp |
---|
33 | { |
---|
34 | // [ContextAction("Removes a field from a class", Description = "It removes also the empty assingments and the usages")] |
---|
35 | public class RemoveFieldRefactoryAction : ICodeActionProvider |
---|
36 | { |
---|
37 | public IEnumerable<CodeAction> GetActions(RefactoringContext context) |
---|
38 | { |
---|
39 | var fieldDeclaration = GetFieldDeclaration(context); |
---|
40 | if(fieldDeclaration==null) |
---|
41 | yield break; |
---|
42 | |
---|
43 | |
---|
44 | yield return new CodeAction(string.Format(context.TranslateString("Remove field '{0}'"), fieldDeclaration.Name) |
---|
45 | , script => GenerateNewScript( |
---|
46 | script, fieldDeclaration, context), fieldDeclaration); |
---|
47 | } |
---|
48 | |
---|
49 | |
---|
50 | void GenerateNewScript(Script script, FieldDeclaration fieldDeclaration, RefactoringContext context) |
---|
51 | { |
---|
52 | var firstOrNullObject = fieldDeclaration.Variables.FirstOrNullObject(); |
---|
53 | if(firstOrNullObject==null) |
---|
54 | return; |
---|
55 | var matchedNodes = ComputeMatchNodes(context, firstOrNullObject); |
---|
56 | |
---|
57 | foreach (var matchNode in matchedNodes) |
---|
58 | { |
---|
59 | var parent = matchNode.Parent; |
---|
60 | if (matchNode is VariableInitializer) |
---|
61 | { |
---|
62 | script.Remove(parent); |
---|
63 | } |
---|
64 | else |
---|
65 | if (matchNode is IdentifierExpression) |
---|
66 | { |
---|
67 | if(parent is AssignmentExpression) |
---|
68 | { |
---|
69 | script.Remove(parent.Parent); |
---|
70 | } |
---|
71 | else |
---|
72 | { |
---|
73 | var clone = (IdentifierExpression)matchNode.Clone(); |
---|
74 | clone.Identifier = "TODO"; |
---|
75 | script.Replace(matchNode, clone); |
---|
76 | } |
---|
77 | } |
---|
78 | } |
---|
79 | } |
---|
80 | |
---|
81 | private static List<AstNode> ComputeMatchNodes(RefactoringContext context, VariableInitializer firstOrNullObject) |
---|
82 | { |
---|
83 | var referenceFinder = new FindReferences(); |
---|
84 | var matchedNodes = new List<AstNode>(); |
---|
85 | |
---|
86 | var resolveResult = context.Resolver.Resolve(firstOrNullObject); |
---|
87 | var member = resolveResult as MemberResolveResult; |
---|
88 | if (member == null)//not a member is unexpected case, so is better to return no match than to break the code |
---|
89 | return matchedNodes; |
---|
90 | |
---|
91 | FoundReferenceCallback callback = (matchNode, result) => matchedNodes.Add(matchNode); |
---|
92 | |
---|
93 | var searchScopes = referenceFinder.GetSearchScopes(member.Member); |
---|
94 | referenceFinder.FindReferencesInFile(searchScopes, |
---|
95 | context.UnresolvedFile, |
---|
96 | context.RootNode as SyntaxTree, |
---|
97 | context.Compilation, callback, |
---|
98 | context.CancellationToken); |
---|
99 | return matchedNodes; |
---|
100 | } |
---|
101 | |
---|
102 | FieldDeclaration GetFieldDeclaration(RefactoringContext context) |
---|
103 | { |
---|
104 | var result = context.GetNode<FieldDeclaration>(); |
---|
105 | |
---|
106 | return result; |
---|
107 | } |
---|
108 | } |
---|
109 | } |
---|
110 | |
---|