Plugin Hub: Rework search to something remotely useful

This commit is contained in:
loldudester
2020-02-27 01:49:17 +00:00
parent fdd75b1296
commit dcc2c577bd

View File

@@ -37,7 +37,9 @@ import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
@@ -45,7 +47,6 @@ import java.util.Map;
import java.util.Set;
import java.util.concurrent.ScheduledExecutorService;
import java.util.function.Function;
import java.util.function.ToDoubleFunction;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@@ -85,8 +86,8 @@ import net.runelite.client.ui.components.IconTextField;
import net.runelite.client.util.ImageUtil;
import net.runelite.client.util.LinkBrowser;
import net.runelite.client.util.SwingUtil;
import net.runelite.client.util.Text;
import net.runelite.client.util.VerificationException;
import org.apache.commons.text.similarity.JaroWinklerDistance;
@Slf4j
@Singleton
@@ -98,7 +99,6 @@ class PluginHubPanel extends PluginPanel
private static final ImageIcon CONFIGURE_ICON;
private static final ImageIcon CONFIGURE_ICON_HOVER;
private static final Pattern SPACES = Pattern.compile(" +");
private static final JaroWinklerDistance DISTANCE = new JaroWinklerDistance();
static
{
@@ -119,16 +119,13 @@ class PluginHubPanel extends PluginPanel
private static final int HEIGHT = 70;
private static final int ICON_WIDTH = 48;
private static final int BOTTOM_LINE_HEIGHT = 16;
static final float MIN_FILTER_SCORE = .8f;
private final ExternalPluginManifest manifest;
private final List<String> keywords = new ArrayList<>();
@Getter
private final boolean installed;
@Getter
private float filter;
PluginItem(ExternalPluginManifest newManifest, Collection<Plugin> loadedPlugins, boolean installed)
{
ExternalPluginManifest loaded = null;
@@ -140,6 +137,23 @@ class PluginHubPanel extends PluginPanel
manifest = newManifest == null ? loaded : newManifest;
this.installed = installed;
if (manifest != null)
{
Collections.addAll(keywords, SPACES.split(manifest.getDisplayName().toLowerCase()));
if (manifest.getDescription() != null)
{
Collections.addAll(keywords, SPACES.split(manifest.getDescription().toLowerCase()));
}
Collections.addAll(keywords, manifest.getAuthor().toLowerCase());
if (manifest.getTags() != null)
{
Collections.addAll(keywords, manifest.getTags());
}
}
setBackground(ColorScheme.DARKER_GRAY_COLOR);
setOpaque(true);
@@ -302,23 +316,6 @@ class PluginHubPanel extends PluginPanel
.addComponent(addrm, BOTTOM_LINE_HEIGHT, BOTTOM_LINE_HEIGHT, BOTTOM_LINE_HEIGHT))
.addGap(5)));
}
float setFilter(String[] filter)
{
ToDoubleFunction<String> match = r -> Stream.of(filter)
.mapToDouble(l -> Math.pow(DISTANCE.apply(l, r), 2))
.max()
.orElse(0.D);
double sim = SPACES.splitAsStream(manifest.getDisplayName()).collect(Collectors.averagingDouble(match)) * 2;
if (manifest.getTags() != null)
{
sim += Stream.of(manifest.getTags()).mapToDouble(match).sum();
}
return this.filter = (float) sim;
}
}
private final PluginListPanel pluginListPanel;
@@ -537,8 +534,8 @@ class PluginHubPanel extends PluginPanel
{
String[] searchArray = SPACES.split(search.toLowerCase());
stream = stream
.filter(p -> p.setFilter(searchArray) > PluginItem.MIN_FILTER_SCORE)
.sorted(Comparator.comparing(PluginItem::getFilter));
.filter(p -> Text.matchesSearchTerms(searchArray, p.keywords))
.sorted(Comparator.comparing(p -> p.manifest.getDisplayName()));
}
else
{