5
// Mike Krüger <mkrueger@novell.com>
7
// Copyright (c) 2011 Mike Krüger <mkrueger@novell.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 braces", Description = "Removes redundant braces around a statement.")]
34
public class RemoveBracesAction : ICodeActionProvider
36
public IEnumerable<CodeAction> GetActions(RefactoringContext context)
38
var block = GetBlockStatement(context);
43
yield return new CodeAction (context.TranslateString("Remove braces"), script => {
44
var start = script.GetCurrentOffset (block.LBraceToken.GetPrevNode ().EndLocation);
45
var end = script.GetCurrentOffset (block.LBraceToken.EndLocation);
48
script.RemoveText (start, end - start);
49
script.Remove(block.LBraceToken);
50
script.Remove(block.RBraceToken);
51
script.FormatText(block.Parent);
55
static BlockStatement GetBlockStatement(RefactoringContext context)
57
var block = context.GetNode<BlockStatement>();
58
if (block == null || block.LBraceToken.IsNull || block.RBraceToken.IsNull)
60
if (!(block.LBraceToken.IsInside(context.Location) || block.RBraceToken.IsInside(context.Location)))
62
if (!(block.Parent is Statement) || block.Parent is TryCatchStatement)
64
if (block.Statements.Count != 1 || block.Statements.First () is VariableDeclarationStatement)