Fix tab completion when *not* using %.

- Also allow tab-completion of % pattern when it's the first/only pattern being used
 - Also don't error when attempting to tab complete an invalid percent value
Dieser Commit ist enthalten in:
dordsor21 2021-11-18 18:42:19 +00:00
Ursprung 5604000ae2
Commit ef61ecccaa
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: 1E53E88969FFCF0B
2 geänderte Dateien mit 21 neuen und 8 gelöschten Zeilen

Datei anzeigen

@ -32,6 +32,8 @@ import java.util.stream.Stream;
public class RichPatternParser extends FaweParser<Pattern> { public class RichPatternParser extends FaweParser<Pattern> {
private static final java.util.regex.Pattern percentPatternRegex = java.util.regex.Pattern.compile("[0-9]+(\\.[0-9]*)?%.*");
/** /**
* Create a new rich pattern-parser. * Create a new rich pattern-parser.
* *
@ -110,7 +112,7 @@ public class RichPatternParser extends FaweParser<Pattern> {
pattern = parseFromInput(command.substring(1, end == -1 ? command.length() : end), context); pattern = parseFromInput(command.substring(1, end == -1 ? command.length() : end), context);
} else { } else {
int percentIndex = command.indexOf('%'); int percentIndex = command.indexOf('%');
if (percentIndex != -1) { // Legacy percent pattern if (percentIndex != -1 && percentPatternRegex.matcher(command).matches()) { // Legacy percent pattern
chance = Expression.compile(command.substring(0, percentIndex)).evaluate(); chance = Expression.compile(command.substring(0, percentIndex)).evaluate();
String value = command.substring(percentIndex + 1); String value = command.substring(percentIndex + 1);
if (!entry.getValue().isEmpty()) { if (!entry.getValue().isEmpty()) {

Datei anzeigen

@ -15,6 +15,8 @@ import java.util.stream.Stream;
public class RandomPatternParser extends InputParser<Pattern> { public class RandomPatternParser extends InputParser<Pattern> {
private static final java.util.regex.Pattern regex = java.util.regex.Pattern.compile("[0-9]+(\\.[0-9]*)?%.*");
/** /**
* Create a new input parser. * Create a new input parser.
* *
@ -28,26 +30,35 @@ public class RandomPatternParser extends InputParser<Pattern> {
public Stream<String> getSuggestions(String input) { public Stream<String> getSuggestions(String input) {
//FAWE start //FAWE start
List<String> patterns = StringUtil.split(input, ',', '[', ']'); List<String> patterns = StringUtil.split(input, ',', '[', ']');
if (patterns.size() == 1) { if (patterns.size() == 0) {
return Stream.empty(); return Stream.empty();
} }
// get suggestions for the last token only // get suggestions for the last token only
String token = patterns.get(patterns.size() - 1); String token = patterns.get(patterns.size() - 1);
String randString = ""; String percent;
String previous = String.join(",", patterns.subList(0, patterns.size() - 1)); String previous;
if (token.matches("[0-9]+(\\.[0-9]*)?%.*")) { if (patterns.size() != 1) {
previous = String.join(",", patterns.subList(0, patterns.size() - 1));
percent = ",";
} else {
previous = "";
percent = "";
}
if (regex.matcher(token).matches()) {
String[] p = token.split("%"); String[] p = token.split("%");
if (p.length < 2) { if (p.length < 2) {
return Stream.empty(); return Stream.empty();
} else { } else {
randString = p[0]; percent += p[0] + "%";
token = p[1]; token = p[1];
} }
} else {
return Stream.empty();
} }
final List<String> innerSuggestions = worldEdit.getPatternFactory().getSuggestions(token); final List<String> innerSuggestions = worldEdit.getPatternFactory().getSuggestions(token);
String prev = previous + "," + randString + "%"; String prefix = previous + percent;
return innerSuggestions.stream().map(s -> prev + s);
//FAWE end //FAWE end
return innerSuggestions.stream().map(s -> prefix + s);
} }
@Override @Override