Skip to content

Commit 5494093

Browse files
committed
refactor: now all filesystem related trees/lists are sorted in case-insensitive mode
Signed-off-by: leo <longshuang@msn.cn>
1 parent c3c7d32 commit 5494093

File tree

6 files changed

+22
-19
lines changed

6 files changed

+22
-19
lines changed

src/Commands/CompareRevisions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public CompareRevisions(string repo, string start, string end, string path)
3939
foreach (var line in lines)
4040
ParseLine(line);
4141

42-
_changes.Sort((l, r) => string.Compare(l.Path, r.Path, StringComparison.Ordinal));
42+
_changes.Sort((l, r) => Models.NumericSort.Compare(l.Path, r.Path));
4343
return _changes;
4444
}
4545

src/Models/NumericSort.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
namespace SourceGit.Models
1+
using System;
2+
using System.Globalization;
3+
4+
namespace SourceGit.Models
25
{
36
public static class NumericSort
47
{
@@ -23,7 +26,7 @@ public static int Compare(string s1, string s2)
2326
bool isDigit1 = char.IsDigit(c1);
2427
bool isDigit2 = char.IsDigit(c2);
2528
if (isDigit1 != isDigit2)
26-
return c1.CompareTo(c2);
29+
return char.ToUpper(c1, CultureInfo.CurrentCulture).CompareTo(char.ToUpper(c2, CultureInfo.CurrentCulture));
2730

2831
do
2932
{
@@ -55,7 +58,7 @@ public static int Compare(string s1, string s2)
5558
if (isDigit1)
5659
result = loc1 == loc2 ? string.CompareOrdinal(sub1, sub2) : loc1 - loc2;
5760
else
58-
result = string.CompareOrdinal(sub1, sub2);
61+
result = string.Compare(sub1, sub2, StringComparison.OrdinalIgnoreCase);
5962

6063
if (result != 0)
6164
return result;

src/ViewModels/Preferences.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ public void SortNodes(List<RepositoryNode> collection)
449449
if (l.IsRepository != r.IsRepository)
450450
return l.IsRepository ? 1 : -1;
451451

452-
return string.Compare(l.Name, r.Name, StringComparison.Ordinal);
452+
return Models.NumericSort.Compare(l.Name, r.Name);
453453
});
454454
}
455455

src/ViewModels/Repository.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1275,6 +1275,7 @@ public void RefreshWorkingCopyChanges()
12751275
if (_workingCopy == null)
12761276
return;
12771277

1278+
changes.Sort((l, r) => Models.NumericSort.Compare(l.Path, r.Path));
12781279
_workingCopy.SetData(changes);
12791280

12801281
Dispatcher.UIThread.Invoke(() =>

src/ViewModels/StashesPage.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public Models.Stash SelectedStash
7171
changes.Add(c);
7272

7373
if (needSort)
74-
changes.Sort((l, r) => string.Compare(l.Path, r.Path, StringComparison.Ordinal));
74+
changes.Sort((l, r) => Models.NumericSort.Compare(l.Path, r.Path));
7575
}
7676

7777
Dispatcher.UIThread.Invoke(() =>

src/Views/RevisionFileTreeView.axaml.cs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -270,12 +270,7 @@ protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs chang
270270
foreach (var obj in objects)
271271
_tree.Add(new ViewModels.RevisionFileTreeNode { Backend = obj });
272272

273-
_tree.Sort((l, r) =>
274-
{
275-
if (l.IsFolder == r.IsFolder)
276-
return string.Compare(l.Name, r.Name, StringComparison.Ordinal);
277-
return l.IsFolder ? -1 : 1;
278-
});
273+
SortNodes(_tree);
279274

280275
var topTree = new List<ViewModels.RevisionFileTreeNode>();
281276
MakeRows(topTree, _tree, 0);
@@ -341,13 +336,7 @@ private void OnRowsSelectionChanged(object sender, SelectionChangedEventArgs _)
341336
foreach (var obj in objects)
342337
node.Children.Add(new ViewModels.RevisionFileTreeNode() { Backend = obj });
343338

344-
node.Children.Sort((l, r) =>
345-
{
346-
if (l.IsFolder == r.IsFolder)
347-
return Models.NumericSort.Compare(l.Name, r.Name);
348-
return l.IsFolder ? -1 : 1;
349-
});
350-
339+
SortNodes(node.Children);
351340
return node.Children;
352341
}
353342

@@ -365,6 +354,16 @@ private void MakeRows(List<ViewModels.RevisionFileTreeNode> rows, List<ViewModel
365354
}
366355
}
367356

357+
private void SortNodes(List<ViewModels.RevisionFileTreeNode> nodes)
358+
{
359+
nodes.Sort((l, r) =>
360+
{
361+
if (l.IsFolder == r.IsFolder)
362+
return Models.NumericSort.Compare(l.Name, r.Name);
363+
return l.IsFolder ? -1 : 1;
364+
});
365+
}
366+
368367
private List<ViewModels.RevisionFileTreeNode> _tree = [];
369368
private AvaloniaList<ViewModels.RevisionFileTreeNode> _rows = [];
370369
private bool _disableSelectionChangingEvent = false;

0 commit comments

Comments
 (0)