Skip to content

Commit 64a41dc

Browse files
committed
refactor: rewrite searching commit by file path
Signed-off-by: leo <longshuang@msn.cn>
1 parent fa4d9d2 commit 64a41dc

File tree

3 files changed

+53
-76
lines changed

3 files changed

+53
-76
lines changed

src/ViewModels/Repository.cs

Lines changed: 48 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -271,14 +271,13 @@ public bool IsSearching
271271
{
272272
SearchedCommits = new List<Models.Commit>();
273273
SearchCommitFilter = string.Empty;
274-
SearchCommitFilterSuggestion.Clear();
275-
IsSearchCommitSuggestionOpen = false;
276-
_revisionFiles.Clear();
274+
MatchedFilesForSearching = null;
275+
_worktreeFiles = null;
277276

278277
if (value)
279278
{
280279
SelectedViewIndex = 0;
281-
UpdateCurrentRevisionFilesForSearchSuggestion();
280+
CalcWorktreeFilesForSearching();
282281
}
283282
}
284283
}
@@ -307,7 +306,7 @@ public int SearchCommitFilterType
307306
{
308307
if (SetProperty(ref _searchCommitFilterType, value))
309308
{
310-
UpdateCurrentRevisionFilesForSearchSuggestion();
309+
CalcWorktreeFilesForSearching();
311310

312311
if (!string.IsNullOrEmpty(_searchCommitFilter))
313312
StartSearchCommits();
@@ -320,47 +319,22 @@ public string SearchCommitFilter
320319
get => _searchCommitFilter;
321320
set
322321
{
323-
if (SetProperty(ref _searchCommitFilter, value) &&
324-
_searchCommitFilterType == 4 &&
325-
!string.IsNullOrEmpty(value) &&
326-
value.Length >= 2 &&
327-
_revisionFiles.Count > 0)
322+
if (SetProperty(ref _searchCommitFilter, value))
328323
{
329-
var suggestion = new List<string>();
330-
foreach (var file in _revisionFiles)
331-
{
332-
if (file.Contains(value, StringComparison.OrdinalIgnoreCase) && file.Length != value.Length)
333-
{
334-
suggestion.Add(file);
335-
if (suggestion.Count > 100)
336-
break;
337-
}
338-
}
339-
340-
SearchCommitFilterSuggestion.Clear();
341-
SearchCommitFilterSuggestion.AddRange(suggestion);
342-
IsSearchCommitSuggestionOpen = SearchCommitFilterSuggestion.Count > 0;
343-
}
344-
else if (SearchCommitFilterSuggestion.Count > 0)
345-
{
346-
SearchCommitFilterSuggestion.Clear();
347-
IsSearchCommitSuggestionOpen = false;
324+
if (_searchCommitFilterType == 4 && value is { Length: > 2 })
325+
CalcMatchedFilesForSearching();
326+
else if (_matchedFilesForSearching is { })
327+
MatchedFilesForSearching = null;
348328
}
349329
}
350330
}
351331

352-
public bool IsSearchCommitSuggestionOpen
332+
public List<string> MatchedFilesForSearching
353333
{
354-
get => _isSearchCommitSuggestionOpen;
355-
set => SetProperty(ref _isSearchCommitSuggestionOpen, value);
334+
get => _matchedFilesForSearching;
335+
private set => SetProperty(ref _matchedFilesForSearching, value);
356336
}
357337

358-
public AvaloniaList<string> SearchCommitFilterSuggestion
359-
{
360-
get;
361-
private set;
362-
} = new AvaloniaList<string>();
363-
364338
public List<Models.Commit> SearchedCommits
365339
{
366340
get => _searchedCommits;
@@ -551,8 +525,8 @@ public void Close()
551525
_visibleSubmodules.Clear();
552526
_searchedCommits.Clear();
553527

554-
_revisionFiles.Clear();
555-
SearchCommitFilterSuggestion.Clear();
528+
_worktreeFiles = null;
529+
_matchedFilesForSearching = null;
556530
}
557531

558532
public bool CanCreatePopup()
@@ -723,15 +697,19 @@ public void ClearSearchCommitFilter()
723697
SearchCommitFilter = string.Empty;
724698
}
725699

700+
public void ClearMatchedFilesForSearching()
701+
{
702+
MatchedFilesForSearching = null;
703+
}
704+
726705
public void StartSearchCommits()
727706
{
728707
if (_histories == null)
729708
return;
730709

731710
IsSearchLoadingVisible = true;
732711
SearchResultSelectedCommit = null;
733-
IsSearchCommitSuggestionOpen = false;
734-
SearchCommitFilterSuggestion.Clear();
712+
MatchedFilesForSearching = null;
735713

736714
Task.Run(() =>
737715
{
@@ -2391,9 +2369,9 @@ private void TryToAddCustomActionsToBranchContextMenu(ContextMenu menu, Models.B
23912369
menu.Items.Add(new MenuItem() { Header = "-" });
23922370
}
23932371

2394-
private void UpdateCurrentRevisionFilesForSearchSuggestion()
2372+
private void CalcWorktreeFilesForSearching()
23952373
{
2396-
_revisionFiles.Clear();
2374+
_worktreeFiles = null;
23972375

23982376
if (_searchCommitFilterType == 4)
23992377
{
@@ -2405,30 +2383,36 @@ private void UpdateCurrentRevisionFilesForSearchSuggestion()
24052383
if (_searchCommitFilterType != 4)
24062384
return;
24072385

2408-
_revisionFiles.AddRange(files);
2386+
_worktreeFiles = new List<string>();
2387+
foreach (var f in files)
2388+
_worktreeFiles.Add(f);
24092389

2410-
if (!string.IsNullOrEmpty(_searchCommitFilter) && _searchCommitFilter.Length > 2 && _revisionFiles.Count > 0)
2411-
{
2412-
var suggestion = new List<string>();
2413-
foreach (var file in _revisionFiles)
2414-
{
2415-
if (file.Contains(_searchCommitFilter, StringComparison.OrdinalIgnoreCase) && file.Length != _searchCommitFilter.Length)
2416-
{
2417-
suggestion.Add(file);
2418-
if (suggestion.Count > 100)
2419-
break;
2420-
}
2421-
}
2422-
2423-
SearchCommitFilterSuggestion.Clear();
2424-
SearchCommitFilterSuggestion.AddRange(suggestion);
2425-
IsSearchCommitSuggestionOpen = SearchCommitFilterSuggestion.Count > 0;
2426-
}
2390+
if (_searchCommitFilter is { Length: > 2 })
2391+
CalcMatchedFilesForSearching();
24272392
});
24282393
});
24292394
}
24302395
}
24312396

