Suggestions should ultimately just contain a list of strings at a range, not each with an individual range
This commit is contained in:
@@ -354,7 +354,7 @@ public class CommandDispatcher<S> {
|
||||
for (final CompletableFuture<Suggestions> future : futures) {
|
||||
suggestions.add(future.join());
|
||||
}
|
||||
result.complete(Suggestions.merge(suggestions));
|
||||
result.complete(Suggestions.merge(parse.getReader().getString(), suggestions));
|
||||
});
|
||||
|
||||
return result;
|
||||
|
||||
@@ -61,4 +61,5 @@ public class StringRange {
|
||||
", end=" + end +
|
||||
'}';
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -65,4 +65,19 @@ public class Suggestion implements Comparable<Suggestion> {
|
||||
public int compareTo(final Suggestion o) {
|
||||
return text.compareTo(o.text);
|
||||
}
|
||||
|
||||
public String expand(final String command, final StringRange range) {
|
||||
if (range.equals(this.range)) {
|
||||
return text;
|
||||
}
|
||||
final StringBuilder result = new StringBuilder();
|
||||
if (range.getStart() < this.range.getStart()) {
|
||||
result.append(command.substring(range.getStart(), this.range.getStart()));
|
||||
}
|
||||
result.append(text);
|
||||
if (range.getEnd() > this.range.getEnd()) {
|
||||
result.append(command.substring(this.range.getEnd(), range.getEnd()));
|
||||
}
|
||||
return result.toString();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,44 +6,28 @@ 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;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
public class Suggestions {
|
||||
private static final Suggestions EMPTY = new Suggestions("", Lists.newArrayList());
|
||||
private static final Suggestions EMPTY = new Suggestions(new StringRange(0, 0), Lists.newArrayList());
|
||||
|
||||
private final String input;
|
||||
private final StringRange range;
|
||||
private final List<Suggestion> suggestions;
|
||||
private final List<String> suggestions;
|
||||
|
||||
public Suggestions(final String input, final List<Suggestion> suggestions) {
|
||||
this.input = input;
|
||||
public Suggestions(final StringRange range, final List<String> suggestions) {
|
||||
this.range = range;
|
||||
this.suggestions = suggestions;
|
||||
|
||||
if (suggestions.isEmpty()) {
|
||||
range = new StringRange(input.length(), input.length());
|
||||
} else {
|
||||
int start = Integer.MAX_VALUE;
|
||||
int end = Integer.MIN_VALUE;
|
||||
for (final Suggestion suggestion : suggestions) {
|
||||
start = Math.min(start, suggestion.getRange().getStart());
|
||||
end = Math.max(end, suggestion.getRange().getEnd());
|
||||
}
|
||||
range = new StringRange(start, end);
|
||||
}
|
||||
}
|
||||
|
||||
public String getInput() {
|
||||
return input;
|
||||
}
|
||||
|
||||
public StringRange getRange() {
|
||||
return range;
|
||||
}
|
||||
|
||||
public List<Suggestion> getList() {
|
||||
public List<String> getList() {
|
||||
return suggestions;
|
||||
}
|
||||
|
||||
@@ -60,33 +44,60 @@ public class Suggestions {
|
||||
return false;
|
||||
}
|
||||
final Suggestions that = (Suggestions) o;
|
||||
return Objects.equals(input, that.input) &&
|
||||
Objects.equals(range, that.range) &&
|
||||
return Objects.equals(range, that.range) &&
|
||||
Objects.equals(suggestions, that.suggestions);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(input, range, suggestions);
|
||||
return Objects.hash(range, suggestions);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Suggestions{" +
|
||||
"range=" + range +
|
||||
", suggestions=" + suggestions +
|
||||
'}';
|
||||
}
|
||||
|
||||
public static CompletableFuture<Suggestions> empty() {
|
||||
return CompletableFuture.completedFuture(EMPTY);
|
||||
}
|
||||
|
||||
public static Suggestions merge(final Collection<Suggestions> inputs) {
|
||||
if (inputs.isEmpty()) {
|
||||
public static Suggestions merge(final String command, final Collection<Suggestions> input) {
|
||||
if (input.isEmpty()) {
|
||||
return EMPTY;
|
||||
} else if (inputs.size() == 1) {
|
||||
return inputs.iterator().next();
|
||||
} else if (input.size() == 1) {
|
||||
return input.iterator().next();
|
||||
}
|
||||
|
||||
final Set<Suggestion> suggestions = Sets.newHashSet();
|
||||
for (final Suggestions input : inputs) {
|
||||
suggestions.addAll(input.getList());
|
||||
final Set<Suggestion> texts = new HashSet<>();
|
||||
for (final Suggestions suggestions : input) {
|
||||
for (final String text : suggestions.getList()) {
|
||||
texts.add(new Suggestion(suggestions.getRange(), text));
|
||||
}
|
||||
}
|
||||
final List<Suggestion> sorted = Lists.newArrayList(suggestions);
|
||||
return create(command, texts);
|
||||
}
|
||||
|
||||
public static Suggestions create(final String command, final Collection<Suggestion> suggestions) {
|
||||
if (suggestions.isEmpty()) {
|
||||
return EMPTY;
|
||||
}
|
||||
int start = Integer.MAX_VALUE;
|
||||
int end = Integer.MIN_VALUE;
|
||||
for (final Suggestion suggestion : suggestions) {
|
||||
start = Math.min(suggestion.getRange().getStart(), start);
|
||||
end = Math.max(suggestion.getRange().getEnd(), end);
|
||||
}
|
||||
final StringRange range = new StringRange(start, end);
|
||||
final Set<String> texts = Sets.newHashSet();
|
||||
for (final Suggestion suggestion : suggestions) {
|
||||
texts.add(suggestion.expand(command, range));
|
||||
}
|
||||
final List<String> sorted = Lists.newArrayList(texts);
|
||||
Collections.sort(sorted);
|
||||
return new Suggestions(inputs.iterator().next().getInput(), sorted);
|
||||
return new Suggestions(range, sorted);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,11 +32,11 @@ public class SuggestionsBuilder {
|
||||
}
|
||||
|
||||
public Suggestions build() {
|
||||
return new Suggestions(input, result);
|
||||
return Suggestions.create(input, result);
|
||||
}
|
||||
|
||||
public CompletableFuture<Suggestions> buildFuture() {
|
||||
return CompletableFuture.completedFuture(new Suggestions(input, result));
|
||||
return CompletableFuture.completedFuture(build());
|
||||
}
|
||||
|
||||
public SuggestionsBuilder suggest(final String text) {
|
||||
|
||||
Reference in New Issue
Block a user