From c89b6e21321397e15be0f87c175e010a91878898 Mon Sep 17 00:00:00 2001 From: "gel@microsoft.com" Date: Fri, 15 Dec 2023 15:11:00 -0800 Subject: [PATCH 1/2] Provide 'return' keyword in attribute context for accessor --- .../ReturnKeywordRecommenderTests.cs | 19 +++++++++++++++++++ .../ReturnKeywordRecommender.cs | 12 +++++++++++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/EditorFeatures/CSharpTest2/Recommendations/ReturnKeywordRecommenderTests.cs b/src/EditorFeatures/CSharpTest2/Recommendations/ReturnKeywordRecommenderTests.cs index d05e69eeee1f7..14b36eb239165 100644 --- a/src/EditorFeatures/CSharpTest2/Recommendations/ReturnKeywordRecommenderTests.cs +++ b/src/EditorFeatures/CSharpTest2/Recommendations/ReturnKeywordRecommenderTests.cs @@ -446,5 +446,24 @@ await VerifyKeywordAsync(AddInsideMethod( $$ """)); } + + [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/71200")] + public async Task TestAccessorAttibuteList() + { + await VerifyKeywordAsync( + """ + public class C + { + public int Property + { + [$$] + get + { + return 1; + } + } + } + """); + } } } diff --git a/src/Features/CSharp/Portable/Completion/KeywordRecommenders/ReturnKeywordRecommender.cs b/src/Features/CSharp/Portable/Completion/KeywordRecommenders/ReturnKeywordRecommender.cs index ac2ad5c246edd..6528bbdef7421 100644 --- a/src/Features/CSharp/Portable/Completion/KeywordRecommenders/ReturnKeywordRecommender.cs +++ b/src/Features/CSharp/Portable/Completion/KeywordRecommenders/ReturnKeywordRecommender.cs @@ -4,6 +4,7 @@ using System.Threading; using Microsoft.CodeAnalysis.CSharp.Extensions.ContextQuery; +using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.CodeAnalysis.CSharp.Utilities; using Microsoft.CodeAnalysis.Shared.Extensions; @@ -30,7 +31,16 @@ private static bool IsAttributeContext(CSharpSyntaxContext context, Cancellation return context.IsMemberAttributeContext(SyntaxKindSet.ClassInterfaceStructRecordTypeDeclarations, cancellationToken) || (context.SyntaxTree.IsScript() && context.IsTypeAttributeContext(cancellationToken)) || - context.IsStatementAttributeContext(); + context.IsStatementAttributeContext() || + IsAccessorAttributeContext(); + + bool IsAccessorAttributeContext() + { + var token = context.TargetToken; + return token.Kind() == SyntaxKind.OpenBracketToken && + token.Parent is AttributeListSyntax && + token.Parent.Parent is AccessorDeclarationSyntax; + } } } } From eabb2fc61a1e325a575e3a487b2cc95f441b8bf1 Mon Sep 17 00:00:00 2001 From: "gel@microsoft.com" Date: Tue, 26 Dec 2023 14:18:06 -0800 Subject: [PATCH 2/2] Fix tests --- .../ReturnKeywordRecommenderTests.cs | 31 ++++--------------- 1 file changed, 6 insertions(+), 25 deletions(-) diff --git a/src/EditorFeatures/CSharpTest2/Recommendations/ReturnKeywordRecommenderTests.cs b/src/EditorFeatures/CSharpTest2/Recommendations/ReturnKeywordRecommenderTests.cs index 14b36eb239165..246dd11b1bd54 100644 --- a/src/EditorFeatures/CSharpTest2/Recommendations/ReturnKeywordRecommenderTests.cs +++ b/src/EditorFeatures/CSharpTest2/Recommendations/ReturnKeywordRecommenderTests.cs @@ -305,20 +305,20 @@ void Goo([$$ """); } - [Fact] - public async Task TestNotInPropertyAttribute() + [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/71200")] + public async Task TestInPropertyAttribute() { - await VerifyAbsenceAsync( + await VerifyKeywordAsync( """ class C { int Goo { [$$ """); } - [Fact] - public async Task TestNotInEventAttribute() + [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/71200")] + public async Task TestInEventAttribute() { - await VerifyAbsenceAsync( + await VerifyKeywordAsync( """ class C { event Action Goo { [$$ @@ -446,24 +446,5 @@ await VerifyKeywordAsync(AddInsideMethod( $$ """)); } - - [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/71200")] - public async Task TestAccessorAttibuteList() - { - await VerifyKeywordAsync( - """ - public class C - { - public int Property - { - [$$] - get - { - return 1; - } - } - } - """); - } } }