1 | // |
---|
2 | // BaseRefactoringContext.cs |
---|
3 | // |
---|
4 | // Author: |
---|
5 | // Mike Krüger <mkrueger@xamarin.com> |
---|
6 | // |
---|
7 | // Copyright (c) 2012 Xamarin <http://xamarin.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.ComponentModel.Design; |
---|
37 | using ICSharpCode.NRefactory.CSharp.Analysis; |
---|
38 | using ICSharpCode.NRefactory.Utils; |
---|
39 | using System.Collections.Generic; |
---|
40 | using ICSharpCode.NRefactory.Analysis; |
---|
41 | |
---|
42 | namespace ICSharpCode.NRefactory.CSharp.Refactoring |
---|
43 | { |
---|
44 | public abstract class BaseRefactoringContext : IServiceProvider |
---|
45 | { |
---|
46 | readonly CSharpAstResolver resolver; |
---|
47 | readonly CancellationToken cancellationToken; |
---|
48 | |
---|
49 | public virtual bool Supports(Version version) |
---|
50 | { |
---|
51 | return true; |
---|
52 | } |
---|
53 | |
---|
54 | /// <summary> |
---|
55 | /// Gets the default namespace which should be defined in this file. |
---|
56 | /// </summary> |
---|
57 | public abstract string DefaultNamespace { |
---|
58 | get; |
---|
59 | } |
---|
60 | |
---|
61 | /// <summary> |
---|
62 | /// Gets a value indicating if 'var' keyword should be used or explicit types. |
---|
63 | /// </summary> |
---|
64 | public virtual bool UseExplicitTypes { |
---|
65 | get; |
---|
66 | set; |
---|
67 | } |
---|
68 | |
---|
69 | public CancellationToken CancellationToken { |
---|
70 | get { return cancellationToken; } |
---|
71 | } |
---|
72 | |
---|
73 | public virtual AstNode RootNode { |
---|
74 | get { |
---|
75 | return resolver.RootNode; |
---|
76 | } |
---|
77 | } |
---|
78 | |
---|
79 | public CSharpAstResolver Resolver { |
---|
80 | get { |
---|
81 | return resolver; |
---|
82 | } |
---|
83 | } |
---|
84 | |
---|
85 | public virtual CSharpUnresolvedFile UnresolvedFile { |
---|
86 | get { |
---|
87 | return resolver.UnresolvedFile; |
---|
88 | } |
---|
89 | } |
---|
90 | |
---|
91 | public ICompilation Compilation { |
---|
92 | get { return resolver.Compilation; } |
---|
93 | } |
---|
94 | |
---|
95 | /// <summary> |
---|
96 | /// Gets the type graph for the current compilation. |
---|
97 | /// </summary> |
---|
98 | public virtual TypeGraph TypeGraph { |
---|
99 | get { return new TypeGraph(Compilation.Assemblies); } |
---|
100 | } |
---|
101 | |
---|
102 | public BaseRefactoringContext (ICSharpCode.NRefactory.CSharp.Resolver.CSharpAstResolver resolver, System.Threading.CancellationToken cancellationToken) |
---|
103 | { |
---|
104 | this.resolver = resolver; |
---|
105 | this.cancellationToken = cancellationToken; |
---|
106 | this.referenceFinder = new LocalReferenceFinder(resolver); |
---|
107 | } |
---|
108 | |
---|
109 | |
---|
110 | #region Resolving |
---|
111 | public ResolveResult Resolve (AstNode node) |
---|
112 | { |
---|
113 | return resolver.Resolve (node, cancellationToken); |
---|
114 | } |
---|
115 | |
---|
116 | public CSharpResolver GetResolverStateBefore(AstNode node) |
---|
117 | { |
---|
118 | return resolver.GetResolverStateBefore (node, cancellationToken); |
---|
119 | } |
---|
120 | |
---|
121 | public CSharpResolver GetResolverStateAfter(AstNode node) |
---|
122 | { |
---|
123 | return resolver.GetResolverStateAfter (node, cancellationToken); |
---|
124 | } |
---|
125 | |
---|
126 | public IType ResolveType (AstType type) |
---|
127 | { |
---|
128 | return resolver.Resolve (type, cancellationToken).Type; |
---|
129 | } |
---|
130 | |
---|
131 | public IType GetExpectedType (Expression expression) |
---|
132 | { |
---|
133 | return resolver.GetExpectedType(expression, cancellationToken); |
---|
134 | } |
---|
135 | |
---|
136 | public Conversion GetConversion (Expression expression) |
---|
137 | { |
---|
138 | return resolver.GetConversion(expression, cancellationToken); |
---|
139 | } |
---|
140 | |
---|
141 | public TypeSystemAstBuilder CreateTypeSystemAstBuilder(AstNode node) |
---|
142 | { |
---|
143 | var csResolver = resolver.GetResolverStateBefore(node); |
---|
144 | return new TypeSystemAstBuilder(csResolver); |
---|
145 | } |
---|
146 | #endregion |
---|
147 | |
---|
148 | #region Code Analyzation |
---|
149 | /// <summary> |
---|
150 | /// Creates a new definite assignment analysis object with a given root statement. |
---|
151 | /// </summary> |
---|
152 | /// <returns> |
---|
153 | /// The definite assignment analysis object. |
---|
154 | /// </returns> |
---|
155 | /// <param name='root'> |
---|
156 | /// The root statement. |
---|
157 | /// </param> |
---|
158 | public DefiniteAssignmentAnalysis CreateDefiniteAssignmentAnalysis (Statement root) |
---|
159 | { |
---|
160 | return new DefiniteAssignmentAnalysis (root, resolver, CancellationToken); |
---|
161 | } |
---|
162 | |
---|
163 | /// <summary> |
---|
164 | /// Creates a new reachability analysis object with a given statement. |
---|
165 | /// </summary> |
---|
166 | /// <param name="statement"> |
---|
167 | /// The statement to start the analysis. |
---|
168 | /// </param> |
---|
169 | /// <param name="recursiveDetectorVisitor"> |
---|
170 | /// TODO. |
---|
171 | /// </param> |
---|
172 | /// <returns> |
---|
173 | /// The reachability analysis object. |
---|
174 | /// </returns> |
---|
175 | public ReachabilityAnalysis CreateReachabilityAnalysis (Statement statement, ReachabilityAnalysis.RecursiveDetectorVisitor recursiveDetectorVisitor = null) |
---|
176 | { |
---|
177 | return ReachabilityAnalysis.Create (statement, resolver, recursiveDetectorVisitor, CancellationToken); |
---|
178 | } |
---|
179 | |
---|
180 | /// <summary> |
---|
181 | /// Parses a composite format string. |
---|
182 | /// </summary> |
---|
183 | /// <returns> |
---|
184 | /// The format string parsing result. |
---|
185 | /// </returns> |
---|
186 | public virtual FormatStringParseResult ParseFormatString(string source) |
---|
187 | { |
---|
188 | return new CompositeFormatStringParser().Parse(source); |
---|
189 | } |
---|
190 | |
---|
191 | LocalReferenceFinder referenceFinder; |
---|
192 | |
---|
193 | public IList<ReferenceResult> FindReferences(AstNode rootNode, IVariable variable) |
---|
194 | { |
---|
195 | return referenceFinder.FindReferences(rootNode, variable); |
---|
196 | } |
---|
197 | |
---|
198 | #endregion |
---|
199 | |
---|
200 | |
---|
201 | #region Naming |
---|
202 | public virtual string GetNameProposal (string name, TextLocation loc, bool camelCase = true) |
---|
203 | { |
---|
204 | string baseName = (camelCase ? char.ToLower (name [0]) : char.ToUpper (name [0])) + name.Substring (1); |
---|
205 | |
---|
206 | var type = RootNode.GetNodeAt<TypeDeclaration>(loc); |
---|
207 | if (type == null) |
---|
208 | return baseName; |
---|
209 | |
---|
210 | int number = -1; |
---|
211 | string proposedName; |
---|
212 | do { |
---|
213 | proposedName = AppendNumberToName (baseName, number++); |
---|
214 | } while (type.Members.Select (m => m.GetChildByRole (Roles.Identifier)).Any (n => n.Name == proposedName)); |
---|
215 | return proposedName; |
---|
216 | } |
---|
217 | |
---|
218 | public virtual string GetLocalNameProposal (string name, TextLocation loc, bool camelCase = true) |
---|
219 | { |
---|
220 | string baseName = (camelCase ? char.ToLower (name [0]) : char.ToUpper (name [0])) + name.Substring (1); |
---|
221 | var node = RootNode.GetNodeAt(loc); |
---|
222 | if (node == null) |
---|
223 | return baseName; |
---|
224 | |
---|
225 | var context = GetResolverStateBefore (node); |
---|
226 | int number = -1; |
---|
227 | string proposedName; |
---|
228 | do { |
---|
229 | proposedName = AppendNumberToName (baseName, number++); |
---|
230 | } while (!(context.ResolveSimpleName (proposedName, EmptyList<IType>.Instance) is UnknownIdentifierResolveResult)); |
---|
231 | return proposedName; |
---|
232 | } |
---|
233 | |
---|
234 | static string AppendNumberToName (string baseName, int number) |
---|
235 | { |
---|
236 | return baseName + (number > 0 ? (number + 1).ToString () : ""); |
---|
237 | } |
---|
238 | #endregion |
---|
239 | |
---|
240 | #region Text stuff |
---|
241 | public virtual TextEditorOptions TextEditorOptions { |
---|
242 | get { |
---|
243 | return TextEditorOptions.Default; |
---|
244 | } |
---|
245 | } |
---|
246 | |
---|
247 | public virtual bool IsSomethingSelected { |
---|
248 | get { |
---|
249 | return SelectionStart != TextLocation.Empty; |
---|
250 | } |
---|
251 | } |
---|
252 | |
---|
253 | public virtual string SelectedText { |
---|
254 | get { return string.Empty; } |
---|
255 | } |
---|
256 | |
---|
257 | public virtual TextLocation SelectionStart { |
---|
258 | get { |
---|
259 | return TextLocation.Empty; |
---|
260 | } |
---|
261 | } |
---|
262 | |
---|
263 | public virtual TextLocation SelectionEnd { |
---|
264 | get { |
---|
265 | return TextLocation.Empty; |
---|
266 | } |
---|
267 | } |
---|
268 | |
---|
269 | public abstract int GetOffset (TextLocation location); |
---|
270 | |
---|
271 | public abstract IDocumentLine GetLineByOffset (int offset); |
---|
272 | |
---|
273 | public int GetOffset (int line, int col) |
---|
274 | { |
---|
275 | return GetOffset (new TextLocation (line, col)); |
---|
276 | } |
---|
277 | |
---|
278 | public abstract TextLocation GetLocation (int offset); |
---|
279 | |
---|
280 | public abstract string GetText (int offset, int length); |
---|
281 | |
---|
282 | public abstract string GetText (ISegment segment); |
---|
283 | #endregion |
---|
284 | |
---|
285 | |
---|
286 | /// <summary> |
---|
287 | /// Translates the english input string to the context language. |
---|
288 | /// </summary> |
---|
289 | /// <returns> |
---|
290 | /// The translated string. |
---|
291 | /// </returns> |
---|
292 | public virtual string TranslateString(string str) |
---|
293 | { |
---|
294 | return str; |
---|
295 | } |
---|
296 | |
---|
297 | #region IServiceProvider implementation |
---|
298 | IServiceContainer services = new ServiceContainer(); |
---|
299 | |
---|
300 | /// <summary> |
---|
301 | /// Gets a service container used to associate services with this context. |
---|
302 | /// </summary> |
---|
303 | public IServiceContainer Services { |
---|
304 | get { return services; } |
---|
305 | protected set { services = value; } |
---|
306 | } |
---|
307 | |
---|
308 | /// <summary> |
---|
309 | /// Retrieves a service from the refactoring context. |
---|
310 | /// If the service is not found in the <see cref="Services"/> container. |
---|
311 | /// </summary> |
---|
312 | public object GetService(Type serviceType) |
---|
313 | { |
---|
314 | return services.GetService(serviceType); |
---|
315 | } |
---|
316 | #endregion |
---|
317 | } |
---|
318 | |
---|
319 | } |
---|