mirror of
https://github.com/GTNewHorizons/NotEnoughItems.git
synced 2026-11-17 02:55:04 +00:00
Compare commits
2
Commits
2.9.7-GTNH
...
2.9.8-GTNH
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4257235e6e | ||
|
|
1c33a5b7d6 |
No files matched your search
@@ -24,6 +24,7 @@ DASH : '-' ;
|
||||
RECIPE_INGREDIENTS : '<&' | '<' ;
|
||||
RECIPE_RESULT : '>&' | '>' ;
|
||||
RECIPE_OTHERS : '^&' | '^' ;
|
||||
RECIPE_TEXT : '~' ;
|
||||
|
||||
OR : '|' ;
|
||||
LEFT_BRACKET : '{' ;
|
||||
@@ -33,7 +34,7 @@ SPACE : ' ' ;
|
||||
ESCAPED_SYMBOL : '\\' . ;
|
||||
|
||||
PREFIX : CLEAN_SYMBOL {searchParser.hasRedefinedPrefix(getText().charAt(0))}? ;
|
||||
CLEAN_SYMBOL : ~[-<>^{}|/\\ "] ;
|
||||
CLEAN_SYMBOL : ~[-<>^~{}|/\\ "] ;
|
||||
|
||||
mode REGEX;
|
||||
|
||||
|
||||
@@ -19,6 +19,7 @@ recipeClauseExpression
|
||||
: ingredientsPrefix=RECIPE_INGREDIENTS searchExpression[0, $ingredientsPrefix.getText().length() > 1]
|
||||
| resultPrefix=RECIPE_RESULT searchExpression[1, $resultPrefix.getText().length() > 1]
|
||||
| othersPrefix=RECIPE_OTHERS searchExpression[2, $othersPrefix.getText().length() > 1]
|
||||
| RECIPE_TEXT searchExpression[4, false]
|
||||
| searchExpression[3, false]
|
||||
;
|
||||
|
||||
|
||||
@@ -58,7 +58,9 @@ public class SearchTextFormatter implements TextFormatter {
|
||||
|
||||
int startIndex = 0;
|
||||
|
||||
for (SearchToken token : searchParser.splitSearchText(contextPart[1])) {
|
||||
final String prefixes = contextPart[0].startsWith("~") ? "" : searchParser.getPrefixes();
|
||||
|
||||
for (SearchToken token : searchParser.splitSearchText(contextPart[1], prefixes)) {
|
||||
formattedText.append(contextPart[1].substring(startIndex, token.start));
|
||||
EnumChatFormatting tokenColor = EnumChatFormatting.RESET;
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import net.minecraft.client.Minecraft;
|
||||
@@ -21,6 +22,7 @@ import codechicken.nei.filter.AllMultiRecipeFilter;
|
||||
import codechicken.nei.filter.AnyMultiRecipeFilter;
|
||||
import codechicken.nei.filter.RecipeFilter;
|
||||
import codechicken.nei.filter.RecipeFilter.FilterContext;
|
||||
import codechicken.nei.filter.RecipeTextFilter;
|
||||
import codechicken.nei.search.ItemFilterVisitor;
|
||||
import codechicken.nei.search.RecipeFilterVisitor;
|
||||
import codechicken.nei.search.SearchExpressionUtils;
|
||||
@@ -207,7 +209,8 @@ public class SearchTokenParser {
|
||||
for (String[] contextQuery : splitByDelimiters(subQuery[1], contextTokens, false)) {
|
||||
|
||||
if (contextQuery[1].isEmpty() || contextQuery[0].startsWith(">")
|
||||
|| contextQuery[0].startsWith("^")) {
|
||||
|| contextQuery[0].startsWith("^")
|
||||
|| contextQuery[0].startsWith("~")) {
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -286,6 +289,7 @@ public class SearchTokenParser {
|
||||
}
|
||||
|
||||
final List<IRecipeFilter> tokens = new ArrayList<>();
|
||||
final boolean recipeText = contextQuery[0].startsWith("~");
|
||||
FilterContext context = FilterContext.ANY;
|
||||
|
||||
if (!contextQuery[0].isEmpty()) {
|
||||
@@ -293,12 +297,26 @@ public class SearchTokenParser {
|
||||
contextQuery[0] = contextQuery[0].substring(1);
|
||||
}
|
||||
|
||||
for (SearchToken token : splitSearchText(contextQuery[0] + contextQuery[1])) {
|
||||
final ItemFilter result = token.getFilter(this);
|
||||
if (result != null) {
|
||||
tokens.add(
|
||||
new RecipeFilter(context, token.ignore == null || token.ignore != '!', result));
|
||||
for (SearchToken token : splitSearchText(
|
||||
contextQuery[0] + contextQuery[1],
|
||||
recipeText ? "" : getPrefixes())) {
|
||||
|
||||
if (recipeText) {
|
||||
final IRecipeFilter text = recipeTextFilter(token);
|
||||
if (text != null) {
|
||||
tokens.add(text);
|
||||
}
|
||||
} else {
|
||||
final ItemFilter result = token.getFilter(this);
|
||||
if (result != null) {
|
||||
tokens.add(
|
||||
new RecipeFilter(
|
||||
context,
|
||||
token.ignore == null || token.ignore != '!',
|
||||
result));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (tokens.size() == 1) {
|
||||
@@ -338,14 +356,31 @@ public class SearchTokenParser {
|
||||
|
||||
}
|
||||
|
||||
private static IRecipeFilter recipeTextFilter(SearchToken token) {
|
||||
if (token.rawText.isEmpty()) return null;
|
||||
final List<Predicate<List<String>>> words = new ArrayList<>();
|
||||
|
||||
for (String word : token.words) {
|
||||
final Pattern pattern = SearchField.getPattern(word);
|
||||
if (pattern != null) words.add(RecipeTextFilter.matching(pattern));
|
||||
}
|
||||
|
||||
if (words.isEmpty()) return null;
|
||||
final Predicate<List<String>> text = lines -> words.stream().allMatch(word -> word.test(lines));
|
||||
return new RecipeTextFilter(token.ignore == null ? text : text.negate());
|
||||
}
|
||||
|
||||
public List<SearchToken> splitSearchText(String filterText) {
|
||||
return splitSearchText(filterText, getPrefixes());
|
||||
}
|
||||
|
||||
public List<SearchToken> splitSearchText(String filterText, String prefixes) {
|
||||
if (filterText.isEmpty()) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
final List<SearchToken> tokens = new ArrayList<>();
|
||||
final int spaceMode = NEIClientConfig.getIntSetting("inventory.search.spaceMode");
|
||||
final String prefixes = getPrefixes();
|
||||
SearchToken lastToken = null;
|
||||
int lastEnd = 0;
|
||||
|
||||
@@ -514,7 +549,7 @@ public class SearchTokenParser {
|
||||
|
||||
public String getRecipeContextTokens() {
|
||||
// '^' is the start anchor and the negated class in regex mode
|
||||
return NEIClientConfig.getIntSetting("inventory.search.patternMode") == 2 ? "<>" : "<>^";
|
||||
return NEIClientConfig.getIntSetting("inventory.search.patternMode") == 2 ? "<>~" : "<>~^";
|
||||
}
|
||||
|
||||
public char getRedefinedPrefix(char prefix) {
|
||||
|
||||
@@ -9,6 +9,7 @@ import codechicken.nei.api.IRecipeFilter;
|
||||
import codechicken.nei.api.ItemFilter;
|
||||
import codechicken.nei.api.ItemInfo;
|
||||
import codechicken.nei.recipe.IRecipeHandler;
|
||||
import codechicken.nei.recipe.RecipeRole;
|
||||
|
||||
public class RecipeFilter implements IRecipeFilter {
|
||||
|
||||
@@ -47,17 +48,17 @@ public class RecipeFilter implements IRecipeFilter {
|
||||
public boolean matches(IRecipeHandler handler, int recipeIndex) {
|
||||
|
||||
if ((this.context == FilterContext.ANY || this.context == FilterContext.INPUT)
|
||||
&& matchPositionedStack(handler.getIngredientStacks(recipeIndex), this.anyMatch)) {
|
||||
&& matchPositionedStack(handler.getSearchStacks(recipeIndex, RecipeRole.INPUT), this.anyMatch)) {
|
||||
return this.anyMatch;
|
||||
}
|
||||
|
||||
if ((this.context == FilterContext.ANY || this.context == FilterContext.OUTPUT)
|
||||
&& matchPositionedStack(handler.getResultStacks(recipeIndex), this.anyMatch)) {
|
||||
&& matchPositionedStack(handler.getSearchStacks(recipeIndex, RecipeRole.OUTPUT), this.anyMatch)) {
|
||||
return this.anyMatch;
|
||||
}
|
||||
|
||||
if ((this.context == FilterContext.ANY || this.context == FilterContext.AUXILIARY)
|
||||
&& matchPositionedStack(handler.getAuxiliaryStacks(recipeIndex), this.anyMatch)) {
|
||||
&& matchPositionedStack(handler.getSearchStacks(recipeIndex, RecipeRole.AUXILIARY), this.anyMatch)) {
|
||||
return this.anyMatch;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
package codechicken.nei.filter;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import codechicken.nei.api.IRecipeFilter;
|
||||
import codechicken.nei.recipe.IRecipeHandler;
|
||||
|
||||
public class RecipeTextFilter implements IRecipeFilter {
|
||||
|
||||
private final Predicate<List<String>> lines;
|
||||
|
||||
public RecipeTextFilter(Predicate<List<String>> lines) {
|
||||
this.lines = lines;
|
||||
}
|
||||
|
||||
public static Predicate<List<String>> matching(Pattern pattern) {
|
||||
return lines -> {
|
||||
for (String line : lines) {
|
||||
if (pattern.matcher(line).find()) return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean matches(IRecipeHandler handler, int recipeIndex) {
|
||||
final List<String> text = handler.getSearchText(recipeIndex);
|
||||
return !text.isEmpty() && this.lines.test(text);
|
||||
}
|
||||
}
|
||||
@@ -199,6 +199,10 @@ public abstract class GuiRecipe<H extends IRecipeHandler> extends GuiContainer i
|
||||
NEIClientUtils.translate(
|
||||
"recipe.search.tooltip.output",
|
||||
SearchExpressionUtils.HIGHLIGHTS.RECIPE + ">"));
|
||||
tooltip.add(
|
||||
NEIClientUtils.translate(
|
||||
"recipe.search.tooltip.text",
|
||||
SearchExpressionUtils.HIGHLIGHTS.RECIPE + "~"));
|
||||
|
||||
final int patternMode = NEIClientConfig.getIntSetting("inventory.search.patternMode");
|
||||
|
||||
|
||||
@@ -121,6 +121,26 @@ public interface IRecipeHandler {
|
||||
return getResultStack(recipe) != null ? getOtherStacks(recipe) : Collections.emptyList();
|
||||
}
|
||||
|
||||
/**
|
||||
* Override it to let the recipe search see stacks that the recipe doesn't draw and doesn't give to the overlay.
|
||||
*
|
||||
* @return The stacks of the role the recipe search looks at; called from the search threads.
|
||||
*/
|
||||
default List<PositionedStack> getSearchStacks(int recipe, RecipeRole role) {
|
||||
return switch (role) {
|
||||
case INPUT -> getIngredientStacks(recipe);
|
||||
case OUTPUT -> getResultStacks(recipe);
|
||||
case AUXILIARY -> getAuxiliaryStacks(recipe);
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The text the recipe text search looks at, none by default; called from the search threads.
|
||||
*/
|
||||
default List<String> getSearchText(int recipe) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param recipe The recipe index to get the group id for.
|
||||
* @return An id shared by every recipe in this recipe's group, or {@code null} if this recipe isn't grouped.
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
package codechicken.nei.recipe;
|
||||
|
||||
public enum RecipeRole {
|
||||
INPUT,
|
||||
OUTPUT,
|
||||
AUXILIARY
|
||||
}
|
||||
@@ -536,11 +536,7 @@ public class DebugHandlerWidget extends Widget implements IContainerTooltipHandl
|
||||
@Override
|
||||
public boolean handleKeyPress(int keyID, char keyChar) {
|
||||
|
||||
if (NEIClientConfig.getBooleanSetting("inventory.guirecipe.handlerInfo") && keyID == Keyboard.KEY_D
|
||||
&& NEIClientUtils.shiftKey()) {
|
||||
this.showWidget = !this.showWidget;
|
||||
return true;
|
||||
} else if (this.record != null) {
|
||||
if (this.record != null) {
|
||||
|
||||
if (this.container.handleKeyPress(keyID, keyChar)) {
|
||||
return true;
|
||||
@@ -556,6 +552,13 @@ public class DebugHandlerWidget extends Widget implements IContainerTooltipHandl
|
||||
|
||||
@Override
|
||||
public boolean onHotkeyPressed() {
|
||||
|
||||
if (NEIClientConfig.getBooleanSetting("inventory.guirecipe.handlerInfo") && Keyboard.isKeyDown(Keyboard.KEY_D)
|
||||
&& NEIClientUtils.shiftKey()) {
|
||||
this.showWidget = !this.showWidget;
|
||||
return true;
|
||||
}
|
||||
|
||||
return this.record != null && this.container.onHotkeyPressed();
|
||||
}
|
||||
|
||||
|
||||
@@ -3,7 +3,6 @@ package codechicken.nei.search;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.function.Function;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.antlr.v4.runtime.ParserRuleContext;
|
||||
import org.antlr.v4.runtime.tree.ParseTree;
|
||||
@@ -13,17 +12,11 @@ import codechicken.nei.ItemList.AllMultiItemFilter;
|
||||
import codechicken.nei.ItemList.AnyMultiItemFilter;
|
||||
import codechicken.nei.ItemList.NegatedItemFilter;
|
||||
import codechicken.nei.ItemList.NothingItemFilter;
|
||||
import codechicken.nei.NEIClientConfig;
|
||||
import codechicken.nei.SearchTokenParser;
|
||||
import codechicken.nei.api.ItemFilter;
|
||||
|
||||
public class ItemFilterVisitor extends AbstractSearchExpressionVisitor<ItemFilter> {
|
||||
|
||||
private static final Pattern REGEX_ESCAPED_SPACE_PATTERN = Pattern.compile("([^\\\\](?:\\\\\\\\)+)?\\\\ ");
|
||||
private static final Pattern ESCAPED_SYMBOL_PATTERN = Pattern.compile("\\\\(.)");
|
||||
private static final Pattern ESCAPED_QUOTE_PATTERN = Pattern.compile("\\\\\"");
|
||||
private static final Pattern ESCAPED_SPACE_PATTERN = Pattern.compile("\\\\ ");
|
||||
|
||||
public ItemFilterVisitor(SearchTokenParser searchParser) {
|
||||
super(searchParser);
|
||||
}
|
||||
@@ -69,7 +62,7 @@ public class ItemFilterVisitor extends AbstractSearchExpressionVisitor<ItemFilte
|
||||
@Override
|
||||
public ItemFilter visitPrefixedExpression(SearchExpressionParser.PrefixedExpressionContext ctx) {
|
||||
if (ctx.token() != null) {
|
||||
final String cleanPattern = getCleanPattern(ctx.token());
|
||||
final String cleanPattern = SearchExpressionUtils.getCleanPattern(ctx.token());
|
||||
final SearchTokenParser.ISearchParserProvider provider = searchParser.getProvider(ctx.token().prefix);
|
||||
if (cleanPattern == null || provider == null) {
|
||||
return defaultResult();
|
||||
@@ -81,7 +74,7 @@ public class ItemFilterVisitor extends AbstractSearchExpressionVisitor<ItemFilte
|
||||
|
||||
@Override
|
||||
public ItemFilter visitToken(SearchExpressionParser.TokenContext ctx) {
|
||||
final String cleanPattern = getCleanPattern(ctx);
|
||||
final String cleanPattern = SearchExpressionUtils.getCleanPattern(ctx);
|
||||
if (cleanPattern != null) {
|
||||
return getAlwaysProvidersFilter(cleanPattern);
|
||||
}
|
||||
@@ -118,38 +111,6 @@ public class ItemFilterVisitor extends AbstractSearchExpressionVisitor<ItemFilte
|
||||
return constructFilter(filters, AllMultiItemFilter::new);
|
||||
}
|
||||
|
||||
private String getCleanPattern(SearchExpressionParser.TokenContext ctx) {
|
||||
String cleanPattern = null;
|
||||
if (ctx.plainText() != null) {
|
||||
cleanPattern = Pattern.quote(
|
||||
ESCAPED_SYMBOL_PATTERN.matcher(ctx.plainText().getText())
|
||||
// Unescape everything in search expression
|
||||
.replaceAll("$1"));
|
||||
} else if (ctx.regex() != null) {
|
||||
if (ctx.regex().REGEX_CONTENT() != null) {
|
||||
cleanPattern = ctx.regex().REGEX_CONTENT().getSymbol().getText();
|
||||
final int spaceModeEnabled = NEIClientConfig.getIntSetting("inventory.search.spaceMode");
|
||||
// Unescape spaces
|
||||
if (spaceModeEnabled == 1) {
|
||||
cleanPattern = REGEX_ESCAPED_SPACE_PATTERN.matcher(cleanPattern).replaceAll("$1 ");
|
||||
}
|
||||
}
|
||||
} else if (ctx.quoted() != null) {
|
||||
if (ctx.quoted().QUOTED_CONTENT() != null) {
|
||||
cleanPattern = ctx.quoted().QUOTED_CONTENT().getSymbol().getText();
|
||||
// Unescape quotes
|
||||
cleanPattern = ESCAPED_QUOTE_PATTERN.matcher(cleanPattern).replaceAll("\"");
|
||||
cleanPattern = Pattern.quote(cleanPattern);
|
||||
final int spaceModeEnabled = NEIClientConfig.getIntSetting("inventory.search.spaceMode");
|
||||
// Unescape spaces
|
||||
if (spaceModeEnabled == 1) {
|
||||
cleanPattern = ESCAPED_SPACE_PATTERN.matcher(cleanPattern).replaceAll(" ");
|
||||
}
|
||||
}
|
||||
}
|
||||
return cleanPattern;
|
||||
}
|
||||
|
||||
private ItemFilter visitChildren(ParserRuleContext node, Function<List<ItemFilter>, ItemFilter> filterConstructor) {
|
||||
if (node.children != null && !node.children.isEmpty()) {
|
||||
// By default return any found rule child filter (there should be only one)
|
||||
|
||||
@@ -2,6 +2,7 @@ package codechicken.nei.search;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
import codechicken.nei.ItemList.EverythingItemFilter;
|
||||
import codechicken.nei.SearchTokenParser;
|
||||
@@ -10,6 +11,7 @@ import codechicken.nei.api.ItemFilter;
|
||||
import codechicken.nei.filter.AllMultiRecipeFilter;
|
||||
import codechicken.nei.filter.RecipeFilter;
|
||||
import codechicken.nei.filter.RecipeFilter.FilterContext;
|
||||
import codechicken.nei.filter.RecipeTextFilter;
|
||||
|
||||
public class RecipeFilterVisitor extends AbstractSearchExpressionVisitor<IRecipeFilter> {
|
||||
|
||||
@@ -41,6 +43,10 @@ public class RecipeFilterVisitor extends AbstractSearchExpressionVisitor<IRecipe
|
||||
if (ctx == null) {
|
||||
return defaultResult();
|
||||
}
|
||||
if (ctx.type == 4) {
|
||||
final Predicate<List<String>> text = new RecipeTextVisitor(this.searchParser).visitSearchExpression(ctx);
|
||||
return text == null ? defaultResult() : new RecipeTextFilter(text);
|
||||
}
|
||||
final ItemFilter itemFilter = itemFilterVisitor.visitSearchExpression(ctx);
|
||||
switch (ctx.type) {
|
||||
case 0:
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
package codechicken.nei.search;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.antlr.v4.runtime.ParserRuleContext;
|
||||
import org.antlr.v4.runtime.tree.ParseTree;
|
||||
import org.antlr.v4.runtime.tree.RuleNode;
|
||||
|
||||
import codechicken.nei.SearchField;
|
||||
import codechicken.nei.SearchTokenParser;
|
||||
import codechicken.nei.filter.RecipeTextFilter;
|
||||
|
||||
public class RecipeTextVisitor extends AbstractSearchExpressionVisitor<Predicate<List<String>>> {
|
||||
|
||||
public RecipeTextVisitor(SearchTokenParser searchParser) {
|
||||
super(searchParser);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Predicate<List<String>> visitChildren(RuleNode node) {
|
||||
if (!(node instanceof ParserRuleContext context) || context.children == null) return null;
|
||||
|
||||
for (ParseTree child : context.children) {
|
||||
if (child instanceof RuleNode) return visit(child);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Predicate<List<String>> visitOrExpression(SearchExpressionParser.OrExpressionContext ctx) {
|
||||
final List<Predicate<List<String>>> parts = children(ctx);
|
||||
return parts == null ? null : lines -> parts.stream().anyMatch(part -> part.test(lines));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Predicate<List<String>> visitSequenceExpression(SearchExpressionParser.SequenceExpressionContext ctx) {
|
||||
final List<Predicate<List<String>>> parts = children(ctx);
|
||||
return parts == null ? null : lines -> parts.stream().allMatch(part -> part.test(lines));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Predicate<List<String>> visitNegateExpression(SearchExpressionParser.NegateExpressionContext ctx) {
|
||||
final Predicate<List<String>> inner = visitChildren(ctx);
|
||||
return inner == null ? null : inner.negate();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Predicate<List<String>> visitPrefixedExpression(SearchExpressionParser.PrefixedExpressionContext ctx) {
|
||||
final String prefix = Pattern.quote(String.valueOf(ctx.prefix));
|
||||
final String cleanPattern = ctx.token() != null ? SearchExpressionUtils.getCleanPattern(ctx.token()) : null;
|
||||
return matcher(cleanPattern == null ? prefix : prefix + cleanPattern);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Predicate<List<String>> visitToken(SearchExpressionParser.TokenContext ctx) {
|
||||
return matcher(SearchExpressionUtils.getCleanPattern(ctx));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Predicate<List<String>> defaultResult() {
|
||||
return null;
|
||||
}
|
||||
|
||||
private static Predicate<List<String>> matcher(String cleanPattern) {
|
||||
final Pattern pattern = cleanPattern != null ? SearchField.getPattern(cleanPattern) : null;
|
||||
return pattern == null ? null : RecipeTextFilter.matching(pattern);
|
||||
}
|
||||
|
||||
private List<Predicate<List<String>>> children(ParserRuleContext ctx) {
|
||||
if (ctx.children == null) return null;
|
||||
final List<Predicate<List<String>>> parts = new ArrayList<>();
|
||||
|
||||
for (ParseTree child : ctx.children) {
|
||||
if (!(child instanceof RuleNode)) continue;
|
||||
final Predicate<List<String>> part = visit(child);
|
||||
if (part == null) return null;
|
||||
parts.add(part);
|
||||
}
|
||||
|
||||
return parts.isEmpty() ? null : parts;
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
package codechicken.nei.search;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import net.minecraft.util.EnumChatFormatting;
|
||||
@@ -17,9 +16,6 @@ import codechicken.nei.SearchTokenParser.ISearchParserProvider;
|
||||
|
||||
public class SearchExpressionFormatVisitor extends AbstractSearchExpressionVisitor<String> {
|
||||
|
||||
private static final Pattern REGEX_ESCAPED_SPACE_PATTERN = Pattern.compile("([^\\\\](?:\\\\\\\\)+)?\\\\ ");
|
||||
private static final Pattern ESCAPED_SPACE_PATTERN = Pattern.compile("\\\\ ");
|
||||
|
||||
public SearchExpressionFormatVisitor(SearchTokenParser searchParser) {
|
||||
super(searchParser);
|
||||
}
|
||||
@@ -35,7 +31,7 @@ public class SearchExpressionFormatVisitor extends AbstractSearchExpressionVisit
|
||||
|
||||
@Override
|
||||
public String visitPrefixedExpression(SearchExpressionParser.PrefixedExpressionContext ctx) {
|
||||
final EnumChatFormatting format = getFormatting(ctx.prefix, null);
|
||||
final EnumChatFormatting format = getFormatting(ctx, ctx.prefix, null);
|
||||
return format + String.valueOf(ctx.prefix) + visitToken(ctx.token());
|
||||
}
|
||||
|
||||
@@ -66,19 +62,19 @@ public class SearchExpressionFormatVisitor extends AbstractSearchExpressionVisit
|
||||
cleanText = ctx.plainText().getText();
|
||||
// Unescape spaces
|
||||
if (spaceModeEnabled == 1) {
|
||||
cleanText = ESCAPED_SPACE_PATTERN.matcher(cleanText).replaceAll(" ");
|
||||
cleanText = SearchExpressionUtils.ESCAPED_SPACE_PATTERN.matcher(cleanText).replaceAll(" ");
|
||||
}
|
||||
final EnumChatFormatting format = getFormatting(prefix, EnumChatFormatting.RESET);
|
||||
final EnumChatFormatting format = getFormatting(ctx, prefix, EnumChatFormatting.RESET);
|
||||
return format + cleanText;
|
||||
} else if (ctx.regex() != null) {
|
||||
cleanText = visitRegex(ctx.regex());
|
||||
if (spaceModeEnabled == 1) {
|
||||
cleanText = REGEX_ESCAPED_SPACE_PATTERN.matcher(cleanText).replaceAll("$1 ");
|
||||
cleanText = SearchExpressionUtils.REGEX_ESCAPED_SPACE_PATTERN.matcher(cleanText).replaceAll("$1 ");
|
||||
}
|
||||
} else if (ctx.quoted() != null) {
|
||||
cleanText = visitQuoted(ctx.quoted());
|
||||
if (spaceModeEnabled == 1) {
|
||||
cleanText = ESCAPED_SPACE_PATTERN.matcher(cleanText).replaceAll(" ");
|
||||
cleanText = SearchExpressionUtils.ESCAPED_SPACE_PATTERN.matcher(cleanText).replaceAll(" ");
|
||||
}
|
||||
}
|
||||
return cleanText;
|
||||
@@ -91,7 +87,7 @@ public class SearchExpressionFormatVisitor extends AbstractSearchExpressionVisit
|
||||
// check if highlight is defined for the token
|
||||
Optional.ofNullable(SearchExpressionUtils.getHighlight(type))
|
||||
// check if highlight is defined for the prefix
|
||||
.orElse(getFormatting(prefix, null))
|
||||
.orElse(getFormatting(child, prefix, null))
|
||||
// use default highlight otherwise
|
||||
).map(EnumChatFormatting::toString).orElse("");
|
||||
|
||||
@@ -108,9 +104,16 @@ public class SearchExpressionFormatVisitor extends AbstractSearchExpressionVisit
|
||||
return defaultResult();
|
||||
}
|
||||
|
||||
private EnumChatFormatting getFormatting(Character prefix, EnumChatFormatting defaultFormatting) {
|
||||
private static boolean isRecipeText(ParseTree node) {
|
||||
while (node != null && !(node instanceof SearchExpressionParser.SearchExpressionContext)) {
|
||||
node = node.getParent();
|
||||
}
|
||||
return node != null && ((SearchExpressionParser.SearchExpressionContext) node).type == 4;
|
||||
}
|
||||
|
||||
private EnumChatFormatting getFormatting(ParseTree node, Character prefix, EnumChatFormatting defaultFormatting) {
|
||||
if (prefix != null) {
|
||||
if (prefix == '\0') {
|
||||
if (prefix == '\0' || isRecipeText(node)) {
|
||||
return EnumChatFormatting.RESET;
|
||||
}
|
||||
final ISearchParserProvider provider = searchParser.getProvider(prefix);
|
||||
|
||||
@@ -2,6 +2,7 @@ package codechicken.nei.search;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import net.minecraft.util.EnumChatFormatting;
|
||||
|
||||
@@ -14,6 +15,11 @@ import codechicken.nei.SearchTokenParser;
|
||||
|
||||
public class SearchExpressionUtils {
|
||||
|
||||
static final Pattern REGEX_ESCAPED_SPACE_PATTERN = Pattern.compile("([^\\\\](?:\\\\\\\\)+)?\\\\ ");
|
||||
private static final Pattern ESCAPED_SYMBOL_PATTERN = Pattern.compile("\\\\(.)");
|
||||
private static final Pattern ESCAPED_QUOTE_PATTERN = Pattern.compile("\\\\\"");
|
||||
static final Pattern ESCAPED_SPACE_PATTERN = Pattern.compile("\\\\ ");
|
||||
|
||||
private static final Map<Integer, EnumChatFormatting> HIGHLIGHT_MAP = new HashMap<>();
|
||||
static {
|
||||
for (HIGHLIGHTS elem : HIGHLIGHTS.values()) {
|
||||
@@ -26,7 +32,8 @@ public class SearchExpressionUtils {
|
||||
public static enum HIGHLIGHTS {
|
||||
|
||||
RECIPE(EnumChatFormatting.GREEN, SearchExpressionParser.RECIPE_INGREDIENTS,
|
||||
SearchExpressionParser.RECIPE_RESULT, SearchExpressionParser.RECIPE_OTHERS),
|
||||
SearchExpressionParser.RECIPE_RESULT, SearchExpressionParser.RECIPE_OTHERS,
|
||||
SearchExpressionParser.RECIPE_TEXT),
|
||||
OR(EnumChatFormatting.GRAY, SearchExpressionParser.OR),
|
||||
BRACKETS(EnumChatFormatting.GRAY, SearchExpressionParser.LEFT_BRACKET, SearchExpressionParser.RIGHT_BRACKET),
|
||||
NEGATE(EnumChatFormatting.BLUE, SearchExpressionParser.DASH),
|
||||
@@ -67,6 +74,38 @@ public class SearchExpressionUtils {
|
||||
return visitor.visitRecipeSearchExpression(parser.recipeSearchExpression());
|
||||
}
|
||||
|
||||
public static String getCleanPattern(SearchExpressionParser.TokenContext ctx) {
|
||||
String cleanPattern = null;
|
||||
if (ctx.plainText() != null) {
|
||||
cleanPattern = Pattern.quote(
|
||||
ESCAPED_SYMBOL_PATTERN.matcher(ctx.plainText().getText())
|
||||
// Unescape everything in search expression
|
||||
.replaceAll("$1"));
|
||||
} else if (ctx.regex() != null) {
|
||||
if (ctx.regex().REGEX_CONTENT() != null) {
|
||||
cleanPattern = ctx.regex().REGEX_CONTENT().getSymbol().getText();
|
||||
final int spaceModeEnabled = NEIClientConfig.getIntSetting("inventory.search.spaceMode");
|
||||
// Unescape spaces
|
||||
if (spaceModeEnabled == 1) {
|
||||
cleanPattern = REGEX_ESCAPED_SPACE_PATTERN.matcher(cleanPattern).replaceAll("$1 ");
|
||||
}
|
||||
}
|
||||
} else if (ctx.quoted() != null) {
|
||||
if (ctx.quoted().QUOTED_CONTENT() != null) {
|
||||
cleanPattern = ctx.quoted().QUOTED_CONTENT().getSymbol().getText();
|
||||
// Unescape quotes
|
||||
cleanPattern = ESCAPED_QUOTE_PATTERN.matcher(cleanPattern).replaceAll("\"");
|
||||
cleanPattern = Pattern.quote(cleanPattern);
|
||||
final int spaceModeEnabled = NEIClientConfig.getIntSetting("inventory.search.spaceMode");
|
||||
// Unescape spaces
|
||||
if (spaceModeEnabled == 1) {
|
||||
cleanPattern = ESCAPED_SPACE_PATTERN.matcher(cleanPattern).replaceAll(" ");
|
||||
}
|
||||
}
|
||||
}
|
||||
return cleanPattern;
|
||||
}
|
||||
|
||||
private static <T> SearchExpressionParser createSearchExpressionParser(String text, SearchTokenParser searchParser,
|
||||
SearchExpressionParserBaseVisitor<T> visitor) {
|
||||
final boolean doLogExceptions = NEIClientConfig.getBooleanSetting("inventory.search.logSearchExceptions");
|
||||
|
||||
@@ -646,7 +646,8 @@ nei.recipe.search.tooltip.name=Filter Syntax
|
||||
nei.recipe.search.tooltip.shift=§7Hold §6Shift§7 for more info
|
||||
nei.recipe.search.tooltip.input=%s§ftext§8 - §7search in inputs
|
||||
nei.recipe.search.tooltip.output=%s§ftext§8 - §7search in outputs
|
||||
nei.recipe.search.tooltip.auxiliary=%s§ftext§8 - §7search in other stacks (fuel, catalysts)
|
||||
nei.recipe.search.tooltip.text=%s§ftext§8 - §7search in recipe text
|
||||
nei.recipe.search.tooltip.auxiliary=%s§ftext§8 - §7search in other stacks
|
||||
nei.recipe.search.tooltip.all=%s§ftext§8 - §7all inputs must match (same with > and ^)
|
||||
nei.recipe.search.tooltip.prefix=%stext§8 - §7%s
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ import static org.junit.jupiter.api.Named.named;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
@@ -35,6 +36,7 @@ import codechicken.nei.SearchField.SearchParserProvider;
|
||||
import codechicken.nei.api.ItemFilter;
|
||||
import codechicken.nei.api.ItemInfo;
|
||||
import codechicken.nei.recipe.IRecipeHandler;
|
||||
import codechicken.nei.recipe.RecipeRole;
|
||||
import codechicken.nei.search.IdentifierFilter;
|
||||
|
||||
@DisplayName("Search expression")
|
||||
@@ -152,6 +154,10 @@ public class SearchTokenParserTest {
|
||||
spaceModes,
|
||||
nonRegexPatternModes);
|
||||
|
||||
private static final List<Arguments> regexAllSpaceModesTestProvider = combinationsProvider(
|
||||
spaceModes,
|
||||
new ModeValue[] { patternModes[2] });
|
||||
|
||||
private static final List<Arguments> spaceIsAndExtendedPlusModeTestProvider = combinationsProvider(
|
||||
spaceIsAndMode,
|
||||
extendedPlusPatternMode);
|
||||
@@ -379,9 +385,9 @@ public class SearchTokenParserTest {
|
||||
SearchTokenParser searchParser = new SearchTokenParser();
|
||||
searchParser.addProvider(defaultParserProvider);
|
||||
IRecipeHandler handler = mock(IRecipeHandler.class);
|
||||
when(handler.getIngredientStacks(0)).thenReturn(positionedStacks(itemA));
|
||||
when(handler.getResultStacks(0)).thenReturn(positionedStacks(itemB));
|
||||
when(handler.getAuxiliaryStacks(0)).thenReturn(positionedStacks(itemC));
|
||||
when(handler.getSearchStacks(0, RecipeRole.INPUT)).thenReturn(positionedStacks(itemA));
|
||||
when(handler.getSearchStacks(0, RecipeRole.OUTPUT)).thenReturn(positionedStacks(itemB));
|
||||
when(handler.getSearchStacks(0, RecipeRole.AUXILIARY)).thenReturn(positionedStacks(itemC));
|
||||
|
||||
assertTrue(testRecipeFilter(spaceMode, patternMode, searchParser, "<" + itemA.getDisplayName(), handler));
|
||||
assertTrue(testRecipeFilter(spaceMode, patternMode, searchParser, ">" + itemB.getDisplayName(), handler));
|
||||
@@ -390,6 +396,84 @@ public class SearchTokenParserTest {
|
||||
assertFalse(testRecipeFilter(spaceMode, patternMode, searchParser, ">" + itemC.getDisplayName(), handler));
|
||||
}
|
||||
|
||||
@DisplayName("Recipe text")
|
||||
@ParameterizedTest(name = "{argumentsWithNames}")
|
||||
@FieldSource("nonRegexAllSpaceModesTestProvider")
|
||||
public void testRecipeText(int spaceMode, int patternMode) {
|
||||
SearchTokenParser searchParser = new SearchTokenParser();
|
||||
searchParser.addProvider(defaultParserProvider);
|
||||
IRecipeHandler handler = recipeTextHandler(Arrays.asList("Foo", "xyz"));
|
||||
|
||||
assertTrue(testRecipeFilter(spaceMode, patternMode, searchParser, "~foo", handler));
|
||||
assertTrue(testRecipeFilter(spaceMode, patternMode, searchParser, "~xyz", handler));
|
||||
assertFalse(testRecipeFilter(spaceMode, patternMode, searchParser, "~zzz", handler));
|
||||
// the text search does not look at the stacks
|
||||
assertFalse(testRecipeFilter(spaceMode, patternMode, searchParser, "~a", handler));
|
||||
assertTrue(testRecipeFilter(spaceMode, patternMode, searchParser, "a", handler));
|
||||
// the plain search does not look at the text
|
||||
assertFalse(testRecipeFilter(spaceMode, patternMode, searchParser, "foo", handler));
|
||||
assertFalse(testRecipeFilter(spaceMode, patternMode, searchParser, "~-foo", handler));
|
||||
assertTrue(testRecipeFilter(spaceMode, patternMode, searchParser, "~-zzz", handler));
|
||||
assertTrue(testRecipeFilter(spaceMode, patternMode, searchParser, "<a~foo", handler));
|
||||
assertFalse(testRecipeFilter(spaceMode, patternMode, searchParser, "<a~zzz", handler));
|
||||
assertFalse(testRecipeFilter(spaceMode, patternMode, searchParser, "<b~foo", handler));
|
||||
assertTrue(testRecipeFilter(spaceMode, patternMode, searchParser, ">b~xyz", handler));
|
||||
}
|
||||
|
||||
@DisplayName("Recipe text of a recipe without text")
|
||||
@ParameterizedTest(name = "{argumentsWithNames}")
|
||||
@FieldSource("nonRegexAllSpaceModesTestProvider")
|
||||
public void testRecipeTextWithoutText(int spaceMode, int patternMode) {
|
||||
SearchTokenParser searchParser = new SearchTokenParser();
|
||||
searchParser.addProvider(defaultParserProvider);
|
||||
IRecipeHandler handler = recipeTextHandler(Collections.emptyList());
|
||||
|
||||
assertFalse(testRecipeFilter(spaceMode, patternMode, searchParser, "~a", handler));
|
||||
assertFalse(testRecipeFilter(spaceMode, patternMode, searchParser, "~-a", handler));
|
||||
}
|
||||
|
||||
@DisplayName("Recipe text with wildcards and regex in Extended mode")
|
||||
@ParameterizedTest(name = "{argumentsWithNames}")
|
||||
@FieldSource("extendedOnlyAllSpaceModesTestProvider")
|
||||
public void testRecipeTextExtended(int spaceMode, int patternMode) {
|
||||
SearchTokenParser searchParser = new SearchTokenParser();
|
||||
searchParser.addProvider(defaultParserProvider);
|
||||
IRecipeHandler handler = recipeTextHandler(Arrays.asList("Foo", "xyz"));
|
||||
|
||||
assertTrue(testRecipeFilter(spaceMode, patternMode, searchParser, "~f?o", handler));
|
||||
assertTrue(testRecipeFilter(spaceMode, patternMode, searchParser, "~x*z", handler));
|
||||
assertFalse(testRecipeFilter(spaceMode, patternMode, searchParser, "~x?z?", handler));
|
||||
assertTrue(testRecipeFilter(spaceMode, patternMode, searchParser, "~r/^fo+$/", handler));
|
||||
assertFalse(testRecipeFilter(spaceMode, patternMode, searchParser, "~r/^o+$/", handler));
|
||||
}
|
||||
|
||||
@DisplayName("Recipe text in Regex mode")
|
||||
@ParameterizedTest(name = "{argumentsWithNames}")
|
||||
@FieldSource("regexAllSpaceModesTestProvider")
|
||||
public void testRecipeTextRegex(int spaceMode, int patternMode) {
|
||||
SearchTokenParser searchParser = new SearchTokenParser();
|
||||
searchParser.addProvider(defaultParserProvider);
|
||||
IRecipeHandler handler = recipeTextHandler(Arrays.asList("Foo", "xyz"));
|
||||
|
||||
assertTrue(testRecipeFilter(spaceMode, patternMode, searchParser, "~f.o", handler));
|
||||
assertTrue(testRecipeFilter(spaceMode, patternMode, searchParser, "~x[y]z", handler));
|
||||
assertFalse(testRecipeFilter(spaceMode, patternMode, searchParser, "~x[^y]z", handler));
|
||||
}
|
||||
|
||||
@DisplayName("Recipe text with brackets in Extended+ mode")
|
||||
@ParameterizedTest(name = "{argumentsWithNames}")
|
||||
@FieldSource("extendedPlusAllSpaceModesTestProvider")
|
||||
public void testRecipeTextBrackets(int spaceMode, int patternMode) {
|
||||
SearchTokenParser searchParser = new SearchTokenParser();
|
||||
searchParser.addProvider(defaultParserProvider);
|
||||
IRecipeHandler handler = recipeTextHandler(Arrays.asList("Foo", "xyz"));
|
||||
|
||||
assertTrue(testRecipeFilter(spaceMode, patternMode, searchParser, "~{foo|zzz}", handler));
|
||||
assertFalse(testRecipeFilter(spaceMode, patternMode, searchParser, "~{zzz|qqq}", handler));
|
||||
assertTrue(testRecipeFilter(spaceMode, patternMode, searchParser, "~-{zzz|qqq}", handler));
|
||||
assertFalse(testRecipeFilter(spaceMode, patternMode, searchParser, "~-{foo|qqq}", handler));
|
||||
}
|
||||
|
||||
// Extended+ specific tests
|
||||
|
||||
@DisplayName("Sequence")
|
||||
@@ -488,6 +572,15 @@ public class SearchTokenParserTest {
|
||||
return matchedItems;
|
||||
}
|
||||
|
||||
private static IRecipeHandler recipeTextHandler(List<String> text) {
|
||||
IRecipeHandler handler = mock(IRecipeHandler.class);
|
||||
when(handler.getSearchStacks(0, RecipeRole.INPUT)).thenReturn(positionedStacks(itemA));
|
||||
when(handler.getSearchStacks(0, RecipeRole.OUTPUT)).thenReturn(positionedStacks(itemB));
|
||||
when(handler.getSearchStacks(0, RecipeRole.AUXILIARY)).thenReturn(Collections.emptyList());
|
||||
when(handler.getSearchText(0)).thenReturn(text);
|
||||
return handler;
|
||||
}
|
||||
|
||||
private static boolean testRecipeFilter(int spaceMode, int patternMode, SearchTokenParser searchParser,
|
||||
String search, IRecipeHandler handler) {
|
||||
setModes(spaceMode, patternMode);
|
||||
|
||||
Reference in new issue
Block a user