Fix tabcomplete for random pattern / multiple pattern (#1675)

* Merge RandomPatternParser#getSuggestions with upstream

* fix: don't throw AIIBE if patterns are empty

shouldn't be the case in the first place
Dieser Commit ist enthalten in:
Pierre Maurice Schwang 2022-03-27 09:46:36 +02:00 committet von GitHub
Ursprung 465c81d193
Commit d1588f9207
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: 4AEE18F83AFDEB23

Datei anzeigen

@ -30,34 +30,23 @@ public class RandomPatternParser extends InputParser<Pattern> {
public Stream<String> getSuggestions(String input) {
//FAWE start
List<String> patterns = StringUtil.split(input, ',', '[', ']');
if (patterns.size() == 0) {
if (patterns.isEmpty()) {
return Stream.empty();
}
// get suggestions for the last token only
String token = patterns.get(patterns.size() - 1);
String percent;
String previous;
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("%");
if (p.length < 2) {
return Stream.empty();
} else {
percent += p[0] + "%";
token = p[1];
}
} else {
return Stream.empty();
}
final List<String> innerSuggestions = worldEdit.getPatternFactory().getSuggestions(token);
String prefix = previous + percent;
//FAWE end
// get suggestions for the last token only
String percent = null;
String token = patterns.get(patterns.size() - 1);
if (regex.matcher(token).matches()) {
String[] p = token.split("%", 2);
percent = p[0];
token = p[1];
} else if (patterns.size() == 1) {
return Stream.empty(); // handled by DefaultBlockParser
}
String previous = patterns.size() == 1 ? "" : String.join(",", patterns.subList(0, patterns.size() - 1)) + ",";
String prefix = previous + (percent == null ? "" : percent + "%");
final List<String> innerSuggestions = worldEdit.getPatternFactory().getSuggestions(token);
return innerSuggestions.stream().map(s -> prefix + s);
}