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

« back to all changes in this revision

Viewing changes to contrib/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/RemoveRegionAction.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
 
// RemoveRegion.cs
3
 
//  
4
 
// Author:
5
 
//       Mike Krüger <mkrueger@xamarin.com>
6
 
// 
7
 
// Copyright (c) 2012 Xamarin Inc. (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
 
using System;
27
 
using System.Linq;
28
 
using System.Threading;
29
 
using System.Collections.Generic;
30
 
 
31
 
namespace ICSharpCode.NRefactory.CSharp.Refactoring
32
 
{
33
 
        [ContextAction("Remove region", Description = "Removes a pre processor #region/#endregion directive.")]
34
 
        public class RemoveRegionAction : ICodeActionProvider
35
 
        {
36
 
                public IEnumerable<CodeAction> GetActions(RefactoringContext context)
37
 
                {
38
 
                        var directive = GetDirective(context);
39
 
                        if (directive == null) {
40
 
                                yield break;
41
 
                        }
42
 
                        var endDirective = DirectiveSearcher.GetEndRegion(context.RootNode, directive);
43
 
                        if (endDirective == null) {
44
 
                                yield break;
45
 
                        }
46
 
                        yield return new CodeAction (context.TranslateString("Remove region"), script => {
47
 
                                script.Remove (directive);
48
 
                                script.Remove (endDirective);
49
 
                        });
50
 
                }
51
 
                
52
 
                class DirectiveSearcher : DepthFirstAstVisitor
53
 
                {
54
 
                        readonly PreProcessorDirective regionDirective;
55
 
                        bool searchDirectives = false;
56
 
                        int depth;
57
 
                        PreProcessorDirective endregion;
58
 
                        
59
 
                        DirectiveSearcher (PreProcessorDirective regionDirective)
60
 
                        {
61
 
                                if (regionDirective == null)
62
 
                                        throw new ArgumentNullException ("regionDirective");
63
 
                                this.regionDirective = regionDirective;
64
 
                        }
65
 
                        
66
 
                        public static PreProcessorDirective GetEndRegion (AstNode rootNode, PreProcessorDirective regionDirective)
67
 
                        {
68
 
                                var visitor = new DirectiveSearcher (regionDirective);
69
 
                                rootNode.AcceptVisitor (visitor);
70
 
                                return visitor.endregion;
71
 
                        }
72
 
                        
73
 
                        protected override void VisitChildren (AstNode node)
74
 
                        {
75
 
                                if (endregion != null)
76
 
                                        return;
77
 
                                if (!searchDirectives && !regionDirective.Ancestors.Any (a => a == node))
78
 
                                        return;
79
 
                                base.VisitChildren (node);
80
 
                        }
81
 
                        
82
 
                        public override void VisitPreProcessorDirective (PreProcessorDirective preProcessorDirective)
83
 
                        {
84
 
                                if (searchDirectives) {
85
 
                                        if (preProcessorDirective.Type == PreProcessorDirectiveType.Region) {
86
 
                                                depth++;
87
 
                                        } else if (preProcessorDirective.Type == PreProcessorDirectiveType.Endregion) {
88
 
                                                depth--;
89
 
                                                if (depth == 0) {
90
 
                                                        endregion = preProcessorDirective;
91
 
                                                        searchDirectives = false;
92
 
                                                }
93
 
                                        }
94
 
                                } else if (preProcessorDirective == regionDirective) {
95
 
                                        searchDirectives = true;
96
 
                                        depth = 1;
97
 
                                }
98
 
                                
99
 
                                base.VisitPreProcessorDirective (preProcessorDirective);
100
 
                        }
101
 
                }
102
 
                
103
 
                static PreProcessorDirective GetDirective (RefactoringContext context)
104
 
                {
105
 
                        var directive = context.GetNode<PreProcessorDirective> ();
106
 
                        if (directive == null || directive.Type != PreProcessorDirectiveType.Region)
107
 
                                return null;
108
 
                        return directive;
109
 
                }
110
 
        }
111
 
}
112