-
Notifications
You must be signed in to change notification settings - Fork 1.2k
simplify sln-remove implementation #45797
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,28 +19,6 @@ internal class RemoveProjectFromSolutionCommand : CommandBase | |
| private readonly string _fileOrDirectory; | ||
| private readonly IReadOnlyCollection<string> _projects; | ||
|
|
||
| private int CountNonFolderDescendants( | ||
| SolutionModel solution, | ||
| SolutionFolderModel item, | ||
| Dictionary<SolutionFolderModel, SolutionItemModel[]> solutionItemsGroupedByParent, | ||
| Dictionary<SolutionFolderModel, int> cached) | ||
| { | ||
| if (cached.ContainsKey(item)) | ||
| { | ||
| return cached[item]; | ||
| } | ||
| int count = item.Files?.Count ?? 0; | ||
| var children = solutionItemsGroupedByParent.TryGetValue(item, out var items) ? items : Array.Empty<SolutionItemModel>(); | ||
| foreach (var child in children) | ||
| { | ||
| count += child is SolutionFolderModel folderModel | ||
| ? CountNonFolderDescendants(solution, folderModel, solutionItemsGroupedByParent, cached) | ||
| : 1; | ||
| } | ||
| cached.Add(item, count); | ||
| return count; | ||
| } | ||
|
|
||
| public RemoveProjectFromSolutionCommand(ParseResult parseResult) : base(parseResult) | ||
| { | ||
| _fileOrDirectory = parseResult.GetValue(SlnCommandParser.SlnArgument); | ||
|
|
@@ -91,7 +69,7 @@ private async Task RemoveProjectsAsync(string solutionFileFullPath, IEnumerable< | |
| ISolutionSerializer serializer = SlnCommandParser.GetSolutionSerializer(solutionFileFullPath); | ||
| SolutionModel solution = await serializer.OpenAsync(solutionFileFullPath, cancellationToken); | ||
|
|
||
| // set UTF8 BOM encoding for .sln | ||
| // set UTF-8 BOM encoding for .sln | ||
| if (serializer is ISolutionSerializer<SlnV12SerializerSettings> v12Serializer) | ||
| { | ||
| solution.SerializerExtension = v12Serializer.CreateModelExtension(new() | ||
|
|
@@ -114,21 +92,40 @@ private async Task RemoveProjectsAsync(string solutionFileFullPath, IEnumerable< | |
| } | ||
| } | ||
|
|
||
| Dictionary<SolutionFolderModel, SolutionItemModel[]> solutionItemsGroupedByParent = solution.SolutionItems | ||
| .Where(i => i.Parent != null) | ||
| .GroupBy(i => i.Parent) | ||
| .ToDictionary(g => g.Key, g => g.ToArray()); | ||
|
|
||
| Dictionary<SolutionFolderModel, int> nonFolderDescendantsCount = new(); | ||
| foreach (var item in solution.SolutionFolders) | ||
| for (int i = 0; i < solution.SolutionFolders.Count; i++) | ||
| { | ||
| CountNonFolderDescendants(solution, item, solutionItemsGroupedByParent, nonFolderDescendantsCount); | ||
| } | ||
| var folder = solution.SolutionFolders[i]; | ||
| int nonFolderDescendants = 0; | ||
| Stack<SolutionFolderModel> stack = new(); | ||
| stack.Push(folder); | ||
|
|
||
| var emptyFolders = nonFolderDescendantsCount.Where(i => i.Value == 0).Select(i => i.Key); | ||
| foreach (var folder in emptyFolders) | ||
| { | ||
| solution.RemoveFolder(folder); | ||
| while (stack.Count > 0) | ||
| { | ||
| var current = stack.Pop(); | ||
|
|
||
| nonFolderDescendants += current.Files?.Count ?? 0; | ||
| foreach (var child in solution.SolutionItems) | ||
| { | ||
| if (child is { Parent: var parent } && parent == current) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice property pattern :) |
||
| { | ||
| if (child is SolutionFolderModel childFolder) | ||
| { | ||
| stack.Push(childFolder); | ||
| } | ||
| else | ||
| { | ||
| nonFolderDescendants++; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (nonFolderDescendants == 0) | ||
| { | ||
| solution.RemoveFolder(folder); | ||
| // After removal, adjust index and continue to avoid skipping folders after removal | ||
| i--; | ||
| } | ||
| } | ||
|
|
||
| await serializer.SaveAsync(solutionFileFullPath, solution, cancellationToken); | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.