Skip to content

Commit a04dc55

Browse files
committed
Minor cleanup. Using foreach instead of for. Using original Source path instead of Destination path for the dictionary.
1 parent 6d0c5e5 commit a04dc55

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

src/Microsoft.VisualStudio.ProjectSystem.Managed.VS/ProjectSystem/VS/Rename/FileMoveNotificationListener.cs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using Microsoft.VisualStudio.Threading;
1212
using Path = System.IO.Path;
1313
using static System.Diagnostics.Debug;
14+
using static Microsoft.CodeAnalysis.Rename.Renamer;
1415

1516
namespace Microsoft.VisualStudio.ProjectSystem.VS.Rename
1617
{
@@ -30,7 +31,7 @@ internal class FileMoveNotificationListener : IFileMoveNotificationListener
3031
private readonly IRoslynServices _roslynServices;
3132
private readonly IVsService<SVsSettingsPersistenceManager, ISettingsManager> _settingsManagerService;
3233

33-
private readonly Dictionary<string, Renamer.RenameDocumentActionSet> _renameActionSets = new();
34+
private readonly Dictionary<string, RenameDocumentActionSet> _renameActionSets = new();
3435
private string? _renameMessage;
3536

3637
[ImportingConstructor]
@@ -59,6 +60,7 @@ public async Task OnBeforeFilesMovedAsync(IReadOnlyCollection<IFileMoveItem> ite
5960
{
6061
// Always start with an empty collection when activated.
6162
_renameActionSets.Clear();
63+
6264
Project? project = _workspace.CurrentSolution.Projects.FirstOrDefault(p => StringComparers.Paths.Equals(p.FilePath, _unconfiguredProject.FullPath));
6365
if (project is null)
6466
{
@@ -75,22 +77,23 @@ public async Task OnBeforeFilesMovedAsync(IReadOnlyCollection<IFileMoveItem> ite
7577

7678
// Get the relative folder path from the project to the destination.
7779
string destinationFolderPath = Path.GetDirectoryName(_unconfiguredProject.MakeRelative(itemToMove.Destination));
78-
string[] documentFolders = destinationFolderPath.Split(Delimiter.Path, StringSplitOptions.RemoveEmptyEntries);
80+
string[] destinationFolders = destinationFolderPath.Split(Delimiter.Path, StringSplitOptions.RemoveEmptyEntries);
7981

8082
// Since this rename only moves the location of the file to another directory, it will use the SyncNamespaceDocumentAction in Roslyn as the rename action within this set.
8183
// The logic for selecting this rename action can be found here: https://github.com/dotnet/roslyn/blob/960f375f4825a189937d4bfd9fea8162ecc63177/src/Workspaces/Core/Portable/Rename/Renamer.cs#L133-L136
82-
Renamer.RenameDocumentActionSet renameActionSet = await Renamer.RenameDocumentAsync(currentDocument, s_renameOptions, null, documentFolders);
83-
84-
if (renameActionSet.ApplicableActions.IsEmpty || renameActionSet.ApplicableActions.Any(a => a.GetErrors().Any()))
84+
RenameDocumentActionSet renameActionSet = await RenameDocumentAsync(currentDocument, s_renameOptions, null, destinationFolders);
85+
if (renameActionSet.ApplicableActions.IsEmpty || renameActionSet.ApplicableActions.Any(aa => aa.GetErrors().Any()))
8586
{
8687
continue;
8788
}
88-
// Getting the rename message requires an instance of Renamer.RenameDocumentAction.
89+
90+
// Getting the rename message requires an instance of RenameDocumentAction.
8991
// We only need to set this message text once for the lifetime of the class, since it isn't dynamic.
9092
// Even though it isn't dynamic, it does get localized appropriately in Roslyn.
9193
// The text in English is "Sync namespace to folder structure".
9294
_renameMessage ??= renameActionSet.ApplicableActions.First().GetDescription();
93-
_renameActionSets.Add(itemToMove.Destination, renameActionSet);
95+
96+
_renameActionSets.Add(itemToMove.Source, renameActionSet);
9497
}
9598

9699
return;
@@ -153,14 +156,13 @@ async Task ApplyRenamesAsync(IWaitContext context)
153156
// After waiting, a "new" published Solution is available.
154157
Solution solution = _workspace.CurrentSolution;
155158

156-
for (int i = 0; i < _renameActionSets.Count; i++)
159+
int currentStep = 1;
160+
foreach (KeyValuePair<string, RenameDocumentActionSet> renameActionSet in _renameActionSets)
157161
{
158-
string destinationPath = _renameActionSets.Keys.ElementAt(i);
159162
// Display the filename being updated to the user in the progress dialog.
160-
context.Update(currentStep: i + 1, progressText: Path.GetFileName(destinationPath));
163+
context.Update(currentStep: currentStep++, progressText: Path.GetFileName(renameActionSet.Key));
161164

162-
Renamer.RenameDocumentActionSet renameActionSet = _renameActionSets[destinationPath];
163-
solution = await renameActionSet.UpdateSolutionAsync(solution, token);
165+
solution = await renameActionSet.Value.UpdateSolutionAsync(solution, token);
164166
}
165167

166168
await _threadingService.SwitchToUIThread(token);

0 commit comments

Comments
 (0)