提交 c6561868 编写于 作者: L Lillo

Add more test case.

Add method to IsTypeAttributeContextBefore
上级 0514595d
......@@ -128,16 +128,41 @@ namespace Goo {}");
[Fact, Trait(Traits.Feature, Traits.Features.KeywordRecommending)]
[WorkItem(362, "https://github.com/dotnet/roslyn/issues/362")]
public async Task TestNotBeforeAttributeNamespaceWithNoOpenBrace()
public async Task TestNotBeforeAttributeNamespaceWithNoOpenBracket()
{
await VerifyAbsenceAsync(
@"$$
namespace Goo {}");
}
[Fact, Trait(Traits.Feature, Traits.Features.KeywordRecommending)]
[WorkItem(362, "https://github.com/dotnet/roslyn/issues/362")]
public async Task TestNotBeforeAttributeNamespaceAndAfterUsingWithNoOpenBracket()
{
await VerifyAbsenceAsync(
@"
using System;
$$
namespace Goo {}");
}
[Fact, Trait(Traits.Feature, Traits.Features.KeywordRecommending)]
[WorkItem(362, "https://github.com/dotnet/roslyn/issues/362")]
public async Task TestBeforeAssemblyAttributeWithOpenBrace()
public async Task TestBeforeAttributeNamespaceAndAfterUsingWithNoOpenBracket()
{
await VerifyKeywordAsync(
@"
using System;
[$$
namespace Goo {}");
}
[Fact, Trait(Traits.Feature, Traits.Features.KeywordRecommending)]
[WorkItem(362, "https://github.com/dotnet/roslyn/issues/362")]
public async Task TestBeforeAssemblyAttributeWithOpenBracket()
{
await VerifyKeywordAsync(
@"
......@@ -148,7 +173,7 @@ namespace Goo {}");
[Fact, Trait(Traits.Feature, Traits.Features.KeywordRecommending)]
[WorkItem(362, "https://github.com/dotnet/roslyn/issues/362")]
public async Task TestNotBeforeAssemblyAttributeWithoutOpenBrace()
public async Task TestNotBeforeAssemblyAttributeWithoutOpenBracket()
{
await VerifyAbsenceAsync(
@"
......@@ -157,6 +182,32 @@ public async Task TestNotBeforeAssemblyAttributeWithoutOpenBrace()
namespace Goo {}");
}
[Fact, Trait(Traits.Feature, Traits.Features.KeywordRecommending)]
[WorkItem(362, "https://github.com/dotnet/roslyn/issues/362")]
public async Task TestNotBeforeAssemblyAttributeAndAfterUsingWithoutOpenBracket()
{
await VerifyAbsenceAsync(
@"
using System;
$$
[assembly: Whatever]
namespace Goo {}");
}
[Fact, Trait(Traits.Feature, Traits.Features.KeywordRecommending)]
[WorkItem(362, "https://github.com/dotnet/roslyn/issues/362")]
public async Task TestBeforeAssemblyAttributeAndAfterUsingWithoutOpenBracket()
{
await VerifyKeywordAsync(
@"
using System;
[$$
[assembly: Whatever]
namespace Goo {}");
}
[Fact, Trait(Traits.Feature, Traits.Features.KeywordRecommending)]
public async Task TestNotInOuterAttributeInNamespace()
{
......
......@@ -17,14 +17,13 @@ public AssemblyKeywordRecommender()
protected override bool IsValidContext(int position, CSharpSyntaxContext context, CancellationToken cancellationToken)
{
if (context.TargetToken.IsBeforeNamespace()|| context.IsTypeAttributeContext(cancellationToken) )
if (context.IsTypeAttributeContextBefore(SyntaxKind.NamespaceKeyword, cancellationToken))
{
var token = context.LeftToken;
var type = token.GetAncestor<MemberDeclarationSyntax>();
return type == null || type.IsParentKind(SyntaxKind.CompilationUnit);
}
return false;
}
}
......
......@@ -283,6 +283,27 @@ 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,
ISet<SyntaxKind> validTypeDeclarations = null,
......
......@@ -670,13 +670,5 @@ public static bool IsMandatoryNamedParameterPosition(this SyntaxToken token)
return false;
}
public static bool IsBeforeNamespace(this SyntaxToken token)
{
// cases:
// | namespace Goo
// | [N] namespace Goo
return token.GetNextToken().Kind() == SyntaxKind.NamespaceKeyword;
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册