From 182168acd5e6b0a31c4ae7d6faaac00484a5eba0 Mon Sep 17 00:00:00 2001 From: Nathan Adams Date: Mon, 5 Feb 2018 15:51:48 +0100 Subject: [PATCH] Don't automatically remove common prefixes from suggestions --- .../brigadier/suggestion/SuggestionsBuilder.java | 3 +-- .../com/mojang/brigadier/CommandSuggestionsTest.java | 12 ++++++------ .../brigadier/suggestion/SuggestionsBuilderTest.java | 4 ++-- .../brigadier/tree/LiteralCommandNodeTest.java | 11 ++++++++--- 4 files changed, 17 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/mojang/brigadier/suggestion/SuggestionsBuilder.java b/src/main/java/com/mojang/brigadier/suggestion/SuggestionsBuilder.java index 3b6fb78..f8993b4 100644 --- a/src/main/java/com/mojang/brigadier/suggestion/SuggestionsBuilder.java +++ b/src/main/java/com/mojang/brigadier/suggestion/SuggestionsBuilder.java @@ -43,8 +43,7 @@ public class SuggestionsBuilder { if (text.equals(remaining)) { return this; } - final String prefix = Strings.commonPrefix(text, remaining); - result.add(new Suggestion(StringRange.between(start + prefix.length(), input.length()), text.substring(prefix.length()))); + result.add(new Suggestion(StringRange.between(start, input.length()), text)); return this; } diff --git a/src/test/java/com/mojang/brigadier/CommandSuggestionsTest.java b/src/test/java/com/mojang/brigadier/CommandSuggestionsTest.java index ec40c78..b2286a1 100644 --- a/src/test/java/com/mojang/brigadier/CommandSuggestionsTest.java +++ b/src/test/java/com/mojang/brigadier/CommandSuggestionsTest.java @@ -49,8 +49,8 @@ public class CommandSuggestionsTest { final Suggestions result = subject.getCompletionSuggestions(subject.parse("b", source)).join(); - assertThat(result.getRange(), equalTo(StringRange.at(1))); - assertThat(result.getList(), equalTo(Lists.newArrayList("ar", "az"))); + assertThat(result.getRange(), equalTo(StringRange.between(0, 1))); + assertThat(result.getList(), equalTo(Lists.newArrayList("bar", "baz"))); } @Test @@ -80,8 +80,8 @@ public class CommandSuggestionsTest { final ParseResults parse = subject.parse("parent b", source); final Suggestions result = subject.getCompletionSuggestions(parse).join(); - assertThat(result.getRange(), equalTo(StringRange.at(8))); - assertThat(result.getList(), equalTo(Lists.newArrayList("ar", "az"))); + assertThat(result.getRange(), equalTo(StringRange.between(7, 8))); + assertThat(result.getList(), equalTo(Lists.newArrayList("bar", "baz"))); } @Test @@ -104,8 +104,8 @@ public class CommandSuggestionsTest { final ParseResults parse = subject.parse("redirect s", source); final Suggestions result = subject.getCompletionSuggestions(parse).join(); - assertThat(result.getRange(), equalTo(StringRange.at(10))); - assertThat(result.getList(), equalTo(Lists.newArrayList("ub"))); + assertThat(result.getRange(), equalTo(StringRange.between(9, 10))); + assertThat(result.getList(), equalTo(Lists.newArrayList("sub"))); } @Test diff --git a/src/test/java/com/mojang/brigadier/suggestion/SuggestionsBuilderTest.java b/src/test/java/com/mojang/brigadier/suggestion/SuggestionsBuilderTest.java index 53bbd27..8d0c98b 100644 --- a/src/test/java/com/mojang/brigadier/suggestion/SuggestionsBuilderTest.java +++ b/src/test/java/com/mojang/brigadier/suggestion/SuggestionsBuilderTest.java @@ -21,8 +21,8 @@ public class SuggestionsBuilderTest { @Test public void suggest_appends() { final Suggestions result = builder.suggest("world!").build(); - assertThat(result.getList(), equalTo(Lists.newArrayList("orld!"))); - assertThat(result.getRange(), equalTo(StringRange.at(7))); + assertThat(result.getList(), equalTo(Lists.newArrayList("world!"))); + assertThat(result.getRange(), equalTo(StringRange.between(6, 7))); assertThat(result.isEmpty(), is(false)); } diff --git a/src/test/java/com/mojang/brigadier/tree/LiteralCommandNodeTest.java b/src/test/java/com/mojang/brigadier/tree/LiteralCommandNodeTest.java index 21212ec..2f080c9 100644 --- a/src/test/java/com/mojang/brigadier/tree/LiteralCommandNodeTest.java +++ b/src/test/java/com/mojang/brigadier/tree/LiteralCommandNodeTest.java @@ -53,9 +53,14 @@ public class LiteralCommandNodeTest extends AbstractCommandNodeTest { @Test public void testParseSimilar() throws Exception { final StringReader reader = new StringReader("foobar"); - node.parse(reader, contextBuilder); - assertThat(reader.getRemaining(), equalTo("bar")); - // This should succeed, because it's the responsibility of the dispatcher to realize there's trailing text + try { + node.parse(reader, contextBuilder); + fail(); + } catch (final CommandSyntaxException ex) { + assertThat(ex.getType(), is(LiteralCommandNode.ERROR_INCORRECT_LITERAL)); + assertThat(ex.getData(), is(ImmutableMap.of("expected", "foo"))); + assertThat(ex.getCursor(), is(0)); + } } @Test