Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make FuzzyMatcher more convenient to use #1583

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1093,7 +1093,7 @@ private void DrawChangelogList(bool displayDalamud, bool displayPlugins)
return;
}

IEnumerable<IChangelogEntry> changelogs = null;
IEnumerable<IChangelogEntry>? changelogs = null;
if (displayDalamud && displayPlugins && this.dalamudChangelogManager.Changelogs != null)
{
changelogs = this.dalamudChangelogManager.Changelogs;
Expand All @@ -1107,10 +1107,15 @@ private void DrawChangelogList(bool displayDalamud, bool displayPlugins)
changelogs = this.dalamudChangelogManager.Changelogs.OfType<PluginChangelogEntry>();
}

var sortedChangelogs = changelogs?.Where(x => this.searchText.IsNullOrWhitespace() || new FuzzyMatcher(this.searchText.ToLowerInvariant(), MatchMode.FuzzyParts).Matches(x.Title.ToLowerInvariant()) > 0)
.OrderByDescending(x => x.Date).ToList();
changelogs ??= Array.Empty<IChangelogEntry>();
var sortedChangelogs =
this.searchText.IsNullOrWhitespace()
? changelogs.ToList()
: changelogs.Where(x => x.Title.FuzzyMatchesParts(this.searchText))
.OrderByDescending(x => x.Date)
.ToList();

if (sortedChangelogs == null || !sortedChangelogs.Any())
if (!sortedChangelogs.Any())
{
ImGui.TextColored(
ImGuiColors.DalamudGrey2,
Expand Down Expand Up @@ -3185,21 +3190,25 @@ private bool DrawPluginImages(LocalPlugin? plugin, IPluginManifest manifest, boo

private bool IsManifestFiltered(IPluginManifest manifest)
{
var searchString = this.searchText.ToLowerInvariant();
var matcher = new FuzzyMatcher(searchString, MatchMode.FuzzyParts);
var hasSearchString = !string.IsNullOrWhiteSpace(this.searchText);
var oldApi = manifest.DalamudApiLevel < PluginManager.DalamudApiLevel;
var installed = this.IsManifestInstalled(manifest).IsInstalled;

if (oldApi && !hasSearchString && !installed)
return true;
// Are we not searching at all? Hide plugins of older API levels that aren't installed.
if (!hasSearchString)
return oldApi && !installed;

return hasSearchString && !(
(!manifest.Name.IsNullOrEmpty() && matcher.Matches(manifest.Name.ToLowerInvariant()) > 0) ||
(!manifest.InternalName.IsNullOrEmpty() && matcher.Matches(manifest.InternalName.ToLowerInvariant()) > 0) ||
(!manifest.Author.IsNullOrEmpty() && matcher.Matches(manifest.Author.ToLowerInvariant()) > 0) ||
(!manifest.Punchline.IsNullOrEmpty() && manifest.Punchline.ToLowerInvariant().Contains(searchString)) ||
(manifest.Tags != null && matcher.MatchesAny(manifest.Tags.Select(term => term.ToLowerInvariant()).ToArray()) > 0));
if (manifest.Name.FuzzyMatchesParts(this.searchText))
return false;
if (manifest.InternalName.FuzzyMatchesParts(this.searchText))
return false;
if (manifest.Author.FuzzyMatchesParts(this.searchText))
return false;
if (manifest.Punchline?.Contains(this.searchText, StringComparison.InvariantCultureIgnoreCase) is true)
return false;
if (manifest.Tags?.Any(x => x.FuzzyMatchesParts(this.searchText)) is true)
return false;
return true;
}

private (bool IsInstalled, LocalPlugin Plugin) IsManifestInstalled(IPluginManifest? manifest)
Expand Down
Loading
Loading