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:
@@ -19,7 +19,6 @@ package org.springframework.shell.standard;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.shell.CompletionContext;
|
||||
import org.springframework.shell.CompletionProposal;
|
||||
import org.springframework.shell.command.CommandCatalog;
|
||||
@@ -28,8 +27,9 @@ import org.springframework.shell.command.CommandCatalog;
|
||||
* A {@link ValueProvider} that can be used to auto-complete names of shell commands.
|
||||
*
|
||||
* @author Eric Bottard
|
||||
* @author Janne Valkealahti
|
||||
*/
|
||||
public class CommandValueProvider extends ValueProviderSupport {
|
||||
public class CommandValueProvider implements ValueProvider {
|
||||
|
||||
private final CommandCatalog commandRegistry;
|
||||
|
||||
@@ -38,7 +38,7 @@ public class CommandValueProvider extends ValueProviderSupport {
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<CompletionProposal> complete(MethodParameter parameter, CompletionContext completionContext, String[] hints) {
|
||||
public List<CompletionProposal> complete(CompletionContext completionContext) {
|
||||
return commandRegistry.getRegistrations().keySet().stream()
|
||||
.map(CompletionProposal::new)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2016 the original author or authors.
|
||||
* Copyright 2016-2022 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -19,9 +19,10 @@ package org.springframework.shell.standard;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.core.ResolvableType;
|
||||
import org.springframework.shell.CompletionContext;
|
||||
import org.springframework.shell.CompletionProposal;
|
||||
import org.springframework.shell.command.CommandOption;
|
||||
|
||||
/**
|
||||
* A {@link ValueProvider} that knows how to complete values for {@link Enum} typed parameters.
|
||||
@@ -30,21 +31,28 @@ import org.springframework.shell.CompletionProposal;
|
||||
public class EnumValueProvider implements ValueProvider {
|
||||
|
||||
@Override
|
||||
public boolean supports(MethodParameter parameter, CompletionContext completionContext) {
|
||||
return Enum.class.isAssignableFrom(parameter.getParameterType());
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<CompletionProposal> complete(MethodParameter parameter, CompletionContext completionContext, String[] hints) {
|
||||
public List<CompletionProposal> complete(CompletionContext completionContext) {
|
||||
List<CompletionProposal> result = new ArrayList<>();
|
||||
for (Object v : parameter.getParameterType().getEnumConstants()) {
|
||||
Enum<?> e = (Enum<?>) v;
|
||||
String prefix = completionContext.currentWordUpToCursor();
|
||||
if (prefix == null) {
|
||||
prefix = "";
|
||||
}
|
||||
if (e.name().startsWith(prefix)) {
|
||||
result.add(new CompletionProposal(e.name()));
|
||||
CommandOption commandOption = completionContext.getCommandOption();
|
||||
if (commandOption != null) {
|
||||
ResolvableType type = commandOption.getType();
|
||||
if (type != null) {
|
||||
Class<?> clazz = type.getRawClass();
|
||||
if (clazz != null) {
|
||||
Object[] enumConstants = clazz.getEnumConstants();
|
||||
if (enumConstants != null) {
|
||||
for (Object v : enumConstants) {
|
||||
Enum<?> e = (Enum<?>) v;
|
||||
String prefix = completionContext.currentWordUpToCursor();
|
||||
if (prefix == null) {
|
||||
prefix = "";
|
||||
}
|
||||
if (e.name().startsWith(prefix)) {
|
||||
result.add(new CompletionProposal(e.name()));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2017 the original author or authors.
|
||||
* Copyright 2017-2022 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -25,7 +25,6 @@ import java.nio.file.Paths;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.shell.CompletionContext;
|
||||
import org.springframework.shell.CompletionProposal;
|
||||
|
||||
@@ -36,29 +35,25 @@ import static java.nio.file.FileVisitOption.FOLLOW_LINKS;
|
||||
* current working directory.
|
||||
*
|
||||
* @author Eric Bottard
|
||||
* @author Janne Valkealahti
|
||||
*/
|
||||
public class FileValueProvider implements ValueProvider {
|
||||
@Override
|
||||
public boolean supports(MethodParameter parameter, CompletionContext completionContext) {
|
||||
return parameter.getParameterType().equals(File.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<CompletionProposal> complete(MethodParameter parameter, CompletionContext completionContext, String[] hints) {
|
||||
|
||||
@Override
|
||||
public List<CompletionProposal> complete(CompletionContext completionContext) {
|
||||
String input = completionContext.currentWordUpToCursor();
|
||||
int lastSlash = input.lastIndexOf(File.separatorChar);
|
||||
Path dir = lastSlash > -1 ? Paths.get(input.substring(0, lastSlash+1)) : Paths.get("");
|
||||
String prefix = input.substring(lastSlash + 1, input.length());
|
||||
|
||||
try {
|
||||
return Files.find(dir, 1, (p, a) -> p.getFileName() != null && p.getFileName().toString().startsWith(prefix), FOLLOW_LINKS)
|
||||
return Files
|
||||
.find(dir, 1, (p, a) -> p.getFileName() != null && p.getFileName().toString().startsWith(prefix),
|
||||
FOLLOW_LINKS)
|
||||
.map(p -> new CompletionProposal(p.toString()))
|
||||
.collect(Collectors.toList());
|
||||
} catch (IOException e) {
|
||||
throw new UncheckedIOException(e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,6 +23,7 @@ import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@@ -36,13 +37,17 @@ import org.springframework.core.MethodParameter;
|
||||
import org.springframework.core.annotation.AnnotationUtils;
|
||||
import org.springframework.messaging.handler.invocation.InvocableHandlerMethod;
|
||||
import org.springframework.shell.Availability;
|
||||
import org.springframework.shell.CompletionContext;
|
||||
import org.springframework.shell.CompletionProposal;
|
||||
import org.springframework.shell.MethodTargetRegistrar;
|
||||
import org.springframework.shell.Utils;
|
||||
import org.springframework.shell.command.CommandCatalog;
|
||||
import org.springframework.shell.command.CommandRegistration;
|
||||
import org.springframework.shell.command.CommandRegistration.Builder;
|
||||
import org.springframework.shell.command.CommandRegistration.OptionSpec;
|
||||
import org.springframework.shell.standard.ShellOption.NoValueProvider;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ClassUtils;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
@@ -143,6 +148,14 @@ public class StandardMethodTargetRegistrar implements MethodTargetRegistrar, App
|
||||
if (ObjectUtils.nullSafeEquals(so.defaultValue(), ShellOption.NONE)) {
|
||||
optionSpec.required();
|
||||
}
|
||||
if (!ClassUtils.isAssignable(NoValueProvider.class, so.valueProvider())) {
|
||||
Function<CompletionContext, List<CompletionProposal>> completionFunction = ctx -> {
|
||||
ValueProvider valueProviderBean = this.applicationContext.getBean(so.valueProvider());
|
||||
List<CompletionProposal> complete = valueProviderBean.complete(ctx);
|
||||
return complete;
|
||||
};
|
||||
optionSpec.completion(completionFunction);
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2016 the original author or authors.
|
||||
* Copyright 2016-2022 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -13,23 +13,27 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.shell.standard;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.shell.CompletionContext;
|
||||
import org.springframework.shell.CompletionProposal;
|
||||
|
||||
/**
|
||||
* Beans implementing this interface are queried during TAB completion to gather possible values of a parameter.
|
||||
* Beans implementing this interface are queried during TAB completion to gather
|
||||
* possible values of a parameter.
|
||||
*
|
||||
* @author Eric Bottard
|
||||
* @author Janne Valkealahti
|
||||
*/
|
||||
public interface ValueProvider {
|
||||
|
||||
boolean supports(MethodParameter parameter, CompletionContext completionContext);
|
||||
|
||||
List<CompletionProposal> complete(MethodParameter parameter, CompletionContext completionContext, String[] hints);
|
||||
/**
|
||||
* Complete completion proposals.
|
||||
*
|
||||
* @param completionContext the context
|
||||
* @return the completion proposals
|
||||
*/
|
||||
List<CompletionProposal> complete(CompletionContext completionContext);
|
||||
}
|
||||
|
||||
@@ -1,38 +0,0 @@
|
||||
/*
|
||||
* Copyright 2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.shell.standard;
|
||||
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.shell.CompletionContext;
|
||||
|
||||
/**
|
||||
* Base class for {@link ValueProvider} that match by type. Subclasses {@literal C} will be selected for parameters
|
||||
* whose {@literal @}{@link ShellOption#valueProvider()} return the concrete class {@literal C}.
|
||||
*
|
||||
* @author Eric Bottard
|
||||
*/
|
||||
public abstract class ValueProviderSupport implements ValueProvider {
|
||||
|
||||
@Override
|
||||
public boolean supports(MethodParameter parameter, CompletionContext completionContext) {
|
||||
ShellOption annotation = parameter.getParameterAnnotation(ShellOption.class);
|
||||
if (annotation == null) {
|
||||
return false;
|
||||
}
|
||||
return annotation.valueProvider().isAssignableFrom(this.getClass());
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2017 the original author or authors.
|
||||
* Copyright 2017-2022 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -13,11 +13,8 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.shell.standard;
|
||||
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
@@ -28,13 +25,10 @@ import org.junit.jupiter.api.Test;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.shell.CompletionContext;
|
||||
import org.springframework.shell.CompletionProposal;
|
||||
import org.springframework.shell.Utils;
|
||||
import org.springframework.shell.command.CommandCatalog;
|
||||
import org.springframework.shell.command.CommandRegistration;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.when;
|
||||
@@ -44,7 +38,7 @@ import static org.mockito.Mockito.when;
|
||||
*
|
||||
* @author Eric Bottard
|
||||
*/
|
||||
public class CommandValueProviderTest {
|
||||
public class CommandValueProviderTests {
|
||||
|
||||
@Mock
|
||||
private CommandCatalog catalog;
|
||||
@@ -58,30 +52,17 @@ public class CommandValueProviderTest {
|
||||
public void testValues() {
|
||||
CommandValueProvider valueProvider = new CommandValueProvider(catalog);
|
||||
|
||||
Method help = ReflectionUtils.findMethod(Command.class, "help", String.class);
|
||||
MethodParameter methodParameter = Utils.createMethodParameter(help, 0);
|
||||
CompletionContext completionContext = new CompletionContext(Arrays.asList("help", "m"), 0, 0);
|
||||
boolean supports = valueProvider.supports(methodParameter, completionContext);
|
||||
CompletionContext completionContext = new CompletionContext(Arrays.asList("help", "m"), 0, 0, null, null);
|
||||
|
||||
assertThat(supports).isEqualTo(true);
|
||||
Map<String, CommandRegistration> registrations = new HashMap<>();
|
||||
registrations.put("me", null);
|
||||
registrations.put("meow", null);
|
||||
registrations.put("yourself", null);
|
||||
|
||||
when(catalog.getRegistrations()).thenReturn(registrations);
|
||||
List<CompletionProposal> proposals = valueProvider.complete(methodParameter, completionContext, new String[0]);
|
||||
List<CompletionProposal> proposals = valueProvider.complete(completionContext);
|
||||
|
||||
assertThat(proposals).extracting("value", String.class)
|
||||
.contains("me", "meow", "yourself");
|
||||
}
|
||||
|
||||
|
||||
public static class Command {
|
||||
|
||||
public void help(@ShellOption(valueProvider = CommandValueProvider.class) String command) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,91 +0,0 @@
|
||||
/*
|
||||
* Copyright 2015 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.shell.standard;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.shell.CompletionContext;
|
||||
import org.springframework.shell.CompletionProposal;
|
||||
|
||||
/**
|
||||
* An example commands class.
|
||||
*
|
||||
* @author Eric Bottard
|
||||
* @author Florent Biville
|
||||
*/
|
||||
public class Remote {
|
||||
|
||||
/**
|
||||
* A command method that showcases<ul>
|
||||
* <li>default handling for booleans (force)</li>
|
||||
* <li>default parameter name discovery (name)</li>
|
||||
* <li>default value supplying (foo and bar)</li>
|
||||
* </ul>
|
||||
*/
|
||||
@ShellMethod(value = "switch channels")
|
||||
public void zap(boolean force,
|
||||
String name,
|
||||
@ShellOption(defaultValue="defoolt") String foo,
|
||||
@ShellOption(value = {"--bar", "--baz"}, defaultValue = "last") String bar) {
|
||||
}
|
||||
|
||||
@ShellMethod(value = "bye bye")
|
||||
public void shutdown(@ShellOption Delay delay) {
|
||||
|
||||
}
|
||||
|
||||
@ShellMethod(value = "a different prefix", prefix = "-")
|
||||
public void prefixTest(@ShellOption String message) {
|
||||
|
||||
}
|
||||
|
||||
@ShellMethod(value = "add 3 numbers together")
|
||||
public void add(@ShellOption(arity = 3, valueProvider = NumberValueProvider.class) List<Integer> numbers) {
|
||||
|
||||
}
|
||||
|
||||
@ShellMethod(value = "add 3 numbers together (array)")
|
||||
public void addAsArray(@ShellOption(arity = 3, valueProvider = NumberValueProvider.class) int[] numbers) {
|
||||
|
||||
}
|
||||
|
||||
public enum Delay {
|
||||
small, medium, big;
|
||||
}
|
||||
|
||||
|
||||
public static class NumberValueProvider extends ValueProviderSupport {
|
||||
|
||||
private final String[] values;
|
||||
|
||||
public NumberValueProvider(String... values) {
|
||||
this.values = values;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<CompletionProposal> complete(MethodParameter parameter, CompletionContext completionContext, String[] hints) {
|
||||
String prefix = completionContext.currentWord() != null ? completionContext.currentWord() : "";
|
||||
return Stream.of(values)
|
||||
.filter(n -> n.startsWith(prefix))
|
||||
.map(CompletionProposal::new)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user