Skip to content
Permalink
Browse files

Potenially fix quoted string completion

  • Loading branch information...
kenzierocks committed Jul 28, 2019
1 parent d134820 commit 3a5170a0e80736bf7d966b3c142509195dabe3b4
@@ -431,7 +431,7 @@ void removeCommands() {
}

private Stream<Substring> parseArgs(String input) {
return new CommandArgParser(CommandArgParser.spaceSplit(input.substring(1))).parseArgs();
return CommandArgParser.forArgString(input.substring(1)).parseArgs();
}

@Subscribe
@@ -31,6 +31,10 @@

public class CommandArgParser {

public static CommandArgParser forArgString(String argString) {
return new CommandArgParser(spaceSplit(argString));
}

public static ImmutableList<Substring> spaceSplit(String string) {
ImmutableList.Builder<Substring> result = ImmutableList.builder();
int index = 0;
@@ -79,14 +83,14 @@ private void handleNormal(Substring part) {
if (strPart.endsWith("\"") && strPart.length() > 1) {
currentArg.add(Substring.wrap(
strPart.substring(1, strPart.length() - 1),
part.getStart(), part.getEnd()
part.getStart() + 1, part.getEnd() - 1
));
finishArg();
} else {
state = State.QUOTE;
currentArg.add(Substring.wrap(
strPart.substring(1),
part.getStart(), part.getEnd()
part.getStart() + 1, part.getEnd()
));
}
} else {
@@ -100,7 +104,7 @@ private void handleQuote(Substring part) {
state = State.NORMAL;
currentArg.add(Substring.wrap(
part.getSubstring().substring(0, part.getSubstring().length() - 1),
part.getStart(), part.getEnd()
part.getStart(), part.getEnd() - 1
));
finishArg();
} else {
@@ -21,6 +21,7 @@

import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import com.google.common.collect.Iterators;
import com.sk89q.worldedit.extension.platform.PlatformCommandManager;
import com.sk89q.worldedit.internal.util.Substring;
import com.sk89q.worldedit.util.formatting.text.Component;
@@ -65,7 +66,9 @@ private static String clean(String input) {
* 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));
Substring lastArg = Iterables.getLast(
CommandArgParser.spaceSplit(arguments)
);
return suggestions.stream()
.map(suggestion -> CommandUtil.suggestLast(lastArg, suggestion))
.filter(Optional::isPresent)
@@ -23,29 +23,36 @@
import com.sk89q.worldedit.internal.util.Substring;
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;

public class CommandArgParserTest {
class CommandArgParserTest {

private static List<Substring> argParse(String s) {
return CommandArgParser.forArgString(s).parseArgs().collect(Collectors.toList());
}

@Test
public void testSpaceSplit() {
void testArgumentParsing() {
assertEquals(ImmutableList.of(
Substring.wrap("", 0, 0)
), spaceSplit(""));
), argParse(""));
assertEquals(ImmutableList.of(
Substring.wrap("ab", 0, 2)
), spaceSplit("ab"));
), argParse("ab"));
assertEquals(ImmutableList.of(
Substring.wrap("", 0, 0),
Substring.wrap("", 1, 1)
), spaceSplit(" "));
), argParse(" "));
assertEquals(ImmutableList.of(
Substring.wrap("a", 0, 1),
Substring.wrap("", 2, 2)
), spaceSplit("a "));
), argParse("a "));
assertEquals(ImmutableList.of(
Substring.wrap("a", 0, 1),
Substring.wrap("b", 2, 3)
), spaceSplit("a b"));
), argParse("a b"));
}
}

0 comments on commit 3a5170a

Please sign in to comment.
You can’t perform that action at this time.