|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using Rubberduck.Parsing.Symbols; |
| 4 | +using Rubberduck.VBEditor.SafeComWrappers; |
| 5 | + |
| 6 | +namespace Rubberduck.Navigation.CodeExplorer |
| 7 | +{ |
| 8 | + public class CompareByName : Comparer<CodeExplorerItemViewModel> |
| 9 | + { |
| 10 | + public override int Compare(CodeExplorerItemViewModel x, CodeExplorerItemViewModel y) |
| 11 | + { |
| 12 | + if (x == y) |
| 13 | + { |
| 14 | + return 0; |
| 15 | + } |
| 16 | + |
| 17 | + var nodeComparison = new CompareByNodeType().Compare(x, y); |
| 18 | + |
| 19 | + return nodeComparison != 0 |
| 20 | + ? nodeComparison |
| 21 | + : string.Compare(x?.NameWithSignature, y?.NameWithSignature, StringComparison.OrdinalIgnoreCase); |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + public class CompareByType : Comparer<CodeExplorerItemViewModel> |
| 26 | + { |
| 27 | + private static readonly Dictionary<DeclarationType, int> SortOrder = new Dictionary<DeclarationType, int> |
| 28 | + { |
| 29 | + // Some DeclarationTypes we want to treat the same, like Subs and Functions, |
| 30 | + // or Property Gets, Lets, and Sets. |
| 31 | + // Give them the same number. |
| 32 | + {DeclarationType.LibraryFunction, 0}, |
| 33 | + {DeclarationType.LibraryProcedure, 0}, |
| 34 | + {DeclarationType.UserDefinedType, 1}, |
| 35 | + {DeclarationType.Enumeration, 2}, |
| 36 | + {DeclarationType.Event, 3}, |
| 37 | + {DeclarationType.Constant, 4}, |
| 38 | + {DeclarationType.Variable, 5}, |
| 39 | + {DeclarationType.PropertyGet, 6}, |
| 40 | + {DeclarationType.PropertyLet, 6}, |
| 41 | + {DeclarationType.PropertySet, 6}, |
| 42 | + {DeclarationType.Function, 7}, |
| 43 | + {DeclarationType.Procedure, 7} |
| 44 | + }; |
| 45 | + |
| 46 | + public override int Compare(CodeExplorerItemViewModel x, CodeExplorerItemViewModel y) |
| 47 | + { |
| 48 | + if (x == y) |
| 49 | + { |
| 50 | + return 0; |
| 51 | + } |
| 52 | + |
| 53 | + var nodeComparison = new CompareByNodeType().Compare(x, y); |
| 54 | + if (nodeComparison != 0) |
| 55 | + { |
| 56 | + return nodeComparison; |
| 57 | + } |
| 58 | + |
| 59 | + //null sorts last. |
| 60 | + if (x?.Declaration == null) |
| 61 | + { |
| 62 | + return 1; |
| 63 | + } |
| 64 | + |
| 65 | + if (y?.Declaration == null) |
| 66 | + { |
| 67 | + return -1; |
| 68 | + } |
| 69 | + |
| 70 | + // keep separate types separate |
| 71 | + if (x.Declaration.DeclarationType != y.Declaration.DeclarationType) |
| 72 | + { |
| 73 | + if (SortOrder.TryGetValue(x.Declaration.DeclarationType, out var xValue) && |
| 74 | + SortOrder.TryGetValue(y.Declaration.DeclarationType, out var yValue)) |
| 75 | + { |
| 76 | + return xValue.CompareTo(yValue); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + // The Tree shows Public and Private Subs/Functions with a separate icon. |
| 81 | + // But Public and Implicit Subs/Functions appear the same, so treat Implicits like Publics. |
| 82 | + var xNodeAcc = x.Declaration.Accessibility == Accessibility.Implicit ? Accessibility.Public : x.Declaration.Accessibility; |
| 83 | + var yNodeAcc = y.Declaration.Accessibility == Accessibility.Implicit ? Accessibility.Public : y.Declaration.Accessibility; |
| 84 | + |
| 85 | + if (xNodeAcc != yNodeAcc) |
| 86 | + { |
| 87 | + // These are reversed because Accessibility is ordered lowest to highest. |
| 88 | + return yNodeAcc.CompareTo(xNodeAcc); |
| 89 | + } |
| 90 | + |
| 91 | + if (x.ExpandedIcon != y.ExpandedIcon) |
| 92 | + { |
| 93 | + // ReSharper disable PossibleInvalidOperationException - this will have a QualifiedSelection |
| 94 | + var xQmn = x.QualifiedSelection.Value.QualifiedName; |
| 95 | + var yQmn = y.QualifiedSelection.Value.QualifiedName; |
| 96 | + |
| 97 | + if (xQmn.ComponentType == ComponentType.Document ^ yQmn.ComponentType == ComponentType.Document) |
| 98 | + { |
| 99 | + return xQmn.ComponentType == ComponentType.Document ? -1 : 1; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + return 0; |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + public class CompareBySelection : Comparer<CodeExplorerItemViewModel> |
| 108 | + { |
| 109 | + public override int Compare(CodeExplorerItemViewModel x, CodeExplorerItemViewModel y) |
| 110 | + { |
| 111 | + if (x == y) |
| 112 | + { |
| 113 | + return 0; |
| 114 | + } |
| 115 | + |
| 116 | + var nodeComparison = new CompareByNodeType().Compare(x, y); |
| 117 | + if (nodeComparison != 0) |
| 118 | + { |
| 119 | + return nodeComparison; |
| 120 | + } |
| 121 | + |
| 122 | + // ReSharper disable PossibleNullReferenceException - tested in CompareByNodeType() above |
| 123 | + if (!x.QualifiedSelection.HasValue && !y.QualifiedSelection.HasValue) |
| 124 | + |
| 125 | + { |
| 126 | + return 0; |
| 127 | + } |
| 128 | + |
| 129 | + if (x.QualifiedSelection.HasValue ^ y.QualifiedSelection.HasValue) |
| 130 | + { |
| 131 | + return x.QualifiedSelection.HasValue ? -1 : 1; |
| 132 | + } |
| 133 | + |
| 134 | + if (x.QualifiedSelection.Value.Selection.StartLine == y.QualifiedSelection.Value.Selection.StartLine) |
| 135 | + { |
| 136 | + return 0; |
| 137 | + } |
| 138 | + |
| 139 | + return x.QualifiedSelection.Value.Selection.StartLine < y.QualifiedSelection.Value.Selection.StartLine ? -1 : 1; |
| 140 | + // ReSharper restore PossibleNullReferenceException |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + public class CompareByNodeType : Comparer<CodeExplorerItemViewModel> |
| 145 | + { |
| 146 | + public override int Compare(CodeExplorerItemViewModel x, CodeExplorerItemViewModel y) |
| 147 | + { |
| 148 | + if (x == y) |
| 149 | + { |
| 150 | + return 0; |
| 151 | + } |
| 152 | + |
| 153 | + //null sorts last. |
| 154 | + if (x == null) |
| 155 | + { |
| 156 | + return 1; |
| 157 | + } |
| 158 | + |
| 159 | + if (y == null) |
| 160 | + { |
| 161 | + return -1; |
| 162 | + } |
| 163 | + |
| 164 | + // references come first |
| 165 | + if (x is CodeExplorerReferenceFolderViewModel ^ |
| 166 | + y is CodeExplorerReferenceFolderViewModel) |
| 167 | + { |
| 168 | + return x is CodeExplorerReferenceFolderViewModel ? -1 : 1; |
| 169 | + } |
| 170 | + |
| 171 | + // references always sort by priority |
| 172 | + if (x is CodeExplorerReferenceViewModel first && |
| 173 | + y is CodeExplorerReferenceViewModel second) |
| 174 | + { |
| 175 | + return first.Priority > second.Priority ? 1 : -1; |
| 176 | + } |
| 177 | + |
| 178 | + // folders come next |
| 179 | + if (x is CodeExplorerCustomFolderViewModel ^ |
| 180 | + y is CodeExplorerCustomFolderViewModel) |
| 181 | + { |
| 182 | + return x is CodeExplorerCustomFolderViewModel ? -1 : 1; |
| 183 | + } |
| 184 | + |
| 185 | + // folders are always sorted by name |
| 186 | + if (x is CodeExplorerCustomFolderViewModel) |
| 187 | + { |
| 188 | + return string.CompareOrdinal(x.NameWithSignature, y.NameWithSignature); |
| 189 | + } |
| 190 | + |
| 191 | + return 0; |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | +} |
0 commit comments