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

« back to all changes in this revision

Viewing changes to external/nrefactory/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/SortUsingsAction.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
// SortUsingsAction.cs
 
3
//
 
4
// Author:
 
5
//      Lopatkin Ilja
 
6
//
 
7
// Copyright (c) 2012 Lopatkin Ilja
 
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
using ICSharpCode.NRefactory.Semantics;
 
31
using ICSharpCode.NRefactory.TypeSystem;
 
32
using ICSharpCode.NRefactory.TypeSystem.Implementation;
 
33
 
 
34
namespace ICSharpCode.NRefactory.CSharp.Refactoring
 
35
{
 
36
        [ContextAction("Sort usings", Description = "Sorts usings by their origin and then alphabetically.")]
 
37
        public class SortUsingsAction: ICodeActionProvider
 
38
        {
 
39
                public IEnumerable<CodeAction> GetActions(RefactoringContext context)
 
40
                {
 
41
                        var usingNode = FindUsingNodeAtCursor(context);
 
42
                        if (usingNode == null)
 
43
                                yield break;
 
44
 
 
45
                        yield return new CodeAction(context.TranslateString("Sort usings"), script =>
 
46
                        {
 
47
                                var blocks = EnumerateUsingBlocks(context.RootNode);
 
48
 
 
49
                                foreach (var block in blocks)
 
50
                                {
 
51
                                        var originalNodes = block.ToArray();
 
52
                                        var sortedNodes = UsingHelper.SortUsingBlock(originalNodes, context).ToArray();
 
53
 
 
54
                                        for (var i = 0; i < originalNodes.Length; ++i)
 
55
                                                script.Replace(originalNodes[i], sortedNodes[i].Clone());
 
56
                                }
 
57
                        }, usingNode);
 
58
                }
 
59
 
 
60
                private static AstNode FindUsingNodeAtCursor(RefactoringContext context)
 
61
                {
 
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);
 
66
 
 
67
                        var possibleNodes = new[] { locationAsIs, locationLeft }
 
68
                                .Select(_ => context.RootNode.GetNodeAt(_, IsUsingDeclaration));
 
69
                        var usingNode = possibleNodes.Where(_ => _ != null).Distinct().SingleOrDefault();
 
70
 
 
71
                        return usingNode;
 
72
                }
 
73
 
 
74
                private static bool IsUsingDeclaration(AstNode node)
 
75
                {
 
76
                        return node is UsingDeclaration || node is UsingAliasDeclaration;
 
77
                }
 
78
 
 
79
                private static IEnumerable<IEnumerable<AstNode>> EnumerateUsingBlocks(AstNode root)
 
80
                {
 
81
                        var alreadyAddedNodes = new HashSet<AstNode>();
 
82
 
 
83
                        foreach (var child in root.Descendants)
 
84
                                if (IsUsingDeclaration(child) && !alreadyAddedNodes.Contains(child)) {
 
85
                                        var blockNodes = EnumerateUsingBlockNodes(child);
 
86
 
 
87
                                        alreadyAddedNodes.UnionWith(blockNodes);
 
88
                                        yield return blockNodes;
 
89
                                }
 
90
                }
 
91
 
 
92
                private static IEnumerable<AstNode> EnumerateUsingBlockNodes(AstNode firstNode)
 
93
                {
 
94
                        for (var node = firstNode; IsUsingDeclaration(node); node = node.GetNextSibling (n => n.Role != Roles.NewLine))
 
95
                                yield return node;
 
96
                }
 
97
        }
 
98
}