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:
Janne Valkealahti
2022-06-28 10:03:50 +01:00
parent 341a69e6e0
commit 5eaa5dd093
18 changed files with 364 additions and 247 deletions

View File

@@ -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) {
}
}
}

View File

@@ -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());
}
}
}