未验证 提交 005f4f63 编写于 作者: J Julien Couvreur 提交者: GitHub

Recommend "when" keyword after case-pattern-switch-label (#24131)

上级 14848064
......@@ -83,7 +83,7 @@ private async Task CheckResultAsync(bool absent, int position, CSharpSyntaxConte
else
{
var result = (await RecommendKeywordsAsync(position, context)).Single();
Assert.NotNull(result);
Assert.True(result != null, "No recommended keywords");
Assert.Equal(keywordText, result.Keyword);
if (matchPriority != null)
{
......
......@@ -63,5 +63,13 @@ public async Task TestNotAfterFilter3()
await VerifyAbsenceAsync(AddInsideMethod(
@"try {} catch (Exception e) when (true) $$"));
}
[Fact, Trait(Traits.Feature, Traits.Features.KeywordRecommending)]
[WorkItem(24113, "https://github.com/dotnet/roslyn/issues/24113")]
public async Task TestInCasePattern()
{
await VerifyKeywordAsync(AddInsideMethod(
@"switch (1) { case int i $$ }"));
}
}
}
// 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;
namespace Microsoft.CodeAnalysis.CSharp.Completion.KeywordRecommenders
......@@ -14,7 +15,26 @@ public WhenKeywordRecommender()
protected override bool IsValidContext(int position, CSharpSyntaxContext context, CancellationToken cancellationToken)
{
return context.IsCatchFilterContext;
if (context.IsCatchFilterContext)
{
return true;
}
// case int i $$
// case int i w$$
if (IsIdentifierInCasePattern(context.TargetToken.Parent))
{
return true;
}
return false;
}
private static bool IsIdentifierInCasePattern(SyntaxNode node)
{
return node.IsKind(SyntaxKind.SingleVariableDesignation)
&& node.IsParentKind(SyntaxKind.DeclarationPattern)
&& node.Parent.IsParentKind(SyntaxKind.CasePatternSwitchLabel);
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册