diff --git a/src/main/java/com/mojang/brigadier/CommandDispatcher.java b/src/main/java/com/mojang/brigadier/CommandDispatcher.java index 69803342..9942a210 100644 --- a/src/main/java/com/mojang/brigadier/CommandDispatcher.java +++ b/src/main/java/com/mojang/brigadier/CommandDispatcher.java @@ -183,7 +183,7 @@ private ParseResults parseNodes(final CommandNode node, final StringReader reader.skip(); if (child.getRedirect() != null) { final CommandContextBuilder 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 parse = parseNodes(child.getRedirect(), reader, childContext); context.withChild(parse.getContext()); return new ParseResults<>(context, parse.getReader(), parse.getExceptions()); diff --git a/src/main/java/com/mojang/brigadier/context/CommandContextBuilder.java b/src/main/java/com/mojang/brigadier/context/CommandContextBuilder.java index f88bafd8..dac2c0b9 100644 --- a/src/main/java/com/mojang/brigadier/context/CommandContextBuilder.java +++ b/src/main/java/com/mojang/brigadier/context/CommandContextBuilder.java @@ -19,7 +19,7 @@ public class CommandContextBuilder { public CommandContextBuilder(final CommandDispatcher 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 withSource(final S source) { @@ -47,7 +47,7 @@ public CommandContextBuilder withCommand(final Command command) { public CommandContextBuilder withNode(final CommandNode 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; } diff --git a/src/main/java/com/mojang/brigadier/context/ParsedArgument.java b/src/main/java/com/mojang/brigadier/context/ParsedArgument.java index f8224419..17628669 100644 --- a/src/main/java/com/mojang/brigadier/context/ParsedArgument.java +++ b/src/main/java/com/mojang/brigadier/context/ParsedArgument.java @@ -7,7 +7,7 @@ public class ParsedArgument { 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; } diff --git a/src/main/java/com/mojang/brigadier/context/StringRange.java b/src/main/java/com/mojang/brigadier/context/StringRange.java index 3765bb93..27673fbc 100644 --- a/src/main/java/com/mojang/brigadier/context/StringRange.java +++ b/src/main/java/com/mojang/brigadier/context/StringRange.java @@ -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; } @@ -61,5 +73,4 @@ public String toString() { ", end=" + end + '}'; } - } diff --git a/src/main/java/com/mojang/brigadier/suggestion/Suggestions.java b/src/main/java/com/mojang/brigadier/suggestion/Suggestions.java index ffa8f86d..c69b1453 100644 --- a/src/main/java/com/mojang/brigadier/suggestion/Suggestions.java +++ b/src/main/java/com/mojang/brigadier/suggestion/Suggestions.java @@ -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 suggestions; diff --git a/src/main/java/com/mojang/brigadier/suggestion/SuggestionsBuilder.java b/src/main/java/com/mojang/brigadier/suggestion/SuggestionsBuilder.java index eeb55e74..3b6fb78c 100644 --- a/src/main/java/com/mojang/brigadier/suggestion/SuggestionsBuilder.java +++ b/src/main/java/com/mojang/brigadier/suggestion/SuggestionsBuilder.java @@ -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; } diff --git a/src/main/java/com/mojang/brigadier/tree/LiteralCommandNode.java b/src/main/java/com/mojang/brigadier/tree/LiteralCommandNode.java index 2e641d79..393ae120 100644 --- a/src/main/java/com/mojang/brigadier/tree/LiteralCommandNode.java +++ b/src/main/java/com/mojang/brigadier/tree/LiteralCommandNode.java @@ -46,7 +46,7 @@ public void parse(final StringReader reader, final CommandContextBuilder cont } } - contextBuilder.withNode(this, new StringRange(start, reader.getCursor())); + contextBuilder.withNode(this, StringRange.between(start, reader.getCursor())); } @Override diff --git a/src/test/java/com/mojang/brigadier/CommandSuggestionsTest.java b/src/test/java/com/mojang/brigadier/CommandSuggestionsTest.java index a3e38819..ec40c786 100644 --- a/src/test/java/com/mojang/brigadier/CommandSuggestionsTest.java +++ b/src/test/java/com/mojang/brigadier/CommandSuggestionsTest.java @@ -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"))); } @@ -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"))); } @@ -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"))); } @@ -80,7 +80,7 @@ public void getCompletionSuggestions_subCommands_partial() throws Exception { final ParseResults 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 void getCompletionSuggestions_redirect() throws Exception { final ParseResults 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 void getCompletionSuggestions_redirectPartial() throws Exception { final ParseResults 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 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"))); } @@ -185,7 +185,7 @@ public void getCompletionSuggestions_execute_simulation_partial() throws Excepti final ParseResults 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"))); } } \ No newline at end of file diff --git a/src/test/java/com/mojang/brigadier/context/CommandContextTest.java b/src/test/java/com/mojang/brigadier/context/CommandContextTest.java index fcde78bf..113addab 100644 --- a/src/test/java/com/mojang/brigadier/context/CommandContextTest.java +++ b/src/test/java/com/mojang/brigadier/context/CommandContextTest.java @@ -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; @@ -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(); } } \ 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 d43640e3..924713cb 100644 --- a/src/test/java/com/mojang/brigadier/suggestion/SuggestionTest.java +++ b/src/test/java/com/mojang/brigadier/suggestion/SuggestionTest.java @@ -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!")); } } \ 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 9feceb9e..53bbd277 100644 --- a/src/test/java/com/mojang/brigadier/suggestion/SuggestionsBuilderTest.java +++ b/src/test/java/com/mojang/brigadier/suggestion/SuggestionsBuilderTest.java @@ -22,7 +22,7 @@ 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)); } @@ -30,7 +30,7 @@ public void suggest_appends() { 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 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)); } diff --git a/src/test/java/com/mojang/brigadier/suggestion/SuggestionsTest.java b/src/test/java/com/mojang/brigadier/suggestion/SuggestionsTest.java index a2926a13..213a9920 100644 --- a/src/test/java/com/mojang/brigadier/suggestion/SuggestionsTest.java +++ b/src/test/java/com/mojang/brigadier/suggestion/SuggestionsTest.java @@ -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"))); }