Suggestions:getList() now returns <Suggestion> not <String>
This commit is contained in:
parent
2def16ba85
commit
ecfa4b31dd
7 changed files with 34 additions and 34 deletions
|
@ -66,9 +66,9 @@ public class Suggestion implements Comparable<Suggestion> {
|
||||||
return text.compareTo(o.text);
|
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)) {
|
if (range.equals(this.range)) {
|
||||||
return text;
|
return this;
|
||||||
}
|
}
|
||||||
final StringBuilder result = new StringBuilder();
|
final StringBuilder result = new StringBuilder();
|
||||||
if (range.getStart() < this.range.getStart()) {
|
if (range.getStart() < this.range.getStart()) {
|
||||||
|
@ -78,6 +78,6 @@ public class Suggestion implements Comparable<Suggestion> {
|
||||||
if (range.getEnd() > this.range.getEnd()) {
|
if (range.getEnd() > this.range.getEnd()) {
|
||||||
result.append(command.substring(this.range.getEnd(), range.getEnd()));
|
result.append(command.substring(this.range.getEnd(), range.getEnd()));
|
||||||
}
|
}
|
||||||
return result.toString();
|
return new Suggestion(range, result.toString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,6 @@ import com.google.common.collect.Sets;
|
||||||
import com.mojang.brigadier.context.StringRange;
|
import com.mojang.brigadier.context.StringRange;
|
||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
@ -16,9 +15,9 @@ public class Suggestions {
|
||||||
private static final Suggestions EMPTY = new Suggestions(StringRange.at(0), Lists.newArrayList());
|
private static final Suggestions EMPTY = new Suggestions(StringRange.at(0), Lists.newArrayList());
|
||||||
|
|
||||||
private final StringRange range;
|
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.range = range;
|
||||||
this.suggestions = suggestions;
|
this.suggestions = suggestions;
|
||||||
}
|
}
|
||||||
|
@ -27,7 +26,7 @@ public class Suggestions {
|
||||||
return range;
|
return range;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<String> getList() {
|
public List<Suggestion> getList() {
|
||||||
return suggestions;
|
return suggestions;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -74,9 +73,7 @@ public class Suggestions {
|
||||||
|
|
||||||
final Set<Suggestion> texts = new HashSet<>();
|
final Set<Suggestion> texts = new HashSet<>();
|
||||||
for (final Suggestions suggestions : input) {
|
for (final Suggestions suggestions : input) {
|
||||||
for (final String text : suggestions.getList()) {
|
texts.addAll(suggestions.getList());
|
||||||
texts.add(new Suggestion(suggestions.getRange(), text));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return create(command, texts);
|
return create(command, texts);
|
||||||
}
|
}
|
||||||
|
@ -92,12 +89,12 @@ public class Suggestions {
|
||||||
end = Math.max(suggestion.getRange().getEnd(), end);
|
end = Math.max(suggestion.getRange().getEnd(), end);
|
||||||
}
|
}
|
||||||
final StringRange range = new StringRange(start, 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) {
|
for (final Suggestion suggestion : suggestions) {
|
||||||
texts.add(suggestion.expand(command, range));
|
texts.add(suggestion.expand(command, range));
|
||||||
}
|
}
|
||||||
final List<String> sorted = Lists.newArrayList(texts);
|
final List<Suggestion> sorted = Lists.newArrayList(texts);
|
||||||
sorted.sort(String::compareToIgnoreCase);
|
sorted.sort((a, b) -> a.getText().compareToIgnoreCase(b.getText()));
|
||||||
return new Suggestions(range, sorted);
|
return new Suggestions(range, sorted);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@ package com.mojang.brigadier;
|
||||||
|
|
||||||
import com.google.common.collect.Lists;
|
import com.google.common.collect.Lists;
|
||||||
import com.mojang.brigadier.context.StringRange;
|
import com.mojang.brigadier.context.StringRange;
|
||||||
|
import com.mojang.brigadier.suggestion.Suggestion;
|
||||||
import com.mojang.brigadier.suggestion.Suggestions;
|
import com.mojang.brigadier.suggestion.Suggestions;
|
||||||
import com.mojang.brigadier.tree.LiteralCommandNode;
|
import com.mojang.brigadier.tree.LiteralCommandNode;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
|
@ -38,7 +39,7 @@ public class CommandSuggestionsTest {
|
||||||
final Suggestions result = subject.getCompletionSuggestions(subject.parse("", source)).join();
|
final Suggestions result = subject.getCompletionSuggestions(subject.parse("", source)).join();
|
||||||
|
|
||||||
assertThat(result.getRange(), equalTo(StringRange.at(0)));
|
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
|
@Test
|
||||||
|
@ -50,7 +51,7 @@ public class CommandSuggestionsTest {
|
||||||
final Suggestions result = subject.getCompletionSuggestions(subject.parse("b", source)).join();
|
final Suggestions result = subject.getCompletionSuggestions(subject.parse("b", source)).join();
|
||||||
|
|
||||||
assertThat(result.getRange(), equalTo(StringRange.between(0, 1)));
|
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
|
@Test
|
||||||
|
@ -65,7 +66,7 @@ public class CommandSuggestionsTest {
|
||||||
final Suggestions result = subject.getCompletionSuggestions(subject.parse("parent ", source)).join();
|
final Suggestions result = subject.getCompletionSuggestions(subject.parse("parent ", source)).join();
|
||||||
|
|
||||||
assertThat(result.getRange(), equalTo(StringRange.at(7)));
|
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
|
@Test
|
||||||
|
@ -81,7 +82,7 @@ public class CommandSuggestionsTest {
|
||||||
final Suggestions result = subject.getCompletionSuggestions(parse).join();
|
final Suggestions result = subject.getCompletionSuggestions(parse).join();
|
||||||
|
|
||||||
assertThat(result.getRange(), equalTo(StringRange.between(7, 8)));
|
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
|
@Test
|
||||||
|
@ -93,7 +94,7 @@ public class CommandSuggestionsTest {
|
||||||
final Suggestions result = subject.getCompletionSuggestions(parse).join();
|
final Suggestions result = subject.getCompletionSuggestions(parse).join();
|
||||||
|
|
||||||
assertThat(result.getRange(), equalTo(StringRange.at(9)));
|
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
|
@Test
|
||||||
|
@ -105,7 +106,7 @@ public class CommandSuggestionsTest {
|
||||||
final Suggestions result = subject.getCompletionSuggestions(parse).join();
|
final Suggestions result = subject.getCompletionSuggestions(parse).join();
|
||||||
|
|
||||||
assertThat(result.getRange(), equalTo(StringRange.between(9, 10)));
|
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
|
@Test
|
||||||
|
@ -125,7 +126,7 @@ public class CommandSuggestionsTest {
|
||||||
final Suggestions result = subject.getCompletionSuggestions(subject.parse("redirect loop 1 loop 02 loop 003 ", source)).join();
|
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.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
|
@Test
|
||||||
|
@ -186,6 +187,6 @@ public class CommandSuggestionsTest {
|
||||||
final Suggestions result = subject.getCompletionSuggestions(parse).join();
|
final Suggestions result = subject.getCompletionSuggestions(parse).join();
|
||||||
|
|
||||||
assertThat(result.getRange(), equalTo(StringRange.at(18)));
|
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"))));
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -52,30 +52,30 @@ public class SuggestionTest {
|
||||||
@Test
|
@Test
|
||||||
public void expand_unchanged() {
|
public void expand_unchanged() {
|
||||||
final Suggestion suggestion = new Suggestion(StringRange.at(1), "oo");
|
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
|
@Test
|
||||||
public void expand_left() {
|
public void expand_left() {
|
||||||
final Suggestion suggestion = new Suggestion(StringRange.at(1), "oo");
|
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
|
@Test
|
||||||
public void expand_right() {
|
public void expand_right() {
|
||||||
final Suggestion suggestion = new Suggestion(StringRange.at(0), "minecraft:");
|
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
|
@Test
|
||||||
public void expand_both() {
|
public void expand_both() {
|
||||||
final Suggestion suggestion = new Suggestion(StringRange.at(11), "minecraft:");
|
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
|
@Test
|
||||||
public void expand_replacement() {
|
public void expand_replacement() {
|
||||||
final Suggestion suggestion = new Suggestion(StringRange.between(6, 11), "strangers");
|
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!")));
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -21,7 +21,7 @@ public class SuggestionsBuilderTest {
|
||||||
@Test
|
@Test
|
||||||
public void suggest_appends() {
|
public void suggest_appends() {
|
||||||
final Suggestions result = builder.suggest("world!").build();
|
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.getRange(), equalTo(StringRange.between(6, 7)));
|
||||||
assertThat(result.isEmpty(), is(false));
|
assertThat(result.isEmpty(), is(false));
|
||||||
}
|
}
|
||||||
|
@ -29,7 +29,7 @@ public class SuggestionsBuilderTest {
|
||||||
@Test
|
@Test
|
||||||
public void suggest_replaces() {
|
public void suggest_replaces() {
|
||||||
final Suggestions result = builder.suggest("everybody").build();
|
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.getRange(), equalTo(StringRange.between(6, 7)));
|
||||||
assertThat(result.isEmpty(), is(false));
|
assertThat(result.isEmpty(), is(false));
|
||||||
}
|
}
|
||||||
|
@ -44,7 +44,7 @@ public class SuggestionsBuilderTest {
|
||||||
@Test
|
@Test
|
||||||
public void suggest_multiple() {
|
public void suggest_multiple() {
|
||||||
final Suggestions result = builder.suggest("world!").suggest("everybody").suggest("weekend").build();
|
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.getRange(), equalTo(StringRange.between(6, 7)));
|
||||||
assertThat(result.isEmpty(), is(false));
|
assertThat(result.isEmpty(), is(false));
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,16 +19,16 @@ public class SuggestionsTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void merge_single() {
|
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));
|
final Suggestions merged = Suggestions.merge("foo b", Collections.singleton(suggestions));
|
||||||
assertThat(merged, equalTo(suggestions));
|
assertThat(merged, equalTo(suggestions));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void merge_multiple() {
|
public void merge_multiple() {
|
||||||
final Suggestions a = new Suggestions(StringRange.at(5), Lists.newArrayList("ar", "az", "Az"));
|
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("foo", "qux", "apple", "Bar"));
|
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));
|
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"))));
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -8,7 +8,9 @@ import com.mojang.brigadier.CommandDispatcher;
|
||||||
import com.mojang.brigadier.StringReader;
|
import com.mojang.brigadier.StringReader;
|
||||||
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
|
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
|
||||||
import com.mojang.brigadier.context.CommandContextBuilder;
|
import com.mojang.brigadier.context.CommandContextBuilder;
|
||||||
|
import com.mojang.brigadier.context.StringRange;
|
||||||
import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
||||||
|
import com.mojang.brigadier.suggestion.Suggestion;
|
||||||
import com.mojang.brigadier.suggestion.Suggestions;
|
import com.mojang.brigadier.suggestion.Suggestions;
|
||||||
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
|
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
|
@ -84,7 +86,7 @@ public class LiteralCommandNodeTest extends AbstractCommandNodeTest {
|
||||||
@Test
|
@Test
|
||||||
public void testSuggestions() throws Exception {
|
public void testSuggestions() throws Exception {
|
||||||
final Suggestions empty = node.listSuggestions(contextBuilder.build(""), new SuggestionsBuilder("", 0)).join();
|
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();
|
final Suggestions foo = node.listSuggestions(contextBuilder.build("foo"), new SuggestionsBuilder("foo", 0)).join();
|
||||||
assertThat(foo.isEmpty(), is(true));
|
assertThat(foo.isEmpty(), is(true));
|
||||||
|
|
Loading…
Reference in a new issue