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

« back to all changes in this revision

Viewing changes to external/nrefactory/ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/RedundantUsingIssue.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
// RedundantUsingInspector.cs
 
3
//
 
4
// Author:
 
5
//       Mike Krüger <mkrueger@xamarin.com>
 
6
// 
 
7
// Copyright (c) 2012 Xamarin <http://xamarin.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 ICSharpCode.NRefactory.PatternMatching;
 
29
using System.Collections.Generic;
 
30
using ICSharpCode.NRefactory.TypeSystem;
 
31
using ICSharpCode.NRefactory.Semantics;
 
32
using ICSharpCode.NRefactory.CSharp.Resolver;
 
33
using System.Linq;
 
34
 
 
35
namespace ICSharpCode.NRefactory.CSharp.Refactoring
 
36
{
 
37
        /// <summary>
 
38
        /// Finds redundant using declarations.
 
39
        /// </summary>
 
40
        [IssueDescription("Remove unused usings",
 
41
                          Description = "Removes used declarations that are not required.",
 
42
                          Category = IssueCategories.Redundancies,
 
43
                          Severity = Severity.Hint,
 
44
                          IssueMarker = IssueMarker.GrayOut,
 
45
                          ResharperDisableKeyword = "RedundantUsingDirective"
 
46
        )]
 
47
        public class RedundantUsingIssue : ICodeIssueProvider
 
48
        {
 
49
                List<string> namespacesToKeep = new List<string>();
 
50
                
 
51
                /// <summary>
 
52
                /// The list of namespaces that should be kept even if they are not being used.
 
53
                /// Used in SharpDevelop to always keep the "System" namespace around.
 
54
                /// </summary>
 
55
                public IList<string> NamespacesToKeep {
 
56
                        get { return namespacesToKeep; }
 
57
                }
 
58
                
 
59
                public IEnumerable<CodeIssue> GetIssues(BaseRefactoringContext context)
 
60
                {
 
61
                        var visitor = new GatherVisitor (context, this);
 
62
                        context.RootNode.AcceptVisitor (visitor);
 
63
                        visitor.Collect ();
 
64
                        return visitor.FoundIssues;
 
65
                }
 
66
 
 
67
                class GatherVisitor : GatherVisitorBase<RedundantUsingIssue>
 
68
                {
 
69
                        Dictionary<UsingDeclaration, bool> isInUse = new Dictionary<UsingDeclaration, bool>();
 
70
                        Dictionary<string, UsingDeclaration> namespaceToUsingDecl = new Dictionary<string, UsingDeclaration>();
 
71
                        
 
72
                        public GatherVisitor (BaseRefactoringContext ctx, RedundantUsingIssue issueProvider) : base (ctx, issueProvider)
 
73
                        {
 
74
                        }
 
75
 
 
76
                        public void Collect()
 
77
                        {
 
78
                                foreach (var u in isInUse.Where (u => !u.Value)) {
 
79
                                        var decl = u.Key;
 
80
                                        AddIssue(decl, ctx.TranslateString("Remove redundant usings"), script => {
 
81
                                                foreach (var u2 in isInUse.Where (a => !a.Value)) {
 
82
                                                        script.Remove (u2.Key);
 
83
                                                }
 
84
                                        }
 
85
                                        );
 
86
                                }
 
87
                        }
 
88
 
 
89
                        public override void VisitUsingDeclaration(UsingDeclaration usingDeclaration)
 
90
                        {
 
91
                                base.VisitUsingDeclaration(usingDeclaration);
 
92
                                if (IsSuppressed(usingDeclaration.StartLocation))
 
93
                                        return;
 
94
                                var nrr = ctx.Resolve(usingDeclaration.Import) as NamespaceResolveResult;
 
95
                                if (nrr != null) {
 
96
                                        isInUse[usingDeclaration] = IssueProvider.namespacesToKeep.Contains(nrr.NamespaceName);
 
97
                                        namespaceToUsingDecl[nrr.NamespaceName] = usingDeclaration;
 
98
                                }
 
99
                        }
 
100
                        
 
101
                        public override void VisitNamespaceDeclaration(NamespaceDeclaration namespaceDeclaration)
 
102
                        {
 
103
                                var oldNamespaceToUsingDecl = new Dictionary<string, UsingDeclaration>(namespaceToUsingDecl);
 
104
                                base.VisitNamespaceDeclaration(namespaceDeclaration);
 
105
                                namespaceToUsingDecl = oldNamespaceToUsingDecl;
 
106
                        }
 
107
                        
 
108
                        void UseNamespace(string ns)
 
109
                        {
 
110
                                UsingDeclaration decl;
 
111
                                if (namespaceToUsingDecl.TryGetValue(ns, out decl)) {
 
112
                                        isInUse[decl] = true;
 
113
                                }
 
114
                        }
 
115
 
 
116
                        public override void VisitIdentifierExpression(IdentifierExpression identifierExpression)
 
117
                        {
 
118
                                base.VisitIdentifierExpression(identifierExpression);
 
119
                                var trr = ctx.Resolve(identifierExpression) as TypeResolveResult;
 
120
                                if (trr != null) {
 
121
                                        UseNamespace(trr.Type.Namespace);
 
122
                                }
 
123
                        }
 
124
 
 
125
                        public override void VisitSimpleType(SimpleType simpleType)
 
126
                        {
 
127
                                base.VisitSimpleType(simpleType);
 
128
                                UseNamespace(ctx.Resolve(simpleType).Type.Namespace);
 
129
                        }
 
130
 
 
131
                        public override void VisitInvocationExpression (InvocationExpression invocationExpression)
 
132
                        {
 
133
                                base.VisitInvocationExpression (invocationExpression);
 
134
                                UseExtensionMethod(ctx.Resolve(invocationExpression));
 
135
                        }
 
136
                        
 
137
                        void UseExtensionMethod(ResolveResult rr)
 
138
                        {
 
139
                                var mg = rr as CSharpInvocationResolveResult;
 
140
                                if (mg != null && mg.IsExtensionMethodInvocation) {
 
141
                                        UseNamespace (mg.Member.DeclaringType.Namespace);
 
142
                                }
 
143
                        }
 
144
                        
 
145
                        public override void VisitQueryExpression(QueryExpression queryExpression)
 
146
                        {
 
147
                                base.VisitQueryExpression(queryExpression);
 
148
                                foreach (var clause in queryExpression.Clauses) {
 
149
                                        UseExtensionMethod(ctx.Resolve(clause));
 
150
                                }
 
151
                        }
 
152
                }
 
153
        }
 
154
}