refactor: rewrite searching commit by file path

Signed-off-by: leo <longshuang@msn.cn>
This commit is contained in:
leo 2025-03-11 20:55:39 +08:00
parent fa4d9d24e9
commit 64a41dce39
No known key found for this signature in database
3 changed files with 53 additions and 76 deletions

View file

@ -271,14 +271,13 @@ namespace SourceGit.ViewModels
{ {
SearchedCommits = new List<Models.Commit>(); SearchedCommits = new List<Models.Commit>();
SearchCommitFilter = string.Empty; SearchCommitFilter = string.Empty;
SearchCommitFilterSuggestion.Clear(); MatchedFilesForSearching = null;
IsSearchCommitSuggestionOpen = false; _worktreeFiles = null;
_revisionFiles.Clear();
if (value) if (value)
{ {
SelectedViewIndex = 0; SelectedViewIndex = 0;
UpdateCurrentRevisionFilesForSearchSuggestion(); CalcWorktreeFilesForSearching();
} }
} }
} }
@ -307,7 +306,7 @@ namespace SourceGit.ViewModels
{ {
if (SetProperty(ref _searchCommitFilterType, value)) if (SetProperty(ref _searchCommitFilterType, value))
{ {
UpdateCurrentRevisionFilesForSearchSuggestion(); CalcWorktreeFilesForSearching();
if (!string.IsNullOrEmpty(_searchCommitFilter)) if (!string.IsNullOrEmpty(_searchCommitFilter))
StartSearchCommits(); StartSearchCommits();
@ -320,47 +319,22 @@ namespace SourceGit.ViewModels
get => _searchCommitFilter; get => _searchCommitFilter;
set set
{ {
if (SetProperty(ref _searchCommitFilter, value) && if (SetProperty(ref _searchCommitFilter, value))
_searchCommitFilterType == 4 &&
!string.IsNullOrEmpty(value) &&
value.Length >= 2 &&
_revisionFiles.Count > 0)
{ {
var suggestion = new List<string>(); if (_searchCommitFilterType == 4 && value is { Length: > 2 })
foreach (var file in _revisionFiles) CalcMatchedFilesForSearching();
{ else if (_matchedFilesForSearching is { })
if (file.Contains(value, StringComparison.OrdinalIgnoreCase) && file.Length != value.Length) MatchedFilesForSearching = null;
{
suggestion.Add(file);
if (suggestion.Count > 100)
break;
}
}
SearchCommitFilterSuggestion.Clear();
SearchCommitFilterSuggestion.AddRange(suggestion);
IsSearchCommitSuggestionOpen = SearchCommitFilterSuggestion.Count > 0;
}
else if (SearchCommitFilterSuggestion.Count > 0)
{
SearchCommitFilterSuggestion.Clear();
IsSearchCommitSuggestionOpen = false;
} }
} }
} }
public bool IsSearchCommitSuggestionOpen public List<string> MatchedFilesForSearching
{ {
get => _isSearchCommitSuggestionOpen; get => _matchedFilesForSearching;
set => SetProperty(ref _isSearchCommitSuggestionOpen, value); private set => SetProperty(ref _matchedFilesForSearching, value);
} }
public AvaloniaList<string> SearchCommitFilterSuggestion
{
get;
private set;
} = new AvaloniaList<string>();
public List<Models.Commit> SearchedCommits public List<Models.Commit> SearchedCommits
{ {
get => _searchedCommits; get => _searchedCommits;
@ -551,8 +525,8 @@ namespace SourceGit.ViewModels
_visibleSubmodules.Clear(); _visibleSubmodules.Clear();
_searchedCommits.Clear(); _searchedCommits.Clear();
_revisionFiles.Clear(); _worktreeFiles = null;
SearchCommitFilterSuggestion.Clear(); _matchedFilesForSearching = null;
} }
public bool CanCreatePopup() public bool CanCreatePopup()
@ -723,6 +697,11 @@ namespace SourceGit.ViewModels
SearchCommitFilter = string.Empty; SearchCommitFilter = string.Empty;
} }
public void ClearMatchedFilesForSearching()
{
MatchedFilesForSearching = null;
}
public void StartSearchCommits() public void StartSearchCommits()
{ {
if (_histories == null) if (_histories == null)
@ -730,8 +709,7 @@ namespace SourceGit.ViewModels
IsSearchLoadingVisible = true; IsSearchLoadingVisible = true;
SearchResultSelectedCommit = null; SearchResultSelectedCommit = null;
IsSearchCommitSuggestionOpen = false; MatchedFilesForSearching = null;
SearchCommitFilterSuggestion.Clear();
Task.Run(() => Task.Run(() =>
{ {
@ -2391,9 +2369,9 @@ namespace SourceGit.ViewModels
menu.Items.Add(new MenuItem() { Header = "-" }); menu.Items.Add(new MenuItem() { Header = "-" });
} }
private void UpdateCurrentRevisionFilesForSearchSuggestion() private void CalcWorktreeFilesForSearching()
{ {
_revisionFiles.Clear(); _worktreeFiles = null;
if (_searchCommitFilterType == 4) if (_searchCommitFilterType == 4)
{ {
@ -2405,30 +2383,36 @@ namespace SourceGit.ViewModels
if (_searchCommitFilterType != 4) if (_searchCommitFilterType != 4)
return; return;
_revisionFiles.AddRange(files); _worktreeFiles = new List<string>();
foreach (var f in files)
_worktreeFiles.Add(f);
if (!string.IsNullOrEmpty(_searchCommitFilter) && _searchCommitFilter.Length > 2 && _revisionFiles.Count > 0) if (_searchCommitFilter is { Length: > 2 })
{ CalcMatchedFilesForSearching();
var suggestion = new List<string>();
foreach (var file in _revisionFiles)
{
if (file.Contains(_searchCommitFilter, StringComparison.OrdinalIgnoreCase) && file.Length != _searchCommitFilter.Length)
{
suggestion.Add(file);
if (suggestion.Count > 100)
break;
}
}
SearchCommitFilterSuggestion.Clear();
SearchCommitFilterSuggestion.AddRange(suggestion);
IsSearchCommitSuggestionOpen = SearchCommitFilterSuggestion.Count > 0;
}
}); });
}); });
} }
} }
private void CalcMatchedFilesForSearching()
{
if (_worktreeFiles == null || _worktreeFiles.Count == 0)
return;
var matched = new List<string>();
foreach (var file in _worktreeFiles)
{
if (file.Contains(_searchCommitFilter, StringComparison.OrdinalIgnoreCase) && file.Length != _searchCommitFilter.Length)
{
matched.Add(file);
if (matched.Count > 100)
break;
}
}
MatchedFilesForSearching = matched;
}
private void AutoFetchImpl(object sender) private void AutoFetchImpl(object sender)
{ {
if (!_settings.EnableAutoFetch || _isAutoFetching) if (!_settings.EnableAutoFetch || _isAutoFetching)
@ -2475,13 +2459,13 @@ namespace SourceGit.ViewModels
private bool _isSearching = false; private bool _isSearching = false;
private bool _isSearchLoadingVisible = false; private bool _isSearchLoadingVisible = false;
private bool _isSearchCommitSuggestionOpen = false;
private int _searchCommitFilterType = 3; private int _searchCommitFilterType = 3;
private bool _onlySearchCommitsInCurrentBranch = false; private bool _onlySearchCommitsInCurrentBranch = false;
private string _searchCommitFilter = string.Empty; private string _searchCommitFilter = string.Empty;
private List<Models.Commit> _searchedCommits = new List<Models.Commit>(); private List<Models.Commit> _searchedCommits = new List<Models.Commit>();
private Models.Commit _searchResultSelectedCommit = null; private Models.Commit _searchResultSelectedCommit = null;
private List<string> _revisionFiles = new List<string>(); private List<string> _worktreeFiles = null;
private List<string> _matchedFilesForSearching = null;
private string _filter = string.Empty; private string _filter = string.Empty;
private object _lockRemotes = new object(); private object _lockRemotes = new object();

View file

@ -424,7 +424,7 @@
HorizontalOffset="-8" VerticalAlignment="-8"> HorizontalOffset="-8" VerticalAlignment="-8">
<Popup.IsOpen> <Popup.IsOpen>
<MultiBinding Converter="{x:Static BoolConverters.And}"> <MultiBinding Converter="{x:Static BoolConverters.And}">
<Binding Path="IsSearchCommitSuggestionOpen"/> <Binding Path="MatchedFilesForSearching" Converter="{x:Static c:ListConverters.IsNotNullOrEmpty}"/>
<Binding Path="$parent[Window].IsActive"/> <Binding Path="$parent[Window].IsActive"/>
</MultiBinding> </MultiBinding>
</Popup.IsOpen> </Popup.IsOpen>
@ -434,7 +434,7 @@
<ListBox x:Name="SearchSuggestionBox" <ListBox x:Name="SearchSuggestionBox"
Background="Transparent" Background="Transparent"
SelectionMode="Single" SelectionMode="Single"
ItemsSource="{Binding SearchCommitFilterSuggestion}" ItemsSource="{Binding MatchedFilesForSearching}"
MaxHeight="400" MaxHeight="400"
Focusable="True" Focusable="True"
KeyDown="OnSearchSuggestionBoxKeyDown"> KeyDown="OnSearchSuggestionBoxKeyDown">

View file

@ -134,7 +134,7 @@ namespace SourceGit.Views
} }
else if (e.Key == Key.Down) else if (e.Key == Key.Down)
{ {
if (repo.IsSearchCommitSuggestionOpen) if (repo.MatchedFilesForSearching is { Count: > 0 })
{ {
SearchSuggestionBox.Focus(NavigationMethod.Tab); SearchSuggestionBox.Focus(NavigationMethod.Tab);
SearchSuggestionBox.SelectedIndex = 0; SearchSuggestionBox.SelectedIndex = 0;
@ -144,12 +144,7 @@ namespace SourceGit.Views
} }
else if (e.Key == Key.Escape) else if (e.Key == Key.Escape)
{ {
if (repo.IsSearchCommitSuggestionOpen) repo.ClearMatchedFilesForSearching();
{
repo.SearchCommitFilterSuggestion.Clear();
repo.IsSearchCommitSuggestionOpen = false;
}
e.Handled = true; e.Handled = true;
} }
} }
@ -369,9 +364,7 @@ namespace SourceGit.Views
if (e.Key == Key.Escape) if (e.Key == Key.Escape)
{ {
repo.IsSearchCommitSuggestionOpen = false; repo.ClearMatchedFilesForSearching();
repo.SearchCommitFilterSuggestion.Clear();
e.Handled = true; e.Handled = true;
} }
else if (e.Key == Key.Enter && SearchSuggestionBox.SelectedItem is string content) else if (e.Key == Key.Enter && SearchSuggestionBox.SelectedItem is string content)