Free cookie consent management tool by TermsFeed Policy Generator

source: branches/CodeEditor/HeuristicLab.ExtLibs/HeuristicLab.NRefactory/5.5.0/NRefactory.CSharp-5.5.0/Refactoring/RefactoringContext.cs @ 11700

Last change on this file since 11700 was 11700, checked in by jkarder, 9 years ago

#2077: created branch and added first version

File size: 3.3 KB
Line 
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
27using System;
28using System.Linq;
29using System.Threading;
30using ICSharpCode.NRefactory.CSharp.Resolver;
31using ICSharpCode.NRefactory.CSharp.TypeSystem;
32using ICSharpCode.NRefactory.Semantics;
33using ICSharpCode.NRefactory.TypeSystem;
34using ICSharpCode.NRefactory.TypeSystem.Implementation;
35using ICSharpCode.NRefactory.Editor;
36using System.Collections.Generic;
37
38namespace 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
Note: See TracBrowser for help on using the repository browser.