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 | |
---|
47 | public abstract TextLocation Location { get; } |
---|
48 | |
---|
49 | public TypeSystemAstBuilder CreateTypeSystemAstBuilder() |
---|
50 | { |
---|
51 | var astNode = GetNode() ?? RootNode.GetNodeAt(Location) ?? RootNode; |
---|
52 | var csResolver = Resolver.GetResolverStateBefore(astNode); |
---|
53 | return new TypeSystemAstBuilder(csResolver); |
---|
54 | } |
---|
55 | |
---|
56 | public virtual AstType CreateShortType (IType fullType) |
---|
57 | { |
---|
58 | var builder = CreateTypeSystemAstBuilder(); |
---|
59 | return builder.ConvertType(fullType); |
---|
60 | } |
---|
61 | |
---|
62 | public virtual AstType CreateShortType(string ns, string name, int typeParameterCount = 0) |
---|
63 | { |
---|
64 | var builder = CreateTypeSystemAstBuilder(); |
---|
65 | return builder.ConvertType(new TopLevelTypeName(ns, name, typeParameterCount)); |
---|
66 | } |
---|
67 | |
---|
68 | public virtual IEnumerable<AstNode> GetSelectedNodes() |
---|
69 | { |
---|
70 | if (!IsSomethingSelected) { |
---|
71 | return Enumerable.Empty<AstNode> (); |
---|
72 | } |
---|
73 | |
---|
74 | return RootNode.GetNodesBetween(SelectionStart, SelectionEnd); |
---|
75 | } |
---|
76 | |
---|
77 | public AstNode GetNode () |
---|
78 | { |
---|
79 | return RootNode.GetNodeAt (Location); |
---|
80 | } |
---|
81 | |
---|
82 | public AstNode GetNode (Predicate<AstNode> pred) |
---|
83 | { |
---|
84 | return RootNode.GetNodeAt (Location, pred); |
---|
85 | } |
---|
86 | |
---|
87 | public T GetNode<T> () where T : AstNode |
---|
88 | { |
---|
89 | return RootNode.GetNodeAt<T> (Location); |
---|
90 | } |
---|
91 | |
---|
92 | public CSharpTypeResolveContext GetTypeResolveContext() |
---|
93 | { |
---|
94 | if (UnresolvedFile != null) |
---|
95 | return UnresolvedFile.GetTypeResolveContext(Compilation, Location); |
---|
96 | else |
---|
97 | return null; |
---|
98 | } |
---|
99 | |
---|
100 | #region Naming |
---|
101 | public virtual string GetNameProposal (string name, bool camelCase = true) |
---|
102 | { |
---|
103 | return GetNameProposal(name, Location, camelCase); |
---|
104 | } |
---|
105 | #endregion |
---|
106 | } |
---|
107 | } |
---|
108 | |
---|