Free cookie consent management tool by TermsFeed Policy Generator

source: branches/CodeEditor/HeuristicLab.ExtLibs/HeuristicLab.NRefactory/5.5.0/NRefactory.CSharp-5.5.0/Refactoring/CodeActions/RemoveFieldRefactoryActionRefactoringAction.cs @ 11700

Last change on this file since 11700 was 11700, checked in by jkarder, 9 years ago

#2077: created branch and added first version

File size: 4.3 KB
Line 
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.
26using System;
27using ICSharpCode.NRefactory.CSharp.Refactoring;
28using System.Collections.Generic;
29using ICSharpCode.NRefactory.CSharp.Resolver;
30using ICSharpCode.NRefactory.Semantics;
31
32namespace 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
Note: See TracBrowser for help on using the repository browser.