1
// Copyright (c) 2010-2013 AlphaSierraPapa for the SharpDevelop Team
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:
9
// The above copyright notice and this permission notice shall be included in all copies or
10
// substantial portions of the Software.
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.
20
using System.Threading;
21
using ICSharpCode.NRefactory.CSharp.TypeSystem;
22
using ICSharpCode.NRefactory.Semantics;
23
using ICSharpCode.NRefactory.TypeSystem;
25
namespace ICSharpCode.NRefactory.CSharp.Resolver
28
/// Helper class that resolves the node at a specified location.
29
/// Can be used for implementing tool tips.
31
public static class ResolveAtLocation
33
public static ResolveResult Resolve (ICompilation compilation, CSharpUnresolvedFile unresolvedFile, SyntaxTree syntaxTree, TextLocation location,
34
CancellationToken cancellationToken = default(CancellationToken))
36
return Resolve (new Lazy<ICompilation>(() => compilation), unresolvedFile, syntaxTree, location, cancellationToken);
38
public static ResolveResult Resolve(Lazy<ICompilation> compilation, CSharpUnresolvedFile unresolvedFile, SyntaxTree syntaxTree, TextLocation location,
39
CancellationToken cancellationToken = default(CancellationToken))
42
return Resolve(compilation, unresolvedFile, syntaxTree, location, out node, cancellationToken);
45
public static ResolveResult Resolve (ICompilation compilation, CSharpUnresolvedFile unresolvedFile, SyntaxTree syntaxTree, TextLocation location, out AstNode node,
46
CancellationToken cancellationToken = default(CancellationToken))
48
return Resolve (new Lazy<ICompilation>(() => compilation), unresolvedFile, syntaxTree, location, out node, cancellationToken);
50
public static ResolveResult Resolve(Lazy<ICompilation> compilation, CSharpUnresolvedFile unresolvedFile, SyntaxTree syntaxTree, TextLocation location, out AstNode node,
51
CancellationToken cancellationToken = default(CancellationToken))
53
node = syntaxTree.GetNodeAt(location);
54
if (node == null || node is ArrayInitializerExpression)
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);
60
if (CSharpAstResolver.IsUnresolvableNode(node)) {
61
if (node is Identifier) {
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.
73
// don't resolve arbitrary nodes - we don't want to show tooltips for everything
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.
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:
92
if (node.Parent is ObjectCreateExpression && node.Role == Roles.Type) {
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;
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;
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);