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

« back to all changes in this revision

Viewing changes to external/nrefactory/ICSharpCode.NRefactory.CSharp/Resolver/ResolveAtLocation.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
// Copyright (c) 2010-2013 AlphaSierraPapa for the SharpDevelop Team
 
2
// 
 
3
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
 
4
// software and associated documentation files (the "Software"), to deal in the Software
 
5
// without restriction, including without limitation the rights to use, copy, modify, merge,
 
6
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
 
7
// to whom the Software is furnished to do so, subject to the following conditions:
 
8
// 
 
9
// The above copyright notice and this permission notice shall be included in all copies or
 
10
// substantial portions of the Software.
 
11
// 
 
12
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
 
13
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 
14
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
 
15
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 
16
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 
17
// DEALINGS IN THE SOFTWARE.
 
18
 
 
19
using System;
 
20
using System.Threading;
 
21
using ICSharpCode.NRefactory.CSharp.TypeSystem;
 
22
using ICSharpCode.NRefactory.Semantics;
 
23
using ICSharpCode.NRefactory.TypeSystem;
 
24
 
 
25
namespace ICSharpCode.NRefactory.CSharp.Resolver
 
26
{
 
27
        /// <summary>
 
28
        /// Helper class that resolves the node at a specified location.
 
29
        /// Can be used for implementing tool tips.
 
30
        /// </summary>
 
31
        public static class ResolveAtLocation
 
32
        {
 
33
                public static ResolveResult Resolve (ICompilation compilation, CSharpUnresolvedFile unresolvedFile, SyntaxTree syntaxTree, TextLocation location,
 
34
                                                    CancellationToken cancellationToken = default(CancellationToken))
 
35
                {
 
36
                        return Resolve (new Lazy<ICompilation>(() => compilation), unresolvedFile, syntaxTree, location, cancellationToken);
 
37
                }
 
38
                public static ResolveResult Resolve(Lazy<ICompilation> compilation, CSharpUnresolvedFile unresolvedFile, SyntaxTree syntaxTree, TextLocation location,
 
39
                                                    CancellationToken cancellationToken = default(CancellationToken))
 
40
                {
 
41
                        AstNode node;
 
42
                        return Resolve(compilation, unresolvedFile, syntaxTree, location, out node, cancellationToken);
 
43
                }
 
44
                
 
45
                public static ResolveResult Resolve (ICompilation compilation, CSharpUnresolvedFile unresolvedFile, SyntaxTree syntaxTree, TextLocation location, out AstNode node,
 
46
                                                    CancellationToken cancellationToken = default(CancellationToken))
 
47
                {
 
48
                        return Resolve (new Lazy<ICompilation>(() => compilation), unresolvedFile, syntaxTree, location, out node, cancellationToken);
 
49
                }
 
50
                public static ResolveResult Resolve(Lazy<ICompilation> compilation, CSharpUnresolvedFile unresolvedFile, SyntaxTree syntaxTree, TextLocation location, out AstNode node,
 
51
                                                    CancellationToken cancellationToken = default(CancellationToken))
 
52
                {
 
53
                        node = syntaxTree.GetNodeAt(location);
 
54
                        if (node == null || node is ArrayInitializerExpression)
 
55
                                return null;
 
56
                        if (node.Parent is UsingAliasDeclaration && node.Role == UsingAliasDeclaration.AliasRole) {
 
57
                                var r = new CSharpAstResolver(compilation.Value, syntaxTree, unresolvedFile);
 
58
                                return r.Resolve(((UsingAliasDeclaration)node.Parent).Import, cancellationToken);
 
59
                        }
 
60
                        if (CSharpAstResolver.IsUnresolvableNode(node)) {
 
61
                                if (node is Identifier) {
 
62
                                        node = node.Parent;
 
63
                                } else if (node.NodeType == NodeType.Token) {
 
64
                                        if (node.Parent is IndexerExpression || node.Parent is ConstructorInitializer) {
 
65
                                                // There's no other place where one could hover to see the indexer's tooltip,
 
66
                                                // so we need to resolve it when hovering over the '[' or ']'.
 
67
                                                // For constructor initializer, the same applies to the 'base'/'this' token.
 
68
                                                node = node.Parent;
 
69
                                        } else {
 
70
                                                return null;
 
71
                                        }
 
72
                                } else {
 
73
                                        // don't resolve arbitrary nodes - we don't want to show tooltips for everything
 
74
                                        return null;
 
75
                                }
 
76
                        } else {
 
77
                                // It's a resolvable node.
 
78
                                // However, we usually don't want to show the tooltip everywhere
 
79
                                // For example, hovering with the mouse over an empty line between two methods causes
 
80
                                // node==TypeDeclaration, but we don't want to show any tooltip.
 
81
                                
 
82
                                if (!node.GetChildByRole(Roles.Identifier).IsNull) {
 
83
                                        // We'll suppress the tooltip for resolvable nodes if there is an identifier that
 
84
                                        // could be hovered over instead:
 
85
                                        return null;
 
86
                                }
 
87
                        }
 
88
 
 
89
                        if (node == null)
 
90
                                return null;
 
91
                        
 
92
                        if (node.Parent is ObjectCreateExpression && node.Role == Roles.Type) {
 
93
                                node = node.Parent;
 
94
                        }
 
95
                        
 
96
                        InvocationExpression parentInvocation = null;
 
97
                        if ((node is IdentifierExpression || node is MemberReferenceExpression || node is PointerReferenceExpression) && node.Role != Roles.Argument) {
 
98
                                // we also need to resolve the invocation
 
99
                                parentInvocation = node.Parent as InvocationExpression;
 
100
                        }
 
101
                        
 
102
                        // TODO: I think we should provide an overload so that an existing CSharpAstResolver can be reused
 
103
                        CSharpAstResolver resolver = new CSharpAstResolver(compilation.Value, syntaxTree, unresolvedFile);
 
104
                        ResolveResult rr = resolver.Resolve(node, cancellationToken);
 
105
                        MethodGroupResolveResult mgrr = rr as MethodGroupResolveResult;
 
106
                        if (mgrr != null) {
 
107
                                // For method groups, resolve the parent invocation instead.
 
108
                                if (parentInvocation != null)
 
109
                                        return resolver.Resolve(parentInvocation);
 
110
                                if (node is Expression) {
 
111
                                        // If it's not an invocation, try if it's a conversion to a delegate type:
 
112
                                        Conversion c = resolver.GetConversion((Expression)node, cancellationToken);
 
113
                                        if (c.IsMethodGroupConversion)
 
114
                                                return new MemberResolveResult(mgrr.TargetResult, c.Method);
 
115
                                }
 
116
                        }
 
117
                        return rr;
 
118
                }
 
119
        }
 
120
}