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

« back to all changes in this revision

Viewing changes to src/addins/MonoDevelop.Refactoring/MonoDevelop.Refactoring/ExtensionMethods.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
// ExtensionMethods.cs
 
3
//
 
4
// Author:
 
5
//       Mike Krüger <mkrueger@xamarin.com>
 
6
//
 
7
// Copyright (c) 2013 Xamarin Inc. (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
using System;
 
27
using ICSharpCode.NRefactory.CSharp.Resolver;
 
28
using MonoDevelop.Ide.Gui;
 
29
using ICSharpCode.NRefactory.CSharp;
 
30
using ICSharpCode.NRefactory.CSharp.TypeSystem;
 
31
using ICSharpCode.NRefactory.TypeSystem;
 
32
using ICSharpCode.NRefactory.Semantics;
 
33
using System.Threading.Tasks;
 
34
 
 
35
namespace MonoDevelop.Refactoring
 
36
{
 
37
    public static class ExtensionMethods
 
38
    {
 
39
                class ResolverAnnotation
 
40
                {
 
41
                        public Task<CSharpAstResolver> Task;
 
42
                        public CSharpUnresolvedFile ParsedFile;
 
43
                }
 
44
 
 
45
                /// <summary>
 
46
                /// Returns a full C# syntax tree resolver which is shared between semantic highlighting, source analysis and refactoring.
 
47
                /// For code analysis tasks this should be used instead of generating an own resolver. Only exception is if a local resolving is done using a 
 
48
                /// resolve navigator.
 
49
                /// Note: The shared resolver is fully resolved.
 
50
                /// </summary>
 
51
                public static Task<CSharpAstResolver> GetSharedResolver (this Document document)
 
52
                {
 
53
                        var parsedDocument = document.ParsedDocument;
 
54
                        if (parsedDocument == null)
 
55
                                return null;
 
56
                        
 
57
                        var unit       = parsedDocument.GetAst<SyntaxTree> ();
 
58
                        var parsedFile = parsedDocument.ParsedFile as CSharpUnresolvedFile;
 
59
                        if (unit == null || parsedFile == null)
 
60
                                return null;
 
61
                        
 
62
                        var resolverAnnotation = document.Annotation<ResolverAnnotation> ();
 
63
 
 
64
                        if (resolverAnnotation != null) {
 
65
                                if (resolverAnnotation.ParsedFile == parsedFile)
 
66
                                        return resolverAnnotation.Task;
 
67
                                document.RemoveAnnotations<ResolverAnnotation> ();
 
68
                        }
 
69
 
 
70
                        var resolveTask = Task.Factory.StartNew (delegate {
 
71
                                var result = new CSharpAstResolver (document.Compilation, unit, parsedFile);
 
72
                                result.ApplyNavigator (new ConstantModeResolveVisitorNavigator (ResolveVisitorNavigationMode.Resolve, null));
 
73
                                return result;
 
74
                        });
 
75
                        document.AddAnnotation (new ResolverAnnotation {
 
76
                                Task = resolveTask,
 
77
                                ParsedFile = parsedFile
 
78
                        });
 
79
                        return resolveTask;
 
80
                }
 
81
 
 
82
                sealed class ConstantModeResolveVisitorNavigator : IResolveVisitorNavigator
 
83
                {
 
84
                        readonly ResolveVisitorNavigationMode mode;
 
85
                        readonly IResolveVisitorNavigator targetForResolveCalls;
 
86
 
 
87
                        public ConstantModeResolveVisitorNavigator(ResolveVisitorNavigationMode mode, IResolveVisitorNavigator targetForResolveCalls)
 
88
                        {
 
89
                                this.mode = mode;
 
90
                                this.targetForResolveCalls = targetForResolveCalls;
 
91
                        }
 
92
 
 
93
                        ResolveVisitorNavigationMode IResolveVisitorNavigator.Scan(AstNode node)
 
94
                        {
 
95
                                return mode;
 
96
                        }
 
97
 
 
98
                        void IResolveVisitorNavigator.Resolved(AstNode node, ResolveResult result)
 
99
                        {
 
100
                                if (targetForResolveCalls != null)
 
101
                                        targetForResolveCalls.Resolved(node, result);
 
102
                        }
 
103
 
 
104
                        void IResolveVisitorNavigator.ProcessConversion(Expression expression, ResolveResult result, Conversion conversion, IType targetType)
 
105
                        {
 
106
                                if (targetForResolveCalls != null)
 
107
                                        targetForResolveCalls.ProcessConversion(expression, result, conversion, targetType);
 
108
                        }
 
109
                }
 
110
 
 
111
    }
 
112
}
 
 
b'\\ No newline at end of file'