2
// BaseRefactoringContext.cs
5
// Mike KrĆ¼ger <mkrueger@xamarin.com>
7
// Copyright (c) 2012 Xamarin <http://xamarin.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
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;
41
namespace ICSharpCode.NRefactory.CSharp.Refactoring
43
public abstract class BaseRefactoringContext : IServiceProvider
45
readonly CSharpAstResolver resolver;
46
readonly CancellationToken cancellationToken;
48
public virtual bool Supports(Version version)
54
/// Gets a value indicating if 'var' keyword should be used or explicit types.
56
public virtual bool UseExplicitTypes {
61
public CancellationToken CancellationToken {
62
get { return cancellationToken; }
65
public virtual AstNode RootNode {
67
return resolver.RootNode;
71
public CSharpAstResolver Resolver {
77
public virtual CSharpUnresolvedFile UnresolvedFile {
79
return resolver.UnresolvedFile;
83
public ICompilation Compilation {
84
get { return resolver.Compilation; }
87
public BaseRefactoringContext (ICSharpCode.NRefactory.CSharp.Resolver.CSharpAstResolver resolver, System.Threading.CancellationToken cancellationToken)
89
this.resolver = resolver;
90
this.cancellationToken = cancellationToken;
91
this.referenceFinder = new LocalReferenceFinder(resolver);
96
public ResolveResult Resolve (AstNode node)
98
return resolver.Resolve (node, cancellationToken);
101
public CSharpResolver GetResolverStateBefore(AstNode node)
103
return resolver.GetResolverStateBefore (node, cancellationToken);
106
public CSharpResolver GetResolverStateAfter(AstNode node)
108
return resolver.GetResolverStateAfter (node, cancellationToken);
111
public IType ResolveType (AstType type)
113
return resolver.Resolve (type, cancellationToken).Type;
116
public IType GetExpectedType (Expression expression)
118
return resolver.GetExpectedType(expression, cancellationToken);
121
public Conversion GetConversion (Expression expression)
123
return resolver.GetConversion(expression, cancellationToken);
126
public TypeSystemAstBuilder CreateTypeSytemAstBuilder(AstNode node)
128
var csResolver = resolver.GetResolverStateBefore(node);
129
return new TypeSystemAstBuilder(csResolver);
133
#region Code Analyzation
135
/// Creates a new definite assignment analysis object with a given root statement.
138
/// The definite assignment analysis object.
140
/// <param name='root'>
141
/// The root statement.
143
public DefiniteAssignmentAnalysis CreateDefiniteAssignmentAnalysis (Statement root)
145
return new DefiniteAssignmentAnalysis (root, resolver, CancellationToken);
149
/// Creates a new reachability analysis object with a given statement.
151
/// <param name="statement">
152
/// The statement to start the analysis.
155
/// The reachability analysis object.
157
public ReachabilityAnalysis CreateReachabilityAnalysis (Statement statement)
159
return ReachabilityAnalysis.Create (statement, resolver, CancellationToken);
163
/// Parses a composite format string.
166
/// The format string parsing result.
168
public virtual FormatStringParseResult ParseFormatString(string source)
170
return new CompositeFormatStringParser().Parse(source);
173
LocalReferenceFinder referenceFinder;
175
public IList<ReferenceResult> FindReferences(AstNode rootNode, IVariable variable)
177
return referenceFinder.FindReferences(rootNode, variable);
183
/// Translates the english input string to the context language.
186
/// The translated string.
188
public virtual string TranslateString(string str)
193
#region IServiceProvider implementation
194
IServiceContainer services = new ServiceContainer();
197
/// Gets a service container used to associate services with this context.
199
public IServiceContainer Services {
200
get { return services; }
201
protected set { services = value; }
205
/// Retrieves a service from the refactoring context.
206
/// If the service is not found in the <see cref="Services"/> container.
208
public object GetService(Type serviceType)
210
return services.GetService(serviceType);