-
Notifications
You must be signed in to change notification settings - Fork 419
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Intellisense not showing methods from the Base class in Signature Help #1030
Changes from all commits
30c5092
de3de6a
ef0352a
eff068d
cf8f790
6362fda
997d104
41d4d2c
2c0bc6f
9cb58b4
07cee28
29b3803
2e1e0a0
cda2c28
a13a6b0
17f52cf
fbc4c5f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
// Adapted from https://github.com/dotnet/roslyn/blob/master/src/Workspaces/CSharp/Portable/Extensions/SyntaxNodeExtensions.cs | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
|
||
namespace OmniSharp.Roslyn.CSharp.Services.Signatures | ||
{ | ||
//TO DO: Remove this class once a public API for Signature Help from Roslyn is available | ||
internal static class CheckForStaticExtension | ||
{ | ||
public static bool IsInStaticContext(this SyntaxNode node) | ||
{ | ||
// this/base calls are always static. | ||
if (node.FirstAncestorOrSelf<ConstructorInitializerSyntax>() != null) | ||
{ | ||
return true; | ||
} | ||
|
||
var memberDeclaration = node.FirstAncestorOrSelf<MemberDeclarationSyntax>(); | ||
if (memberDeclaration == null) | ||
{ | ||
return false; | ||
} | ||
|
||
switch (memberDeclaration.Kind()) | ||
{ | ||
case SyntaxKind.MethodDeclaration: | ||
case SyntaxKind.ConstructorDeclaration: | ||
case SyntaxKind.EventDeclaration: | ||
case SyntaxKind.IndexerDeclaration: | ||
return GetModifiers(memberDeclaration).Any(SyntaxKind.StaticKeyword); | ||
|
||
case SyntaxKind.PropertyDeclaration: | ||
return GetModifiers(memberDeclaration).Any(SyntaxKind.StaticKeyword) || | ||
node.IsFoundUnder((PropertyDeclarationSyntax p) => p.Initializer); | ||
|
||
case SyntaxKind.FieldDeclaration: | ||
case SyntaxKind.EventFieldDeclaration: | ||
// Inside a field one can only access static members of a type (unless it's top-level). | ||
return !memberDeclaration.Parent.IsKind(SyntaxKind.CompilationUnit); | ||
|
||
case SyntaxKind.DestructorDeclaration: | ||
return false; | ||
} | ||
|
||
// Global statements are not a static context. | ||
if (node.FirstAncestorOrSelf<GlobalStatementSyntax>() != null) | ||
{ | ||
return false; | ||
} | ||
|
||
// any other location is considered static | ||
return true; | ||
} | ||
|
||
public static SyntaxTokenList GetModifiers(SyntaxNode member) | ||
{ | ||
if (member != null) | ||
{ | ||
switch (member.Kind()) | ||
{ | ||
case SyntaxKind.EnumDeclaration: | ||
return ((EnumDeclarationSyntax)member).Modifiers; | ||
case SyntaxKind.ClassDeclaration: | ||
case SyntaxKind.InterfaceDeclaration: | ||
case SyntaxKind.StructDeclaration: | ||
return ((TypeDeclarationSyntax)member).Modifiers; | ||
case SyntaxKind.DelegateDeclaration: | ||
return ((DelegateDeclarationSyntax)member).Modifiers; | ||
case SyntaxKind.FieldDeclaration: | ||
return ((FieldDeclarationSyntax)member).Modifiers; | ||
case SyntaxKind.EventFieldDeclaration: | ||
return ((EventFieldDeclarationSyntax)member).Modifiers; | ||
case SyntaxKind.ConstructorDeclaration: | ||
return ((ConstructorDeclarationSyntax)member).Modifiers; | ||
case SyntaxKind.DestructorDeclaration: | ||
return ((DestructorDeclarationSyntax)member).Modifiers; | ||
case SyntaxKind.PropertyDeclaration: | ||
return ((PropertyDeclarationSyntax)member).Modifiers; | ||
case SyntaxKind.EventDeclaration: | ||
return ((EventDeclarationSyntax)member).Modifiers; | ||
case SyntaxKind.IndexerDeclaration: | ||
return ((IndexerDeclarationSyntax)member).Modifiers; | ||
case SyntaxKind.OperatorDeclaration: | ||
return ((OperatorDeclarationSyntax)member).Modifiers; | ||
case SyntaxKind.ConversionOperatorDeclaration: | ||
return ((ConversionOperatorDeclarationSyntax)member).Modifiers; | ||
case SyntaxKind.MethodDeclaration: | ||
return ((MethodDeclarationSyntax)member).Modifiers; | ||
case SyntaxKind.GetAccessorDeclaration: | ||
case SyntaxKind.SetAccessorDeclaration: | ||
case SyntaxKind.AddAccessorDeclaration: | ||
case SyntaxKind.RemoveAccessorDeclaration: | ||
return ((AccessorDeclarationSyntax)member).Modifiers; | ||
} | ||
} | ||
|
||
return default; | ||
} | ||
|
||
public static bool IsFoundUnder<TParent>(this SyntaxNode node, Func<TParent, SyntaxNode> childGetter) | ||
where TParent : SyntaxNode | ||
{ | ||
var ancestor = node.GetAncestor<TParent>(); | ||
if (ancestor == null) | ||
{ | ||
return false; | ||
} | ||
|
||
var child = childGetter(ancestor); | ||
|
||
// See if node passes through child on the way up to ancestor. | ||
return node.GetAncestorsOrThis<SyntaxNode>().Contains(child); | ||
} | ||
|
||
public static TNode GetAncestor<TNode>(this SyntaxNode node) | ||
where TNode : SyntaxNode | ||
{ | ||
var current = node.Parent; | ||
while (current != null) | ||
{ | ||
if (current is TNode tNode) | ||
{ | ||
return tNode; | ||
} | ||
|
||
current = current.GetParent(); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
private static SyntaxNode GetParent(this SyntaxNode node) | ||
{ | ||
return node is IStructuredTriviaSyntax trivia ? trivia.ParentTrivia.Token.Parent : node.Parent; | ||
} | ||
|
||
public static TNode FirstAncestorOrSelfUntil<TNode>(this SyntaxNode node, Func<SyntaxNode, bool> predicate) | ||
where TNode : SyntaxNode | ||
{ | ||
for (var current = node; current != null; current = current.GetParent()) | ||
{ | ||
if (current is TNode tnode) | ||
{ | ||
return tnode; | ||
} | ||
|
||
if (predicate(current)) | ||
{ | ||
break; | ||
} | ||
} | ||
|
||
return default; | ||
} | ||
|
||
public static TNode GetAncestorOrThis<TNode>(this SyntaxNode node) | ||
where TNode : SyntaxNode | ||
{ | ||
return node?.GetAncestorsOrThis<TNode>().FirstOrDefault(); | ||
} | ||
|
||
public static IEnumerable<TNode> GetAncestorsOrThis<TNode>(this SyntaxNode node) | ||
where TNode : SyntaxNode | ||
{ | ||
var current = node; | ||
while (current != null) | ||
{ | ||
if (current is TNode tNode) | ||
{ | ||
yield return tNode; | ||
} | ||
|
||
current = current.GetParent(); | ||
} | ||
} | ||
} | ||
} | ||
|
||
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using Microsoft.CodeAnalysis.Text; | ||
using OmniSharp.Mef; | ||
|
@@ -59,7 +60,24 @@ public async Task<SignatureHelpResponse> Handle(SignatureHelpRequest request) | |
foreach (var invocation in invocations) | ||
{ | ||
var types = invocation.ArgumentTypes; | ||
foreach (var methodOverload in GetMethodOverloads(invocation.SemanticModel, invocation.Receiver)) | ||
ISymbol throughSymbol = null; | ||
ISymbol throughType = null; | ||
var methodGroup = invocation.SemanticModel.GetMemberGroup(invocation.Receiver).OfType<IMethodSymbol>(); | ||
if (invocation.Receiver is MemberAccessExpressionSyntax) | ||
{ | ||
var throughExpression = ((MemberAccessExpressionSyntax)invocation.Receiver).Expression; | ||
throughSymbol = invocation.SemanticModel.GetSpeculativeSymbolInfo(invocation.Position, throughExpression, SpeculativeBindingOption.BindAsExpression).Symbol; | ||
throughType = invocation.SemanticModel.GetSpeculativeTypeInfo(invocation.Position, throughExpression, SpeculativeBindingOption.BindAsTypeOrNamespace).Type; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we need to retrieve speculative symbol and type info here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Our heuristic is to show instance members if the thing left of the dot can bind as a non-type and to show static members if the thing left of the dot can bind as a type (and both, in the case of Color Color). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this just copied and pasted from Roslyn? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Roslyn uses a different API for doing the same thing. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Roslyn does awkward type lookup to achieve the same effect: http://source.roslyn.io/#Microsoft.CodeAnalysis.CSharp.Features/SignatureHelp/InvocationExpressionSignatureHelpProvider_MethodGroup.cs,44. @DustinCampbell does what they are doing seem materially different to you? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yup. It's similar. This looks fine. |
||
var includeInstance = throughSymbol != null && !(throughSymbol is ITypeSymbol); | ||
var includeStatic = (throughSymbol is INamedTypeSymbol) || throughType != null; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are the two clauses in the statements above reversed? Was that intentional? Why are we checking for null? Doesn't the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the first case if we remove the null check,then if throughSymbol is null,the second statement will evaluate to true (and we want it to be false on a null). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see it now -- thanks! |
||
methodGroup = methodGroup.Where(m => (m.IsStatic && includeStatic) || (!m.IsStatic && includeInstance)); | ||
} | ||
else if (invocation.Receiver is SimpleNameSyntax && invocation.IsInStaticContext) | ||
{ | ||
methodGroup = methodGroup.Where(m => m.IsStatic); | ||
} | ||
|
||
foreach (var methodOverload in methodGroup) | ||
{ | ||
var signature = BuildSignature(methodOverload); | ||
signaturesSet.Add(signature); | ||
|
@@ -94,19 +112,19 @@ private async Task<InvocationContext> GetInvocation(Document document, Request r | |
if (node is InvocationExpressionSyntax invocation && invocation.ArgumentList.Span.Contains(position)) | ||
{ | ||
var semanticModel = await document.GetSemanticModelAsync(); | ||
return new InvocationContext(semanticModel, position, invocation.Expression, invocation.ArgumentList); | ||
return new InvocationContext(semanticModel, position, invocation.Expression, invocation.ArgumentList, invocation.IsInStaticContext()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can InvocationContext take care of calling IsInStaticContext()? You've making the same call in all the initializations. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In that case we will have to pass the invocation object to InvocationContext and since there are three different types of such objects we will have to add another constructor in InvocationContext. Which is better of the two ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, I'll leave it up to you. |
||
} | ||
|
||
if (node is ObjectCreationExpressionSyntax objectCreation && objectCreation.ArgumentList.Span.Contains(position)) | ||
{ | ||
var semanticModel = await document.GetSemanticModelAsync(); | ||
return new InvocationContext(semanticModel, position, objectCreation, objectCreation.ArgumentList); | ||
return new InvocationContext(semanticModel, position, objectCreation, objectCreation.ArgumentList, objectCreation.IsInStaticContext()); | ||
} | ||
|
||
if (node is AttributeSyntax attributeSyntax && attributeSyntax.ArgumentList.Span.Contains(position)) | ||
{ | ||
var semanticModel = await document.GetSemanticModelAsync(); | ||
return new InvocationContext(semanticModel, position, attributeSyntax, attributeSyntax.ArgumentList); | ||
return new InvocationContext(semanticModel, position, attributeSyntax, attributeSyntax.ArgumentList, attributeSyntax.IsInStaticContext()); | ||
} | ||
|
||
node = node.Parent; | ||
|
@@ -115,30 +133,9 @@ private async Task<InvocationContext> GetInvocation(Document document, Request r | |
return null; | ||
} | ||
|
||
private IEnumerable<IMethodSymbol> GetMethodOverloads(SemanticModel semanticModel, SyntaxNode node) | ||
{ | ||
ISymbol symbol = null; | ||
var symbolInfo = semanticModel.GetSymbolInfo(node); | ||
if (symbolInfo.Symbol != null) | ||
{ | ||
symbol = symbolInfo.Symbol; | ||
} | ||
else if (!symbolInfo.CandidateSymbols.IsEmpty) | ||
{ | ||
symbol = symbolInfo.CandidateSymbols.First(); | ||
} | ||
|
||
if (symbol == null || symbol.ContainingType == null) | ||
{ | ||
return new IMethodSymbol[] { }; | ||
} | ||
|
||
return symbol.ContainingType.GetMembers(symbol.Name).OfType<IMethodSymbol>(); | ||
} | ||
|
||
private int InvocationScore(IMethodSymbol symbol, IEnumerable<TypeInfo> types) | ||
{ | ||
var parameters = GetParameters(symbol); | ||
var parameters = symbol.Parameters; | ||
if (parameters.Count() < types.Count()) | ||
{ | ||
return int.MinValue; | ||
|
@@ -172,7 +169,7 @@ private static SignatureHelpItem BuildSignature(IMethodSymbol symbol) | |
signature.Name = symbol.MethodKind == MethodKind.Constructor ? symbol.ContainingType.Name : symbol.Name; | ||
signature.Label = symbol.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat); | ||
|
||
signature.Parameters = GetParameters(symbol).Select(parameter => | ||
signature.Parameters = symbol.Parameters.Select(parameter => | ||
{ | ||
return new SignatureHelpParameter() | ||
{ | ||
|
@@ -185,16 +182,5 @@ private static SignatureHelpItem BuildSignature(IMethodSymbol symbol) | |
return signature; | ||
} | ||
|
||
private static IEnumerable<IParameterSymbol> GetParameters(IMethodSymbol methodSymbol) | ||
{ | ||
if (!methodSymbol.IsExtensionMethod) | ||
{ | ||
return methodSymbol.Parameters; | ||
} | ||
else | ||
{ | ||
return methodSymbol.Parameters.RemoveAt(0); | ||
} | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this a bunch of code copied from Roslyn? If so, this should be commented per the MIT license in the Roslyn code base. It might also be worth adding a TODO comment to remove this once we've added a proper public API for Signature Help to Roslyn.