remove insane overbuilt search in contribs

This commit is contained in:
Ben Fry
2022-03-12 15:29:05 -05:00
parent 8986cf7872
commit 3d9300a2a5
3 changed files with 20 additions and 76 deletions

View File

@@ -25,10 +25,8 @@ import java.awt.EventQueue;
import java.io.*;
import java.lang.reflect.InvocationTargetException;
import java.net.*;
import java.text.Normalizer;
import java.util.*;
import java.util.concurrent.locks.ReentrantLock;
import java.util.regex.Pattern;
import processing.app.Base;
import processing.app.Library;
@@ -55,7 +53,7 @@ public class ContributionListing {
Map<String, List<Contribution>> librariesByCategory;
Map<String, Contribution> librariesByImportHeader;
// TODO: Every contribution is getting added twice
// and nothing is replaced ever.
// and nothing is replaced ever. [akarshit 151031]
Set<Contribution> allContributions;
boolean listDownloaded;
boolean listDownloadFailed;
@@ -71,9 +69,10 @@ public class ContributionListing {
downloadingListingLock = new ReentrantLock();
listingFile = Base.getSettingsFile(LOCAL_FILENAME);
boolean writable = listingFile.setWritable(true, false);
if (writable && listingFile.exists()) {
setAdvertisedList(listingFile);
if (listingFile.exists()) {
// On the EDT already, but do this later on the EDT so that the
// constructor can finish more efficiently for getInstance().
EventQueue.invokeLater(() -> setAdvertisedList(listingFile));
}
}
@@ -211,6 +210,9 @@ public class ContributionListing {
}
/**
* @param filter Filter for either the contrib type or whether it's an update.
*/
protected Set<String> getCategories(Contribution.Filter filter) {
Set<String> outgoing = new HashSet<>();
@@ -232,79 +234,17 @@ public class ContributionListing {
public boolean matches(Contribution contrib, String typed) {
int colon = typed.indexOf(":");
if (colon != -1) {
String isText = typed.substring(0, colon);
String property = typed.substring(colon + 1);
String search = ".*" + typed.toLowerCase() + ".*";
// Chances are the person is still typing the property,
// so rather than make the list flash empty (because nothing
// contains "is:" or "has:"), just return true.
if (!isProperty(property)) {
return true;
}
if ("is".equals(isText) || "has".equals(isText)) {
return hasProperty(contrib, typed.substring(colon + 1));
} else if ("not".equals(isText)) {
return !hasProperty(contrib, typed.substring(colon + 1));
}
}
typed = ".*" + typed.toLowerCase() + ".*";
return (matchField(contrib.getName(), typed) ||
matchField(contrib.getAuthorList(), typed) ||
matchField(contrib.getSentence(), typed) ||
matchField(contrib.getParagraph(), typed) ||
contrib.hasCategory(typed));
return (matchField(contrib.getName(), search) ||
matchField(contrib.getSentence(), search) ||
matchField(contrib.getAuthorList(), search) ||
matchField(contrib.getParagraph(), search));
}
static private boolean matchField(String field, String typed) {
return (field != null) &&
removeAccents(field.toLowerCase()).matches(typed);
}
// TODO is this removing characters with accents, not ascii normalizing them? [fry]
static private String removeAccents(String str) {
String nfdNormalizedString = Normalizer.normalize(str, Normalizer.Form.NFD);
Pattern pattern = Pattern.compile("\\p{InCombiningDiacriticalMarks}+");
return pattern.matcher(nfdNormalizedString).replaceAll("");
}
static private boolean isProperty(String property) {
return property.startsWith("updat") || property.startsWith("upgrad")
|| property.startsWith("instal") && !property.startsWith("installabl")
|| property.equals("tool") || property.startsWith("lib")
|| property.equals("mode") || property.equals("compilation");
}
/**
* Returns true if the contribution fits the given property, false otherwise.
* If the property is invalid, returns false.
*/
private boolean hasProperty(Contribution contrib, String property) {
// update, updates, updatable, upgrade
if (property.startsWith("updat") || property.startsWith("upgrad")) {
return hasUpdates(contrib);
}
if (property.startsWith("instal") && !property.startsWith("installabl")) {
return contrib.isInstalled();
}
if (property.equals("tool")) {
return contrib.getType() == ContributionType.TOOL;
}
if (property.startsWith("lib")) {
return contrib.getType() == ContributionType.LIBRARY;
}
if (property.equals("mode")) {
return contrib.getType() == ContributionType.MODE;
}
return false;
return (field != null) && field.toLowerCase().matches(typed);
}

View File

@@ -232,9 +232,8 @@ public class ContributionTab extends JPanel {
protected void updateCategoryChooser() {
if (categoryChooser != null) {
ArrayList<String> categories;
categoryChooser.removeAllItems();
categories = new ArrayList<>(contribListing.getCategories(filter));
List<String> categories = new ArrayList<>(contribListing.getCategories(filter));
Collections.sort(categories);
boolean categoriesFound = false;
categoryChooser.addItem(ManagerFrame.ANY_CATEGORY);

View File

@@ -38,6 +38,11 @@ X remove "Starred" as a category (it was unused)
X use isFoundation() instead of isSpecial() for tagging list entries
X add "Renderer" as a category
X use https to get the contribs listing
X remove insane overbuilt search in contribs
X sure, we have over a hundred libraries, but we don't have millions
X search by contrib type (is:) wasn't even working properly
X removing accents was trashing some input too
_ allow update of the current Mode
_ if doing an update (not just delete), close sketches and re-open