mirror of
https://github.com/GTNewHorizons/NotEnoughItems.git
synced 2026-11-18 19:39:06 +00:00
Add recipe text search with ~ prefix (#1120)
This commit is contained in:
1 parent
8e6b31de18
commit
1c33a5b7d6
16 files changed
+363
-71
No files matched your search
@@ -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
|
||||
}
|
||||
@@ -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");
|
||||
|
||||
Reference in new issue
Block a user