Static helper methods for StringRange to make them more readable
This commit is contained in:
parent
ab95e809e2
commit
0a4d2363a2
12 changed files with 52 additions and 43 deletions
|
@ -183,7 +183,7 @@ public class CommandDispatcher<S> {
|
|||
reader.skip();
|
||||
if (child.getRedirect() != null) {
|
||||
final CommandContextBuilder<S> childContext = new CommandContextBuilder<>(this, source, reader.getCursor());
|
||||
childContext.withNode(child.getRedirect(), new StringRange(cursor, reader.getCursor() - 1));
|
||||
childContext.withNode(child.getRedirect(), StringRange.between(cursor, reader.getCursor() - 1));
|
||||
final ParseResults<S> parse = parseNodes(child.getRedirect(), reader, childContext);
|
||||
context.withChild(parse.getContext());
|
||||
return new ParseResults<>(context, parse.getReader(), parse.getExceptions());
|
||||
|
|
|
@ -19,7 +19,7 @@ public class CommandContextBuilder<S> {
|
|||
public CommandContextBuilder(final CommandDispatcher<S> dispatcher, final S source, final int start) {
|
||||
this.dispatcher = dispatcher;
|
||||
this.source = source;
|
||||
this.range = new StringRange(start, start);
|
||||
this.range = StringRange.at(start);
|
||||
}
|
||||
|
||||
public CommandContextBuilder<S> withSource(final S source) {
|
||||
|
@ -47,7 +47,7 @@ public class CommandContextBuilder<S> {
|
|||
|
||||
public CommandContextBuilder<S> withNode(final CommandNode<S> node, final StringRange range) {
|
||||
nodes.put(node, range);
|
||||
this.range = new StringRange(Math.min(this.range.getStart(), range.getStart()), Math.max(this.range.getEnd(), range.getEnd()));
|
||||
this.range = StringRange.encompassing(this.range, range);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
|
|
@ -7,7 +7,7 @@ public class ParsedArgument<S, T> {
|
|||
private final T result;
|
||||
|
||||
public ParsedArgument(final int start, final int end, final T result) {
|
||||
this.range = new StringRange(start, end);
|
||||
this.range = StringRange.between(start, end);
|
||||
this.result = result;
|
||||
}
|
||||
|
||||
|
|
|
@ -13,6 +13,18 @@ public class StringRange {
|
|||
this.end = end;
|
||||
}
|
||||
|
||||
public static StringRange at(final int pos) {
|
||||
return new StringRange(pos, pos);
|
||||
}
|
||||
|
||||
public static StringRange between(final int start, final int end) {
|
||||
return new StringRange(start, end);
|
||||
}
|
||||
|
||||
public static StringRange encompassing(final StringRange a, final StringRange b) {
|
||||
return new StringRange(Math.min(a.getStart(), b.getStart()), Math.max(a.getEnd(), b.getEnd()));
|
||||
}
|
||||
|
||||
public int getStart() {
|
||||
return start;
|
||||
}
|
||||
|
@ -61,5 +73,4 @@ public class StringRange {
|
|||
", end=" + end +
|
||||
'}';
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -13,7 +13,7 @@ import java.util.Set;
|
|||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
public class Suggestions {
|
||||
private static final Suggestions EMPTY = new Suggestions(new StringRange(0, 0), Lists.newArrayList());
|
||||
private static final Suggestions EMPTY = new Suggestions(StringRange.at(0), Lists.newArrayList());
|
||||
|
||||
private final StringRange range;
|
||||
private final List<String> suggestions;
|
||||
|
|
|
@ -44,7 +44,7 @@ public class SuggestionsBuilder {
|
|||
return this;
|
||||
}
|
||||
final String prefix = Strings.commonPrefix(text, remaining);
|
||||
result.add(new Suggestion(new StringRange(start + prefix.length(), input.length()), text.substring(prefix.length())));
|
||||
result.add(new Suggestion(StringRange.between(start + prefix.length(), input.length()), text.substring(prefix.length())));
|
||||
return this;
|
||||
}
|
||||
|
||||
|
|
|
@ -46,7 +46,7 @@ public class LiteralCommandNode<S> extends CommandNode<S> {
|
|||
}
|
||||
}
|
||||
|
||||
contextBuilder.withNode(this, new StringRange(start, reader.getCursor()));
|
||||
contextBuilder.withNode(this, StringRange.between(start, reader.getCursor()));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -37,7 +37,7 @@ public class CommandSuggestionsTest {
|
|||
|
||||
final Suggestions result = subject.getCompletionSuggestions(subject.parse("", source)).join();
|
||||
|
||||
assertThat(result.getRange(), equalTo(new StringRange(0, 0)));
|
||||
assertThat(result.getRange(), equalTo(StringRange.at(0)));
|
||||
assertThat(result.getList(), equalTo(Lists.newArrayList("bar", "baz", "foo")));
|
||||
}
|
||||
|
||||
|
@ -49,7 +49,7 @@ public class CommandSuggestionsTest {
|
|||
|
||||
final Suggestions result = subject.getCompletionSuggestions(subject.parse("b", source)).join();
|
||||
|
||||
assertThat(result.getRange(), equalTo(new StringRange(1, 1)));
|
||||
assertThat(result.getRange(), equalTo(StringRange.at(1)));
|
||||
assertThat(result.getList(), equalTo(Lists.newArrayList("ar", "az")));
|
||||
}
|
||||
|
||||
|
@ -64,7 +64,7 @@ public class CommandSuggestionsTest {
|
|||
|
||||
final Suggestions result = subject.getCompletionSuggestions(subject.parse("parent ", source)).join();
|
||||
|
||||
assertThat(result.getRange(), equalTo(new StringRange(7, 7)));
|
||||
assertThat(result.getRange(), equalTo(StringRange.at(7)));
|
||||
assertThat(result.getList(), equalTo(Lists.newArrayList("bar", "baz", "foo")));
|
||||
}
|
||||
|
||||
|
@ -80,7 +80,7 @@ public class CommandSuggestionsTest {
|
|||
final ParseResults<Object> parse = subject.parse("parent b", source);
|
||||
final Suggestions result = subject.getCompletionSuggestions(parse).join();
|
||||
|
||||
assertThat(result.getRange(), equalTo(new StringRange(8, 8)));
|
||||
assertThat(result.getRange(), equalTo(StringRange.at(8)));
|
||||
assertThat(result.getList(), equalTo(Lists.newArrayList("ar", "az")));
|
||||
}
|
||||
|
||||
|
@ -92,7 +92,7 @@ public class CommandSuggestionsTest {
|
|||
final ParseResults<Object> parse = subject.parse("redirect ", source);
|
||||
final Suggestions result = subject.getCompletionSuggestions(parse).join();
|
||||
|
||||
assertThat(result.getRange(), equalTo(new StringRange(9, 9)));
|
||||
assertThat(result.getRange(), equalTo(StringRange.at(9)));
|
||||
assertThat(result.getList(), equalTo(Lists.newArrayList("sub")));
|
||||
}
|
||||
|
||||
|
@ -104,7 +104,7 @@ public class CommandSuggestionsTest {
|
|||
final ParseResults<Object> parse = subject.parse("redirect s", source);
|
||||
final Suggestions result = subject.getCompletionSuggestions(parse).join();
|
||||
|
||||
assertThat(result.getRange(), equalTo(new StringRange(10, 10)));
|
||||
assertThat(result.getRange(), equalTo(StringRange.at(10)));
|
||||
assertThat(result.getList(), equalTo(Lists.newArrayList("ub")));
|
||||
}
|
||||
|
||||
|
@ -124,7 +124,7 @@ public class CommandSuggestionsTest {
|
|||
|
||||
final Suggestions result = subject.getCompletionSuggestions(subject.parse("redirect loop 1 loop 02 loop 003 ", source)).join();
|
||||
|
||||
assertThat(result.getRange(), equalTo(new StringRange(33, 33)));
|
||||
assertThat(result.getRange(), equalTo(StringRange.at(33)));
|
||||
assertThat(result.getList(), equalTo(Lists.newArrayList("loop")));
|
||||
}
|
||||
|
||||
|
@ -185,7 +185,7 @@ public class CommandSuggestionsTest {
|
|||
final ParseResults<Object> parse = subject.parse("execute as bar as ", source);
|
||||
final Suggestions result = subject.getCompletionSuggestions(parse).join();
|
||||
|
||||
assertThat(result.getRange(), equalTo(new StringRange(18, 18)));
|
||||
assertThat(result.getRange(), equalTo(StringRange.at(18)));
|
||||
assertThat(result.getList(), equalTo(Lists.newArrayList("bar", "baz")));
|
||||
}
|
||||
}
|
|
@ -11,8 +11,6 @@ import org.mockito.Mock;
|
|||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
|
||||
import static com.mojang.brigadier.arguments.IntegerArgumentType.integer;
|
||||
import static com.mojang.brigadier.builder.LiteralArgumentBuilder.literal;
|
||||
import static com.mojang.brigadier.builder.RequiredArgumentBuilder.argument;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
@ -66,8 +64,8 @@ public class CommandContextTest {
|
|||
.addEqualityGroup(new CommandContextBuilder<>(dispatcher, source, 0).withCommand(command).build(""), new CommandContextBuilder<>(dispatcher, source, 0).withCommand(command).build(""))
|
||||
.addEqualityGroup(new CommandContextBuilder<>(dispatcher, source, 0).withCommand(otherCommand).build(""), new CommandContextBuilder<>(dispatcher, source, 0).withCommand(otherCommand).build(""))
|
||||
.addEqualityGroup(new CommandContextBuilder<>(dispatcher, source, 0).withArgument("foo", new ParsedArgument<>(0, 1, 123)).build("123"), new CommandContextBuilder<>(dispatcher, source, 0).withArgument("foo", new ParsedArgument<>(0, 1, 123)).build("123"))
|
||||
.addEqualityGroup(new CommandContextBuilder<>(dispatcher, source, 0).withNode(node, new StringRange(0, 3)).withNode(otherNode, new StringRange(4, 6)).build("123 456"), new CommandContextBuilder<>(dispatcher, source, 0).withNode(node, new StringRange(0, 3)).withNode(otherNode, new StringRange(4, 6)).build("123 456"))
|
||||
.addEqualityGroup(new CommandContextBuilder<>(dispatcher, source, 0).withNode(otherNode, new StringRange(0, 3)).withNode(node, new StringRange(4, 6)).build("123 456"), new CommandContextBuilder<>(dispatcher, source, 0).withNode(otherNode, new StringRange(0, 3)).withNode(node, new StringRange(4, 6)).build("123 456"))
|
||||
.addEqualityGroup(new CommandContextBuilder<>(dispatcher, source, 0).withNode(node, StringRange.between(0, 3)).withNode(otherNode, StringRange.between(4, 6)).build("123 456"), new CommandContextBuilder<>(dispatcher, source, 0).withNode(node, StringRange.between(0, 3)).withNode(otherNode, StringRange.between(4, 6)).build("123 456"))
|
||||
.addEqualityGroup(new CommandContextBuilder<>(dispatcher, source, 0).withNode(otherNode, StringRange.between(0, 3)).withNode(node, StringRange.between(4, 6)).build("123 456"), new CommandContextBuilder<>(dispatcher, source, 0).withNode(otherNode, StringRange.between(0, 3)).withNode(node, StringRange.between(4, 6)).build("123 456"))
|
||||
.testEquals();
|
||||
}
|
||||
}
|
|
@ -9,73 +9,73 @@ import static org.junit.Assert.assertThat;
|
|||
public class SuggestionTest {
|
||||
@Test
|
||||
public void apply_insertation_start() {
|
||||
final Suggestion suggestion = new Suggestion(new StringRange(0, 0), "And so I said: ");
|
||||
final Suggestion suggestion = new Suggestion(StringRange.at(0), "And so I said: ");
|
||||
assertThat(suggestion.apply("Hello world!"), equalTo("And so I said: Hello world!"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void apply_insertation_middle() {
|
||||
final Suggestion suggestion = new Suggestion(new StringRange(6, 6), "small ");
|
||||
final Suggestion suggestion = new Suggestion(StringRange.at(6), "small ");
|
||||
assertThat(suggestion.apply("Hello world!"), equalTo("Hello small world!"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void apply_insertation_end() {
|
||||
final Suggestion suggestion = new Suggestion(new StringRange(5, 5), " world!");
|
||||
final Suggestion suggestion = new Suggestion(StringRange.at(5), " world!");
|
||||
assertThat(suggestion.apply("Hello"), equalTo("Hello world!"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void apply_replacement_start() {
|
||||
final Suggestion suggestion = new Suggestion(new StringRange(0, 5), "Goodbye");
|
||||
final Suggestion suggestion = new Suggestion(StringRange.between(0, 5), "Goodbye");
|
||||
assertThat(suggestion.apply("Hello world!"), equalTo("Goodbye world!"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void apply_replacement_middle() {
|
||||
final Suggestion suggestion = new Suggestion(new StringRange(6, 11), "Alex");
|
||||
final Suggestion suggestion = new Suggestion(StringRange.between(6, 11), "Alex");
|
||||
assertThat(suggestion.apply("Hello world!"), equalTo("Hello Alex!"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void apply_replacement_end() {
|
||||
final Suggestion suggestion = new Suggestion(new StringRange(6, 12), "Creeper!");
|
||||
final Suggestion suggestion = new Suggestion(StringRange.between(6, 12), "Creeper!");
|
||||
assertThat(suggestion.apply("Hello world!"), equalTo("Hello Creeper!"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void apply_replacement_everything() {
|
||||
final Suggestion suggestion = new Suggestion(new StringRange(0, 12), "Oh dear.");
|
||||
final Suggestion suggestion = new Suggestion(StringRange.between(0, 12), "Oh dear.");
|
||||
assertThat(suggestion.apply("Hello world!"), equalTo("Oh dear."));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void expand_unchanged() {
|
||||
final Suggestion suggestion = new Suggestion(new StringRange(1, 1), "oo");
|
||||
assertThat(suggestion.expand("f", new StringRange(1, 1)), equalTo("oo"));
|
||||
final Suggestion suggestion = new Suggestion(StringRange.at(1), "oo");
|
||||
assertThat(suggestion.expand("f", StringRange.at(1)), equalTo("oo"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void expand_left() {
|
||||
final Suggestion suggestion = new Suggestion(new StringRange(1, 1), "oo");
|
||||
assertThat(suggestion.expand("f", new StringRange(0, 1)), equalTo("foo"));
|
||||
final Suggestion suggestion = new Suggestion(StringRange.at(1), "oo");
|
||||
assertThat(suggestion.expand("f", StringRange.between(0, 1)), equalTo("foo"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void expand_right() {
|
||||
final Suggestion suggestion = new Suggestion(new StringRange(0, 0), "minecraft:");
|
||||
assertThat(suggestion.expand("fish", new StringRange(0, 4)), equalTo("minecraft:fish"));
|
||||
final Suggestion suggestion = new Suggestion(StringRange.at(0), "minecraft:");
|
||||
assertThat(suggestion.expand("fish", StringRange.between(0, 4)), equalTo("minecraft:fish"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void expand_both() {
|
||||
final Suggestion suggestion = new Suggestion(new StringRange(11, 11), "minecraft:");
|
||||
assertThat(suggestion.expand("give Steve fish_block", new StringRange(5, 21)), equalTo("Steve minecraft:fish_block"));
|
||||
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"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void expand_replacement() {
|
||||
final Suggestion suggestion = new Suggestion(new StringRange(6, 11), "strangers");
|
||||
assertThat(suggestion.expand("Hello world!", new StringRange(0, 12)), equalTo("Hello strangers!"));
|
||||
final Suggestion suggestion = new Suggestion(StringRange.between(6, 11), "strangers");
|
||||
assertThat(suggestion.expand("Hello world!", StringRange.between(0, 12)), equalTo("Hello strangers!"));
|
||||
}
|
||||
}
|
|
@ -22,7 +22,7 @@ public class SuggestionsBuilderTest {
|
|||
public void suggest_appends() {
|
||||
final Suggestions result = builder.suggest("world!").build();
|
||||
assertThat(result.getList(), equalTo(Lists.newArrayList("orld!")));
|
||||
assertThat(result.getRange(), equalTo(new StringRange(7, 7)));
|
||||
assertThat(result.getRange(), equalTo(StringRange.at(7)));
|
||||
assertThat(result.isEmpty(), is(false));
|
||||
}
|
||||
|
||||
|
@ -30,7 +30,7 @@ public class SuggestionsBuilderTest {
|
|||
public void suggest_replaces() {
|
||||
final Suggestions result = builder.suggest("everybody").build();
|
||||
assertThat(result.getList(), equalTo(Lists.newArrayList("everybody")));
|
||||
assertThat(result.getRange(), equalTo(new StringRange(6, 7)));
|
||||
assertThat(result.getRange(), equalTo(StringRange.between(6, 7)));
|
||||
assertThat(result.isEmpty(), is(false));
|
||||
}
|
||||
|
||||
|
@ -45,7 +45,7 @@ public class SuggestionsBuilderTest {
|
|||
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.getRange(), equalTo(new StringRange(6, 7)));
|
||||
assertThat(result.getRange(), equalTo(StringRange.between(6, 7)));
|
||||
assertThat(result.isEmpty(), is(false));
|
||||
}
|
||||
|
||||
|
|
|
@ -19,15 +19,15 @@ public class SuggestionsTest {
|
|||
|
||||
@Test
|
||||
public void merge_single() {
|
||||
final Suggestions suggestions = new Suggestions(new StringRange(5, 5), Lists.newArrayList("ar"));
|
||||
final Suggestions suggestions = new Suggestions(StringRange.at(5), Lists.newArrayList("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(new StringRange(5, 5), Lists.newArrayList("ar", "az"));
|
||||
final Suggestions b = new Suggestions(new StringRange(4, 5), Lists.newArrayList("foo", "qux", "apple"));
|
||||
final Suggestions a = new Suggestions(StringRange.at(5), Lists.newArrayList("ar", "az"));
|
||||
final Suggestions b = new Suggestions(StringRange.between(4, 5), Lists.newArrayList("foo", "qux", "apple"));
|
||||
final Suggestions merged = Suggestions.merge("foo b", Lists.newArrayList(a, b));
|
||||
assertThat(merged.getList(), equalTo(Lists.newArrayList("apple", "bar", "baz", "foo", "qux")));
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue