Skip to content

Commit 62f9277

Browse files
committed
Apply suggestions from code review
1 parent 765016a commit 62f9277

File tree

1 file changed

+13
-4
lines changed
  • src/Cli/dotnet/commands/dotnet-sln/remove

1 file changed

+13
-4
lines changed

src/Cli/dotnet/commands/dotnet-sln/remove/Program.cs

+13-4
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,22 @@ internal class RemoveProjectFromSolutionCommand : CommandBase
1919
private readonly string _fileOrDirectory;
2020
private readonly IReadOnlyCollection<string> _projects;
2121

22-
private int CountNonFolderDescendants(SolutionModel solution, SolutionFolderModel item, Dictionary<SolutionFolderModel, int> cached)
22+
private int CountNonFolderDescendants(
23+
SolutionModel solution,
24+
SolutionFolderModel item,
25+
Dictionary<SolutionFolderModel, SolutionItemModel[]> solutionItemsGroupedByParent,
26+
Dictionary<SolutionFolderModel, int> cached)
2327
{
2428
if (cached.ContainsKey(item))
2529
{
2630
return cached[item];
2731
}
2832
int count = item.Files?.Count ?? 0;
29-
var children = solution.SolutionItems.Where(i => i.Parent == item);
33+
var children = solutionItemsGroupedByParent.TryGetValue(item, out var items) ? items : Array.Empty<SolutionItemModel>();
3034
foreach (var child in children)
3135
{
3236
count += child is SolutionFolderModel folderModel
33-
? CountNonFolderDescendants(solution, folderModel, cached)
37+
? CountNonFolderDescendants(solution, folderModel, solutionItemsGroupedByParent, cached)
3438
: 1;
3539
}
3640
cached.Add(item, count);
@@ -111,10 +115,15 @@ private async Task RemoveProjectsAsync(string solutionFileFullPath, IEnumerable<
111115
}
112116
}
113117

118+
Dictionary<SolutionFolderModel, SolutionItemModel[]> solutionItemsGroupedByParent = solution.SolutionItems
119+
.Where(i => i.Parent != null)
120+
.GroupBy(i => i.Parent)
121+
.ToDictionary(g => g.Key, g => g.ToArray());
122+
114123
Dictionary<SolutionFolderModel, int> nonFolderDescendantsCount = new();
115124
foreach (var item in solution.SolutionFolders)
116125
{
117-
CountNonFolderDescendants(solution, item, nonFolderDescendantsCount);
126+
CountNonFolderDescendants(solution, item, solutionItemsGroupedByParent, nonFolderDescendantsCount);
118127
}
119128

120129
var emptyFolders = nonFolderDescendantsCount.Where(i => i.Value == 0).Select(i => i.Key);

0 commit comments

Comments
 (0)