From a4960614550207494399adb29d44809a5a7f099b Mon Sep 17 00:00:00 2001 From: bigguy345 Date: Sun, 4 Jan 2026 16:00:48 +0200 Subject: [PATCH] Added auto complete strict matching for non-member access first word. i.e "Event" can only show words that start with Event, not contains it --- .../script/autocomplete/AutocompleteItem.java | 51 +++++++++++++++---- .../JavaAutocompleteProvider.java | 15 +++--- .../script/interpreter/type/ClassIndex.java | 2 +- 3 files changed, 51 insertions(+), 17 deletions(-) diff --git a/src/main/java/noppes/npcs/client/gui/util/script/autocomplete/AutocompleteItem.java b/src/main/java/noppes/npcs/client/gui/util/script/autocomplete/AutocompleteItem.java index 96da52b9d..768abf2a3 100644 --- a/src/main/java/noppes/npcs/client/gui/util/script/autocomplete/AutocompleteItem.java +++ b/src/main/java/noppes/npcs/client/gui/util/script/autocomplete/AutocompleteItem.java @@ -36,7 +36,8 @@ public class AutocompleteItem implements Comparable { } } - private final String name; // Display name (e.g., "getPlayer") + private final String name; // Display name (e.g., "getPlayer" or "getPlayer(UUID id)" for methods) + private final String searchName; // Name to search against (just the identifier, e.g., "getPlayer") private final String insertText; // Text to insert (e.g., "getPlayer()") private final Kind kind; // Type of completion private final String typeLabel; // Type label (e.g., "IPlayer", "void") @@ -53,10 +54,11 @@ public class AutocompleteItem implements Comparable { private int matchScore = 0; // How well this matches the query private int[] matchIndices; // Indices of matched characters for highlighting - private AutocompleteItem(String name, String insertText, Kind kind, String typeLabel, + private AutocompleteItem(String name, String searchName, String insertText, Kind kind, String typeLabel, String signature, String documentation, Object sourceData, boolean deprecated, boolean requiresImport, String importPath) { this.name = name; + this.searchName = searchName != null ? searchName : name; // Default to name if not provided this.insertText = insertText; this.kind = kind; this.typeLabel = typeLabel; @@ -106,6 +108,7 @@ public class AutocompleteItem implements Comparable { return new AutocompleteItem( displayName.toString(), + name, // Search against just the method name, not the params insertText.toString(), Kind.METHOD, returnType, @@ -142,6 +145,7 @@ public class AutocompleteItem implements Comparable { return new AutocompleteItem( field.getName(), + field.getName(), // searchName same as display name for fields field.getName(), kind, typeLabel, @@ -172,6 +176,7 @@ public class AutocompleteItem implements Comparable { return new AutocompleteItem( type.getSimpleName(), + type.getSimpleName(), // searchName same as display name for types type.getSimpleName(), kind, type.getPackageName(), @@ -199,6 +204,7 @@ public class AutocompleteItem implements Comparable { return new AutocompleteItem( displayName, + name, // Search against just the method name insertText.toString(), Kind.METHOD, method.getReturnType(), @@ -217,6 +223,7 @@ public class AutocompleteItem implements Comparable { public static AutocompleteItem fromJSField(JSFieldInfo field) { return new AutocompleteItem( field.getName(), + field.getName(), // searchName same as display name field.getName(), Kind.FIELD, field.getType(), @@ -235,6 +242,7 @@ public class AutocompleteItem implements Comparable { public static AutocompleteItem keyword(String keyword) { return new AutocompleteItem( keyword, + keyword, // searchName same as display name keyword, Kind.KEYWORD, "keyword", @@ -272,15 +280,18 @@ public class AutocompleteItem implements Comparable { /** * Calculate fuzzy match score against a query string. * Returns -1 if no match, or a positive score (higher = better match). + * + * @param query The search query + * @param requirePrefix If true, only match items that start with the query (no fuzzy/contains matching) */ - public int calculateMatchScore(String query) { + public int calculateMatchScore(String query, boolean requirePrefix) { if (query == null || query.isEmpty()) { matchScore = 100; // Everything matches empty query matchIndices = new int[0]; return matchScore; } - String lowerName = name.toLowerCase(); + String lowerName = searchName.toLowerCase(); String lowerQuery = query.toLowerCase(); // Exact prefix match is best @@ -293,6 +304,13 @@ public class AutocompleteItem implements Comparable { return matchScore; } + // If requirePrefix is true, stop here - no fuzzy/substring matching + if (requirePrefix) { + matchScore = -1; + matchIndices = new int[0]; + return matchScore; + } + // Exact substring match int subIndex = lowerName.indexOf(lowerQuery); if (subIndex >= 0) { @@ -312,8 +330,8 @@ public class AutocompleteItem implements Comparable { int consecutiveBonus = 0; int lastMatchIdx = -2; - while (queryIdx < query.length() && nameIdx < name.length()) { - if (Character.toLowerCase(name.charAt(nameIdx)) == lowerQuery.charAt(queryIdx)) { + while (queryIdx < query.length() && nameIdx < searchName.length()) { + if (Character.toLowerCase(searchName.charAt(nameIdx)) == lowerQuery.charAt(queryIdx)) { indices[queryIdx] = nameIdx; // Bonus for consecutive matches @@ -322,9 +340,9 @@ public class AutocompleteItem implements Comparable { } // Bonus for matching at word boundaries (camelCase) - if (nameIdx == 0 || !Character.isLetterOrDigit(name.charAt(nameIdx - 1)) || - (Character.isUpperCase(name.charAt(nameIdx)) && nameIdx > 0 && - Character.isLowerCase(name.charAt(nameIdx - 1)))) { + if (nameIdx == 0 || !Character.isLetterOrDigit(searchName.charAt(nameIdx - 1)) || + (Character.isUpperCase(searchName.charAt(nameIdx)) && nameIdx > 0 && + Character.isLowerCase(searchName.charAt(nameIdx - 1)))) { consecutiveBonus += 20; } @@ -348,6 +366,13 @@ public class AutocompleteItem implements Comparable { return matchScore; } + /** + * Backward compatibility overload - defaults to fuzzy matching (no prefix requirement). + */ + public int calculateMatchScore(String query) { + return calculateMatchScore(query, false); + } + // ==================== GETTERS ==================== public String getName() { return name; } @@ -423,6 +448,7 @@ public class AutocompleteItem implements Comparable { */ public static class Builder { private String name; + private String searchName; private String insertText; private Kind kind = Kind.FIELD; private String typeLabel = ""; @@ -438,6 +464,11 @@ public class AutocompleteItem implements Comparable { return this; } + public Builder searchName(String searchName) { + this.searchName = searchName; + return this; + } + public Builder insertText(String insertText) { this.insertText = insertText; return this; @@ -487,7 +518,7 @@ public class AutocompleteItem implements Comparable { if (insertText == null) { insertText = name; } - return new AutocompleteItem(name, insertText, kind, typeLabel, + return new AutocompleteItem(name, searchName, insertText, kind, typeLabel, signature, documentation, sourceData, deprecated, requiresImport, importPath); } } diff --git a/src/main/java/noppes/npcs/client/gui/util/script/autocomplete/JavaAutocompleteProvider.java b/src/main/java/noppes/npcs/client/gui/util/script/autocomplete/JavaAutocompleteProvider.java index 4126d8526..d1fc2c017 100644 --- a/src/main/java/noppes/npcs/client/gui/util/script/autocomplete/JavaAutocompleteProvider.java +++ b/src/main/java/noppes/npcs/client/gui/util/script/autocomplete/JavaAutocompleteProvider.java @@ -46,7 +46,7 @@ public class JavaAutocompleteProvider implements AutocompleteProvider { } // Filter and score by prefix - filterAndScore(items, context.prefix); + filterAndScore(items, context.prefix, context.isMemberAccess); // Sort by score Collections.sort(items); @@ -240,9 +240,8 @@ public class JavaAutocompleteProvider implements AutocompleteProvider { private void addUnimportedClassSuggestions(String prefix, List items) { // Get the type resolver TypeResolver resolver = TypeResolver.getInstance(); - // Find classes matching this prefix (not just exact matches) - List matchingClasses = resolver.findClassesByPrefix(prefix, 15); + List matchingClasses = resolver.findClassesByPrefix(prefix, -1); // Track what's already imported to avoid duplicates Set importedFullNames = new HashSet<>(); @@ -335,20 +334,24 @@ public class JavaAutocompleteProvider implements AutocompleteProvider { /** * Filter items by prefix and calculate match scores. */ - private void filterAndScore(List items, String prefix) { + private void filterAndScore(List items, String prefix, boolean isMemberAccess) { if (prefix == null || prefix.isEmpty()) { // No filtering needed, all items get a base score for (AutocompleteItem item : items) { - item.calculateMatchScore(""); + item.calculateMatchScore("", false); } return; } + // For non-member access (first word), require strict prefix matching + // For member access (after dot), allow fuzzy/contains matching + boolean requirePrefix = !isMemberAccess; + // Filter and score Iterator iter = items.iterator(); while (iter.hasNext()) { AutocompleteItem item = iter.next(); - int score = item.calculateMatchScore(prefix); + int score = item.calculateMatchScore(prefix, requirePrefix); if (score < 0) { iter.remove(); } diff --git a/src/main/java/noppes/npcs/client/gui/util/script/interpreter/type/ClassIndex.java b/src/main/java/noppes/npcs/client/gui/util/script/interpreter/type/ClassIndex.java index 8750f16ab..7a7829fe6 100644 --- a/src/main/java/noppes/npcs/client/gui/util/script/interpreter/type/ClassIndex.java +++ b/src/main/java/noppes/npcs/client/gui/util/script/interpreter/type/ClassIndex.java @@ -166,7 +166,7 @@ public class ClassIndex { for (Class clazz : entry.getValue()) { results.add(clazz.getName()); - if (results.size() >= maxResults) { + if (maxResults != -1 && results.size() >= maxResults) { return results; } }