Skip to content

Commit

Permalink
Static helper methods for StringRange to make them more readable
Browse files Browse the repository at this point in the history
  • Loading branch information
Dinnerbone committed Nov 21, 2017
1 parent ab95e80 commit 0a4d236
Show file tree
Hide file tree
Showing 12 changed files with 52 additions and 43 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/mojang/brigadier/CommandDispatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ private ParseResults<S> parseNodes(final CommandNode<S> node, final StringReader
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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -47,7 +47,7 @@ public CommandContextBuilder<S> withCommand(final Command<S> command) {

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
13 changes: 12 additions & 1 deletion src/main/java/com/mojang/brigadier/context/StringRange.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@ public StringRange(final int start, final int end) {
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;
}
Expand Down Expand Up @@ -61,5 +73,4 @@ public String toString() {
", end=" + end +
'}';
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public SuggestionsBuilder suggest(final String text) {
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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void parse(final StringReader reader, final CommandContextBuilder<S> cont
}
}

contextBuilder.withNode(this, new StringRange(start, reader.getCursor()));
contextBuilder.withNode(this, StringRange.between(start, reader.getCursor()));
}

@Override
Expand Down
16 changes: 8 additions & 8 deletions src/test/java/com/mojang/brigadier/CommandSuggestionsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public void getCompletionSuggestions_rootCommands() throws Exception {

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

Expand All @@ -49,7 +49,7 @@ public void getCompletionSuggestions_rootCommands_partial() throws Exception {

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

Expand All @@ -64,7 +64,7 @@ public void getCompletionSuggestions_subCommands() throws Exception {

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

Expand All @@ -80,7 +80,7 @@ public void getCompletionSuggestions_subCommands_partial() throws Exception {
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")));
}

Expand All @@ -92,7 +92,7 @@ public void getCompletionSuggestions_redirect() throws Exception {
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")));
}

Expand All @@ -104,7 +104,7 @@ public void getCompletionSuggestions_redirectPartial() throws Exception {
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")));
}

Expand All @@ -124,7 +124,7 @@ public void getCompletionSuggestions_redirect_lots() throws Exception {

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

Expand Down Expand Up @@ -185,7 +185,7 @@ public void getCompletionSuggestions_execute_simulation_partial() throws Excepti
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")));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
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;
Expand Down Expand Up @@ -66,8 +64,8 @@ public void testEquals() throws Exception {
.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();
}
}
34 changes: 17 additions & 17 deletions src/test/java/com/mojang/brigadier/suggestion/SuggestionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,73 +9,73 @@
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!"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ public void setUp() throws Exception {
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));
}

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

Expand All @@ -45,7 +45,7 @@ public void suggest_noop() {
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));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ public void merge_empty() {

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

0 comments on commit 0a4d236

Please sign in to comment.