diff --git a/src/EditorFeatures/CSharpTest2/Recommendations/ThisKeywordRecommenderTests.cs b/src/EditorFeatures/CSharpTest2/Recommendations/ThisKeywordRecommenderTests.cs index ee0c00f012719..3a6418d0c8f73 100644 --- a/src/EditorFeatures/CSharpTest2/Recommendations/ThisKeywordRecommenderTests.cs +++ b/src/EditorFeatures/CSharpTest2/Recommendations/ThisKeywordRecommenderTests.cs @@ -269,6 +269,79 @@ await VerifyKeywordAsync( static int Goo($$"); } + [Fact, Trait(Traits.Feature, Traits.Features.KeywordRecommending)] + [WorkItem(27028, "https://github.com/dotnet/roslyn/issues/27028")] + public async Task TestInLocalFunction() + { + await VerifyKeywordAsync( +@"class C +{ + int Method() + { + void local() + { + $$ + } + } +}"); + } + + [Fact, Trait(Traits.Feature, Traits.Features.KeywordRecommending)] + [WorkItem(27028, "https://github.com/dotnet/roslyn/issues/27028")] + public async Task TestInNestedLocalFunction() + { + await VerifyKeywordAsync( +@"class C +{ + int Method() + { + void local() + { + void nested() + { + $$ + } + } + } +}"); + } + + [Fact, Trait(Traits.Feature, Traits.Features.KeywordRecommending)] + [WorkItem(27028, "https://github.com/dotnet/roslyn/issues/27028")] + public async Task TestInLocalFunctionInStaticMethod() + { + await VerifyAbsenceAsync( +@"class C { + static int Method() + { + void local() + { + $$ + } + } +}"); + } + + [Fact, Trait(Traits.Feature, Traits.Features.KeywordRecommending)] + [WorkItem(27028, "https://github.com/dotnet/roslyn/issues/27028")] + public async Task TestInNestedLocalFunctionInStaticMethod() + { + await VerifyAbsenceAsync( +@"class C +{ + static int Method() + { + void local() + { + void nested() + { + $$ + } + } + } +}"); + } + [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 e5b2d9924070b..cc1497d8d8dae 100644 --- a/src/Workspaces/CSharp/Portable/Extensions/ContextQuery/SyntaxTreeExtensions.cs +++ b/src/Workspaces/CSharp/Portable/Extensions/ContextQuery/SyntaxTreeExtensions.cs @@ -1771,6 +1771,13 @@ public static bool IsInstanceContext(this SyntaxTree syntaxTree, SyntaxToken tar #endif var enclosingSymbol = semanticModel.GetEnclosingSymbol(targetToken.SpanStart, cancellationToken); + + while (enclosingSymbol is IMethodSymbol method && method.MethodKind == MethodKind.LocalFunction) + { + // It is allowed to reference the instance (`this`) within a local function, as long as the containing method allows it + enclosingSymbol = method.ContainingSymbol; + } + return !enclosingSymbol.IsStatic; }