Suggestions:getList() now returns <Suggestion> not <String>

This commit is contained in:
Nathan Adams 2018-02-05 16:28:48 +01:00
parent 2def16ba85
commit ecfa4b31dd
7 changed files with 34 additions and 34 deletions

View file

@ -66,9 +66,9 @@ public class Suggestion implements Comparable<Suggestion> {
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<Suggestion> {
if (range.getEnd() > this.range.getEnd()) {
result.append(command.substring(this.range.getEnd(), range.getEnd()));
}
return result.toString();
return new Suggestion(range, result.toString());
}
}

View file

@ -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<String> suggestions;
private final List<Suggestion> suggestions;
public Suggestions(final StringRange range, final List<String> suggestions) {
public Suggestions(final StringRange range, final List<Suggestion> suggestions) {
this.range = range;
this.suggestions = suggestions;
}
@ -27,7 +26,7 @@ public class Suggestions {
return range;
}
public List<String> getList() {
public List<Suggestion> getList() {
return suggestions;
}
@ -74,9 +73,7 @@ public class Suggestions {
final Set<Suggestion> 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<String> texts = Sets.newHashSet();
final Set<Suggestion> texts = Sets.newHashSet();
for (final Suggestion suggestion : suggestions) {
texts.add(suggestion.expand(command, range));
}
final List<String> sorted = Lists.newArrayList(texts);
sorted.sort(String::compareToIgnoreCase);
final List<Suggestion> sorted = Lists.newArrayList(texts);
sorted.sort((a, b) -> a.getText().compareToIgnoreCase(b.getText()));
return new Suggestions(range, sorted);
}
}

View file

@ -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"))));
}
}

View file

@ -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!")));
}
}

View file

@ -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));
}

View file

@ -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"))));
}
}

View file

@ -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));