Add completions to all integer types

This commit is contained in:
Alexander Söderberg 2020-09-18 22:51:33 +02:00
parent 36f680cff0
commit 04a6919c6a
No known key found for this signature in database
GPG key ID: C0207FF7EA146678
6 changed files with 40 additions and 5 deletions

View file

@ -30,6 +30,7 @@ import com.intellectualsites.commands.context.CommandContext;
import com.intellectualsites.commands.exceptions.parsing.NumberParseException; import com.intellectualsites.commands.exceptions.parsing.NumberParseException;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import java.util.List;
import java.util.Queue; import java.util.Queue;
@SuppressWarnings("unused") @SuppressWarnings("unused")
@ -207,6 +208,14 @@ public final class ByteArgument<C> extends CommandArgument<C, Byte> {
public boolean isContextFree() { public boolean isContextFree() {
return true; return true;
} }
@Nonnull
@Override
public List<String> suggestions(@Nonnull final CommandContext<C> commandContext,
@Nonnull final String input) {
return IntegerArgument.IntegerParser.getSuggestions(this.min, this.max, input);
}
} }

View file

@ -132,6 +132,7 @@ public final class CharArgument<C> extends CommandArgument<C, Character> {
public boolean isContextFree() { public boolean isContextFree() {
return true; return true;
} }
} }

View file

@ -206,6 +206,7 @@ public final class DoubleArgument<C> extends CommandArgument<C, Double> {
public boolean isContextFree() { public boolean isContextFree() {
return true; return true;
} }
} }

View file

@ -237,26 +237,32 @@ public final class IntegerArgument<C> extends CommandArgument<C, Integer> {
@Override @Override
public List<String> suggestions(@Nonnull final CommandContext<C> commandContext, public List<String> suggestions(@Nonnull final CommandContext<C> commandContext,
@Nonnull final String input) { @Nonnull final String input) {
return getSuggestions(this.min, this.max, input);
}
@Nonnull
static List<String> getSuggestions(final long min, final long max, @Nonnull final String input) {
if (input.isEmpty()) { if (input.isEmpty()) {
return IntStream.range(0, MAX_SUGGESTIONS_INCREMENT).mapToObj(Integer::toString).collect(Collectors.toList()); return IntStream.range(0, MAX_SUGGESTIONS_INCREMENT).mapToObj(Integer::toString).collect(Collectors.toList());
} }
try { try {
final int inputInt = Integer.parseInt(input); final long inputNum = Long.parseLong(input);
if (inputInt > this.getMax()) { if (inputNum > max) {
return Collections.emptyList(); return Collections.emptyList();
} else { } else {
final List<String> suggestions = new LinkedList<>(); final List<String> suggestions = new LinkedList<>();
suggestions.add(input); /* It's a valid number, so we suggest it */ suggestions.add(input); /* It's a valid number, so we suggest it */
for (int i = 0; i < MAX_SUGGESTIONS_INCREMENT for (int i = 0; i < MAX_SUGGESTIONS_INCREMENT
&& (inputInt * NUMBER_SHIFT_MULTIPLIER) + i <= this.getMax(); i++) { && (inputNum * NUMBER_SHIFT_MULTIPLIER) + i <= max; i++) {
suggestions.add(Integer.toString((inputInt * NUMBER_SHIFT_MULTIPLIER) + i)); suggestions.add(Long.toString((inputNum * NUMBER_SHIFT_MULTIPLIER) + i));
} }
return suggestions; return suggestions;
} }
} catch (final Exception ignored) { } catch (final Exception ignored) {
return Collections.emptyList(); /* Invalid input */ return Collections.emptyList();
} }
} }
} }

View file

@ -30,6 +30,7 @@ import com.intellectualsites.commands.context.CommandContext;
import com.intellectualsites.commands.exceptions.parsing.NumberParseException; import com.intellectualsites.commands.exceptions.parsing.NumberParseException;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import java.util.List;
import java.util.Queue; import java.util.Queue;
@SuppressWarnings("unused") @SuppressWarnings("unused")
@ -200,6 +201,14 @@ public final class LongArgument<C> extends CommandArgument<C, Long> {
public boolean isContextFree() { public boolean isContextFree() {
return true; return true;
} }
@Nonnull
@Override
public List<String> suggestions(@Nonnull final CommandContext<C> commandContext,
@Nonnull final String input) {
return IntegerArgument.IntegerParser.getSuggestions(this.min, this.max, input);
}
} }

View file

@ -30,6 +30,7 @@ import com.intellectualsites.commands.context.CommandContext;
import com.intellectualsites.commands.exceptions.parsing.NumberParseException; import com.intellectualsites.commands.exceptions.parsing.NumberParseException;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import java.util.List;
import java.util.Queue; import java.util.Queue;
@SuppressWarnings("unused") @SuppressWarnings("unused")
@ -206,6 +207,14 @@ public final class ShortArgument<C> extends CommandArgument<C, Short> {
public boolean isContextFree() { public boolean isContextFree() {
return true; return true;
} }
@Nonnull
@Override
public List<String> suggestions(@Nonnull final CommandContext<C> commandContext,
@Nonnull final String input) {
return IntegerArgument.IntegerParser.getSuggestions(this.min, this.max, input);
}
} }