1
// Copyright (c) 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, CSharpParsedFile parsedFile, CompilationUnit cu, TextLocation location,
34
CancellationToken cancellationToken = default(CancellationToken))
36
return Resolve (new Lazy<ICompilation>(() => compilation), parsedFile, cu, location, cancellationToken);
38
public static ResolveResult Resolve(Lazy<ICompilation> compilation, CSharpParsedFile parsedFile, CompilationUnit cu, TextLocation location,
39
CancellationToken cancellationToken = default(CancellationToken))
42
return Resolve(compilation, parsedFile, cu, location, out node, cancellationToken);
45
public static ResolveResult Resolve (ICompilation compilation, CSharpParsedFile parsedFile, CompilationUnit cu, TextLocation location, out AstNode node,
46
CancellationToken cancellationToken = default(CancellationToken))
48
return Resolve (new Lazy<ICompilation>(() => compilation), parsedFile, cu, location, out node, cancellationToken);
50
public static ResolveResult Resolve(Lazy<ICompilation> compilation, CSharpParsedFile parsedFile, CompilationUnit cu, TextLocation location, out AstNode node,
51
CancellationToken cancellationToken = default(CancellationToken))
53
node = cu.GetNodeAt(location);
56
if (CSharpAstResolver.IsUnresolvableNode(node)) {
57
if (node is Identifier) {
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.
69
// don't resolve arbitrary nodes - we don't want to show tooltips for everything
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.
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:
87
if (node.Parent is ObjectCreateExpression && node.Role == Roles.Type) {
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;
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);