Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ocalles committed May 11, 2020
1 parent d1909bf commit 7304ec8
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
// 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.md file in the project root for more information.

using System;
using System.ComponentModel.Composition;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Runtime.Remoting.Contexts;
using System.Threading;
using System.Threading.Tasks;
using EnvDTE;
using Microsoft.CodeAnalysis;
Expand All @@ -30,8 +33,9 @@ internal class RenamerProjectTreeActionHandler : ProjectTreeActionHandlerBase, I
private readonly IWaitIndicator _waitService;
private readonly IRoslynServices _roslynServices;
private readonly Workspace _workspace;
private bool _isLanguageServiceDone;
private string _oldFilePath;
private string _newFileNameWithExtension;
private SemaphoreSlim _semLanguegeServiceDone = new SemaphoreSlim(0,1);

[ImportingConstructor]
public RenamerProjectTreeActionHandler(
Expand Down Expand Up @@ -60,30 +64,31 @@ public RenamerProjectTreeActionHandler(

public void HandleRename(string oldFilePath, string newFilePath)
{
if (_oldFilePath == oldFilePath)
string newFileWithExtension = Path.GetFileName(newFilePath);
if (StringComparer.Ordinal.Compare(_oldFilePath, oldFilePath) == 0 &&
StringComparer.Ordinal.Compare(_newFileNameWithExtension, newFileWithExtension) == 0)
{
_isLanguageServiceDone = true;
_semLanguegeServiceDone.Release();
}
}

public override async Task RenameAsync(IProjectTreeActionHandlerContext context, IProjectTree node, string value)
{
Assumes.Present(context);
Assumes.Present(node);
Assumes.Present(value);
Requires.NotNull(context, nameof(Context));
Requires.NotNull(node, nameof(node));
Requires.NotNullOrEmpty(value, nameof(value));

_oldFilePath = node.FilePath;
string? newNameWithExtension = value;
// These variables are need to synchronize with Roslyn
_isLanguageServiceDone = false;
_oldFilePath = node.FilePath;
_newFileNameWithExtension = value;

// Do not offer to rename the file in VS Online scenarios.
if (_vsOnlineServices.ConnectedToVSOnline)
{
return;
}

if (await IsAutomationFunction() || node.IsFolder)
if (await IsAutomationFunctionAsync() || node.IsFolder)
{
// Do not display rename Prompt
await base.RenameAsync(context, node, value);
Expand All @@ -104,27 +109,28 @@ public override async Task RenameAsync(IProjectTreeActionHandlerContext context,
return;
}

var documentRenameResult = await CodeAnalysis.Rename.Renamer.RenameDocumentAsync(oldDocument, newNameWithExtension, oldDocument.Folders);
var documentRenameResult = await CodeAnalysis.Rename.Renamer.RenameDocumentAsync(oldDocument, _newFileNameWithExtension, oldDocument.Folders);

bool errorsDetected = false;
foreach (var action in documentRenameResult.ApplicableActions)
{
foreach (var error in action.GetErrors())
{
errorsDetected = true;
break;
}
}

// Check errors before applying changes
if (errorsDetected)
{
string failureMessage = string.Format(CultureInfo.CurrentCulture, VSResources.RenameSymbolFailed, oldName);
string failureMessage = string.Format(CultureInfo.CurrentCulture, VSResources.CannotApplyRename, oldName);
_userNotificationServices.ShowWarning(failureMessage);
return;
}

// Rename the file
await base.RenameAsync(context, node, value); // Async process (1 of which updating Roslyn)
await base.RenameAsync(context, node, value);

//Check if there are any symbols that need to be renamed
if (documentRenameResult.ApplicableActions.IsEmpty)
Expand All @@ -143,7 +149,7 @@ public override async Task RenameAsync(IProjectTreeActionHandlerContext context,
//
// Because HandleRename() is called after Roslyn, we can set this
// flag when node.FilePath == HandlRename(FilePath)
while (_isLanguageServiceDone == false) ;
await _semLanguegeServiceDone.WaitAsync();

// Apply actions and notify other VS features
CodeAnalysis.Solution currentSolution = GetCurrentProject().Solution;
Expand All @@ -154,14 +160,17 @@ public override async Task RenameAsync(IProjectTreeActionHandlerContext context,
allowCancel: true,
token => documentRenameResult.UpdateSolutionAsync(currentSolution, token));

_roslynServices.ApplyChangesToSolution(currentSolution.Workspace, renamedSolution);
if (!_roslynServices.ApplyChangesToSolution(currentSolution.Workspace, renamedSolution))
{
string failureMessage = string.Format(CultureInfo.CurrentCulture, VSResources.RenameSymbolFailed, oldName);
_userNotificationServices.ShowWarning(failureMessage);
}
return;

}, _unconfiguredProject);

}

private async Task<bool> IsAutomationFunction()
private async Task<bool> IsAutomationFunctionAsync()
{
await _threadingService.SwitchToUIThread();

Expand Down Expand Up @@ -193,7 +202,6 @@ private async Task<bool> CheckUserConfirmation(string oldFileName)
{
string renamePromptMessage = string.Format(CultureInfo.CurrentCulture, VSResources.RenameSymbolPrompt, oldFileName);

await _projectVsServices.ThreadingService.SwitchToUIThread();
return _userNotificationServices.Confirm(renamePromptMessage);
}

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -221,4 +221,7 @@ In order to debug this project, add an executable project to this solution which
<data name="Renaming_Type_from_0_to_1" xml:space="preserve">
<value>Renaming Type from '{0}' to '{1}'</value>
</data>
<data name="CannotApplyRename" xml:space="preserve">
<value>Cannot apply rename</value>
</data>
</root>

0 comments on commit 7304ec8

Please sign in to comment.