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

« back to all changes in this revision

Viewing changes to external/nrefactory/ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/RedundantNamespaceUsageIssue.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
// RedundantNamespaceUsageInspector.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 System.Collections.Generic;
 
29
using System.Linq;
 
30
 
 
31
using ICSharpCode.NRefactory.Semantics;
 
32
using ICSharpCode.NRefactory.TypeSystem;
 
33
 
 
34
namespace ICSharpCode.NRefactory.CSharp.Refactoring
 
35
{
 
36
        /// <summary>
 
37
        /// Finds redundant namespace usages.
 
38
        /// </summary>
 
39
        [IssueDescription("Remove redundant namespace usages",
 
40
                          Description = "Removes namespace usages that are obsolete.",
 
41
                          Category = IssueCategories.Redundancies,
 
42
                          Severity = Severity.Hint,
 
43
                          IssueMarker = IssueMarker.GrayOut)]
 
44
        public class RedundantNamespaceUsageIssue : ICodeIssueProvider
 
45
        {
 
46
                public IEnumerable<CodeIssue> GetIssues(BaseRefactoringContext context)
 
47
                {
 
48
                        return new GatherVisitor(context, this).GetIssues();
 
49
                }
 
50
 
 
51
                class GatherVisitor : GatherVisitorBase<RedundantNamespaceUsageIssue>
 
52
                {
 
53
                        public GatherVisitor (BaseRefactoringContext ctx, RedundantNamespaceUsageIssue issueProvider) : base (ctx, issueProvider)
 
54
                        {
 
55
                        }
 
56
 
 
57
                        public override void VisitMemberReferenceExpression(MemberReferenceExpression memberReferenceExpression)
 
58
                        {
 
59
                                base.VisitMemberReferenceExpression(memberReferenceExpression);
 
60
                                HandleMemberReference(
 
61
                                        memberReferenceExpression, memberReferenceExpression.Target, memberReferenceExpression.MemberNameToken, memberReferenceExpression.TypeArguments, NameLookupMode.Expression,
 
62
                                        script => {
 
63
                                                script.Replace(memberReferenceExpression, RefactoringAstHelper.RemoveTarget(memberReferenceExpression));
 
64
                                        });
 
65
                        }
 
66
                        
 
67
                        public override void VisitMemberType(MemberType memberType)
 
68
                        {
 
69
                                base.VisitMemberType(memberType);
 
70
                                HandleMemberReference(
 
71
                                        memberType, memberType.Target, memberType.MemberNameToken, memberType.TypeArguments, memberType.GetNameLookupMode(),
 
72
                                        script => {
 
73
                                                script.Replace(memberType, RefactoringAstHelper.RemoveTarget(memberType));
 
74
                                        });
 
75
                        }
 
76
                        
 
77
                        void HandleMemberReference(AstNode wholeNode, AstNode targetNode, Identifier memberName, IEnumerable<AstType> typeArguments, NameLookupMode mode, Action<Script> action)
 
78
                        {
 
79
                                var result = ctx.Resolve(targetNode);
 
80
                                if (!(result is NamespaceResolveResult)) {
 
81
                                        return;
 
82
                                }
 
83
                                var wholeResult = ctx.Resolve(wholeNode);
 
84
                                if (!(wholeResult is TypeResolveResult)) {
 
85
                                        return;
 
86
                                }
 
87
 
 
88
                                var state = ctx.GetResolverStateBefore(wholeNode);
 
89
                                var resolvedTypeArguments = typeArguments.Select(ctx.ResolveType).ToList();
 
90
                                var lookupName = state.LookupSimpleNameOrTypeName(memberName.Name, resolvedTypeArguments, mode);
 
91
                                
 
92
                                if (lookupName is TypeResolveResult && !lookupName.IsError && wholeResult.Type.Equals(lookupName.Type)) {
 
93
                                        AddIssue(wholeNode.StartLocation, memberName.StartLocation, ctx.TranslateString("Remove redundant namespace usage"), action);
 
94
                                }
 
95
                        }
 
96
                }
 
97
        }
 
98
}