~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/nrefactory/ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/AccessToClosureIssues/AccessToModifiedClosureIssue.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
Import upstream version 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// 
 
2
// AccessToModifiedClosureIssue.cs
 
3
//  
 
4
// Author:
 
5
//      Mansheng Yang <lightyang0@gmail.com>
 
6
// 
 
7
// Copyright (c) 2012 Mansheng Yang <lightyang0@gmail.com>
 
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
 
 
27
using System;
 
28
using System.Collections.Generic;
 
29
using System.Linq;
 
30
 
 
31
namespace ICSharpCode.NRefactory.CSharp.Refactoring
 
32
{
 
33
    //[IssueDescription("Access to modified closure variable",
 
34
    //                   Description = "Access to closure variable from anonymous method when the variable is modified " +
 
35
    //                                 "externally",
 
36
    //                   Category = IssueCategories.CodeQualityIssues,
 
37
    //                   Severity = Severity.Warning,
 
38
    //                   IssueMarker = IssueMarker.Underline,
 
39
    //                   ResharperDisableKeyword = "AccessToModifiedClosure")]  
 
40
        public class AccessToModifiedClosureIssue : AccessToClosureIssue
 
41
        {
 
42
                public AccessToModifiedClosureIssue ()
 
43
                        : base ("Access to modified closure")
 
44
                {
 
45
                }
 
46
 
 
47
                protected override NodeKind GetNodeKind (AstNode node)
 
48
                {
 
49
                        var assignment = node.GetParent<AssignmentExpression> ();
 
50
                        if (assignment != null && assignment.Left == node) {
 
51
                                if (assignment.Operator == AssignmentOperatorType.Assign) 
 
52
                                        return NodeKind.Modification;
 
53
                                return NodeKind.ReferenceAndModification;
 
54
                        }
 
55
                        var unaryExpr = node.GetParent<UnaryOperatorExpression> ();
 
56
                        if (unaryExpr != null && unaryExpr.Expression == node &&
 
57
                                (unaryExpr.Operator == UnaryOperatorType.Increment ||
 
58
                                 unaryExpr.Operator == UnaryOperatorType.PostIncrement ||
 
59
                                 unaryExpr.Operator == UnaryOperatorType.Decrement ||
 
60
                                 unaryExpr.Operator == UnaryOperatorType.PostDecrement)) {
 
61
                                return NodeKind.ReferenceAndModification;
 
62
                        }
 
63
                        if (node.Parent is ForeachStatement)
 
64
                                return NodeKind.Modification;
 
65
 
 
66
                        return NodeKind.Reference;
 
67
                }
 
68
 
 
69
                protected override IEnumerable<CodeAction> GetFixes (BaseRefactoringContext context, Node env,
 
70
                                                                                                                         string variableName)
 
71
                {
 
72
                        var containingStatement = env.ContainingStatement;
 
73
 
 
74
                        // we don't give a fix for these cases since the general fix may not work
 
75
                        // lambda in while/do-while/for condition
 
76
                        if (containingStatement is WhileStatement || containingStatement is DoWhileStatement ||
 
77
                                containingStatement is ForStatement)
 
78
                                yield break;
 
79
                        // lambda in for initializer/iterator
 
80
                        if (containingStatement.Parent is ForStatement &&
 
81
                                ((ForStatement)containingStatement.Parent).EmbeddedStatement != containingStatement)
 
82
                                yield break;
 
83
 
 
84
                        Action<Script> action = script =>
 
85
                        {
 
86
                                var newName = LocalVariableNamePicker.PickSafeName (
 
87
                                        containingStatement.GetParent<EntityDeclaration> (),
 
88
                                        Enumerable.Range (1, 100).Select (i => variableName + i));
 
89
 
 
90
                                var variableDecl = new VariableDeclarationStatement (new SimpleType("var"), newName, 
 
91
                                                                                                                                         new IdentifierExpression (variableName));
 
92
                                
 
93
                                if (containingStatement.Parent is BlockStatement || containingStatement.Parent is SwitchSection) {
 
94
                                        script.InsertBefore (containingStatement, variableDecl);
 
95
                                } else {
 
96
                                        var offset = script.GetCurrentOffset (containingStatement.StartLocation);
 
97
                                        script.InsertBefore (containingStatement, variableDecl);
 
98
                                        script.InsertText (offset, "{");
 
99
                                        script.InsertText (script.GetCurrentOffset (containingStatement.EndLocation), "}");
 
100
                                        script.FormatText (containingStatement.Parent);
 
101
                                }
 
102
 
 
103
                                var textNodes = new List<AstNode> ();
 
104
                                textNodes.Add (variableDecl.Variables.First ().NameToken);
 
105
 
 
106
                                foreach (var reference in env.GetAllReferences ()) {
 
107
                                        var identifier = new IdentifierExpression (newName);
 
108
                                        script.Replace (reference.AstNode, identifier);
 
109
                                        textNodes.Add (identifier);
 
110
                                }
 
111
                                script.Link (textNodes.ToArray ());
 
112
                        };
 
113
                        yield return new CodeAction (context.TranslateString ("Copy to local variable"), action, env.AstNode);
 
114
                }
 
115
        }
 
116
}