提交 bfe05f42 编写于 作者: L Lillo

Improve code in AssemblyKeywordRecommender -> IsValidContext

上级 c6561868
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Threading;
using Microsoft.CodeAnalysis.CSharp.Extensions;
using Microsoft.CodeAnalysis.CSharp.Extensions.ContextQuery;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Shared.Extensions;
namespace Microsoft.CodeAnalysis.CSharp.Completion.KeywordRecommenders
{
......@@ -17,14 +15,21 @@ public AssemblyKeywordRecommender()
protected override bool IsValidContext(int position, CSharpSyntaxContext context, CancellationToken cancellationToken)
{
if (context.IsTypeAttributeContextBefore(SyntaxKind.NamespaceKeyword, cancellationToken))
{
var token = context.LeftToken;
var type = token.GetAncestor<MemberDeclarationSyntax>();
var token = context.TargetToken;
return type == null || type.IsParentKind(SyntaxKind.CompilationUnit);
// Note that we pass the token.SpanStart to IsTypeDeclarationContext below. This is a bit subtle,
// but we want to be sure that the attribute itself (i.e. the open square bracket, '[') is in a
// type declaration context.
if (token.Kind() != SyntaxKind.OpenBracketToken)
return false;
if (token.Parent.Kind() == SyntaxKind.AttributeList)
{
var previousSyntax = token.GetPreviousToken().Parent;
return previousSyntax == null;
}
return false;
var nextSyntax = token.GetNextToken().Parent;
var currentSyntax = token.Parent;
return nextSyntax is NamespaceDeclarationSyntax || currentSyntax is CompilationUnitSyntax;
}
}
}
......@@ -283,26 +283,6 @@ public bool IsTypeAttributeContext(CancellationToken cancellationToken)
return false;
}
public bool IsTypeAttributeContextBefore(SyntaxKind syntaxKind, CancellationToken cancellationToken)
{
// cases:
// [ |
// class C {
var token = TargetToken;
// Note that we pass the token.SpanStart to IsTypeDeclarationContext below. This is a bit subtle,
// but we want to be sure that the attribute itself (i.e. the open square bracket, '[') is in a
// type declaration context.
if (token.Kind() == SyntaxKind.OpenBracketToken &&
(token.GetNextToken().Kind() == syntaxKind || token.Parent.Kind() == SyntaxKind.AttributeList) &&
SyntaxTree.IsTypeDeclarationContext(
token.SpanStart, contextOpt: null, validModifiers: null, validTypeDeclarations: SyntaxKindSet.ClassStructTypeDeclarations, canBePartial: false, cancellationToken: cancellationToken))
{
return true;
}
return false;
}
public bool IsTypeDeclarationContext(
ISet<SyntaxKind> validModifiers = null,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册