Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename API handles refactor notify #41126

Closed
wants to merge 12 commits into from
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@
// 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.Collections.Generic;
using System.Composition;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.Editor.Host;
using Microsoft.CodeAnalysis.Editor.Implementation.RenameTracking;
using Microsoft.VisualStudio.Text.Operations;

Expand Down Expand Up @@ -36,9 +34,8 @@ internal sealed class CSharpRenameTrackingCodeFixProvider : AbstractRenameTracki
{
[ImportingConstructor]
public CSharpRenameTrackingCodeFixProvider(
ITextUndoHistoryRegistry undoHistoryRegistry,
[ImportMany] IEnumerable<IRefactorNotifyService> refactorNotifyServices)
: base(undoHistoryRegistry, refactorNotifyServices)
ITextUndoHistoryRegistry undoHistoryRegistry)
: base(undoHistoryRegistry)
{
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,24 @@
// 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.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CodeFixes.NamingStyles;
using Microsoft.CodeAnalysis.CSharp.Diagnostics.NamingStyles;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Diagnostics.Analyzers.NamingStyles;
using Microsoft.CodeAnalysis.Editor.UnitTests;
using Microsoft.CodeAnalysis.Editor.UnitTests.Diagnostics.NamingStyles;
using Microsoft.CodeAnalysis.Editor.UnitTests.Workspaces;
using Microsoft.CodeAnalysis.Rename;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Test.Utilities;
using Microsoft.CodeAnalysis.Test.Utilities.Workspaces;
using Microsoft.VisualStudio.Composition;
using Roslyn.Test.Utilities;
using Xunit;

Expand All @@ -22,6 +29,16 @@ public class NamingStylesTests : AbstractCSharpDiagnosticProviderBasedUserDiagno
{
private readonly NamingStylesTestOptionSets options = new NamingStylesTestOptionSets(LanguageNames.CSharp);

private static readonly IExportProviderFactory ExportProviderFactory =
ExportProviderCache.GetOrCreateExportProviderFactory(
TestExportProvider.EntireAssemblyCatalogWithCSharpAndVisualBasic.WithPart(typeof(TestRefactorNotify)));

protected override TestWorkspace CreateWorkspaceFromFile(string initialMarkup, TestParameters parameters)
=> CreateWorkspaceFromFile(initialMarkup, parameters, ExportProviderFactory);

protected TestWorkspace CreateWorkspaceFromFile(string initialMarkup, TestParameters parameters, IExportProviderFactory exportProviderFactory)
=> TestWorkspace.CreateCSharp(initialMarkup, parameters.parseOptions, parameters.compilationOptions, exportProvider: exportProviderFactory.CreateExportProvider());

internal override (DiagnosticAnalyzer, CodeFixProvider) CreateDiagnosticProviderAndFixer(Workspace workspace)
=> (new CSharpNamingStyleDiagnosticAnalyzer(), new NamingStyleCodeFixProvider());

Expand Down Expand Up @@ -1213,23 +1230,32 @@ internal interface

[Fact, Trait(Traits.Feature, Traits.Features.NamingStyle)]
[WorkItem(16562, "https://github.com/dotnet/roslyn/issues/16562")]
public async Task TestRefactorNotify()
public async Task TestRenameSymbolAnnotation()
{
var markup = @"public class [|c|] { }";
var testParameters = new TestParameters(options: options.ClassNamesArePascalCase);

using var workspace = CreateWorkspaceFromOptions(markup, testParameters);
var (_, action) = await GetCodeActionsAsync(workspace, testParameters);

var previewOperations = await action.GetPreviewOperationsAsync(CancellationToken.None);
Assert.Empty(previewOperations.OfType<TestSymbolRenamedCodeActionOperationFactoryWorkspaceService.Operation>());

var commitOperations = await action.GetOperationsAsync(CancellationToken.None);
Assert.Equal(2, commitOperations.Length);
var originalSolution = workspace.CurrentSolution;

var symbolRenamedOperation = (TestSymbolRenamedCodeActionOperationFactoryWorkspaceService.Operation)commitOperations[1];
Assert.Equal("c", symbolRenamedOperation._symbol.Name);
Assert.Equal("C", symbolRenamedOperation._newName);
var (_, action) = await GetCodeActionsAsync(workspace, testParameters);
var operations = await action.GetOperationsAsync(CancellationToken.None);

foreach (var operation in operations)
{
operation.Apply(workspace, CancellationToken.None);
}

var newSolution = workspace.CurrentSolution;

await Test.Utilities.Utilities.TestRenameSymbolAnnotation.ValidateRenameSymbolAnnotationsAsync(
originalSolution,
newSolution,
new Dictionary<string, string>()
{
{"c", "C" }
}.ToImmutableDictionary());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Test.Utilities;
using Microsoft.CodeAnalysis.Test.Utilities.MoveToNamespace;
using Microsoft.CodeAnalysis.Test.Utilities.Workspaces;
using Microsoft.VisualStudio.Composition;
using Roslyn.Test.Utilities;
using Xunit;
Expand All @@ -23,7 +24,7 @@ public class MoveToNamespaceTests : AbstractMoveToNamespaceTests
{
private static readonly IExportProviderFactory ExportProviderFactory =
ExportProviderCache.GetOrCreateExportProviderFactory(
TestExportProvider.EntireAssemblyCatalogWithCSharpAndVisualBasic.WithPart(typeof(TestMoveToNamespaceOptionsService)));
TestExportProvider.EntireAssemblyCatalogWithCSharpAndVisualBasic.WithPart(typeof(TestMoveToNamespaceOptionsService)).WithPart(typeof(TestRefactorNotify)));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why? What happens if it's not there?


protected override TestWorkspace CreateWorkspaceFromFile(string initialMarkup, TestParameters parameters)
=> CreateWorkspaceFromFile(initialMarkup, parameters, ExportProviderFactory);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ private partial class SymbolInlineRenameInfo : IInlineRenameInfoWithFileRename
private readonly object _gate = new object();

private readonly Document _document;
private readonly IEnumerable<IRefactorNotifyService> _refactorNotifyServices;

private Task<RenameLocations> _underlyingFindRenameLocationsTask;

Expand All @@ -55,7 +54,6 @@ private partial class SymbolInlineRenameInfo : IInlineRenameInfoWithFileRename
public ISymbol RenameSymbol => RenameSymbolAndProjectId.Symbol;

public SymbolInlineRenameInfo(
IEnumerable<IRefactorNotifyService> refactorNotifyServices,
Document document,
TextSpan triggerSpan,
SymbolAndProjectId renameSymbolAndProjectId,
Expand All @@ -64,7 +62,6 @@ public SymbolInlineRenameInfo(
{
this.CanRename = true;

_refactorNotifyServices = refactorNotifyServices;
_document = document;
this.RenameSymbolAndProjectId = renameSymbolAndProjectId;

Expand Down Expand Up @@ -232,18 +229,6 @@ private async Task<IInlineRenameLocationSet> GetLocationSet(Task<RenameLocations
return new InlineRenameLocationSet(this, locationSet);
}

public bool TryOnBeforeGlobalSymbolRenamed(Workspace workspace, IEnumerable<DocumentId> changedDocumentIDs, string replacementText)
{
return _refactorNotifyServices.TryOnBeforeGlobalSymbolRenamed(workspace, changedDocumentIDs, RenameSymbol,
this.GetFinalSymbolName(replacementText), throwOnFailure: false);
}

public bool TryOnAfterGlobalSymbolRenamed(Workspace workspace, IEnumerable<DocumentId> changedDocumentIDs, string replacementText)
{
return _refactorNotifyServices.TryOnAfterGlobalSymbolRenamed(workspace, changedDocumentIDs, RenameSymbol,
this.GetFinalSymbolName(replacementText), throwOnFailure: false);
}

public InlineRenameFileRenameInfo GetFileRenameInfo()
{
if (RenameSymbol.Kind == SymbolKind.NamedType &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,6 @@ namespace Microsoft.CodeAnalysis.Editor.Implementation.InlineRename
{
internal abstract partial class AbstractEditorInlineRenameService : IEditorInlineRenameService
{
private readonly IEnumerable<IRefactorNotifyService> _refactorNotifyServices;

protected AbstractEditorInlineRenameService(IEnumerable<IRefactorNotifyService> refactorNotifyServices)
{
_refactorNotifyServices = refactorNotifyServices;
}

public Task<IInlineRenameInfo> GetRenameInfoAsync(Document document, int position, CancellationToken cancellationToken)
{
// This is unpleasant, but we do everything synchronously. That's because we end up
Expand All @@ -50,11 +43,10 @@ private IInlineRenameInfo GetRenameInfo(Document document, int position, Cancell
return new FailureInlineRenameInfo(EditorFeaturesResources.You_must_rename_an_identifier);
}

return GetRenameInfo(_refactorNotifyServices, document, triggerToken, cancellationToken);
return GetRenameInfo(document, triggerToken, cancellationToken);
}

internal static IInlineRenameInfo GetRenameInfo(
IEnumerable<IRefactorNotifyService> refactorNotifyServices,
Document document, SyntaxToken triggerToken, CancellationToken cancellationToken)
{
var syntaxFactsService = document.GetLanguageService<ISyntaxFactsService>();
Expand Down Expand Up @@ -219,7 +211,7 @@ internal static IInlineRenameInfo GetRenameInfo(
}

return new SymbolInlineRenameInfo(
refactorNotifyServices, document, triggerToken.Span,
document, triggerToken.Span,
symbolAndProjectId, forceRenameOverloads, cancellationToken);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// 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.Collections.Generic;
using System.Composition;
using Microsoft.CodeAnalysis.Editor.Implementation.InlineRename;
using Microsoft.CodeAnalysis.Host.Mef;
Expand All @@ -12,10 +11,5 @@ namespace Microsoft.CodeAnalysis.Editor.CSharp.InlineRename
[ExportLanguageService(typeof(IEditorInlineRenameService), LanguageNames.CSharp), Shared]
internal class CSharpEditorInlineRenameService : AbstractEditorInlineRenameService
{
[ImportingConstructor]
public CSharpEditorInlineRenameService(
[ImportMany]IEnumerable<IRefactorNotifyService> refactorNotifyServices) : base(refactorNotifyServices)
{
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ internal class InlineRenameService : IInlineRenameService
private readonly IWaitIndicator _waitIndicator;
private readonly ITextBufferAssociatedViewService _textBufferAssociatedViewService;
private readonly IAsynchronousOperationListener _asyncListener;
private readonly IEnumerable<IRefactorNotifyService> _refactorNotifyServices;
private readonly ITextBufferFactoryService _textBufferFactoryService;
private readonly IFeatureServiceFactory _featureServiceFactory;
private InlineRenameSession _activeRenameSession;
Expand All @@ -40,15 +39,13 @@ public InlineRenameService(
ITextBufferAssociatedViewService textBufferAssociatedViewService,
ITextBufferFactoryService textBufferFactoryService,
IFeatureServiceFactory featureServiceFactory,
[ImportMany] IEnumerable<IRefactorNotifyService> refactorNotifyServices,
IAsynchronousOperationListenerProvider listenerProvider)
{
_threadingContext = threadingContext;
_waitIndicator = waitIndicator;
_textBufferAssociatedViewService = textBufferAssociatedViewService;
_textBufferFactoryService = textBufferFactoryService;
_featureServiceFactory = featureServiceFactory;
_refactorNotifyServices = refactorNotifyServices;
_asyncListener = listenerProvider.GetListener(FeatureAttribute.Rename);
}

Expand Down Expand Up @@ -80,7 +77,6 @@ public InlineRenameSessionInfo StartInlineSession(
_textBufferAssociatedViewService,
_textBufferFactoryService,
_featureServiceFactory,
_refactorNotifyServices,
_asyncListener);

return new InlineRenameSessionInfo(ActiveSession);
Expand Down
22 changes: 0 additions & 22 deletions src/EditorFeatures/Core.Wpf/InlineRename/InlineRenameSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ internal partial class InlineRenameSession : ForegroundThreadAffinitizedObject,
private readonly ITextBufferFactoryService _textBufferFactoryService;
private readonly IFeatureService _featureService;
private readonly IFeatureDisableToken _completionDisabledToken;
private readonly IEnumerable<IRefactorNotifyService> _refactorNotifyServices;
private readonly IDebuggingWorkspaceService _debuggingWorkspaceService;
private readonly IAsynchronousOperationListener _asyncListener;
private readonly Solution _baseSolution;
Expand Down Expand Up @@ -113,7 +112,6 @@ public InlineRenameSession(
ITextBufferAssociatedViewService textBufferAssociatedViewService,
ITextBufferFactoryService textBufferFactoryService,
IFeatureServiceFactory featureServiceFactory,
IEnumerable<IRefactorNotifyService> refactorNotifyServices,
IAsynchronousOperationListener asyncListener)
: base(threadingContext, assertIsForeground: true)
{
Expand Down Expand Up @@ -141,7 +139,6 @@ public InlineRenameSession(

_renameService = renameService;
_waitIndicator = waitIndicator;
_refactorNotifyServices = refactorNotifyServices;
_asyncListener = asyncListener;
_triggerView = textBufferAssociatedViewService.GetAssociatedTextViews(triggerSpan.Snapshot.TextBuffer).FirstOrDefault(v => v.HasAggregateFocus) ??
textBufferAssociatedViewService.GetAssociatedTextViews(triggerSpan.Snapshot.TextBuffer).First();
Expand Down Expand Up @@ -762,16 +759,6 @@ private void ApplyRename(Solution newSolution, IWaitContext waitContext)
var changes = _baseSolution.GetChanges(newSolution);
var changedDocumentIDs = changes.GetProjectChanges().SelectMany(c => c.GetChangedDocuments()).ToList();

if (!_renameInfo.TryOnBeforeGlobalSymbolRenamed(_workspace, changedDocumentIDs, this.ReplacementText))
{
var notificationService = _workspace.Services.GetService<INotificationService>();
notificationService.SendNotification(
EditorFeaturesResources.Rename_operation_was_cancelled_or_is_not_valid,
EditorFeaturesResources.Rename_Symbol,
NotificationSeverity.Error);
return;
}

using var undoTransaction = _workspace.OpenGlobalUndoTransaction(EditorFeaturesResources.Inline_Rename);
var finalSolution = newSolution.Workspace.CurrentSolution;
foreach (var id in changedDocumentIDs)
Expand Down Expand Up @@ -811,15 +798,6 @@ private void ApplyRename(Solution newSolution, IWaitContext waitContext)
.SelectMany(c => c.GetChangedDocuments().Concat(c.GetAddedDocuments()))
.ToList();

if (!_renameInfo.TryOnAfterGlobalSymbolRenamed(_workspace, finalChangedIds, this.ReplacementText))
{
var notificationService = _workspace.Services.GetService<INotificationService>();
notificationService.SendNotification(
EditorFeaturesResources.Rename_operation_was_not_properly_completed_Some_file_might_not_have_been_updated,
EditorFeaturesResources.Rename_Symbol,
NotificationSeverity.Information);
}

undoTransaction.Commit();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// 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.Collections.Generic;
using System.Composition;
using Microsoft.CodeAnalysis.Editor.Implementation.InlineRename;
using Microsoft.CodeAnalysis.Host.Mef;
Expand All @@ -12,10 +11,5 @@ namespace Microsoft.CodeAnalysis.Editor.CSharp.InlineRename
[ExportLanguageService(typeof(IEditorInlineRenameService), LanguageNames.VisualBasic), Shared]
internal class VisualBasicEditorInlineRenameService : AbstractEditorInlineRenameService
{
[ImportingConstructor]
public VisualBasicEditorInlineRenameService(
[ImportMany]IEnumerable<IRefactorNotifyService> refactorNotifyServices) : base(refactorNotifyServices)
{
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -220,18 +220,6 @@ internal interface IInlineRenameInfo
/// locations to rename, as well as any time the rename options are changed by the user.
/// </summary>
Task<IInlineRenameLocationSet> FindRenameLocationsAsync(OptionSet optionSet, CancellationToken cancellationToken);

/// <summary>
/// Called before the rename is applied to the specified documents in the workspace. Return
/// <see langword="true"/> if rename should proceed, or <see langword="false"/> if it should be canceled.
/// </summary>
bool TryOnBeforeGlobalSymbolRenamed(Workspace workspace, IEnumerable<DocumentId> changedDocumentIDs, string replacementText);

/// <summary>
/// Called after the rename is applied to the specified documents in the workspace. Return
/// <see langword="true"/> if this operation succeeded, or <see langword="false"/> if it failed.
/// </summary>
bool TryOnAfterGlobalSymbolRenamed(Workspace workspace, IEnumerable<DocumentId> changedDocumentIDs, string replacementText);
}

internal interface IInlineRenameInfoWithFileRename : IInlineRenameInfo
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,11 @@ namespace Microsoft.CodeAnalysis.Editor.Implementation.RenameTracking
internal abstract class AbstractRenameTrackingCodeFixProvider : CodeFixProvider
{
private readonly ITextUndoHistoryRegistry _undoHistoryRegistry;
private readonly IEnumerable<IRefactorNotifyService> _refactorNotifyServices;

protected AbstractRenameTrackingCodeFixProvider(
ITextUndoHistoryRegistry undoHistoryRegistry,
IEnumerable<IRefactorNotifyService> refactorNotifyServices)
ITextUndoHistoryRegistry undoHistoryRegistry)
{
_undoHistoryRegistry = undoHistoryRegistry;
_refactorNotifyServices = refactorNotifyServices;
}

public sealed override ImmutableArray<string> FixableDiagnosticIds
Expand All @@ -40,7 +37,7 @@ public sealed override Task RegisterCodeFixesAsync(CodeFixContext context)
// any fixes.
if (RenameTrackingTaggerProvider.CanInvokeRename(document))
{
var action = RenameTrackingTaggerProvider.CreateCodeAction(document, diagnostic, _refactorNotifyServices, _undoHistoryRegistry);
var action = RenameTrackingTaggerProvider.CreateCodeAction(document, diagnostic, _undoHistoryRegistry);
context.RegisterCodeFix(action, diagnostic);
}

Expand Down
Loading