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

« back to all changes in this revision

Viewing changes to contrib/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) 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, CSharpParsedFile parsedFile, CompilationUnit cu, TextLocation location,
34
 
                                                    CancellationToken cancellationToken = default(CancellationToken))
35
 
                {
36
 
                        return Resolve (new Lazy<ICompilation>(() => compilation), parsedFile, cu, location, cancellationToken);
37
 
                }
38
 
                public static ResolveResult Resolve(Lazy<ICompilation> compilation, CSharpParsedFile parsedFile, CompilationUnit cu, TextLocation location,
39
 
                                                    CancellationToken cancellationToken = default(CancellationToken))
40
 
                {
41
 
                        AstNode node;
42
 
                        return Resolve(compilation, parsedFile, cu, location, out node, cancellationToken);
43
 
                }
44
 
                
45
 
                public static ResolveResult Resolve (ICompilation compilation, CSharpParsedFile parsedFile, CompilationUnit cu, TextLocation location, out AstNode node,
46
 
                                                    CancellationToken cancellationToken = default(CancellationToken))
47
 
                {
48
 
                        return Resolve (new Lazy<ICompilation>(() => compilation), parsedFile, cu, location, out node, cancellationToken);
49
 
                }
50
 
                public static ResolveResult Resolve(Lazy<ICompilation> compilation, CSharpParsedFile parsedFile, CompilationUnit cu, TextLocation location, out AstNode node,
51
 
                                                    CancellationToken cancellationToken = default(CancellationToken))
52
 
                {
53
 
                        node = cu.GetNodeAt(location);
54
 
                        if (node == null)
55
 
                                return null;
56
 
                        if (CSharpAstResolver.IsUnresolvableNode(node)) {
57
 
                                if (node is Identifier) {
58
 
                                        node = node.Parent;
59
 
                                } else if (node.NodeType == NodeType.Token) {
60
 
                                        if (node.Parent is IndexerExpression || node.Parent is ConstructorInitializer) {
61
 
                                                // There's no other place where one could hover to see the indexer's tooltip,
62
 
                                                // so we need to resolve it when hovering over the '[' or ']'.
63
 
                                                // For constructor initializer, the same applies to the 'base'/'this' token.
64
 
                                                node = node.Parent;
65
 
                                        } else {
66
 
                                                return null;
67
 
                                        }
68
 
                                } else {
69
 
                                        // don't resolve arbitrary nodes - we don't want to show tooltips for everything
70
 
                                        return null;
71
 
                                }
72
 
                        } else {
73
 
                                // It's a resolvable node.
74
 
                                // However, we usually don't want to show the tooltip everywhere
75
 
                                // For example, hovering with the mouse over an empty line between two methods causes
76
 
                                // node==TypeDeclaration, but we don't want to show any tooltip.
77
 
                                
78
 
                                if (!node.GetChildByRole(Roles.Identifier).IsNull) {
79
 
                                        // We'll suppress the tooltip for resolvable nodes if there is an identifier that
80
 
                                        // could be hovered over instead:
81
 
                                        return null;
82
 
                                }
83
 
                        }
84
 
                        if (node == null)
85
 
                                return null;
86
 
                        
87
 
                        if (node.Parent is ObjectCreateExpression && node.Role == Roles.Type) {
88
 
                                node = node.Parent;
89
 
                        }
90
 
                        
91
 
                        InvocationExpression parentInvocation = null;
92
 
                        if ((node is IdentifierExpression || node is MemberReferenceExpression || node is PointerReferenceExpression) && node.Role != Roles.Argument) {
93
 
                                // we also need to resolve the invocation
94
 
                                parentInvocation = node.Parent as InvocationExpression;
95
 
                        }
96
 
                        
97
 
                        // TODO: I think we should provide an overload so that an existing CSharpAstResolver can be reused
98
 
                        CSharpAstResolver resolver = new CSharpAstResolver(compilation.Value, cu, parsedFile);
99
 
                        ResolveResult rr = resolver.Resolve(node, cancellationToken);
100
 
                        if (rr is MethodGroupResolveResult && parentInvocation != null)
101
 
                                return resolver.Resolve(parentInvocation);
102
 
                        else
103
 
                                return rr;
104
 
                }
105
 
        }
106
 
}