mirror of
https://github.com/plexusorg/Plex-FAWE.git
synced 2025-07-05 04:26:42 +00:00
Implement suggestions for Bukkit + Sponge
This commit is contained in:
committed by
Kenzie Togami
parent
1c54a04fd1
commit
8a3e6a12b9
@ -19,86 +19,94 @@
|
||||
|
||||
package com.sk89q.worldedit.internal.command;
|
||||
|
||||
import com.google.common.base.Splitter;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.collect.Iterables;
|
||||
import com.sk89q.worldedit.internal.util.Substring;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class CommandArgParser {
|
||||
|
||||
public static ImmutableList<Substring> spaceSplit(String string) {
|
||||
ImmutableList.Builder<Substring> result = ImmutableList.builder();
|
||||
int index = 0;
|
||||
for (String part : Splitter.on(' ').split(string)) {
|
||||
result.add(Substring.from(string, index, index + part.length()));
|
||||
index += part.length() + 1;
|
||||
}
|
||||
return result.build();
|
||||
}
|
||||
|
||||
private enum State {
|
||||
NORMAL,
|
||||
QUOTE
|
||||
}
|
||||
|
||||
private final Stream.Builder<String> args = Stream.builder();
|
||||
private final StringBuilder currentArg = new StringBuilder();
|
||||
private final String input;
|
||||
private final Stream.Builder<Substring> args = Stream.builder();
|
||||
private final List<Substring> input;
|
||||
private final List<Substring> currentArg = new ArrayList<>();
|
||||
private int index = 0;
|
||||
private State state = State.NORMAL;
|
||||
|
||||
public CommandArgParser(String input) {
|
||||
public CommandArgParser(List<Substring> input) {
|
||||
this.input = input;
|
||||
}
|
||||
|
||||
public Stream<String> parseArgs() {
|
||||
for (; index < input.length(); index++) {
|
||||
char c = input.charAt(index);
|
||||
public Stream<Substring> parseArgs() {
|
||||
for (; index < input.size(); index++) {
|
||||
Substring nextPart = input.get(index);
|
||||
switch (state) {
|
||||
case NORMAL:
|
||||
handleNormal(c);
|
||||
handleNormal(nextPart);
|
||||
break;
|
||||
case QUOTE:
|
||||
handleQuote(c);
|
||||
handleQuote(nextPart);
|
||||
}
|
||||
}
|
||||
finishArg(true);
|
||||
return args.build();
|
||||
}
|
||||
|
||||
private void handleNormal(char c) {
|
||||
switch (c) {
|
||||
case '"':
|
||||
state = State.QUOTE;
|
||||
break;
|
||||
case ' ':
|
||||
finishArg(true);
|
||||
break;
|
||||
case '\\':
|
||||
if (index + 1 < input.length()) {
|
||||
index++;
|
||||
}
|
||||
appendChar(input.charAt(index));
|
||||
break;
|
||||
default:
|
||||
appendChar(c);
|
||||
private void handleNormal(Substring part) {
|
||||
if (part.getSubstring().startsWith("\"")) {
|
||||
state = State.QUOTE;
|
||||
currentArg.add(Substring.wrap(
|
||||
part.getSubstring().substring(1),
|
||||
part.getStart(), part.getEnd()
|
||||
));
|
||||
} else {
|
||||
currentArg.add(part);
|
||||
finishArg();
|
||||
}
|
||||
}
|
||||
|
||||
private void handleQuote(char c) {
|
||||
switch (c) {
|
||||
case '"':
|
||||
state = State.NORMAL;
|
||||
finishArg(false);
|
||||
break;
|
||||
case '\\':
|
||||
if (index + 1 < input.length()) {
|
||||
index++;
|
||||
}
|
||||
appendChar(input.charAt(index));
|
||||
break;
|
||||
default:
|
||||
appendChar(c);
|
||||
private void handleQuote(Substring part) {
|
||||
if (part.getSubstring().endsWith("\"")) {
|
||||
state = State.NORMAL;
|
||||
currentArg.add(Substring.wrap(
|
||||
part.getSubstring().substring(0, part.getSubstring().length() - 1),
|
||||
part.getStart(), part.getEnd()
|
||||
));
|
||||
finishArg();
|
||||
} else {
|
||||
currentArg.add(part);
|
||||
}
|
||||
}
|
||||
|
||||
private void finishArg(boolean requireText) {
|
||||
if (currentArg.length() == 0 && requireText) {
|
||||
return;
|
||||
}
|
||||
args.add(currentArg.toString());
|
||||
currentArg.setLength(0);
|
||||
}
|
||||
|
||||
private void appendChar(char c) {
|
||||
currentArg.append(c);
|
||||
private void finishArg() {
|
||||
// Merge the arguments into a single, space-joined, string
|
||||
// Keep the original start + end points.
|
||||
int start = currentArg.get(0).getStart();
|
||||
int end = Iterables.getLast(currentArg).getEnd();
|
||||
args.add(Substring.wrap(currentArg.stream()
|
||||
.map(Substring::getSubstring)
|
||||
.collect(Collectors.joining(" ")),
|
||||
start, end
|
||||
));
|
||||
currentArg.clear();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -19,15 +19,22 @@
|
||||
|
||||
package com.sk89q.worldedit.internal.command;
|
||||
|
||||
import com.google.common.collect.Iterables;
|
||||
import com.sk89q.worldedit.extension.platform.PlatformCommandManager;
|
||||
import com.sk89q.worldedit.internal.util.Substring;
|
||||
import org.enginehub.piston.Command;
|
||||
import org.enginehub.piston.part.SubCommandPart;
|
||||
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkState;
|
||||
import static java.util.stream.Collectors.toList;
|
||||
|
||||
public class CommandUtil {
|
||||
|
||||
public static Map<String, Command> getSubCommands(Command currentCommand) {
|
||||
@ -48,6 +55,43 @@ public class CommandUtil {
|
||||
return BY_CLEAN_NAME;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fix {@code suggestions} to replace the last space-separated word in {@code arguments}.
|
||||
*/
|
||||
public static List<String> fixSuggestions(String arguments, List<Substring> suggestions) {
|
||||
Substring lastArg = Iterables.getLast(CommandArgParser.spaceSplit(arguments));
|
||||
return suggestions.stream()
|
||||
.map(suggestion -> CommandUtil.suggestLast(lastArg, suggestion))
|
||||
.filter(Optional::isPresent)
|
||||
.map(Optional::get)
|
||||
.collect(toList());
|
||||
}
|
||||
|
||||
/**
|
||||
* Given the last word of a command, mutate the suggestion to replace the last word, if
|
||||
* possible.
|
||||
*/
|
||||
private static Optional<String> suggestLast(Substring last, Substring suggestion) {
|
||||
if (suggestion.getStart() == last.getEnd()) {
|
||||
// this suggestion is for the next argument.
|
||||
if (last.getSubstring().isEmpty()) {
|
||||
return Optional.of(suggestion.getSubstring());
|
||||
}
|
||||
return Optional.of(last.getSubstring() + " " + suggestion.getSubstring());
|
||||
}
|
||||
StringBuilder builder = new StringBuilder(last.getSubstring());
|
||||
int start = suggestion.getStart() - last.getStart();
|
||||
int end = suggestion.getEnd() - last.getStart();
|
||||
if (start < 0) {
|
||||
// Quoted suggestion, can't complete it here.
|
||||
return Optional.empty();
|
||||
}
|
||||
checkState(end <= builder.length(),
|
||||
"Suggestion ends too late, last=%s, suggestion=", last, suggestion);
|
||||
builder.replace(start, end, suggestion.getSubstring());
|
||||
return Optional.of(builder.toString());
|
||||
}
|
||||
|
||||
private CommandUtil() {
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,97 @@
|
||||
/*
|
||||
* WorldEdit, a Minecraft world manipulation toolkit
|
||||
* Copyright (C) sk89q <http://www.sk89q.com>
|
||||
* Copyright (C) WorldEdit team and contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser General Public License as published by the
|
||||
* Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.worldedit.internal.util;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* An explicit substring. Provides the range from which it was taken.
|
||||
*/
|
||||
public final class Substring {
|
||||
|
||||
/**
|
||||
* Take a substring from {@code original}, and {@link #wrap(String, int, int)} it into
|
||||
* a Substring.
|
||||
*/
|
||||
public static Substring from(String original, int start) {
|
||||
return wrap(original.substring(start), start, original.length());
|
||||
}
|
||||
|
||||
/**
|
||||
* Take a substring from {@code original}, and {@link #wrap(String, int, int)} it into
|
||||
* a Substring.
|
||||
*/
|
||||
public static Substring from(String original, int start, int end) {
|
||||
return wrap(original.substring(start, end), start, end);
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrap the given parameters into a Substring instance.
|
||||
*/
|
||||
public static Substring wrap(String substring, int start, int end) {
|
||||
return new Substring(substring, start, end);
|
||||
}
|
||||
|
||||
private final String substring;
|
||||
private final int start;
|
||||
private final int end;
|
||||
|
||||
private Substring(String substring, int start, int end) {
|
||||
this.substring = substring;
|
||||
this.start = start;
|
||||
this.end = end;
|
||||
}
|
||||
|
||||
public String getSubstring() {
|
||||
return substring;
|
||||
}
|
||||
|
||||
public int getStart() {
|
||||
return start;
|
||||
}
|
||||
|
||||
public int getEnd() {
|
||||
return end;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
Substring substring1 = (Substring) o;
|
||||
return start == substring1.start &&
|
||||
end == substring1.end &&
|
||||
substring.equals(substring1.substring);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(substring, start, end);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Substring{" +
|
||||
"substring='" + substring + "'" +
|
||||
",start=" + start +
|
||||
",end=" + end +
|
||||
"}";
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user