提交 e7d2d3d0 编写于 作者: R Ravi Chande

Use SemanticModel to determine if we're in an instance context

上级 513b59cc
......@@ -237,7 +237,7 @@ private static CSharpSyntaxContext CreateContextWorker(Workspace workspace, Sema
syntaxTree.IsDelegateReturnTypeContext(position, leftToken, cancellationToken),
syntaxTree.IsTypeOfExpressionContext(position, leftToken, cancellationToken),
syntaxTree.GetPrecedingModifiers(position, leftToken, cancellationToken),
syntaxTree.IsInstanceContext(position, leftToken, cancellationToken),
syntaxTree.IsInstanceContext(targetToken, semanticModel, cancellationToken),
syntaxTree.IsCrefContext(position, cancellationToken) && !leftToken.IsKind(SyntaxKind.DotToken),
syntaxTree.IsCatchFilterContext(position, leftToken),
isDestructorTypeContext,
......
......@@ -1708,7 +1708,7 @@ public static bool IsGlobalStatementContext(this SyntaxTree syntaxTree, int posi
return token.IsBeginningOfGlobalStatementContext();
}
public static bool IsInstanceContext(this SyntaxTree syntaxTree, int position, SyntaxToken tokenOnLeftOfPosition, CancellationToken cancellationToken)
public static bool IsInstanceContext(this SyntaxTree syntaxTree, SyntaxToken targetToken, SemanticModel semanticModel, CancellationToken cancellationToken)
{
#if false
if (syntaxTree.IsInPreprocessorDirectiveContext(position, cancellationToken))
......@@ -1717,41 +1717,8 @@ public static bool IsInstanceContext(this SyntaxTree syntaxTree, int position, S
}
#endif
var token = tokenOnLeftOfPosition;
// We're in an instance context if we're in the body of an instance member
var containingMember = token.GetAncestor<MemberDeclarationSyntax>();
if (containingMember == null)
{
return false;
}
var modifiers = containingMember.GetModifiers();
if (modifiers.Any(SyntaxKind.StaticKeyword))
{
return false;
}
var expressionBody = containingMember.GetExpressionBody();
if (expressionBody != null)
{
return TextSpan.FromBounds(expressionBody.ArrowToken.Span.End, expressionBody.FullSpan.End).IntersectsWith(position);
}
// Must be a property or something method-like.
if (containingMember.HasMethodShape())
{
var body = containingMember.GetBody();
return IsInBlock(body, position);
}
var accessor = token.GetAncestor<AccessorDeclarationSyntax>();
if (accessor != null)
{
return IsInBlock(accessor.Body, position);
}
return false;
var enclosingSymbol = semanticModel.GetEnclosingSymbol(targetToken.SpanStart, cancellationToken);
return !enclosingSymbol.IsStatic;
}
private static bool IsInBlock(BlockSyntax bodyOpt, int position)
......
......@@ -331,10 +331,7 @@ public static ArrowExpressionClauseSyntax GetExpressionBody(this MemberDeclarati
switch (memberDeclaration?.Kind())
{
case SyntaxKind.PropertyDeclaration:
var propertyDeclaration = (PropertyDeclarationSyntax)memberDeclaration;
return propertyDeclaration.ExpressionBody ??
propertyDeclaration.AccessorList.Accessors.FirstOrDefault(a => a.ExpressionBody != null)
?.ExpressionBody;
return ((PropertyDeclarationSyntax)memberDeclaration).ExpressionBody;
case SyntaxKind.MethodDeclaration:
return ((MethodDeclarationSyntax)memberDeclaration).ExpressionBody;
case SyntaxKind.IndexerDeclaration:
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册