Skip to content

Commit

Permalink
Refactor to extract methods from RenameAsync
Browse files Browse the repository at this point in the history
  • Loading branch information
ocalles committed May 26, 2020
1 parent f05ae54 commit 1a24a92
Showing 1 changed file with 61 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,61 +70,33 @@ public override async Task RenameAsync(IProjectTreeActionHandlerContext context,
Requires.NotNull(node, nameof(node));
Requires.NotNullOrEmpty(value, nameof(value));

// These variables are need to synchronize with Roslyn
string? oldFilePath = node.FilePath;
string newFileWithExtension = value;

if (await IsAutomationFunctionAsync() || node.IsFolder || _vsOnlineServices.ConnectedToVSOnline)
{
// Do not display rename Prompt
await CPSRenameAsync(context, node, value);
return;
}

// Get the list of possible actions to execute
string oldName = Path.GetFileNameWithoutExtension(oldFilePath);
string newName = Path.GetFileNameWithoutExtension(newFileWithExtension);
string newFileWithExtension = value;
CodeAnalysis.Project? project = GetCurrentProject();

// Rename the file
await CPSRenameAsync(context, node, value);

if (project is null)
{
return;
}

// see if the current project contains a compilation
(bool success, bool isCaseSensitive) = await TryDetermineIfCompilationIsCaseSensitiveAsync(project);
if (!success)
if (await IsAutomationFunctionAsync() || node.IsFolder || _vsOnlineServices.ConnectedToVSOnline)
{
// Do not display rename Prompt
return;
}

if (!CanHandleRename(oldName, newName, isCaseSensitive))
if (project is null)
{
return;
}

CodeAnalysis.Document? oldDocument = GetDocument(project, oldFilePath);
if (oldDocument is null)
string newName = Path.GetFileNameWithoutExtension(newFileWithExtension);
if (!await CanRenameType(project, oldName, newName))
{
return;
}

var documentRenameResult = await CodeAnalysis.Rename.Renamer.RenameDocumentAsync(oldDocument, newFileWithExtension);

// Check errors before applying changes
foreach (var action in documentRenameResult.ApplicableActions)
{
foreach (var e in action.GetErrors())
{
return;
}
}

// Check if there are any symbols that need to be renamed
if (documentRenameResult.ApplicableActions.IsEmpty)
(bool result, var documentRenameResult) = await GetRenameSymbolsActions(project, oldFilePath, newFileWithExtension);
if (result == false || documentRenameResult == null)
{
return;
}
Expand Down Expand Up @@ -169,22 +141,51 @@ public override async Task RenameAsync(IProjectTreeActionHandlerContext context,
}
return;
}, _unconfiguredProject);

}

private static async Task<(bool success, bool isCaseSensitive)> TryDetermineIfCompilationIsCaseSensitiveAsync(CodeAnalysis.Project? project)
private static async Task<(bool, CodeAnalysis.Rename.Renamer.RenameDocumentActionSet?)> GetRenameSymbolsActions(CodeAnalysis.Project project, string? oldFilePath, string newFileWithExtension)
{
if (project is null)
return (false, false);
CodeAnalysis.Document? oldDocument = GetDocument(project, oldFilePath);
if (oldDocument is null)
{
return (false, null);
}

Compilation? compilation = await project.GetCompilationAsync();
if (compilation is null)
// Get the list of possible actions to execute
var documentRenameResult = await CodeAnalysis.Rename.Renamer.RenameDocumentAsync(oldDocument, newFileWithExtension);

// Check if there are any symbols that need to be renamed
if (documentRenameResult.ApplicableActions.IsEmpty)
{
// this project does not support compilations
return (false, false);
return (false, documentRenameResult);
}

return (true, compilation.IsCaseSensitive);
// Check errors before applying changes
foreach (var action in documentRenameResult.ApplicableActions)
{
foreach (var e in action.GetErrors())
{
return (false, documentRenameResult);
}
}

return (true, documentRenameResult);
}

private async Task<bool> CanRenameType(CodeAnalysis.Project? project, string oldName, string newName)
{
// see if the current project contains a compilation
(bool success, bool isCaseSensitive) = await TryDetermineIfCompilationIsCaseSensitiveAsync(project);
if (!success)
{
return false;
}

if (!CanHandleRename(oldName, newName, isCaseSensitive))
{
return false;
}
return true;
}

private bool CanHandleRename(string oldName, string newName, bool isCaseSensitive)
Expand All @@ -197,6 +198,21 @@ private bool CanHandleRename(string oldName, string newName, bool isCaseSensitiv
? StringComparisons.LanguageIdentifiers
: StringComparisons.LanguageIdentifiersIgnoreCase));

private static async Task<(bool success, bool isCaseSensitive)> TryDetermineIfCompilationIsCaseSensitiveAsync(CodeAnalysis.Project? project)
{
if (project is null)
return (false, false);

Compilation? compilation = await project.GetCompilationAsync();
if (compilation is null)
{
// this project does not support compilations
return (false, false);
}

return (true, compilation.IsCaseSensitive);
}

protected virtual async Task<bool> IsAutomationFunctionAsync()
{
await _threadingService.SwitchToUIThread();
Expand Down

0 comments on commit 1a24a92

Please sign in to comment.