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

« back to all changes in this revision

Viewing changes to external/nrefactory/ICSharpCode.NRefactory.CSharp/Refactoring/RefactoringContext.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
// RefactoringContext.cs
 
3
//
 
4
// Author:
 
5
//       Mike Krüger <mkrueger@novell.com>
 
6
// 
 
7
// Copyright (c) 2011 Mike Krüger <mkrueger@novell.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
 
 
27
using System;
 
28
using System.Linq;
 
29
using System.Threading;
 
30
using ICSharpCode.NRefactory.CSharp.Resolver;
 
31
using ICSharpCode.NRefactory.CSharp.TypeSystem;
 
32
using ICSharpCode.NRefactory.Semantics;
 
33
using ICSharpCode.NRefactory.TypeSystem;
 
34
using ICSharpCode.NRefactory.TypeSystem.Implementation;
 
35
using ICSharpCode.NRefactory.Editor;
 
36
using System.Collections.Generic;
 
37
 
 
38
namespace ICSharpCode.NRefactory.CSharp.Refactoring
 
39
{
 
40
        public abstract class RefactoringContext : BaseRefactoringContext
 
41
        {
 
42
                public RefactoringContext(CSharpAstResolver resolver, CancellationToken cancellationToken) : base  (resolver, cancellationToken)
 
43
                {
 
44
                }
 
45
 
 
46
                public abstract TextLocation Location { get; }
 
47
                
 
48
                public TypeSystemAstBuilder CreateTypeSytemAstBuilder()
 
49
                {
 
50
                        var csResolver = Resolver.GetResolverStateBefore(GetNode());
 
51
                        return new TypeSystemAstBuilder(csResolver);
 
52
                }
 
53
                
 
54
                public virtual AstType CreateShortType (IType fullType)
 
55
                {
 
56
                        var builder = CreateTypeSytemAstBuilder();
 
57
                        return builder.ConvertType(fullType);
 
58
                }
 
59
                
 
60
                public virtual AstType CreateShortType(string ns, string name, int typeParameterCount = 0)
 
61
                {
 
62
                        var builder = CreateTypeSytemAstBuilder();
 
63
                        return builder.ConvertType(new TopLevelTypeName(ns, name, typeParameterCount));
 
64
                }
 
65
 
 
66
                public virtual IEnumerable<AstNode> GetSelectedNodes()
 
67
                {
 
68
                        if (!IsSomethingSelected) {
 
69
                                return Enumerable.Empty<AstNode> ();
 
70
                        }
 
71
                        
 
72
                        return RootNode.GetNodesBetween(SelectionStart, SelectionEnd);
 
73
                }
 
74
 
 
75
                public AstNode GetNode ()
 
76
                {
 
77
                        return RootNode.GetNodeAt (Location);
 
78
                }
 
79
                
 
80
                public AstNode GetNode (Predicate<AstNode> pred)
 
81
                {
 
82
                        return RootNode.GetNodeAt (Location, pred);
 
83
                }
 
84
                
 
85
                public T GetNode<T> () where T : AstNode
 
86
                {
 
87
                        return RootNode.GetNodeAt<T> (Location);
 
88
                }
 
89
                
 
90
                #region Text stuff
 
91
                public virtual TextEditorOptions TextEditorOptions {
 
92
                        get {
 
93
                                return TextEditorOptions.Default;
 
94
                        }
 
95
                }
 
96
                
 
97
                public virtual bool IsSomethingSelected {
 
98
                        get {
 
99
                                return SelectionStart != TextLocation.Empty;
 
100
                        }
 
101
                }
 
102
                
 
103
                public virtual string SelectedText {
 
104
                        get { return string.Empty; }
 
105
                }
 
106
                
 
107
                public virtual TextLocation SelectionStart {
 
108
                        get {
 
109
                                return TextLocation.Empty;
 
110
                        }
 
111
                }
 
112
                
 
113
                public virtual TextLocation SelectionEnd {
 
114
                        get {
 
115
                                return TextLocation.Empty;
 
116
                        }
 
117
                }
 
118
 
 
119
                public abstract int GetOffset (TextLocation location);
 
120
 
 
121
                public abstract IDocumentLine GetLineByOffset (int offset);
 
122
                
 
123
                public int GetOffset (int line, int col)
 
124
                {
 
125
                        return GetOffset (new TextLocation (line, col));
 
126
                }
 
127
 
 
128
                public abstract TextLocation GetLocation (int offset);
 
129
 
 
130
                public abstract string GetText (int offset, int length);
 
131
 
 
132
                public abstract string GetText (ISegment segment);
 
133
                #endregion
 
134
 
 
135
                #region Naming
 
136
                public virtual string GetNameProposal (string name, bool camelCase = true)
 
137
                {
 
138
                        string baseName = (camelCase ? char.ToLower (name [0]) : char.ToUpper (name [0])) + name.Substring (1);
 
139
                        
 
140
                        var type = GetNode<TypeDeclaration> ();
 
141
                        if (type == null)
 
142
                                return baseName;
 
143
                        
 
144
                        int number = -1;
 
145
                        string proposedName;
 
146
                        do {
 
147
                                proposedName = AppendNumberToName (baseName, number++);
 
148
                        } while (type.Members.Select (m => m.GetChildByRole (Roles.Identifier)).Any (n => n.Name == proposedName));
 
149
                        return proposedName;
 
150
                }
 
151
                
 
152
                static string AppendNumberToName (string baseName, int number)
 
153
                {
 
154
                        return baseName + (number > 0 ? (number + 1).ToString () : "");
 
155
                }
 
156
                #endregion
 
157
        }
 
158
}
 
159