2397+
private void CalcMatchedFilesForSearching()
2398+
{
2399+
if (_worktreeFiles == null || _worktreeFiles.Count == 0)
2400+
return;
2401+
2402+
var matched = new List<string>();
2403+
foreach (var file in _worktreeFiles)
2404+
{
2405+
if (file.Contains(_searchCommitFilter, StringComparison.OrdinalIgnoreCase) && file.Length != _searchCommitFilter.Length)
2406+
{
2407+
matched.Add(file);
2408+
if (matched.Count > 100)
2409+
break;
2410+
}
2411+
}
2412+
2413+
MatchedFilesForSearching = matched;
2414+
}
2415+
24322416
private void AutoFetchImpl(object sender)
24332417
{
24342418
if (!_settings.EnableAutoFetch || _isAutoFetching)
@@ -2475,13 +2459,13 @@ private void AutoFetchImpl(object sender)
24752459

24762460
private bool _isSearching = false;
24772461
private bool _isSearchLoadingVisible = false;
2478-
private bool _isSearchCommitSuggestionOpen = false;
24792462
private int _searchCommitFilterType = 3;
24802463
private bool _onlySearchCommitsInCurrentBranch = false;
24812464
private string _searchCommitFilter = string.Empty;
24822465
private List<Models.Commit> _searchedCommits = new List<Models.Commit>();
24832466
private Models.Commit _searchResultSelectedCommit = null;
2484-
private List<string> _revisionFiles = new List<string>();
2467+
private List<string> _worktreeFiles = null;
2468+
private List<string> _matchedFilesForSearching = null;
24852469

24862470
private string _filter = string.Empty;
24872471
private object _lockRemotes = new object();

src/Views/Repository.axaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@
424424
HorizontalOffset="-8" VerticalAlignment="-8">
425425
<Popup.IsOpen>
426426
<MultiBinding Converter="{x:Static BoolConverters.And}">
427-
<Binding Path="IsSearchCommitSuggestionOpen"/>
427+
<Binding Path="MatchedFilesForSearching" Converter="{x:Static c:ListConverters.IsNotNullOrEmpty}"/>
428428
<Binding Path="$parent[Window].IsActive"/>
429429
</MultiBinding>
430430
</Popup.IsOpen>
@@ -434,7 +434,7 @@
434434
<ListBox x:Name="SearchSuggestionBox"
435435
Background="Transparent"
436436
SelectionMode="Single"
437-
ItemsSource="{Binding SearchCommitFilterSuggestion}"
437+
ItemsSource="{Binding MatchedFilesForSearching}"
438438
MaxHeight="400"
439439
Focusable="True"
440440
KeyDown="OnSearchSuggestionBoxKeyDown">

src/Views/Repository.axaml.cs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ private void OnSearchKeyDown(object _, KeyEventArgs e)
134134
}
135135
else if (e.Key == Key.Down)
136136
{
137-
if (repo.IsSearchCommitSuggestionOpen)
137+
if (repo.MatchedFilesForSearching is { Count: > 0 })
138138
{
139139
SearchSuggestionBox.Focus(NavigationMethod.Tab);
140140
SearchSuggestionBox.SelectedIndex = 0;
@@ -144,12 +144,7 @@ private void OnSearchKeyDown(object _, KeyEventArgs e)
144144
}
145145
else if (e.Key == Key.Escape)
146146
{
147-
if (repo.IsSearchCommitSuggestionOpen)
148-
{
149-
repo.SearchCommitFilterSuggestion.Clear();
150-
repo.IsSearchCommitSuggestionOpen = false;
151-
}
152-
147+
repo.ClearMatchedFilesForSearching();
153148
e.Handled = true;
154149
}
155150
}
@@ -369,9 +364,7 @@ private void OnSearchSuggestionBoxKeyDown(object _, KeyEventArgs e)
369364

370365
if (e.Key == Key.Escape)
371366
{
372-
repo.IsSearchCommitSuggestionOpen = false;
373-
repo.SearchCommitFilterSuggestion.Clear();
374-
367+
repo.ClearMatchedFilesForSearching();
375368
e.Handled = true;
376369
}
377370
else if (e.Key == Key.Enter && SearchSuggestionBox.SelectedItem is string content)

0 commit comments

Comments
 (0)