-
Notifications
You must be signed in to change notification settings - Fork 394
Improved completion by allowing configuration of whether option values should be completed #1250
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Improved completion by allowing configuration of whether option values should be completed #1250
Conversation
…s should be completed Signed-off-by: czpilar <david@czpilar.net>
|
Hello, I’ve just improved @Option(longName = "first", shortName = 'f', completion = false) String firstI’ve also improved the completion provider so you can control whether a completion proposal should be completed or not, as mentioned in #285. See the following examples. This means completion will behave as follows: package org.springframework.shell.samples.helloworld.boot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.shell.core.command.CommandOption;
import org.springframework.shell.core.command.annotation.Command;
import org.springframework.shell.core.command.annotation.Option;
import org.springframework.shell.core.command.completion.CompletionProposal;
import org.springframework.shell.core.command.completion.CompletionProvider;
import java.util.Collections;
import java.util.stream.Stream;
@SpringBootApplication
public class SpringShellApplication {
public static void main(String[] args) {
SpringApplication.run(SpringShellApplication.class, args);
}
@Command(name = "hello", completionProvider = "helloNameCompletionProvider")
public void sayHello(@Option(longName = "first", shortName = 'f') String first,
@Option(longName = "last", shortName = 'l') String last) {
System.out.println("Hello " + first + " " + last + "!");
}
@Command(name = "hello2", completionProvider = "helloNameCompletionProvider")
public void sayHello2(@Option(longName = "first", shortName = 'f', completion = false) String first,
@Option(longName = "last", shortName = 'l', completion = false) String last) {
System.out.println("Hello2 " + first + " " + last + "!");
}
@Bean
public CompletionProvider helloNameCompletionProvider() {
return completionContext -> {
CommandOption option = completionContext.getCommandOption();
if (option == null) {
return Collections.emptyList();
}
String word = completionContext.getWords().get(completionContext.getWords().size() - 1);
if (word.contains("=")) {
word = word.substring(0, word.indexOf('='));
}
String prefix = word.isEmpty() ? word : word + "=";
Stream<String> options = Stream.empty();
if ("first".equals(option.longName())) {
options = Stream.of("Peter", "Paul", "Mary");
}
else if ("last".equals(option.longName())) {
options = Stream.of("Chan", "Noris");
}
return options.map(str -> new CompletionProposal(prefix + str).displayText(str)).toList();
};
}
}The second example shows how to use a completion provider with completion disabled for its completion proposals. This means completion will behave as follows: In this case, the completion proposals are appended with package org.springframework.shell.samples.helloworld.boot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.shell.core.command.CommandOption;
import org.springframework.shell.core.command.annotation.Command;
import org.springframework.shell.core.command.annotation.Option;
import org.springframework.shell.core.command.completion.CompletionProposal;
import org.springframework.shell.core.command.completion.CompletionProvider;
import java.util.Collections;
import java.util.stream.Stream;
@SpringBootApplication
public class SpringShellApplication {
public static void main(String[] args) {
SpringApplication.run(SpringShellApplication.class, args);
}
@Command(name = "snake", completionProvider = "snakeCompletionProvider")
public void snake(@Option(longName = "first", shortName = 'f', completion = false) String first,
@Option(longName = "last", shortName = 'l', completion = false) String last) {
System.out.println("Snake " + first + " " + last + "!");
}
@Bean
public CompletionProvider snakeCompletionProvider() {
return completionContext -> {
CommandOption option = completionContext.getCommandOption();
if (option == null) {
return Collections.emptyList();
}
String word = completionContext.getWords().get(completionContext.getWords().size() - 1);
String prefix = word.endsWith("=") || word.endsWith("/") ? word : word + "/";
Stream<String> options = Stream.empty();
if ("first".equals(option.longName())) {
options = Stream.of("Peter", "Paul", "Mary");
}
else if ("last".equals(option.longName())) {
options = Stream.of("Chan", "Noris");
}
return options.map(str -> new CompletionProposal(prefix + str).displayText(str).complete(false)).toList();
};
}
}I’ve also added many tests to cover this behavior. As a next step, it could be useful to allow defining a custom separator character instead of just a space or equals sign, but that would require changes to the command parser as well. |
|
@fmbenhassine Please take a look at this PR. |
Improves #1246
Resolves #285