Potenially fix quoted string completion

This commit is contained in:
Kenzie Togami 2019-07-27 18:12:18 -07:00
parent d134820bcb
commit 3a5170a0e8
No known key found for this signature in database
GPG Key ID: 5D200B325E157A81
4 changed files with 27 additions and 13 deletions

View File

@ -431,7 +431,7 @@ public final class PlatformCommandManager {
} }
private Stream<Substring> parseArgs(String input) { private Stream<Substring> parseArgs(String input) {
return new CommandArgParser(CommandArgParser.spaceSplit(input.substring(1))).parseArgs(); return CommandArgParser.forArgString(input.substring(1)).parseArgs();
} }
@Subscribe @Subscribe

View File

@ -31,6 +31,10 @@ import java.util.stream.Stream;
public class CommandArgParser { public class CommandArgParser {
public static CommandArgParser forArgString(String argString) {
return new CommandArgParser(spaceSplit(argString));
}
public static ImmutableList<Substring> spaceSplit(String string) { public static ImmutableList<Substring> spaceSplit(String string) {
ImmutableList.Builder<Substring> result = ImmutableList.builder(); ImmutableList.Builder<Substring> result = ImmutableList.builder();
int index = 0; int index = 0;
@ -79,14 +83,14 @@ public class CommandArgParser {
if (strPart.endsWith("\"") && strPart.length() > 1) { if (strPart.endsWith("\"") && strPart.length() > 1) {
currentArg.add(Substring.wrap( currentArg.add(Substring.wrap(
strPart.substring(1, strPart.length() - 1), strPart.substring(1, strPart.length() - 1),
part.getStart(), part.getEnd() part.getStart() + 1, part.getEnd() - 1
)); ));
finishArg(); finishArg();
} else { } else {
state = State.QUOTE; state = State.QUOTE;
currentArg.add(Substring.wrap( currentArg.add(Substring.wrap(
strPart.substring(1), strPart.substring(1),
part.getStart(), part.getEnd() part.getStart() + 1, part.getEnd()
)); ));
} }
} else { } else {
@ -100,7 +104,7 @@ public class CommandArgParser {
state = State.NORMAL; state = State.NORMAL;
currentArg.add(Substring.wrap( currentArg.add(Substring.wrap(
part.getSubstring().substring(0, part.getSubstring().length() - 1), part.getSubstring().substring(0, part.getSubstring().length() - 1),
part.getStart(), part.getEnd() part.getStart(), part.getEnd() - 1
)); ));
finishArg(); finishArg();
} else { } else {

View File

@ -21,6 +21,7 @@ package com.sk89q.worldedit.internal.command;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables; import com.google.common.collect.Iterables;
import com.google.common.collect.Iterators;
import com.sk89q.worldedit.extension.platform.PlatformCommandManager; import com.sk89q.worldedit.extension.platform.PlatformCommandManager;
import com.sk89q.worldedit.internal.util.Substring; import com.sk89q.worldedit.internal.util.Substring;
import com.sk89q.worldedit.util.formatting.text.Component; import com.sk89q.worldedit.util.formatting.text.Component;
@ -65,7 +66,9 @@ public class CommandUtil {
* Fix {@code suggestions} to replace the last space-separated word in {@code arguments}. * Fix {@code suggestions} to replace the last space-separated word in {@code arguments}.
*/ */
public static List<String> fixSuggestions(String arguments, List<Substring> suggestions) { public static List<String> fixSuggestions(String arguments, List<Substring> suggestions) {
Substring lastArg = Iterables.getLast(CommandArgParser.spaceSplit(arguments)); Substring lastArg = Iterables.getLast(
CommandArgParser.spaceSplit(arguments)
);
return suggestions.stream() return suggestions.stream()
.map(suggestion -> CommandUtil.suggestLast(lastArg, suggestion)) .map(suggestion -> CommandUtil.suggestLast(lastArg, suggestion))
.filter(Optional::isPresent) .filter(Optional::isPresent)

View File

@ -23,29 +23,36 @@ import com.google.common.collect.ImmutableList;
import com.sk89q.worldedit.internal.util.Substring; import com.sk89q.worldedit.internal.util.Substring;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import static com.sk89q.worldedit.internal.command.CommandArgParser.spaceSplit; import java.util.List;
import java.util.stream.Collectors;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
public class CommandArgParserTest { class CommandArgParserTest {
private static List<Substring> argParse(String s) {
return CommandArgParser.forArgString(s).parseArgs().collect(Collectors.toList());
}
@Test @Test
public void testSpaceSplit() { void testArgumentParsing() {
assertEquals(ImmutableList.of( assertEquals(ImmutableList.of(
Substring.wrap("", 0, 0) Substring.wrap("", 0, 0)
), spaceSplit("")); ), argParse(""));
assertEquals(ImmutableList.of( assertEquals(ImmutableList.of(
Substring.wrap("ab", 0, 2) Substring.wrap("ab", 0, 2)
), spaceSplit("ab")); ), argParse("ab"));
assertEquals(ImmutableList.of( assertEquals(ImmutableList.of(
Substring.wrap("", 0, 0), Substring.wrap("", 0, 0),
Substring.wrap("", 1, 1) Substring.wrap("", 1, 1)
), spaceSplit(" ")); ), argParse(" "));
assertEquals(ImmutableList.of( assertEquals(ImmutableList.of(
Substring.wrap("a", 0, 1), Substring.wrap("a", 0, 1),
Substring.wrap("", 2, 2) Substring.wrap("", 2, 2)
), spaceSplit("a ")); ), argParse("a "));
assertEquals(ImmutableList.of( assertEquals(ImmutableList.of(
Substring.wrap("a", 0, 1), Substring.wrap("a", 0, 1),
Substring.wrap("b", 2, 3) Substring.wrap("b", 2, 3)
), spaceSplit("a b")); ), argParse("a b"));
} }
} }