Implement interactive completion
- This is a re-implementation of a interactive completion with breaking changes as it moves away from a direct use of a MethodParameter in favour of a CommandRegistration and its option definitions. - Fixes #449
This commit is contained in:
@@ -20,6 +20,9 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.springframework.shell.command.CommandOption;
|
||||
import org.springframework.shell.command.CommandRegistration;
|
||||
|
||||
/**
|
||||
* Represents the buffer context in which completion was triggered.
|
||||
*
|
||||
@@ -33,16 +36,22 @@ public class CompletionContext {
|
||||
|
||||
private final int position;
|
||||
|
||||
private final CommandOption commandOption;
|
||||
|
||||
private final CommandRegistration commandRegistration;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param words words in the buffer, excluding words for the command name
|
||||
* @param wordIndex the index of the word the cursor is in
|
||||
* @param position the position inside the current word where the cursor is
|
||||
*/
|
||||
public CompletionContext(List<String> words, int wordIndex, int position) {
|
||||
public CompletionContext(List<String> words, int wordIndex, int position, CommandRegistration commandRegistration, CommandOption commandOption) {
|
||||
this.words = words;
|
||||
this.wordIndex = wordIndex;
|
||||
this.position = position;
|
||||
this.commandRegistration = commandRegistration;
|
||||
this.commandOption = commandOption;
|
||||
}
|
||||
|
||||
public List<String> getWords() {
|
||||
@@ -57,6 +66,14 @@ public class CompletionContext {
|
||||
return position;
|
||||
}
|
||||
|
||||
public CommandOption getCommandOption() {
|
||||
return commandOption;
|
||||
}
|
||||
|
||||
public CommandRegistration getCommandRegistration() {
|
||||
return commandRegistration;
|
||||
}
|
||||
|
||||
public String upToCursor() {
|
||||
String start = words.subList(0, wordIndex).stream().collect(Collectors.joining(" "));
|
||||
if (wordIndex < words.size()) {
|
||||
@@ -84,6 +101,11 @@ public class CompletionContext {
|
||||
* Return a copy of this context, as if the first {@literal nbWords} were not present
|
||||
*/
|
||||
public CompletionContext drop(int nbWords) {
|
||||
return new CompletionContext(new ArrayList<String>(words.subList(nbWords, words.size())), wordIndex-nbWords, position);
|
||||
return new CompletionContext(new ArrayList<String>(words.subList(nbWords, words.size())), wordIndex - nbWords,
|
||||
position, commandRegistration, commandOption);
|
||||
}
|
||||
|
||||
public CompletionContext commandOption(CommandOption commandOption) {
|
||||
return new CompletionContext(words, wordIndex, position, commandRegistration, commandOption);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,6 +22,7 @@ import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import javax.validation.Validator;
|
||||
import javax.validation.ValidatorFactory;
|
||||
@@ -38,6 +39,7 @@ import org.springframework.core.convert.support.DefaultConversionService;
|
||||
import org.springframework.shell.command.CommandAlias;
|
||||
import org.springframework.shell.command.CommandCatalog;
|
||||
import org.springframework.shell.command.CommandExecution;
|
||||
import org.springframework.shell.command.CommandOption;
|
||||
import org.springframework.shell.command.CommandExecution.CommandExecutionException;
|
||||
import org.springframework.shell.command.CommandExecution.CommandExecutionHandlerMethodArgumentResolvers;
|
||||
import org.springframework.shell.command.CommandRegistration;
|
||||
@@ -45,6 +47,7 @@ import org.springframework.shell.completion.CompletionResolver;
|
||||
import org.springframework.shell.context.InteractionMode;
|
||||
import org.springframework.shell.context.ShellContext;
|
||||
import org.springframework.shell.exit.ExitCodeMappings;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* Main class implementing a shell loop.
|
||||
@@ -275,17 +278,83 @@ public class Shell {
|
||||
String best = findLongestCommand(prefix);
|
||||
if (best != null) {
|
||||
CompletionContext argsContext = context.drop(best.split(" ").length);
|
||||
// Try to complete arguments
|
||||
CommandRegistration registration = commandRegistry.getRegistrations().get(best);
|
||||
|
||||
for (CompletionResolver resolver : completionResolvers) {
|
||||
List<CompletionProposal> resolved = resolver.resolve(registration, argsContext);
|
||||
candidates.addAll(resolved);
|
||||
}
|
||||
|
||||
// Try to complete arguments
|
||||
List<CommandOption> matchedArgOptions = new ArrayList<>();
|
||||
if (argsContext.getWords().size() > 0) {
|
||||
matchedArgOptions.addAll(matchOptions(registration.getOptions(), argsContext.getWords().get(0)));
|
||||
}
|
||||
|
||||
List<CompletionProposal> argProposals = matchedArgOptions.stream()
|
||||
.flatMap(o -> {
|
||||
Function<CompletionContext, List<CompletionProposal>> completion = o.getCompletion();
|
||||
if (completion != null) {
|
||||
List<CompletionProposal> apply = completion.apply(argsContext.commandOption(o));
|
||||
return apply.stream();
|
||||
}
|
||||
return Stream.empty();
|
||||
})
|
||||
.collect(Collectors.toList());
|
||||
|
||||
candidates.addAll(argProposals);
|
||||
}
|
||||
return candidates;
|
||||
}
|
||||
|
||||
private List<CommandOption> matchOptions(List<CommandOption> options, String arg) {
|
||||
List<CommandOption> matched = new ArrayList<>();
|
||||
String trimmed = StringUtils.trimLeadingCharacter(arg, '-');
|
||||
int count = arg.length() - trimmed.length();
|
||||
if (count == 1) {
|
||||
if (trimmed.length() == 1) {
|
||||
Character trimmedChar = trimmed.charAt(0);
|
||||
options.stream()
|
||||
.filter(o -> {
|
||||
for (Character sn : o.getShortNames()) {
|
||||
if (trimmedChar.equals(sn)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
})
|
||||
.findFirst()
|
||||
.ifPresent(o -> matched.add(o));
|
||||
}
|
||||
else if (trimmed.length() > 1) {
|
||||
trimmed.chars().mapToObj(i -> (char)i)
|
||||
.forEach(c -> {
|
||||
options.stream().forEach(o -> {
|
||||
for (Character sn : o.getShortNames()) {
|
||||
if (c.equals(sn)) {
|
||||
matched.add(o);
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
else if (count == 2) {
|
||||
options.stream()
|
||||
.filter(o -> {
|
||||
for (String ln : o.getLongNames()) {
|
||||
if (trimmed.equals(ln)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
})
|
||||
.findFirst()
|
||||
.ifPresent(o -> matched.add(o));
|
||||
}
|
||||
return matched;
|
||||
}
|
||||
|
||||
private List<CompletionProposal> commandsStartingWith(String prefix) {
|
||||
// Workaround for https://github.com/spring-projects/spring-shell/issues/150
|
||||
// (sadly, this ties this class to JLine somehow)
|
||||
|
||||
@@ -15,7 +15,12 @@
|
||||
*/
|
||||
package org.springframework.shell.command;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.function.Function;
|
||||
|
||||
import org.springframework.core.ResolvableType;
|
||||
import org.springframework.shell.CompletionContext;
|
||||
import org.springframework.shell.CompletionProposal;
|
||||
|
||||
/**
|
||||
* Interface representing an option in a command.
|
||||
@@ -94,6 +99,13 @@ public interface CommandOption {
|
||||
*/
|
||||
String getLabel();
|
||||
|
||||
/**
|
||||
* Gets a completion function.
|
||||
*
|
||||
* @return the completion function
|
||||
*/
|
||||
Function<CompletionContext, List<CompletionProposal>> getCompletion();
|
||||
|
||||
/**
|
||||
* Gets an instance of a default {@link CommandOption}.
|
||||
*
|
||||
@@ -103,7 +115,7 @@ public interface CommandOption {
|
||||
* @return default command option
|
||||
*/
|
||||
public static CommandOption of(String[] longNames, Character[] shortNames, String description) {
|
||||
return of(longNames, shortNames, description, null, false, null, null, null, null, null);
|
||||
return of(longNames, shortNames, description, null, false, null, null, null, null, null, null);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -117,7 +129,7 @@ public interface CommandOption {
|
||||
*/
|
||||
public static CommandOption of(String[] longNames, Character[] shortNames, String description,
|
||||
ResolvableType type) {
|
||||
return of(longNames, shortNames, description, type, false, null, null, null, null, null);
|
||||
return of(longNames, shortNames, description, type, false, null, null, null, null, null, null);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -133,13 +145,14 @@ public interface CommandOption {
|
||||
* @param arityMin the min arity
|
||||
* @param arityMax the max arity
|
||||
* @param label the label
|
||||
* @param completion the completion
|
||||
* @return default command option
|
||||
*/
|
||||
public static CommandOption of(String[] longNames, Character[] shortNames, String description,
|
||||
ResolvableType type, boolean required, String defaultValue, Integer position, Integer arityMin,
|
||||
Integer arityMax, String label) {
|
||||
Integer arityMax, String label, Function<CompletionContext, List<CompletionProposal>> completion) {
|
||||
return new DefaultCommandOption(longNames, shortNames, description, type, required, defaultValue, position,
|
||||
arityMin, arityMax, label);
|
||||
arityMin, arityMax, label, completion);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -157,10 +170,12 @@ public interface CommandOption {
|
||||
private int arityMin;
|
||||
private int arityMax;
|
||||
private String label;
|
||||
private Function<CompletionContext, List<CompletionProposal>> completion;
|
||||
|
||||
public DefaultCommandOption(String[] longNames, Character[] shortNames, String description,
|
||||
ResolvableType type, boolean required, String defaultValue, Integer position,
|
||||
Integer arityMin, Integer arityMax, String label) {
|
||||
Integer arityMin, Integer arityMax, String label,
|
||||
Function<CompletionContext, List<CompletionProposal>> completion) {
|
||||
this.longNames = longNames != null ? longNames : new String[0];
|
||||
this.shortNames = shortNames != null ? shortNames : new Character[0];
|
||||
this.description = description;
|
||||
@@ -171,6 +186,7 @@ public interface CommandOption {
|
||||
this.arityMin = arityMin != null ? arityMin : -1;
|
||||
this.arityMax = arityMax != null ? arityMax : -1;
|
||||
this.label = label;
|
||||
this.completion = completion;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -222,5 +238,10 @@ public interface CommandOption {
|
||||
public String getLabel() {
|
||||
return label;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Function<CompletionContext, List<CompletionProposal>> getCompletion() {
|
||||
return completion;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,6 +29,8 @@ import java.util.stream.Stream;
|
||||
import org.springframework.core.ResolvableType;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.shell.Availability;
|
||||
import org.springframework.shell.CompletionContext;
|
||||
import org.springframework.shell.CompletionProposal;
|
||||
import org.springframework.shell.context.InteractionMode;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
@@ -208,6 +210,14 @@ public interface CommandRegistration {
|
||||
*/
|
||||
OptionSpec label(String label);
|
||||
|
||||
/**
|
||||
* Define a {@code completion function} for an option.
|
||||
*
|
||||
* @param completion the completion function
|
||||
* @return option spec for chaining
|
||||
*/
|
||||
OptionSpec completion(Function<CompletionContext, List<CompletionProposal>> completion);
|
||||
|
||||
/**
|
||||
* Return a builder for chaining.
|
||||
*
|
||||
@@ -528,6 +538,7 @@ public interface CommandRegistration {
|
||||
private Integer arityMin;
|
||||
private Integer arityMax;
|
||||
private String label;
|
||||
private Function<CompletionContext, List<CompletionProposal>> completion;
|
||||
|
||||
DefaultOptionSpec(BaseBuilder builder) {
|
||||
this.builder = builder;
|
||||
@@ -626,6 +637,12 @@ public interface CommandRegistration {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public OptionSpec completion(Function<CompletionContext, List<CompletionProposal>> completion) {
|
||||
this.completion = completion;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder and() {
|
||||
return builder;
|
||||
@@ -670,6 +687,10 @@ public interface CommandRegistration {
|
||||
public String getLabel() {
|
||||
return label;
|
||||
}
|
||||
|
||||
public Function<CompletionContext, List<CompletionProposal>> getCompletion() {
|
||||
return completion;
|
||||
}
|
||||
}
|
||||
|
||||
static class DefaultTargetSpec implements TargetSpec {
|
||||
@@ -840,7 +861,7 @@ public interface CommandRegistration {
|
||||
return optionSpecs.stream()
|
||||
.map(o -> CommandOption.of(o.getLongNames(), o.getShortNames(), o.getDescription(), o.getType(),
|
||||
o.isRequired(), o.getDefaultValue(), o.getPosition(), o.getArityMin(), o.getArityMax(),
|
||||
o.getLabel()))
|
||||
o.getLabel(), o.getCompletion()))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
|
||||
@@ -224,37 +224,37 @@ public class ShellTests {
|
||||
when(commandRegistry.getRegistrations()).thenReturn(registrations);
|
||||
|
||||
// Invoke at very start
|
||||
List<String> proposals = shell.complete(new CompletionContext(Arrays.asList(""), 0, "".length()))
|
||||
List<String> proposals = shell.complete(new CompletionContext(Arrays.asList(""), 0, "".length(), null, null))
|
||||
.stream().map(CompletionProposal::value).collect(Collectors.toList());
|
||||
assertThat(proposals).containsExactlyInAnyOrder("another command", "hello world");
|
||||
|
||||
// Invoke in middle of first word
|
||||
proposals = shell.complete(new CompletionContext(Arrays.asList("hel"), 0, "hel".length()))
|
||||
proposals = shell.complete(new CompletionContext(Arrays.asList("hel"), 0, "hel".length(), null, null))
|
||||
.stream().map(CompletionProposal::value).collect(Collectors.toList());
|
||||
assertThat(proposals).containsExactly("hello world");
|
||||
|
||||
// Invoke at end of first word (no space after yet)
|
||||
proposals = shell.complete(new CompletionContext(Arrays.asList("hello"), 0, "hello".length()))
|
||||
proposals = shell.complete(new CompletionContext(Arrays.asList("hello"), 0, "hello".length(), null, null))
|
||||
.stream().map(CompletionProposal::value).collect(Collectors.toList());
|
||||
assertThat(proposals).containsExactly("hello world");
|
||||
|
||||
// Invoke after first word / start of second word
|
||||
proposals = shell.complete(new CompletionContext(Arrays.asList("hello", ""), 1, "".length()))
|
||||
proposals = shell.complete(new CompletionContext(Arrays.asList("hello", ""), 1, "".length(), null, null))
|
||||
.stream().map(CompletionProposal::value).collect(Collectors.toList());
|
||||
assertThat(proposals).containsExactly("world");
|
||||
|
||||
// Invoke in middle of second word
|
||||
proposals = shell.complete(new CompletionContext(Arrays.asList("hello", "wo"), 1, "wo".length()))
|
||||
proposals = shell.complete(new CompletionContext(Arrays.asList("hello", "wo"), 1, "wo".length(), null, null))
|
||||
.stream().map(CompletionProposal::value).collect(Collectors.toList());
|
||||
assertThat(proposals).containsExactly("world");
|
||||
|
||||
// Invoke at end of whole command (no space after yet)
|
||||
proposals = shell.complete(new CompletionContext(Arrays.asList("hello", "world"), 1, "world".length()))
|
||||
proposals = shell.complete(new CompletionContext(Arrays.asList("hello", "world"), 1, "world".length(), null, null))
|
||||
.stream().map(CompletionProposal::value).collect(Collectors.toList());
|
||||
assertThat(proposals).containsExactly("world");
|
||||
|
||||
// Invoke in middle of second word
|
||||
proposals = shell.complete(new CompletionContext(Arrays.asList("hello", "world", ""), 2, "".length()))
|
||||
proposals = shell.complete(new CompletionContext(Arrays.asList("hello", "world", ""), 2, "".length(), null, null))
|
||||
.stream().map(CompletionProposal::value).collect(Collectors.toList());
|
||||
assertThat(proposals).isEmpty();
|
||||
}
|
||||
@@ -287,7 +287,7 @@ public class ShellTests {
|
||||
registrations.put("hello world", registration1);
|
||||
when(commandRegistry.getRegistrations()).thenReturn(registrations);
|
||||
|
||||
List<String> proposals = shell.complete(new CompletionContext(Arrays.asList("hello", "world", ""), 2, "".length()))
|
||||
List<String> proposals = shell.complete(new CompletionContext(Arrays.asList("hello", "world", ""), 2, "".length(), null, null))
|
||||
.stream().map(CompletionProposal::value).collect(Collectors.toList());
|
||||
assertThat(proposals).containsExactlyInAnyOrder("--arg1");
|
||||
}
|
||||
@@ -311,7 +311,7 @@ public class ShellTests {
|
||||
registrations.put("hello world", registration1);
|
||||
when(commandRegistry.getRegistrations()).thenReturn(registrations);
|
||||
|
||||
List<String> proposals = shell.complete(new CompletionContext(Arrays.asList("hello", "world", ""), 2, "".length()))
|
||||
List<String> proposals = shell.complete(new CompletionContext(Arrays.asList("hello", "world", ""), 2, "".length(), null, null))
|
||||
.stream().map(CompletionProposal::value).collect(Collectors.toList());
|
||||
assertThat(proposals).containsExactlyInAnyOrder("--arg1");
|
||||
}
|
||||
|
||||
@@ -327,7 +327,7 @@ public class CommandParserTests extends AbstractCommandTests {
|
||||
public void testBooleanWithDefault() {
|
||||
ResolvableType type = ResolvableType.forType(boolean.class);
|
||||
CommandOption option1 = CommandOption.of(new String[] { "arg1" }, new Character[0], "description", type, false,
|
||||
"true", null, null, null, null);
|
||||
"true", null, null, null, null, null);
|
||||
|
||||
List<CommandOption> options = Arrays.asList(option1);
|
||||
String[] args = new String[]{};
|
||||
@@ -359,7 +359,7 @@ public class CommandParserTests extends AbstractCommandTests {
|
||||
|
||||
private static CommandOption longOption(String name, ResolvableType type, boolean required, Integer position, Integer arityMin, Integer arityMax) {
|
||||
return CommandOption.of(new String[] { name }, new Character[0], "desc", type, required, null, position,
|
||||
arityMin, arityMax, null);
|
||||
arityMin, arityMax, null, null);
|
||||
}
|
||||
|
||||
private static CommandOption shortOption(char name) {
|
||||
|
||||
@@ -15,6 +15,8 @@
|
||||
*/
|
||||
package org.springframework.shell.command;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.core.ResolvableType;
|
||||
@@ -434,4 +436,23 @@ public class CommandRegistrationTests extends AbstractCommandTests {
|
||||
assertThat(registration.getAvailability()).isNotNull();
|
||||
assertThat(registration.getAvailability().isAvailable()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOptionWithCompletion() {
|
||||
CommandRegistration registration;
|
||||
registration = CommandRegistration.builder()
|
||||
.command("command1")
|
||||
.withOption()
|
||||
.longNames("arg1")
|
||||
.completion(ctx -> {
|
||||
return new ArrayList<>();
|
||||
})
|
||||
.and()
|
||||
.withTarget()
|
||||
.function(function1)
|
||||
.and()
|
||||
.build();
|
||||
assertThat(registration.getOptions()).hasSize(1);
|
||||
assertThat(registration.getOptions().get(0).getCompletion()).isNotNull();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user