From 78e10f3444ff9901d61b92e318a1354372d508bf Mon Sep 17 00:00:00 2001 From: DoctorKrolic Date: Thu, 15 Aug 2024 09:18:01 +0300 Subject: [PATCH 1/3] Do not pass `SnippetContext` by `in` --- .../Snippets/AbstractCSharpAutoPropertySnippetProvider.cs | 2 +- .../Snippets/AbstractCSharpMainMethodSnippetProvider.cs | 2 +- .../Portable/Snippets/AbstractCSharpTypeSnippetProvider.cs | 2 +- .../Portable/Snippets/CSharpConsoleSnippetProvider.cs | 2 +- .../Portable/Snippets/CSharpConstructorSnippetProvider.cs | 2 +- .../CSharp/Portable/Snippets/CSharpElseSnippetProvider.cs | 4 ++-- .../Portable/Snippets/CSharpForEachLoopSnippetProvider.cs | 4 ++-- .../Core/Portable/Snippets/AbstractSnippetService.cs | 2 +- .../AbstractInlineStatementSnippetProvider.cs | 4 ++-- .../Snippets/SnippetProviders/AbstractSnippetProvider.cs | 6 +++--- .../SnippetProviders/AbstractStatementSnippetProvider.cs | 2 +- .../Portable/Snippets/SnippetProviders/ISnippetProvider.cs | 2 +- 12 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/Features/CSharp/Portable/Snippets/AbstractCSharpAutoPropertySnippetProvider.cs b/src/Features/CSharp/Portable/Snippets/AbstractCSharpAutoPropertySnippetProvider.cs index 954a85488b311..a32aeaedc2a8e 100644 --- a/src/Features/CSharp/Portable/Snippets/AbstractCSharpAutoPropertySnippetProvider.cs +++ b/src/Features/CSharp/Portable/Snippets/AbstractCSharpAutoPropertySnippetProvider.cs @@ -30,7 +30,7 @@ internal abstract class AbstractCSharpAutoPropertySnippetProvider : AbstractProp protected virtual AccessorDeclarationSyntax? GenerateSetAccessorDeclaration(CSharpSyntaxContext syntaxContext, SyntaxGenerator generator, CancellationToken cancellationToken) => (AccessorDeclarationSyntax)generator.SetAccessorDeclaration(); - protected override bool IsValidSnippetLocation(in SnippetContext context, CancellationToken cancellationToken) + protected override bool IsValidSnippetLocation(SnippetContext context, CancellationToken cancellationToken) { return context.SyntaxContext.SyntaxTree.IsMemberDeclarationContext(context.Position, (CSharpSyntaxContext)context.SyntaxContext, SyntaxKindSet.AllMemberModifiers, SyntaxKindSet.ClassInterfaceStructRecordTypeDeclarations, canBePartial: true, cancellationToken); diff --git a/src/Features/CSharp/Portable/Snippets/AbstractCSharpMainMethodSnippetProvider.cs b/src/Features/CSharp/Portable/Snippets/AbstractCSharpMainMethodSnippetProvider.cs index aa84a9a5061c8..00f8d27d85fff 100644 --- a/src/Features/CSharp/Portable/Snippets/AbstractCSharpMainMethodSnippetProvider.cs +++ b/src/Features/CSharp/Portable/Snippets/AbstractCSharpMainMethodSnippetProvider.cs @@ -15,7 +15,7 @@ namespace Microsoft.CodeAnalysis.CSharp.Snippets; internal abstract class AbstractCSharpMainMethodSnippetProvider : AbstractMainMethodSnippetProvider { - protected override bool IsValidSnippetLocation(in SnippetContext context, CancellationToken cancellationToken) + protected override bool IsValidSnippetLocation(SnippetContext context, CancellationToken cancellationToken) { var semanticModel = context.SyntaxContext.SemanticModel; var syntaxContext = (CSharpSyntaxContext)context.SyntaxContext; diff --git a/src/Features/CSharp/Portable/Snippets/AbstractCSharpTypeSnippetProvider.cs b/src/Features/CSharp/Portable/Snippets/AbstractCSharpTypeSnippetProvider.cs index 6569ef7bdd066..7a9f558b4d683 100644 --- a/src/Features/CSharp/Portable/Snippets/AbstractCSharpTypeSnippetProvider.cs +++ b/src/Features/CSharp/Portable/Snippets/AbstractCSharpTypeSnippetProvider.cs @@ -27,7 +27,7 @@ internal abstract class AbstractCSharpTypeSnippetProvider ValidModifiers { get; } - protected override bool IsValidSnippetLocation(in SnippetContext context, CancellationToken cancellationToken) + protected override bool IsValidSnippetLocation(SnippetContext context, CancellationToken cancellationToken) { var syntaxContext = (CSharpSyntaxContext)context.SyntaxContext; diff --git a/src/Features/CSharp/Portable/Snippets/CSharpConsoleSnippetProvider.cs b/src/Features/CSharp/Portable/Snippets/CSharpConsoleSnippetProvider.cs index cf175e08e78a8..265c6c0ad5d18 100644 --- a/src/Features/CSharp/Portable/Snippets/CSharpConsoleSnippetProvider.cs +++ b/src/Features/CSharp/Portable/Snippets/CSharpConsoleSnippetProvider.cs @@ -21,7 +21,7 @@ internal sealed class CSharpConsoleSnippetProvider() : AbstractConsoleSnippetPro ArgumentListSyntax, LambdaExpressionSyntax> { - protected override bool IsValidSnippetLocation(in SnippetContext context, CancellationToken cancellationToken) + protected override bool IsValidSnippetLocation(SnippetContext context, CancellationToken cancellationToken) { var syntaxContext = context.SyntaxContext; diff --git a/src/Features/CSharp/Portable/Snippets/CSharpConstructorSnippetProvider.cs b/src/Features/CSharp/Portable/Snippets/CSharpConstructorSnippetProvider.cs index 1e7ab1e7817bc..4bf866aa9635d 100644 --- a/src/Features/CSharp/Portable/Snippets/CSharpConstructorSnippetProvider.cs +++ b/src/Features/CSharp/Portable/Snippets/CSharpConstructorSnippetProvider.cs @@ -36,7 +36,7 @@ internal sealed class CSharpConstructorSnippetProvider() : AbstractConstructorSn SyntaxKind.StaticKeyword, }; - protected override bool IsValidSnippetLocation(in SnippetContext context, CancellationToken cancellationToken) + protected override bool IsValidSnippetLocation(SnippetContext context, CancellationToken cancellationToken) { var syntaxContext = (CSharpSyntaxContext)context.SyntaxContext; diff --git a/src/Features/CSharp/Portable/Snippets/CSharpElseSnippetProvider.cs b/src/Features/CSharp/Portable/Snippets/CSharpElseSnippetProvider.cs index 4fdf90a78525c..fc224ed036b85 100644 --- a/src/Features/CSharp/Portable/Snippets/CSharpElseSnippetProvider.cs +++ b/src/Features/CSharp/Portable/Snippets/CSharpElseSnippetProvider.cs @@ -24,7 +24,7 @@ internal sealed class CSharpElseSnippetProvider() : AbstractElseSnippetProvider< public override string Description => FeaturesResources.else_statement; - protected override bool IsValidSnippetLocation(in SnippetContext context, CancellationToken cancellationToken) + protected override bool IsValidSnippetLocation(SnippetContext context, CancellationToken cancellationToken) { var syntaxContext = context.SyntaxContext; var token = syntaxContext.TargetToken; @@ -51,7 +51,7 @@ protected override bool IsValidSnippetLocation(in SnippetContext context, Cancel } } - return isAfterIfStatement && base.IsValidSnippetLocation(in context, cancellationToken); + return isAfterIfStatement && base.IsValidSnippetLocation(context, cancellationToken); } protected override Task GenerateSnippetTextChangeAsync(Document document, int position, CancellationToken cancellationToken) diff --git a/src/Features/CSharp/Portable/Snippets/CSharpForEachLoopSnippetProvider.cs b/src/Features/CSharp/Portable/Snippets/CSharpForEachLoopSnippetProvider.cs index 1ff99c4351f6d..322099fcf0796 100644 --- a/src/Features/CSharp/Portable/Snippets/CSharpForEachLoopSnippetProvider.cs +++ b/src/Features/CSharp/Portable/Snippets/CSharpForEachLoopSnippetProvider.cs @@ -34,7 +34,7 @@ internal sealed class CSharpForEachLoopSnippetProvider() : AbstractForEachLoopSn public override string Description => FeaturesResources.foreach_loop; - protected override bool IsValidSnippetLocation(in SnippetContext context, CancellationToken cancellationToken) + protected override bool IsValidSnippetLocation(SnippetContext context, CancellationToken cancellationToken) { var syntaxContext = context.SyntaxContext; var token = syntaxContext.TargetToken; @@ -48,7 +48,7 @@ protected override bool IsValidSnippetLocation(in SnippetContext context, Cancel return true; } - return base.IsValidSnippetLocation(in context, cancellationToken); + return base.IsValidSnippetLocation(context, cancellationToken); } protected override ForEachStatementSyntax GenerateStatement(SyntaxGenerator generator, SyntaxContext syntaxContext, InlineExpressionInfo? inlineExpressionInfo) diff --git a/src/Features/Core/Portable/Snippets/AbstractSnippetService.cs b/src/Features/Core/Portable/Snippets/AbstractSnippetService.cs index 24010eb1adeec..4146842d32ec1 100644 --- a/src/Features/Core/Portable/Snippets/AbstractSnippetService.cs +++ b/src/Features/Core/Portable/Snippets/AbstractSnippetService.cs @@ -42,7 +42,7 @@ public async Task> GetSnippetsAsync(SnippetContext c using var _ = ArrayBuilder.GetInstance(out var arrayBuilder); foreach (var provider in GetSnippetProviders(context.Document)) { - if (await provider.IsValidSnippetLocationAsync(in context, cancellationToken).ConfigureAwait(false)) + if (await provider.IsValidSnippetLocationAsync(context, cancellationToken).ConfigureAwait(false)) arrayBuilder.Add(new(provider.Identifier, provider.Description, provider.AdditionalFilterTexts)); } diff --git a/src/Features/Core/Portable/Snippets/SnippetProviders/AbstractInlineStatementSnippetProvider.cs b/src/Features/Core/Portable/Snippets/SnippetProviders/AbstractInlineStatementSnippetProvider.cs index ac60dc0569394..1bd0eabb2d848 100644 --- a/src/Features/Core/Portable/Snippets/SnippetProviders/AbstractInlineStatementSnippetProvider.cs +++ b/src/Features/Core/Portable/Snippets/SnippetProviders/AbstractInlineStatementSnippetProvider.cs @@ -39,7 +39,7 @@ internal abstract class AbstractInlineStatementSnippetProvider /// protected bool ConstructedFromInlineExpression { get; private set; } - protected override bool IsValidSnippetLocation(in SnippetContext context, CancellationToken cancellationToken) + protected override bool IsValidSnippetLocation(SnippetContext context, CancellationToken cancellationToken) { var syntaxContext = context.SyntaxContext; var semanticModel = syntaxContext.SemanticModel; @@ -51,7 +51,7 @@ protected override bool IsValidSnippetLocation(in SnippetContext context, Cancel return IsValidAccessingType(type, semanticModel.Compilation); } - return base.IsValidSnippetLocation(in context, cancellationToken); + return base.IsValidSnippetLocation(context, cancellationToken); } protected sealed override async Task GenerateSnippetTextChangeAsync(Document document, int position, CancellationToken cancellationToken) diff --git a/src/Features/Core/Portable/Snippets/SnippetProviders/AbstractSnippetProvider.cs b/src/Features/Core/Portable/Snippets/SnippetProviders/AbstractSnippetProvider.cs index 85b01302fe601..1b262ba702aad 100644 --- a/src/Features/Core/Portable/Snippets/SnippetProviders/AbstractSnippetProvider.cs +++ b/src/Features/Core/Portable/Snippets/SnippetProviders/AbstractSnippetProvider.cs @@ -33,7 +33,7 @@ internal abstract class AbstractSnippetProvider : ISnippetProvid /// Implemented by each SnippetProvider to determine if that particular position is a valid /// location for the snippet to be inserted. /// - protected abstract bool IsValidSnippetLocation(in SnippetContext context, CancellationToken cancellationToken); + protected abstract bool IsValidSnippetLocation(SnippetContext context, CancellationToken cancellationToken); /// /// Generates the new snippet's TextChanges that are being inserted into the document. @@ -50,7 +50,7 @@ internal abstract class AbstractSnippetProvider : ISnippetProvid /// protected abstract ImmutableArray GetPlaceHolderLocationsList(TSnippetSyntax node, ISyntaxFacts syntaxFacts, CancellationToken cancellationToken); - public ValueTask IsValidSnippetLocationAsync(in SnippetContext context, CancellationToken cancellationToken) + public ValueTask IsValidSnippetLocationAsync(SnippetContext context, CancellationToken cancellationToken) { var syntaxFacts = context.Document.GetRequiredLanguageService(); var syntaxTree = context.SyntaxContext.SyntaxTree; @@ -59,7 +59,7 @@ public ValueTask IsValidSnippetLocationAsync(in SnippetContext context, Ca return ValueTaskFactory.FromResult(false); } - return ValueTaskFactory.FromResult(IsValidSnippetLocation(in context, cancellationToken)); + return ValueTaskFactory.FromResult(IsValidSnippetLocation(context, cancellationToken)); } /// diff --git a/src/Features/Core/Portable/Snippets/SnippetProviders/AbstractStatementSnippetProvider.cs b/src/Features/Core/Portable/Snippets/SnippetProviders/AbstractStatementSnippetProvider.cs index 16b98b988c8e9..410d9a568e049 100644 --- a/src/Features/Core/Portable/Snippets/SnippetProviders/AbstractStatementSnippetProvider.cs +++ b/src/Features/Core/Portable/Snippets/SnippetProviders/AbstractStatementSnippetProvider.cs @@ -9,6 +9,6 @@ namespace Microsoft.CodeAnalysis.Snippets.SnippetProviders; internal abstract class AbstractStatementSnippetProvider : AbstractSingleChangeSnippetProvider where TStatementSyntax : SyntaxNode { - protected override bool IsValidSnippetLocation(in SnippetContext context, CancellationToken cancellationToken) + protected override bool IsValidSnippetLocation(SnippetContext context, CancellationToken cancellationToken) => context.SyntaxContext.IsStatementContext || context.SyntaxContext.IsGlobalStatementContext; } diff --git a/src/Features/Core/Portable/Snippets/SnippetProviders/ISnippetProvider.cs b/src/Features/Core/Portable/Snippets/SnippetProviders/ISnippetProvider.cs index 5c6d298db3e7b..a769b8eb2892b 100644 --- a/src/Features/Core/Portable/Snippets/SnippetProviders/ISnippetProvider.cs +++ b/src/Features/Core/Portable/Snippets/SnippetProviders/ISnippetProvider.cs @@ -28,7 +28,7 @@ internal interface ISnippetProvider /// /// Determines if a snippet can exist at a particular location. /// - ValueTask IsValidSnippetLocationAsync(in SnippetContext context, CancellationToken cancellationToken); + ValueTask IsValidSnippetLocationAsync(SnippetContext context, CancellationToken cancellationToken); /// /// Gets the Snippet change from the corresponding snippet provider. From 9ec86b45a6271ec58e0e421218f631e93e3471bf Mon Sep 17 00:00:00 2001 From: DoctorKrolic Date: Thu, 15 Aug 2024 09:28:29 +0300 Subject: [PATCH 2/3] Rename --- .../Snippets/AbstractCSharpAutoPropertySnippetProvider.cs | 2 +- .../Snippets/AbstractCSharpMainMethodSnippetProvider.cs | 2 +- .../Portable/Snippets/AbstractCSharpTypeSnippetProvider.cs | 2 +- .../CSharp/Portable/Snippets/CSharpConsoleSnippetProvider.cs | 2 +- .../Portable/Snippets/CSharpConstructorSnippetProvider.cs | 2 +- .../CSharp/Portable/Snippets/CSharpElseSnippetProvider.cs | 4 ++-- .../Portable/Snippets/CSharpForEachLoopSnippetProvider.cs | 4 ++-- .../AbstractInlineStatementSnippetProvider.cs | 4 ++-- .../Snippets/SnippetProviders/AbstractSnippetProvider.cs | 4 ++-- .../SnippetProviders/AbstractStatementSnippetProvider.cs | 2 +- 10 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/Features/CSharp/Portable/Snippets/AbstractCSharpAutoPropertySnippetProvider.cs b/src/Features/CSharp/Portable/Snippets/AbstractCSharpAutoPropertySnippetProvider.cs index a32aeaedc2a8e..9e6a532dd26f2 100644 --- a/src/Features/CSharp/Portable/Snippets/AbstractCSharpAutoPropertySnippetProvider.cs +++ b/src/Features/CSharp/Portable/Snippets/AbstractCSharpAutoPropertySnippetProvider.cs @@ -30,7 +30,7 @@ internal abstract class AbstractCSharpAutoPropertySnippetProvider : AbstractProp protected virtual AccessorDeclarationSyntax? GenerateSetAccessorDeclaration(CSharpSyntaxContext syntaxContext, SyntaxGenerator generator, CancellationToken cancellationToken) => (AccessorDeclarationSyntax)generator.SetAccessorDeclaration(); - protected override bool IsValidSnippetLocation(SnippetContext context, CancellationToken cancellationToken) + protected override bool IsValidSnippetLocationCore(SnippetContext context, CancellationToken cancellationToken) { return context.SyntaxContext.SyntaxTree.IsMemberDeclarationContext(context.Position, (CSharpSyntaxContext)context.SyntaxContext, SyntaxKindSet.AllMemberModifiers, SyntaxKindSet.ClassInterfaceStructRecordTypeDeclarations, canBePartial: true, cancellationToken); diff --git a/src/Features/CSharp/Portable/Snippets/AbstractCSharpMainMethodSnippetProvider.cs b/src/Features/CSharp/Portable/Snippets/AbstractCSharpMainMethodSnippetProvider.cs index 00f8d27d85fff..6dd5af1b27d1d 100644 --- a/src/Features/CSharp/Portable/Snippets/AbstractCSharpMainMethodSnippetProvider.cs +++ b/src/Features/CSharp/Portable/Snippets/AbstractCSharpMainMethodSnippetProvider.cs @@ -15,7 +15,7 @@ namespace Microsoft.CodeAnalysis.CSharp.Snippets; internal abstract class AbstractCSharpMainMethodSnippetProvider : AbstractMainMethodSnippetProvider { - protected override bool IsValidSnippetLocation(SnippetContext context, CancellationToken cancellationToken) + protected override bool IsValidSnippetLocationCore(SnippetContext context, CancellationToken cancellationToken) { var semanticModel = context.SyntaxContext.SemanticModel; var syntaxContext = (CSharpSyntaxContext)context.SyntaxContext; diff --git a/src/Features/CSharp/Portable/Snippets/AbstractCSharpTypeSnippetProvider.cs b/src/Features/CSharp/Portable/Snippets/AbstractCSharpTypeSnippetProvider.cs index 7a9f558b4d683..af4945b17c570 100644 --- a/src/Features/CSharp/Portable/Snippets/AbstractCSharpTypeSnippetProvider.cs +++ b/src/Features/CSharp/Portable/Snippets/AbstractCSharpTypeSnippetProvider.cs @@ -27,7 +27,7 @@ internal abstract class AbstractCSharpTypeSnippetProvider ValidModifiers { get; } - protected override bool IsValidSnippetLocation(SnippetContext context, CancellationToken cancellationToken) + protected override bool IsValidSnippetLocationCore(SnippetContext context, CancellationToken cancellationToken) { var syntaxContext = (CSharpSyntaxContext)context.SyntaxContext; diff --git a/src/Features/CSharp/Portable/Snippets/CSharpConsoleSnippetProvider.cs b/src/Features/CSharp/Portable/Snippets/CSharpConsoleSnippetProvider.cs index 265c6c0ad5d18..b45ca8f2e9dec 100644 --- a/src/Features/CSharp/Portable/Snippets/CSharpConsoleSnippetProvider.cs +++ b/src/Features/CSharp/Portable/Snippets/CSharpConsoleSnippetProvider.cs @@ -21,7 +21,7 @@ internal sealed class CSharpConsoleSnippetProvider() : AbstractConsoleSnippetPro ArgumentListSyntax, LambdaExpressionSyntax> { - protected override bool IsValidSnippetLocation(SnippetContext context, CancellationToken cancellationToken) + protected override bool IsValidSnippetLocationCore(SnippetContext context, CancellationToken cancellationToken) { var syntaxContext = context.SyntaxContext; diff --git a/src/Features/CSharp/Portable/Snippets/CSharpConstructorSnippetProvider.cs b/src/Features/CSharp/Portable/Snippets/CSharpConstructorSnippetProvider.cs index 4bf866aa9635d..f885a980d0a48 100644 --- a/src/Features/CSharp/Portable/Snippets/CSharpConstructorSnippetProvider.cs +++ b/src/Features/CSharp/Portable/Snippets/CSharpConstructorSnippetProvider.cs @@ -36,7 +36,7 @@ internal sealed class CSharpConstructorSnippetProvider() : AbstractConstructorSn SyntaxKind.StaticKeyword, }; - protected override bool IsValidSnippetLocation(SnippetContext context, CancellationToken cancellationToken) + protected override bool IsValidSnippetLocationCore(SnippetContext context, CancellationToken cancellationToken) { var syntaxContext = (CSharpSyntaxContext)context.SyntaxContext; diff --git a/src/Features/CSharp/Portable/Snippets/CSharpElseSnippetProvider.cs b/src/Features/CSharp/Portable/Snippets/CSharpElseSnippetProvider.cs index fc224ed036b85..a720e79571b8d 100644 --- a/src/Features/CSharp/Portable/Snippets/CSharpElseSnippetProvider.cs +++ b/src/Features/CSharp/Portable/Snippets/CSharpElseSnippetProvider.cs @@ -24,7 +24,7 @@ internal sealed class CSharpElseSnippetProvider() : AbstractElseSnippetProvider< public override string Description => FeaturesResources.else_statement; - protected override bool IsValidSnippetLocation(SnippetContext context, CancellationToken cancellationToken) + protected override bool IsValidSnippetLocationCore(SnippetContext context, CancellationToken cancellationToken) { var syntaxContext = context.SyntaxContext; var token = syntaxContext.TargetToken; @@ -51,7 +51,7 @@ protected override bool IsValidSnippetLocation(SnippetContext context, Cancellat } } - return isAfterIfStatement && base.IsValidSnippetLocation(context, cancellationToken); + return isAfterIfStatement && base.IsValidSnippetLocationCore(context, cancellationToken); } protected override Task GenerateSnippetTextChangeAsync(Document document, int position, CancellationToken cancellationToken) diff --git a/src/Features/CSharp/Portable/Snippets/CSharpForEachLoopSnippetProvider.cs b/src/Features/CSharp/Portable/Snippets/CSharpForEachLoopSnippetProvider.cs index 322099fcf0796..32c64990213fc 100644 --- a/src/Features/CSharp/Portable/Snippets/CSharpForEachLoopSnippetProvider.cs +++ b/src/Features/CSharp/Portable/Snippets/CSharpForEachLoopSnippetProvider.cs @@ -34,7 +34,7 @@ internal sealed class CSharpForEachLoopSnippetProvider() : AbstractForEachLoopSn public override string Description => FeaturesResources.foreach_loop; - protected override bool IsValidSnippetLocation(SnippetContext context, CancellationToken cancellationToken) + protected override bool IsValidSnippetLocationCore(SnippetContext context, CancellationToken cancellationToken) { var syntaxContext = context.SyntaxContext; var token = syntaxContext.TargetToken; @@ -48,7 +48,7 @@ protected override bool IsValidSnippetLocation(SnippetContext context, Cancellat return true; } - return base.IsValidSnippetLocation(context, cancellationToken); + return base.IsValidSnippetLocationCore(context, cancellationToken); } protected override ForEachStatementSyntax GenerateStatement(SyntaxGenerator generator, SyntaxContext syntaxContext, InlineExpressionInfo? inlineExpressionInfo) diff --git a/src/Features/Core/Portable/Snippets/SnippetProviders/AbstractInlineStatementSnippetProvider.cs b/src/Features/Core/Portable/Snippets/SnippetProviders/AbstractInlineStatementSnippetProvider.cs index 1bd0eabb2d848..c5603d35c862b 100644 --- a/src/Features/Core/Portable/Snippets/SnippetProviders/AbstractInlineStatementSnippetProvider.cs +++ b/src/Features/Core/Portable/Snippets/SnippetProviders/AbstractInlineStatementSnippetProvider.cs @@ -39,7 +39,7 @@ internal abstract class AbstractInlineStatementSnippetProvider /// protected bool ConstructedFromInlineExpression { get; private set; } - protected override bool IsValidSnippetLocation(SnippetContext context, CancellationToken cancellationToken) + protected override bool IsValidSnippetLocationCore(SnippetContext context, CancellationToken cancellationToken) { var syntaxContext = context.SyntaxContext; var semanticModel = syntaxContext.SemanticModel; @@ -51,7 +51,7 @@ protected override bool IsValidSnippetLocation(SnippetContext context, Cancellat return IsValidAccessingType(type, semanticModel.Compilation); } - return base.IsValidSnippetLocation(context, cancellationToken); + return base.IsValidSnippetLocationCore(context, cancellationToken); } protected sealed override async Task GenerateSnippetTextChangeAsync(Document document, int position, CancellationToken cancellationToken) diff --git a/src/Features/Core/Portable/Snippets/SnippetProviders/AbstractSnippetProvider.cs b/src/Features/Core/Portable/Snippets/SnippetProviders/AbstractSnippetProvider.cs index 1b262ba702aad..86f00174ac4d8 100644 --- a/src/Features/Core/Portable/Snippets/SnippetProviders/AbstractSnippetProvider.cs +++ b/src/Features/Core/Portable/Snippets/SnippetProviders/AbstractSnippetProvider.cs @@ -33,7 +33,7 @@ internal abstract class AbstractSnippetProvider : ISnippetProvid /// Implemented by each SnippetProvider to determine if that particular position is a valid /// location for the snippet to be inserted. /// - protected abstract bool IsValidSnippetLocation(SnippetContext context, CancellationToken cancellationToken); + protected abstract bool IsValidSnippetLocationCore(SnippetContext context, CancellationToken cancellationToken); /// /// Generates the new snippet's TextChanges that are being inserted into the document. @@ -59,7 +59,7 @@ public ValueTask IsValidSnippetLocationAsync(SnippetContext context, Cance return ValueTaskFactory.FromResult(false); } - return ValueTaskFactory.FromResult(IsValidSnippetLocation(context, cancellationToken)); + return ValueTaskFactory.FromResult(IsValidSnippetLocationCore(context, cancellationToken)); } /// diff --git a/src/Features/Core/Portable/Snippets/SnippetProviders/AbstractStatementSnippetProvider.cs b/src/Features/Core/Portable/Snippets/SnippetProviders/AbstractStatementSnippetProvider.cs index 410d9a568e049..2cc624a4e736f 100644 --- a/src/Features/Core/Portable/Snippets/SnippetProviders/AbstractStatementSnippetProvider.cs +++ b/src/Features/Core/Portable/Snippets/SnippetProviders/AbstractStatementSnippetProvider.cs @@ -9,6 +9,6 @@ namespace Microsoft.CodeAnalysis.Snippets.SnippetProviders; internal abstract class AbstractStatementSnippetProvider : AbstractSingleChangeSnippetProvider where TStatementSyntax : SyntaxNode { - protected override bool IsValidSnippetLocation(SnippetContext context, CancellationToken cancellationToken) + protected override bool IsValidSnippetLocationCore(SnippetContext context, CancellationToken cancellationToken) => context.SyntaxContext.IsStatementContext || context.SyntaxContext.IsGlobalStatementContext; } From 7d5e4464de70cfb371903058da2c322f7eac69fb Mon Sep 17 00:00:00 2001 From: DoctorKrolic Date: Thu, 15 Aug 2024 09:41:13 +0300 Subject: [PATCH 3/3] Expose `SemanticModel` in `SnippetContext` and make multiple code paths sync --- .../Snippets/AbstractCSharpMainMethodSnippetProvider.cs | 2 +- .../Portable/Snippets/CSharpConsoleSnippetProvider.cs | 4 ++-- .../Providers/Snippets/AbstractSnippetCompletionProvider.cs | 2 +- .../Core/Portable/Snippets/AbstractSnippetService.cs | 5 ++--- src/Features/Core/Portable/Snippets/ISnippetService.cs | 3 +-- src/Features/Core/Portable/Snippets/SnippetContext.cs | 5 +++++ .../AbstractInlineStatementSnippetProvider.cs | 2 +- .../Snippets/SnippetProviders/AbstractSnippetProvider.cs | 6 +++--- .../Portable/Snippets/SnippetProviders/ISnippetProvider.cs | 2 +- 9 files changed, 17 insertions(+), 14 deletions(-) diff --git a/src/Features/CSharp/Portable/Snippets/AbstractCSharpMainMethodSnippetProvider.cs b/src/Features/CSharp/Portable/Snippets/AbstractCSharpMainMethodSnippetProvider.cs index 6dd5af1b27d1d..cc9cce7497814 100644 --- a/src/Features/CSharp/Portable/Snippets/AbstractCSharpMainMethodSnippetProvider.cs +++ b/src/Features/CSharp/Portable/Snippets/AbstractCSharpMainMethodSnippetProvider.cs @@ -17,7 +17,7 @@ internal abstract class AbstractCSharpMainMethodSnippetProvider { protected override bool IsValidSnippetLocationCore(SnippetContext context, CancellationToken cancellationToken) { - var semanticModel = context.SyntaxContext.SemanticModel; + var semanticModel = context.SemanticModel; var syntaxContext = (CSharpSyntaxContext)context.SyntaxContext; if (!syntaxContext.IsMemberDeclarationContext( diff --git a/src/Features/CSharp/Portable/Snippets/CSharpConsoleSnippetProvider.cs b/src/Features/CSharp/Portable/Snippets/CSharpConsoleSnippetProvider.cs index b45ca8f2e9dec..0a91a98893177 100644 --- a/src/Features/CSharp/Portable/Snippets/CSharpConsoleSnippetProvider.cs +++ b/src/Features/CSharp/Portable/Snippets/CSharpConsoleSnippetProvider.cs @@ -24,8 +24,9 @@ internal sealed class CSharpConsoleSnippetProvider() : AbstractConsoleSnippetPro protected override bool IsValidSnippetLocationCore(SnippetContext context, CancellationToken cancellationToken) { var syntaxContext = context.SyntaxContext; + var semanticModel = context.SemanticModel; - var consoleSymbol = GetConsoleSymbolFromMetaDataName(syntaxContext.SemanticModel.Compilation); + var consoleSymbol = GetConsoleSymbolFromMetaDataName(semanticModel.Compilation); if (consoleSymbol is null) return false; @@ -33,7 +34,6 @@ protected override bool IsValidSnippetLocationCore(SnippetContext context, Cance // Action a = () => Console.WriteLine("Action called"); if (syntaxContext.TargetToken is { RawKind: (int)SyntaxKind.EqualsGreaterThanToken, Parent: LambdaExpressionSyntax lambda }) { - var semanticModel = syntaxContext.SemanticModel; var lambdaSymbol = semanticModel.GetSymbolInfo(lambda, cancellationToken).Symbol; // Given that we are in a partially written lambda state compiler might not always infer return type correctly. diff --git a/src/Features/Core/Portable/Completion/Providers/Snippets/AbstractSnippetCompletionProvider.cs b/src/Features/Core/Portable/Completion/Providers/Snippets/AbstractSnippetCompletionProvider.cs index d22d15178c26a..ecc4b1fedba12 100644 --- a/src/Features/Core/Portable/Completion/Providers/Snippets/AbstractSnippetCompletionProvider.cs +++ b/src/Features/Core/Portable/Completion/Providers/Snippets/AbstractSnippetCompletionProvider.cs @@ -82,7 +82,7 @@ public override async Task ProvideCompletionsAsync(CompletionContext context) var syntaxContext = await context.GetSyntaxContextWithExistingSpeculativeModelAsync(document, cancellationToken).ConfigureAwait(false); var snippetContext = new SnippetContext(syntaxContext); - var snippets = await service.GetSnippetsAsync(snippetContext, cancellationToken).ConfigureAwait(false); + var snippets = service.GetSnippets(snippetContext, cancellationToken); foreach (var snippetData in snippets) { diff --git a/src/Features/Core/Portable/Snippets/AbstractSnippetService.cs b/src/Features/Core/Portable/Snippets/AbstractSnippetService.cs index 4146842d32ec1..8ee61c89eb4bc 100644 --- a/src/Features/Core/Portable/Snippets/AbstractSnippetService.cs +++ b/src/Features/Core/Portable/Snippets/AbstractSnippetService.cs @@ -8,7 +8,6 @@ using System.Diagnostics; using System.Linq; using System.Threading; -using System.Threading.Tasks; using Microsoft.CodeAnalysis.Host.Mef; using Microsoft.CodeAnalysis.PooledObjects; using Microsoft.CodeAnalysis.Snippets.SnippetProviders; @@ -37,12 +36,12 @@ public ISnippetProvider GetSnippetProvider(string snippetIdentifier) /// Iterates through all providers and determines if the snippet /// can be added to the Completion list at the corresponding position. /// - public async Task> GetSnippetsAsync(SnippetContext context, CancellationToken cancellationToken) + public ImmutableArray GetSnippets(SnippetContext context, CancellationToken cancellationToken) { using var _ = ArrayBuilder.GetInstance(out var arrayBuilder); foreach (var provider in GetSnippetProviders(context.Document)) { - if (await provider.IsValidSnippetLocationAsync(context, cancellationToken).ConfigureAwait(false)) + if (provider.IsValidSnippetLocation(context, cancellationToken)) arrayBuilder.Add(new(provider.Identifier, provider.Description, provider.AdditionalFilterTexts)); } diff --git a/src/Features/Core/Portable/Snippets/ISnippetService.cs b/src/Features/Core/Portable/Snippets/ISnippetService.cs index b8e45d4bc0812..53f0f1aba624a 100644 --- a/src/Features/Core/Portable/Snippets/ISnippetService.cs +++ b/src/Features/Core/Portable/Snippets/ISnippetService.cs @@ -4,7 +4,6 @@ using System.Collections.Immutable; using System.Threading; -using System.Threading.Tasks; using Microsoft.CodeAnalysis.Host; using Microsoft.CodeAnalysis.Snippets.SnippetProviders; @@ -15,7 +14,7 @@ internal interface ISnippetService : ILanguageService /// /// Retrieves all possible types of snippets for a particular position /// - Task> GetSnippetsAsync(SnippetContext context, CancellationToken cancellationToken); + ImmutableArray GetSnippets(SnippetContext context, CancellationToken cancellationToken); /// /// Gets the corresponding provider from a snippet identifier. diff --git a/src/Features/Core/Portable/Snippets/SnippetContext.cs b/src/Features/Core/Portable/Snippets/SnippetContext.cs index 58d6437794bfe..034a4e0e10fef 100644 --- a/src/Features/Core/Portable/Snippets/SnippetContext.cs +++ b/src/Features/Core/Portable/Snippets/SnippetContext.cs @@ -28,5 +28,10 @@ internal SnippetContext(SyntaxContext syntaxContext) /// public int Position => SyntaxContext.Position; + /// + /// The semantic model of the document. + /// + public SemanticModel SemanticModel => SyntaxContext.SemanticModel; + internal SyntaxContext SyntaxContext { get; } } diff --git a/src/Features/Core/Portable/Snippets/SnippetProviders/AbstractInlineStatementSnippetProvider.cs b/src/Features/Core/Portable/Snippets/SnippetProviders/AbstractInlineStatementSnippetProvider.cs index c5603d35c862b..f29734df6fd92 100644 --- a/src/Features/Core/Portable/Snippets/SnippetProviders/AbstractInlineStatementSnippetProvider.cs +++ b/src/Features/Core/Portable/Snippets/SnippetProviders/AbstractInlineStatementSnippetProvider.cs @@ -42,7 +42,7 @@ internal abstract class AbstractInlineStatementSnippetProvider protected override bool IsValidSnippetLocationCore(SnippetContext context, CancellationToken cancellationToken) { var syntaxContext = context.SyntaxContext; - var semanticModel = syntaxContext.SemanticModel; + var semanticModel = context.SemanticModel; var targetToken = syntaxContext.TargetToken; var syntaxFacts = context.Document.GetRequiredLanguageService(); diff --git a/src/Features/Core/Portable/Snippets/SnippetProviders/AbstractSnippetProvider.cs b/src/Features/Core/Portable/Snippets/SnippetProviders/AbstractSnippetProvider.cs index 86f00174ac4d8..7dd77c9c9d2a7 100644 --- a/src/Features/Core/Portable/Snippets/SnippetProviders/AbstractSnippetProvider.cs +++ b/src/Features/Core/Portable/Snippets/SnippetProviders/AbstractSnippetProvider.cs @@ -50,16 +50,16 @@ internal abstract class AbstractSnippetProvider : ISnippetProvid /// protected abstract ImmutableArray GetPlaceHolderLocationsList(TSnippetSyntax node, ISyntaxFacts syntaxFacts, CancellationToken cancellationToken); - public ValueTask IsValidSnippetLocationAsync(SnippetContext context, CancellationToken cancellationToken) + public bool IsValidSnippetLocation(SnippetContext context, CancellationToken cancellationToken) { var syntaxFacts = context.Document.GetRequiredLanguageService(); var syntaxTree = context.SyntaxContext.SyntaxTree; if (syntaxFacts.IsInNonUserCode(syntaxTree, context.Position, cancellationToken)) { - return ValueTaskFactory.FromResult(false); + return false; } - return ValueTaskFactory.FromResult(IsValidSnippetLocationCore(context, cancellationToken)); + return IsValidSnippetLocationCore(context, cancellationToken); } /// diff --git a/src/Features/Core/Portable/Snippets/SnippetProviders/ISnippetProvider.cs b/src/Features/Core/Portable/Snippets/SnippetProviders/ISnippetProvider.cs index a769b8eb2892b..56052399364ae 100644 --- a/src/Features/Core/Portable/Snippets/SnippetProviders/ISnippetProvider.cs +++ b/src/Features/Core/Portable/Snippets/SnippetProviders/ISnippetProvider.cs @@ -28,7 +28,7 @@ internal interface ISnippetProvider /// /// Determines if a snippet can exist at a particular location. /// - ValueTask IsValidSnippetLocationAsync(SnippetContext context, CancellationToken cancellationToken); + bool IsValidSnippetLocation(SnippetContext context, CancellationToken cancellationToken); /// /// Gets the Snippet change from the corresponding snippet provider.