| 1 | #nullable enable |
| 2 | using System.Collections.ObjectModel; |
| 3 | using CascadeIDE.Models; |
| 4 | |
| 5 | namespace CascadeIDE.Features.Workspace.Application; |
| 6 | |
| 7 | /// <summary>Раскрытие узлов SE: дефолт после load и путь к активному файлу (ADR 0167).</summary> |
| 8 | public static class SolutionTreeExpansionPolicy |
| 9 | { |
| 10 | public static void ApplyDefaultExpansion(IEnumerable<SolutionItem> roots) |
| 11 | { |
| 12 | foreach (var root in roots) |
| 13 | ApplyDefaultExpansionRecursive(root); |
| 14 | } |
| 15 | |
| 16 | public static bool TryExpandPathTo(ObservableCollection<SolutionItem> roots, SolutionItem target) |
| 17 | { |
| 18 | if (!TryFindPath(roots, target, out var path)) |
| 19 | return false; |
| 20 | |
| 21 | foreach (var node in path) |
| 22 | node.IsExpanded = true; |
| 23 | |
| 24 | return true; |
| 25 | } |
| 26 | |
| 27 | private static void ApplyDefaultExpansionRecursive(SolutionItem node) |
| 28 | { |
| 29 | node.IsExpanded = ShouldExpandByDefault(node); |
| 30 | foreach (var child in node.Children) |
| 31 | ApplyDefaultExpansionRecursive(child); |
| 32 | } |
| 33 | |
| 34 | private static bool ShouldExpandByDefault(SolutionItem node) |
| 35 | { |
| 36 | if (node.FullPath is { } path) |
| 37 | { |
| 38 | if (path.EndsWith(".sln", StringComparison.OrdinalIgnoreCase) |
| 39 | || path.EndsWith(".slnx", StringComparison.OrdinalIgnoreCase) |
| 40 | || path.EndsWith(".csproj", StringComparison.OrdinalIgnoreCase) |
| 41 | || path.EndsWith(".fsproj", StringComparison.OrdinalIgnoreCase)) |
| 42 | return true; |
| 43 | } |
| 44 | |
| 45 | return false; |
| 46 | } |
| 47 | |
| 48 | private static bool TryFindPath( |
| 49 | IEnumerable<SolutionItem> nodes, |
| 50 | SolutionItem target, |
| 51 | out List<SolutionItem> path) |
| 52 | { |
| 53 | foreach (var node in nodes) |
| 54 | { |
| 55 | if (ReferenceEquals(node, target)) |
| 56 | { |
| 57 | path = [node]; |
| 58 | return true; |
| 59 | } |
| 60 | |
| 61 | if (TryFindPath(node.Children, target, out var childPath)) |
| 62 | { |
| 63 | childPath.Insert(0, node); |
| 64 | path = childPath; |
| 65 | return true; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | path = []; |
| 70 | return false; |
| 71 | } |
| 72 | } |
| 73 | |