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
This commit is contained in:
dordsor21 2021-11-18 18:42:19 +00:00
parent 5604000ae2
commit ef61ecccaa
No known key found for this signature in database
GPG Key ID: 1E53E88969FFCF0B
2 changed files with 21 additions and 8 deletions

View File

@ -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()) {

View File

@ -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