forked from Mojang/brigadier
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
557352f
commit 095498c
Showing
21 changed files
with
494 additions
and
150 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 0 additions & 55 deletions
55
src/main/java/com/mojang/brigadier/CommandSuggestions.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
src/main/java/com/mojang/brigadier/suggestion/Suggestion.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package com.mojang.brigadier.suggestion; | ||
|
||
import com.mojang.brigadier.context.StringRange; | ||
|
||
import java.util.Objects; | ||
|
||
public class Suggestion implements Comparable<Suggestion> { | ||
private final StringRange range; | ||
private final String text; | ||
|
||
public Suggestion(final StringRange range, final String text) { | ||
this.range = range; | ||
this.text = text; | ||
} | ||
|
||
public StringRange getRange() { | ||
return range; | ||
} | ||
|
||
public String getText() { | ||
return text; | ||
} | ||
|
||
public String apply(final String input) { | ||
if (range.getStart() == 0 && range.getEnd() == input.length()) { | ||
return text; | ||
} | ||
final StringBuilder result = new StringBuilder(); | ||
if (range.getStart() > 0) { | ||
result.append(input.substring(0, range.getStart())); | ||
} | ||
result.append(text); | ||
if (range.getEnd() < input.length()) { | ||
result.append(input.substring(range.getEnd())); | ||
} | ||
return result.toString(); | ||
} | ||
|
||
@Override | ||
public boolean equals(final Object o) { | ||
if (this == o) { | ||
return true; | ||
} | ||
if (!(o instanceof Suggestion)) { | ||
return false; | ||
} | ||
final Suggestion that = (Suggestion) o; | ||
return Objects.equals(range, that.range) && Objects.equals(text, that.text); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(range, text); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Suggestion{" + | ||
"range=" + range + | ||
", text='" + text + '\'' + | ||
'}'; | ||
} | ||
|
||
@Override | ||
public int compareTo(final Suggestion o) { | ||
return text.compareTo(o.text); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/com/mojang/brigadier/suggestion/SuggestionProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.mojang.brigadier.suggestion; | ||
|
||
import com.mojang.brigadier.context.CommandContext; | ||
import com.mojang.brigadier.exceptions.CommandSyntaxException; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
|
||
@FunctionalInterface | ||
public interface SuggestionProvider<S> { | ||
CompletableFuture<Suggestions> getSuggestions(final CommandContext<S> context, final SuggestionsBuilder builder) throws CommandSyntaxException; | ||
} |
92 changes: 92 additions & 0 deletions
92
src/main/java/com/mojang/brigadier/suggestion/Suggestions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package com.mojang.brigadier.suggestion; | ||
|
||
import com.google.common.collect.Lists; | ||
import com.google.common.collect.Sets; | ||
import com.mojang.brigadier.context.StringRange; | ||
|
||
import java.util.Collection; | ||
import java.util.Collections; | ||
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 final String input; | ||
private final StringRange range; | ||
private final List<Suggestion> suggestions; | ||
|
||
public Suggestions(final String input, final List<Suggestion> suggestions) { | ||
this.input = input; | ||
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() { | ||
return suggestions; | ||
} | ||
|
||
public boolean isEmpty() { | ||
return suggestions.isEmpty(); | ||
} | ||
|
||
@Override | ||
public boolean equals(final Object o) { | ||
if (this == o) { | ||
return true; | ||
} | ||
if (!(o instanceof Suggestions)) { | ||
return false; | ||
} | ||
final Suggestions that = (Suggestions) o; | ||
return Objects.equals(input, that.input) && | ||
Objects.equals(range, that.range) && | ||
Objects.equals(suggestions, that.suggestions); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(input, range, suggestions); | ||
} | ||
|
||
public static CompletableFuture<Suggestions> empty() { | ||
return CompletableFuture.completedFuture(EMPTY); | ||
} | ||
|
||
public static Suggestions merge(final Collection<Suggestions> inputs) { | ||
if (inputs.isEmpty()) { | ||
return EMPTY; | ||
} else if (inputs.size() == 1) { | ||
return inputs.iterator().next(); | ||
} | ||
|
||
final Set<Suggestion> suggestions = Sets.newHashSet(); | ||
for (final Suggestions input : inputs) { | ||
suggestions.addAll(input.getList()); | ||
} | ||
final List<Suggestion> sorted = Lists.newArrayList(suggestions); | ||
Collections.sort(sorted); | ||
return new Suggestions(inputs.iterator().next().getInput(), sorted); | ||
} | ||
} |
Oops, something went wrong.