diff --git a/src/EditorFeatures/CSharpTest2/Recommendations/ThisKeywordRecommenderTests.cs b/src/EditorFeatures/CSharpTest2/Recommendations/ThisKeywordRecommenderTests.cs index 3a6418d0c8f73b9daeba722ce94e36e0e3661900..8f2a26a7b2a4695b253bebe5b0153a4bb1a3dfea 100644 --- a/src/EditorFeatures/CSharpTest2/Recommendations/ThisKeywordRecommenderTests.cs +++ b/src/EditorFeatures/CSharpTest2/Recommendations/ThisKeywordRecommenderTests.cs @@ -342,6 +342,230 @@ void nested() }"); } + [Fact, Trait(Traits.Feature, Traits.Features.KeywordRecommending)] + [WorkItem(27923, "https://github.com/dotnet/roslyn/issues/27923")] + public async Task TestInAnonymousMethod() + { + await VerifyKeywordAsync( +@"class C +{ + int Method() + { + Action a = delegate + { + $$ + }; + } +}"); + } + + [Fact, Trait(Traits.Feature, Traits.Features.KeywordRecommending)] + [WorkItem(27923, "https://github.com/dotnet/roslyn/issues/27923")] + public async Task TestInNestedAnonymousMethod() + { + await VerifyKeywordAsync( +@"class C +{ + int Method() + { + Action a = delegate + { + Action b = delegate + { + $$ + }; + }; + } +}"); + } + + [Fact, Trait(Traits.Feature, Traits.Features.KeywordRecommending)] + [WorkItem(27923, "https://github.com/dotnet/roslyn/issues/27923")] + public async Task TestInAnonymousMethodInStaticMethod() + { + await VerifyAbsenceAsync( +@"class C +{ + static int Method() + { + Action a = delegate + { + $$ + }; + } +}"); + } + + [Fact, Trait(Traits.Feature, Traits.Features.KeywordRecommending)] + [WorkItem(27923, "https://github.com/dotnet/roslyn/issues/27923")] + public async Task TestInNestedAnonymousMethodInStaticMethod() + { + await VerifyAbsenceAsync( +@"class C +{ + static int Method() + { + Action a = delegate + { + Action b = delegate + { + $$ + }; + }; + } +}"); + } + + [Fact, Trait(Traits.Feature, Traits.Features.KeywordRecommending)] + [WorkItem(27923, "https://github.com/dotnet/roslyn/issues/27923")] + public async Task TestInLambdaExpression() + { + await VerifyKeywordAsync( +@"class C +{ + int Method() + { + Action a = () => + { + $$ + }; + } +}"); + } + + [Fact, Trait(Traits.Feature, Traits.Features.KeywordRecommending)] + [WorkItem(27923, "https://github.com/dotnet/roslyn/issues/27923")] + public async Task TestInNestedLambdaExpression() + { + await VerifyKeywordAsync( +@"class C +{ + int Method() + { + Action a = () => + { + Action b = () => + { + $$ + }; + }; + } +}"); + } + + [Fact, Trait(Traits.Feature, Traits.Features.KeywordRecommending)] + [WorkItem(27923, "https://github.com/dotnet/roslyn/issues/27923")] + public async Task TestInLambdaExpressionInStaticMethod() + { + await VerifyAbsenceAsync( +@"class C +{ + static int Method() + { + Action a = () => + { + $$ + }; + } +}"); + } + + [Fact, Trait(Traits.Feature, Traits.Features.KeywordRecommending)] + [WorkItem(27923, "https://github.com/dotnet/roslyn/issues/27923")] + public async Task TestInNestedLambdaExpressionInStaticMethod() + { + await VerifyAbsenceAsync( +@"class C +{ + static int Method() + { + Action a = () => + { + Action b = () => + { + $$ + }; + }; + } +}"); + } + + [WorkItem(27923, "https://github.com/dotnet/roslyn/issues/27923")] + public async Task TestInNestedLambdaExpressionInAnonymousMethod() + { + await VerifyKeywordAsync( +@"class C +{ + int Method() + { + Action a = delegate + { + Action b = () => + { + $$ + }; + }; + } +}"); + } + + [WorkItem(27923, "https://github.com/dotnet/roslyn/issues/27923")] + public async Task TestInNestedAnonymousInLambdaExpression() + { + await VerifyKeywordAsync( +@"class C +{ + int Method() + { + Action a = () => + { + Action b = delegate + { + $$ + }; + }; + } +}"); + } + + [WorkItem(27923, "https://github.com/dotnet/roslyn/issues/27923")] + public async Task TestInNestedAnonymousMethodInLambdaExpressionInStaticMethod() + { + await VerifyAbsenceAsync( +@"class C +{ + static int Method() + { + Action a = () => + { + Action b = delegate + { + $$ + }; + }; + } +}"); + } + + [WorkItem(27923, "https://github.com/dotnet/roslyn/issues/27923")] + public async Task TestInNestedLambdaExpressionInAnonymousMethodInStaticMethod() + { + await VerifyAbsenceAsync( +@"class C +{ + static int Method() + { + Action a = delegate + { + Action b = () => + { + $$ + }; + }; + } +}"); + } + [Fact, Trait(Traits.Feature, Traits.Features.KeywordRecommending)] public async Task TestAfterAttribute() { diff --git a/src/Workspaces/CSharp/Portable/Extensions/ContextQuery/SyntaxTreeExtensions.cs b/src/Workspaces/CSharp/Portable/Extensions/ContextQuery/SyntaxTreeExtensions.cs index cc1497d8d8dae03a211fd8fc87bc2003c7a5ac53..23b71f220601dc322b3a18e19dd62485e925a7b0 100644 --- a/src/Workspaces/CSharp/Portable/Extensions/ContextQuery/SyntaxTreeExtensions.cs +++ b/src/Workspaces/CSharp/Portable/Extensions/ContextQuery/SyntaxTreeExtensions.cs @@ -1772,7 +1772,7 @@ public static bool IsInstanceContext(this SyntaxTree syntaxTree, SyntaxToken tar var enclosingSymbol = semanticModel.GetEnclosingSymbol(targetToken.SpanStart, cancellationToken); - while (enclosingSymbol is IMethodSymbol method && method.MethodKind == MethodKind.LocalFunction) + while (enclosingSymbol is IMethodSymbol method && (method.MethodKind == MethodKind.LocalFunction || method.MethodKind == MethodKind.AnonymousFunction)) { // It is allowed to reference the instance (`this`) within a local function, as long as the containing method allows it enclosingSymbol = method.ContainingSymbol;