From ecfa4b31dd34a7e9490a2af876447451c0d4b7b1 Mon Sep 17 00:00:00 2001 From: Nathan Adams Date: Mon, 5 Feb 2018 16:28:48 +0100 Subject: [PATCH] Suggestions:getList() now returns not --- .../mojang/brigadier/suggestion/Suggestion.java | 6 +++--- .../brigadier/suggestion/Suggestions.java | 17 +++++++---------- .../brigadier/CommandSuggestionsTest.java | 17 +++++++++-------- .../brigadier/suggestion/SuggestionTest.java | 10 +++++----- .../suggestion/SuggestionsBuilderTest.java | 6 +++--- .../brigadier/suggestion/SuggestionsTest.java | 8 ++++---- .../brigadier/tree/LiteralCommandNodeTest.java | 4 +++- 7 files changed, 34 insertions(+), 34 deletions(-) diff --git a/src/main/java/com/mojang/brigadier/suggestion/Suggestion.java b/src/main/java/com/mojang/brigadier/suggestion/Suggestion.java index ad91c14..7fd7370 100644 --- a/src/main/java/com/mojang/brigadier/suggestion/Suggestion.java +++ b/src/main/java/com/mojang/brigadier/suggestion/Suggestion.java @@ -66,9 +66,9 @@ public class Suggestion implements Comparable { return text.compareTo(o.text); } - public String expand(final String command, final StringRange range) { + public Suggestion expand(final String command, final StringRange range) { if (range.equals(this.range)) { - return text; + return this; } final StringBuilder result = new StringBuilder(); if (range.getStart() < this.range.getStart()) { @@ -78,6 +78,6 @@ public class Suggestion implements Comparable { if (range.getEnd() > this.range.getEnd()) { result.append(command.substring(this.range.getEnd(), range.getEnd())); } - return result.toString(); + return new Suggestion(range, result.toString()); } } diff --git a/src/main/java/com/mojang/brigadier/suggestion/Suggestions.java b/src/main/java/com/mojang/brigadier/suggestion/Suggestions.java index 0abf7e7..03fdd5e 100644 --- a/src/main/java/com/mojang/brigadier/suggestion/Suggestions.java +++ b/src/main/java/com/mojang/brigadier/suggestion/Suggestions.java @@ -5,7 +5,6 @@ import com.google.common.collect.Sets; import com.mojang.brigadier.context.StringRange; import java.util.Collection; -import java.util.Collections; import java.util.HashSet; import java.util.List; import java.util.Objects; @@ -16,9 +15,9 @@ public class Suggestions { private static final Suggestions EMPTY = new Suggestions(StringRange.at(0), Lists.newArrayList()); private final StringRange range; - private final List suggestions; + private final List suggestions; - public Suggestions(final StringRange range, final List suggestions) { + public Suggestions(final StringRange range, final List suggestions) { this.range = range; this.suggestions = suggestions; } @@ -27,7 +26,7 @@ public class Suggestions { return range; } - public List getList() { + public List getList() { return suggestions; } @@ -74,9 +73,7 @@ public class Suggestions { final Set texts = new HashSet<>(); for (final Suggestions suggestions : input) { - for (final String text : suggestions.getList()) { - texts.add(new Suggestion(suggestions.getRange(), text)); - } + texts.addAll(suggestions.getList()); } return create(command, texts); } @@ -92,12 +89,12 @@ public class Suggestions { end = Math.max(suggestion.getRange().getEnd(), end); } final StringRange range = new StringRange(start, end); - final Set texts = Sets.newHashSet(); + final Set texts = Sets.newHashSet(); for (final Suggestion suggestion : suggestions) { texts.add(suggestion.expand(command, range)); } - final List sorted = Lists.newArrayList(texts); - sorted.sort(String::compareToIgnoreCase); + final List sorted = Lists.newArrayList(texts); + sorted.sort((a, b) -> a.getText().compareToIgnoreCase(b.getText())); return new Suggestions(range, sorted); } } diff --git a/src/test/java/com/mojang/brigadier/CommandSuggestionsTest.java b/src/test/java/com/mojang/brigadier/CommandSuggestionsTest.java index b2286a1..f131a3f 100644 --- a/src/test/java/com/mojang/brigadier/CommandSuggestionsTest.java +++ b/src/test/java/com/mojang/brigadier/CommandSuggestionsTest.java @@ -2,6 +2,7 @@ package com.mojang.brigadier; import com.google.common.collect.Lists; import com.mojang.brigadier.context.StringRange; +import com.mojang.brigadier.suggestion.Suggestion; import com.mojang.brigadier.suggestion.Suggestions; import com.mojang.brigadier.tree.LiteralCommandNode; import org.junit.Before; @@ -38,7 +39,7 @@ public class CommandSuggestionsTest { final Suggestions result = subject.getCompletionSuggestions(subject.parse("", source)).join(); assertThat(result.getRange(), equalTo(StringRange.at(0))); - assertThat(result.getList(), equalTo(Lists.newArrayList("bar", "baz", "foo"))); + assertThat(result.getList(), equalTo(Lists.newArrayList(new Suggestion(StringRange.at(0), "bar"), new Suggestion(StringRange.at(0), "baz"), new Suggestion(StringRange.at(0), "foo")))); } @Test @@ -50,7 +51,7 @@ public class CommandSuggestionsTest { final Suggestions result = subject.getCompletionSuggestions(subject.parse("b", source)).join(); assertThat(result.getRange(), equalTo(StringRange.between(0, 1))); - assertThat(result.getList(), equalTo(Lists.newArrayList("bar", "baz"))); + assertThat(result.getList(), equalTo(Lists.newArrayList(new Suggestion(StringRange.between(0, 1), "bar"), new Suggestion(StringRange.between(0, 1), "baz")))); } @Test @@ -65,7 +66,7 @@ public class CommandSuggestionsTest { final Suggestions result = subject.getCompletionSuggestions(subject.parse("parent ", source)).join(); assertThat(result.getRange(), equalTo(StringRange.at(7))); - assertThat(result.getList(), equalTo(Lists.newArrayList("bar", "baz", "foo"))); + assertThat(result.getList(), equalTo(Lists.newArrayList(new Suggestion(StringRange.at(7), "bar"), new Suggestion(StringRange.at(7), "baz"), new Suggestion(StringRange.at(7), "foo")))); } @Test @@ -81,7 +82,7 @@ public class CommandSuggestionsTest { final Suggestions result = subject.getCompletionSuggestions(parse).join(); assertThat(result.getRange(), equalTo(StringRange.between(7, 8))); - assertThat(result.getList(), equalTo(Lists.newArrayList("bar", "baz"))); + assertThat(result.getList(), equalTo(Lists.newArrayList(new Suggestion(StringRange.between(7, 8), "bar"), new Suggestion(StringRange.between(7, 8), "baz")))); } @Test @@ -93,7 +94,7 @@ public class CommandSuggestionsTest { final Suggestions result = subject.getCompletionSuggestions(parse).join(); assertThat(result.getRange(), equalTo(StringRange.at(9))); - assertThat(result.getList(), equalTo(Lists.newArrayList("sub"))); + assertThat(result.getList(), equalTo(Lists.newArrayList(new Suggestion(StringRange.at(9), "sub")))); } @Test @@ -105,7 +106,7 @@ public class CommandSuggestionsTest { final Suggestions result = subject.getCompletionSuggestions(parse).join(); assertThat(result.getRange(), equalTo(StringRange.between(9, 10))); - assertThat(result.getList(), equalTo(Lists.newArrayList("sub"))); + assertThat(result.getList(), equalTo(Lists.newArrayList(new Suggestion(StringRange.between(9, 10), "sub")))); } @Test @@ -125,7 +126,7 @@ public class CommandSuggestionsTest { final Suggestions result = subject.getCompletionSuggestions(subject.parse("redirect loop 1 loop 02 loop 003 ", source)).join(); assertThat(result.getRange(), equalTo(StringRange.at(33))); - assertThat(result.getList(), equalTo(Lists.newArrayList("loop"))); + assertThat(result.getList(), equalTo(Lists.newArrayList(new Suggestion(StringRange.at(33), "loop")))); } @Test @@ -186,6 +187,6 @@ public class CommandSuggestionsTest { final Suggestions result = subject.getCompletionSuggestions(parse).join(); assertThat(result.getRange(), equalTo(StringRange.at(18))); - assertThat(result.getList(), equalTo(Lists.newArrayList("bar", "baz"))); + assertThat(result.getList(), equalTo(Lists.newArrayList(new Suggestion(StringRange.at(18), "bar"), new Suggestion(StringRange.at(18), "baz")))); } } \ No newline at end of file diff --git a/src/test/java/com/mojang/brigadier/suggestion/SuggestionTest.java b/src/test/java/com/mojang/brigadier/suggestion/SuggestionTest.java index 924713c..f99593b 100644 --- a/src/test/java/com/mojang/brigadier/suggestion/SuggestionTest.java +++ b/src/test/java/com/mojang/brigadier/suggestion/SuggestionTest.java @@ -52,30 +52,30 @@ public class SuggestionTest { @Test public void expand_unchanged() { final Suggestion suggestion = new Suggestion(StringRange.at(1), "oo"); - assertThat(suggestion.expand("f", StringRange.at(1)), equalTo("oo")); + assertThat(suggestion.expand("f", StringRange.at(1)), equalTo(suggestion)); } @Test public void expand_left() { final Suggestion suggestion = new Suggestion(StringRange.at(1), "oo"); - assertThat(suggestion.expand("f", StringRange.between(0, 1)), equalTo("foo")); + assertThat(suggestion.expand("f", StringRange.between(0, 1)), equalTo(new Suggestion(StringRange.between(0, 1), "foo"))); } @Test public void expand_right() { final Suggestion suggestion = new Suggestion(StringRange.at(0), "minecraft:"); - assertThat(suggestion.expand("fish", StringRange.between(0, 4)), equalTo("minecraft:fish")); + assertThat(suggestion.expand("fish", StringRange.between(0, 4)), equalTo(new Suggestion(StringRange.between(0, 4), "minecraft:fish"))); } @Test public void expand_both() { final Suggestion suggestion = new Suggestion(StringRange.at(11), "minecraft:"); - assertThat(suggestion.expand("give Steve fish_block", StringRange.between(5, 21)), equalTo("Steve minecraft:fish_block")); + assertThat(suggestion.expand("give Steve fish_block", StringRange.between(5, 21)), equalTo(new Suggestion(StringRange.between(5, 21), "Steve minecraft:fish_block"))); } @Test public void expand_replacement() { final Suggestion suggestion = new Suggestion(StringRange.between(6, 11), "strangers"); - assertThat(suggestion.expand("Hello world!", StringRange.between(0, 12)), equalTo("Hello strangers!")); + assertThat(suggestion.expand("Hello world!", StringRange.between(0, 12)), equalTo(new Suggestion(StringRange.between(0, 12), "Hello strangers!"))); } } \ No newline at end of file diff --git a/src/test/java/com/mojang/brigadier/suggestion/SuggestionsBuilderTest.java b/src/test/java/com/mojang/brigadier/suggestion/SuggestionsBuilderTest.java index 8d0c98b..e46888c 100644 --- a/src/test/java/com/mojang/brigadier/suggestion/SuggestionsBuilderTest.java +++ b/src/test/java/com/mojang/brigadier/suggestion/SuggestionsBuilderTest.java @@ -21,7 +21,7 @@ public class SuggestionsBuilderTest { @Test public void suggest_appends() { final Suggestions result = builder.suggest("world!").build(); - assertThat(result.getList(), equalTo(Lists.newArrayList("world!"))); + assertThat(result.getList(), equalTo(Lists.newArrayList(new Suggestion(StringRange.between(6, 7), "world!")))); assertThat(result.getRange(), equalTo(StringRange.between(6, 7))); assertThat(result.isEmpty(), is(false)); } @@ -29,7 +29,7 @@ public class SuggestionsBuilderTest { @Test public void suggest_replaces() { final Suggestions result = builder.suggest("everybody").build(); - assertThat(result.getList(), equalTo(Lists.newArrayList("everybody"))); + assertThat(result.getList(), equalTo(Lists.newArrayList(new Suggestion(StringRange.between(6, 7), "everybody")))); assertThat(result.getRange(), equalTo(StringRange.between(6, 7))); assertThat(result.isEmpty(), is(false)); } @@ -44,7 +44,7 @@ public class SuggestionsBuilderTest { @Test public void suggest_multiple() { final Suggestions result = builder.suggest("world!").suggest("everybody").suggest("weekend").build(); - assertThat(result.getList(), equalTo(Lists.newArrayList("everybody", "weekend", "world!"))); + assertThat(result.getList(), equalTo(Lists.newArrayList(new Suggestion(StringRange.between(6, 7), "everybody"), new Suggestion(StringRange.between(6, 7), "weekend"), new Suggestion(StringRange.between(6, 7), "world!")))); assertThat(result.getRange(), equalTo(StringRange.between(6, 7))); assertThat(result.isEmpty(), is(false)); } diff --git a/src/test/java/com/mojang/brigadier/suggestion/SuggestionsTest.java b/src/test/java/com/mojang/brigadier/suggestion/SuggestionsTest.java index 474fa8a..254b7bd 100644 --- a/src/test/java/com/mojang/brigadier/suggestion/SuggestionsTest.java +++ b/src/test/java/com/mojang/brigadier/suggestion/SuggestionsTest.java @@ -19,16 +19,16 @@ public class SuggestionsTest { @Test public void merge_single() { - final Suggestions suggestions = new Suggestions(StringRange.at(5), Lists.newArrayList("ar")); + final Suggestions suggestions = new Suggestions(StringRange.at(5), Lists.newArrayList(new Suggestion(StringRange.at(5), "ar"))); final Suggestions merged = Suggestions.merge("foo b", Collections.singleton(suggestions)); assertThat(merged, equalTo(suggestions)); } @Test public void merge_multiple() { - final Suggestions a = new Suggestions(StringRange.at(5), Lists.newArrayList("ar", "az", "Az")); - final Suggestions b = new Suggestions(StringRange.between(4, 5), Lists.newArrayList("foo", "qux", "apple", "Bar")); + final Suggestions a = new Suggestions(StringRange.at(5), Lists.newArrayList(new Suggestion(StringRange.at(5), "ar"), new Suggestion(StringRange.at(5), "az"), new Suggestion(StringRange.at(5), "Az"))); + final Suggestions b = new Suggestions(StringRange.between(4, 5), Lists.newArrayList(new Suggestion(StringRange.between(4, 5), "foo"), new Suggestion(StringRange.between(4, 5), "qux"), new Suggestion(StringRange.between(4, 5), "apple"), new Suggestion(StringRange.between(4, 5), "Bar"))); final Suggestions merged = Suggestions.merge("foo b", Lists.newArrayList(a, b)); - assertThat(merged.getList(), equalTo(Lists.newArrayList("apple", "bar", "Bar", "baz", "bAz", "foo", "qux"))); + assertThat(merged.getList(), equalTo(Lists.newArrayList(new Suggestion(StringRange.between(4, 5), "apple"), new Suggestion(StringRange.between(4, 5), "bar"), new Suggestion(StringRange.between(4, 5), "Bar"), new Suggestion(StringRange.between(4, 5), "baz"), new Suggestion(StringRange.between(4, 5), "bAz"), new Suggestion(StringRange.between(4, 5), "foo"), new Suggestion(StringRange.between(4, 5), "qux")))); } } \ No newline at end of file diff --git a/src/test/java/com/mojang/brigadier/tree/LiteralCommandNodeTest.java b/src/test/java/com/mojang/brigadier/tree/LiteralCommandNodeTest.java index 2f080c9..6a001b4 100644 --- a/src/test/java/com/mojang/brigadier/tree/LiteralCommandNodeTest.java +++ b/src/test/java/com/mojang/brigadier/tree/LiteralCommandNodeTest.java @@ -8,7 +8,9 @@ import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.StringReader; import com.mojang.brigadier.builder.LiteralArgumentBuilder; import com.mojang.brigadier.context.CommandContextBuilder; +import com.mojang.brigadier.context.StringRange; import com.mojang.brigadier.exceptions.CommandSyntaxException; +import com.mojang.brigadier.suggestion.Suggestion; import com.mojang.brigadier.suggestion.Suggestions; import com.mojang.brigadier.suggestion.SuggestionsBuilder; import org.junit.Before; @@ -84,7 +86,7 @@ public class LiteralCommandNodeTest extends AbstractCommandNodeTest { @Test public void testSuggestions() throws Exception { final Suggestions empty = node.listSuggestions(contextBuilder.build(""), new SuggestionsBuilder("", 0)).join(); - assertThat(empty.getList(), equalTo(Lists.newArrayList("foo"))); + assertThat(empty.getList(), equalTo(Lists.newArrayList(new Suggestion(StringRange.at(0), "foo")))); final Suggestions foo = node.listSuggestions(contextBuilder.build("foo"), new SuggestionsBuilder("foo", 0)).join(); assertThat(foo.isEmpty(), is(true));