From 71f937309343956c8e3ccdf0b03c77a53bdc9483 Mon Sep 17 00:00:00 2001 From: Shen Chen Date: Wed, 26 Apr 2023 11:12:53 -0700 Subject: [PATCH 1/6] Add missing rename service --- .../Microsoft.CodeAnalysis.Features.csproj | 1 + ...eActionOperationFactoryWorkspaceService.cs | 48 +++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 src/Features/LanguageServer/Microsoft.CodeAnalysis.LanguageServer/Services/Rename/LanguageServerSymbolRenamedCodeActionOperationFactoryWorkspaceService.cs diff --git a/src/Features/Core/Portable/Microsoft.CodeAnalysis.Features.csproj b/src/Features/Core/Portable/Microsoft.CodeAnalysis.Features.csproj index 3b57a428d8359..0bf24142cafac 100644 --- a/src/Features/Core/Portable/Microsoft.CodeAnalysis.Features.csproj +++ b/src/Features/Core/Portable/Microsoft.CodeAnalysis.Features.csproj @@ -83,6 +83,7 @@ + diff --git a/src/Features/LanguageServer/Microsoft.CodeAnalysis.LanguageServer/Services/Rename/LanguageServerSymbolRenamedCodeActionOperationFactoryWorkspaceService.cs b/src/Features/LanguageServer/Microsoft.CodeAnalysis.LanguageServer/Services/Rename/LanguageServerSymbolRenamedCodeActionOperationFactoryWorkspaceService.cs new file mode 100644 index 0000000000000..76bb62f588bbd --- /dev/null +++ b/src/Features/LanguageServer/Microsoft.CodeAnalysis.LanguageServer/Services/Rename/LanguageServerSymbolRenamedCodeActionOperationFactoryWorkspaceService.cs @@ -0,0 +1,48 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Collections.Generic; +using System.Composition; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Microsoft.CodeAnalysis.CodeActions; +using Microsoft.CodeAnalysis.CodeActions.WorkspaceServices; +using Microsoft.CodeAnalysis.Host.Mef; + +namespace Microsoft.CodeAnalysis.LanguageServer.Services.Rename +{ + [ExportWorkspaceService(typeof(ISymbolRenamedCodeActionOperationFactoryWorkspaceService), ServiceLayer.Host), Shared] + internal class LanguageServerSymbolRenamedCodeActionOperationFactoryWorkspaceService : ISymbolRenamedCodeActionOperationFactoryWorkspaceService + { + [ImportingConstructor] + [Obsolete(MefConstruction.ImportingConstructorMessage, error: true)] + public LanguageServerSymbolRenamedCodeActionOperationFactoryWorkspaceService() + { + } + + public CodeActionOperation CreateSymbolRenamedOperation(ISymbol symbol, string newName, Solution startingSolution, Solution updatedSolution) + => new RenameCodeActionOperation( + title: string.Format(WorkspacesResources.Rename_0_to_1, symbol.Name, newName), + updateSolution: updatedSolution); + + private class RenameCodeActionOperation : CodeActionOperation + { + private readonly string _title; + private readonly Solution _updateSolution; + + public RenameCodeActionOperation(string title, Solution updateSolution) + { + _title = title; + _updateSolution = updateSolution; + } + + public override void Apply(Workspace workspace, CancellationToken cancellationToken = default) + => workspace.TryApplyChanges(_updateSolution); + + public override string? Title => _title; + } + } +} From 464a5993b8bc52c6d5195653cdf2839176b2195a Mon Sep 17 00:00:00 2001 From: Shen Chen Date: Wed, 26 Apr 2023 11:49:29 -0700 Subject: [PATCH 2/6] Change the ApplyChangesOperation --- ...eActionOperationFactoryWorkspaceService.cs | 21 +------------------ 1 file changed, 1 insertion(+), 20 deletions(-) diff --git a/src/Features/LanguageServer/Microsoft.CodeAnalysis.LanguageServer/Services/Rename/LanguageServerSymbolRenamedCodeActionOperationFactoryWorkspaceService.cs b/src/Features/LanguageServer/Microsoft.CodeAnalysis.LanguageServer/Services/Rename/LanguageServerSymbolRenamedCodeActionOperationFactoryWorkspaceService.cs index 76bb62f588bbd..d656ebe8fcd50 100644 --- a/src/Features/LanguageServer/Microsoft.CodeAnalysis.LanguageServer/Services/Rename/LanguageServerSymbolRenamedCodeActionOperationFactoryWorkspaceService.cs +++ b/src/Features/LanguageServer/Microsoft.CodeAnalysis.LanguageServer/Services/Rename/LanguageServerSymbolRenamedCodeActionOperationFactoryWorkspaceService.cs @@ -24,25 +24,6 @@ public LanguageServerSymbolRenamedCodeActionOperationFactoryWorkspaceService() } public CodeActionOperation CreateSymbolRenamedOperation(ISymbol symbol, string newName, Solution startingSolution, Solution updatedSolution) - => new RenameCodeActionOperation( - title: string.Format(WorkspacesResources.Rename_0_to_1, symbol.Name, newName), - updateSolution: updatedSolution); - - private class RenameCodeActionOperation : CodeActionOperation - { - private readonly string _title; - private readonly Solution _updateSolution; - - public RenameCodeActionOperation(string title, Solution updateSolution) - { - _title = title; - _updateSolution = updateSolution; - } - - public override void Apply(Workspace workspace, CancellationToken cancellationToken = default) - => workspace.TryApplyChanges(_updateSolution); - - public override string? Title => _title; - } + => new ApplyChangesOperation(updatedSolution); } } From 57b45425007615a2d9faab1d3f9e60130278b03b Mon Sep 17 00:00:00 2001 From: Shen Chen Date: Wed, 26 Apr 2023 12:22:03 -0700 Subject: [PATCH 3/6] Revert "Change the ApplyChangesOperation" This reverts commit 6a8d498a5712ad4f646c7ef29f279fb490943faa. --- ...eActionOperationFactoryWorkspaceService.cs | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/Features/LanguageServer/Microsoft.CodeAnalysis.LanguageServer/Services/Rename/LanguageServerSymbolRenamedCodeActionOperationFactoryWorkspaceService.cs b/src/Features/LanguageServer/Microsoft.CodeAnalysis.LanguageServer/Services/Rename/LanguageServerSymbolRenamedCodeActionOperationFactoryWorkspaceService.cs index d656ebe8fcd50..76bb62f588bbd 100644 --- a/src/Features/LanguageServer/Microsoft.CodeAnalysis.LanguageServer/Services/Rename/LanguageServerSymbolRenamedCodeActionOperationFactoryWorkspaceService.cs +++ b/src/Features/LanguageServer/Microsoft.CodeAnalysis.LanguageServer/Services/Rename/LanguageServerSymbolRenamedCodeActionOperationFactoryWorkspaceService.cs @@ -24,6 +24,25 @@ public LanguageServerSymbolRenamedCodeActionOperationFactoryWorkspaceService() } public CodeActionOperation CreateSymbolRenamedOperation(ISymbol symbol, string newName, Solution startingSolution, Solution updatedSolution) - => new ApplyChangesOperation(updatedSolution); + => new RenameCodeActionOperation( + title: string.Format(WorkspacesResources.Rename_0_to_1, symbol.Name, newName), + updateSolution: updatedSolution); + + private class RenameCodeActionOperation : CodeActionOperation + { + private readonly string _title; + private readonly Solution _updateSolution; + + public RenameCodeActionOperation(string title, Solution updateSolution) + { + _title = title; + _updateSolution = updateSolution; + } + + public override void Apply(Workspace workspace, CancellationToken cancellationToken = default) + => workspace.TryApplyChanges(_updateSolution); + + public override string? Title => _title; + } } } From 7aba70a78b03009116cf7e0b9f00d96d1e6ec108 Mon Sep 17 00:00:00 2001 From: Shen Chen Date: Wed, 26 Apr 2023 12:22:05 -0700 Subject: [PATCH 4/6] Revert "Add missing rename service" This reverts commit 0c21c57878f745289946df71aab0434043531047. --- .../Microsoft.CodeAnalysis.Features.csproj | 1 - ...eActionOperationFactoryWorkspaceService.cs | 48 ------------------- 2 files changed, 49 deletions(-) delete mode 100644 src/Features/LanguageServer/Microsoft.CodeAnalysis.LanguageServer/Services/Rename/LanguageServerSymbolRenamedCodeActionOperationFactoryWorkspaceService.cs diff --git a/src/Features/Core/Portable/Microsoft.CodeAnalysis.Features.csproj b/src/Features/Core/Portable/Microsoft.CodeAnalysis.Features.csproj index 0bf24142cafac..3b57a428d8359 100644 --- a/src/Features/Core/Portable/Microsoft.CodeAnalysis.Features.csproj +++ b/src/Features/Core/Portable/Microsoft.CodeAnalysis.Features.csproj @@ -83,7 +83,6 @@ - diff --git a/src/Features/LanguageServer/Microsoft.CodeAnalysis.LanguageServer/Services/Rename/LanguageServerSymbolRenamedCodeActionOperationFactoryWorkspaceService.cs b/src/Features/LanguageServer/Microsoft.CodeAnalysis.LanguageServer/Services/Rename/LanguageServerSymbolRenamedCodeActionOperationFactoryWorkspaceService.cs deleted file mode 100644 index 76bb62f588bbd..0000000000000 --- a/src/Features/LanguageServer/Microsoft.CodeAnalysis.LanguageServer/Services/Rename/LanguageServerSymbolRenamedCodeActionOperationFactoryWorkspaceService.cs +++ /dev/null @@ -1,48 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using System; -using System.Collections.Generic; -using System.Composition; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using Microsoft.CodeAnalysis.CodeActions; -using Microsoft.CodeAnalysis.CodeActions.WorkspaceServices; -using Microsoft.CodeAnalysis.Host.Mef; - -namespace Microsoft.CodeAnalysis.LanguageServer.Services.Rename -{ - [ExportWorkspaceService(typeof(ISymbolRenamedCodeActionOperationFactoryWorkspaceService), ServiceLayer.Host), Shared] - internal class LanguageServerSymbolRenamedCodeActionOperationFactoryWorkspaceService : ISymbolRenamedCodeActionOperationFactoryWorkspaceService - { - [ImportingConstructor] - [Obsolete(MefConstruction.ImportingConstructorMessage, error: true)] - public LanguageServerSymbolRenamedCodeActionOperationFactoryWorkspaceService() - { - } - - public CodeActionOperation CreateSymbolRenamedOperation(ISymbol symbol, string newName, Solution startingSolution, Solution updatedSolution) - => new RenameCodeActionOperation( - title: string.Format(WorkspacesResources.Rename_0_to_1, symbol.Name, newName), - updateSolution: updatedSolution); - - private class RenameCodeActionOperation : CodeActionOperation - { - private readonly string _title; - private readonly Solution _updateSolution; - - public RenameCodeActionOperation(string title, Solution updateSolution) - { - _title = title; - _updateSolution = updateSolution; - } - - public override void Apply(Workspace workspace, CancellationToken cancellationToken = default) - => workspace.TryApplyChanges(_updateSolution); - - public override string? Title => _title; - } - } -} From 05a141feb36e43f3894f39af086a6b132b348b19 Mon Sep 17 00:00:00 2001 From: Shen Chen Date: Wed, 26 Apr 2023 12:26:18 -0700 Subject: [PATCH 5/6] Make change based on if the service is provided or not --- .../NamingStyle/NamingStyleCodeFixProvider.cs | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/Analyzers/Core/CodeFixes/NamingStyle/NamingStyleCodeFixProvider.cs b/src/Analyzers/Core/CodeFixes/NamingStyle/NamingStyleCodeFixProvider.cs index b2d1f25479d08..28468d36ff716 100644 --- a/src/Analyzers/Core/CodeFixes/NamingStyle/NamingStyleCodeFixProvider.cs +++ b/src/Analyzers/Core/CodeFixes/NamingStyle/NamingStyleCodeFixProvider.cs @@ -163,12 +163,17 @@ protected override async Task> ComputeOperation #if CODE_STYLE // https://github.com/dotnet/roslyn/issues/42218 tracks removing this conditional code. return SpecializedCollections.SingletonEnumerable(codeAction); #else - var factory = _startingSolution.Services.GetRequiredService(); - return new CodeActionOperation[] + + using var _ = PooledObjects.ArrayBuilder.GetInstance(out var operations); + + operations.Add(codeAction); + var factory = _startingSolution.Services.GetService(); + if (factory is not null) { - codeAction, - factory.CreateSymbolRenamedOperation(_symbol, _newName, _startingSolution, newSolution) - }; + operations.Add(factory.CreateSymbolRenamedOperation(_symbol, _newName, _startingSolution, newSolution)); + } + + return operations.ToImmutable(); #endif } From 9b6958bf1183dda3a0112999cb7e69246666a698 Mon Sep 17 00:00:00 2001 From: Shen Chen Date: Thu, 4 May 2023 13:42:27 -0700 Subject: [PATCH 6/6] Address feedback --- .../Core/CodeFixes/NamingStyle/NamingStyleCodeFixProvider.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Analyzers/Core/CodeFixes/NamingStyle/NamingStyleCodeFixProvider.cs b/src/Analyzers/Core/CodeFixes/NamingStyle/NamingStyleCodeFixProvider.cs index 28468d36ff716..a055b6fdfed47 100644 --- a/src/Analyzers/Core/CodeFixes/NamingStyle/NamingStyleCodeFixProvider.cs +++ b/src/Analyzers/Core/CodeFixes/NamingStyle/NamingStyleCodeFixProvider.cs @@ -18,6 +18,7 @@ using Microsoft.CodeAnalysis.Rename; using Microsoft.CodeAnalysis.Shared.Extensions; using Roslyn.Utilities; +using Microsoft.CodeAnalysis.Shared.Collections; #if !CODE_STYLE // https://github.com/dotnet/roslyn/issues/42218 removing dependency on WorkspaceServices. using Microsoft.CodeAnalysis.CodeActions.WorkspaceServices; @@ -164,7 +165,7 @@ protected override async Task> ComputeOperation return SpecializedCollections.SingletonEnumerable(codeAction); #else - using var _ = PooledObjects.ArrayBuilder.GetInstance(out var operations); + using var operations = TemporaryArray.Empty; operations.Add(codeAction); var factory = _startingSolution.Services.GetService(); @@ -173,7 +174,7 @@ protected override async Task> ComputeOperation operations.Add(factory.CreateSymbolRenamedOperation(_symbol, _newName, _startingSolution, newSolution)); } - return operations.ToImmutable(); + return operations.ToImmutableAndClear(); #endif }