7
// Copyright (c) 2012 Lopatkin Ilja
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.Collections.Generic;
30
using ICSharpCode.NRefactory.Semantics;
31
using ICSharpCode.NRefactory.TypeSystem;
32
using ICSharpCode.NRefactory.TypeSystem.Implementation;
34
namespace ICSharpCode.NRefactory.CSharp.Refactoring
36
[ContextAction("Sort usings", Description = "Sorts usings by their origin and then alphabetically.")]
37
public class SortUsingsAction: ICodeActionProvider
39
public IEnumerable<CodeAction> GetActions(RefactoringContext context)
41
var usingNode = FindUsingNodeAtCursor(context);
42
if (usingNode == null)
45
yield return new CodeAction(context.TranslateString("Sort usings"), script =>
47
var blocks = EnumerateUsingBlocks(context.RootNode);
49
foreach (var block in blocks)
51
var originalNodes = block.ToArray();
52
var sortedNodes = UsingHelper.SortUsingBlock(originalNodes, context).ToArray();
54
for (var i = 0; i < originalNodes.Length; ++i)
55
script.Replace(originalNodes[i], sortedNodes[i].Clone());
60
private static AstNode FindUsingNodeAtCursor(RefactoringContext context)
62
// If cursor is inside using declaration
63
var locationAsIs = context.Location;
64
// If cursor is at end of line with using declaration
65
var locationLeft = new TextLocation(locationAsIs.Line, locationAsIs.Column - 1);
67
var possibleNodes = new[] { locationAsIs, locationLeft }
68
.Select(_ => context.RootNode.GetNodeAt(_, IsUsingDeclaration));
69
var usingNode = possibleNodes.Where(_ => _ != null).Distinct().SingleOrDefault();
74
private static bool IsUsingDeclaration(AstNode node)
76
return node is UsingDeclaration || node is UsingAliasDeclaration;
79
private static IEnumerable<IEnumerable<AstNode>> EnumerateUsingBlocks(AstNode root)
81
var alreadyAddedNodes = new HashSet<AstNode>();
83
foreach (var child in root.Descendants)
84
if (IsUsingDeclaration(child) && !alreadyAddedNodes.Contains(child)) {
85
var blockNodes = EnumerateUsingBlockNodes(child);
87
alreadyAddedNodes.UnionWith(blockNodes);
88
yield return blockNodes;
92
private static IEnumerable<AstNode> EnumerateUsingBlockNodes(AstNode firstNode)
94
for (var node = firstNode; IsUsingDeclaration(node); node = node.GetNextSibling (n => n.Role != Roles.NewLine))