5
// Mike Krüger <mkrueger@xamarin.com>
7
// Copyright (c) 2012 Xamarin Inc. (http://xamarin.com)
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:
16
// The above copyright notice and this permission notice shall be included in
17
// all copies or substantial portions of the Software.
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
28
using System.Threading;
29
using System.Collections.Generic;
31
namespace ICSharpCode.NRefactory.CSharp.Refactoring
33
[ContextAction("Remove region", Description = "Removes a pre processor #region/#endregion directive.")]
34
public class RemoveRegionAction : ICodeActionProvider
36
public IEnumerable<CodeAction> GetActions(RefactoringContext context)
38
var directive = GetDirective(context);
39
if (directive == null) {
42
var endDirective = DirectiveSearcher.GetEndRegion(context.RootNode, directive);
43
if (endDirective == null) {
46
yield return new CodeAction (context.TranslateString("Remove region"), script => {
47
script.Remove (directive);
48
script.Remove (endDirective);
52
class DirectiveSearcher : DepthFirstAstVisitor
54
readonly PreProcessorDirective regionDirective;
55
bool searchDirectives = false;
57
PreProcessorDirective endregion;
59
DirectiveSearcher (PreProcessorDirective regionDirective)
61
if (regionDirective == null)
62
throw new ArgumentNullException ("regionDirective");
63
this.regionDirective = regionDirective;
66
public static PreProcessorDirective GetEndRegion (AstNode rootNode, PreProcessorDirective regionDirective)
68
var visitor = new DirectiveSearcher (regionDirective);
69
rootNode.AcceptVisitor (visitor);
70
return visitor.endregion;
73
protected override void VisitChildren (AstNode node)
75
if (endregion != null)
77
if (!searchDirectives && !regionDirective.Ancestors.Any (a => a == node))
79
base.VisitChildren (node);
82
public override void VisitPreProcessorDirective (PreProcessorDirective preProcessorDirective)
84
if (searchDirectives) {
85
if (preProcessorDirective.Type == PreProcessorDirectiveType.Region) {
87
} else if (preProcessorDirective.Type == PreProcessorDirectiveType.Endregion) {
90
endregion = preProcessorDirective;
91
searchDirectives = false;
94
} else if (preProcessorDirective == regionDirective) {
95
searchDirectives = true;
99
base.VisitPreProcessorDirective (preProcessorDirective);
103
static PreProcessorDirective GetDirective (RefactoringContext context)
105
var directive = context.GetNode<PreProcessorDirective> ();
106
if (directive == null || directive.Type != PreProcessorDirectiveType.Region)