Skip to content

Completion issues #502

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

Closed
fonimus opened this issue Aug 12, 2022 · 1 comment
Closed

Completion issues #502

fonimus opened this issue Aug 12, 2022 · 1 comment
Labels
status/duplicate There were an existing issue

Comments

@fonimus
Copy link

fonimus commented Aug 12, 2022

Hello there, I bumped to the last released version (2.1.0) and I noticed that the completion does not work properly :
the command options are always displayed (even if we want the enum values of specific option or if the option is already given in the command line.

Here is a small example with the getting started example, we can see the option --a is in completion proposal even if we should put option value (here the number) and if it is already in the command.
Capture d’écran 2022-08-12 à 15 46 52

I made the following fix by extended shell in my project (maybe I didn't think of everything so you can probably check this / test it / improve it :

Location : org.springframework.shell.Shell#complete

public List<CompletionProposal> complete(CompletionContext context) {

	String prefix = context.upToCursor();

	List<CompletionProposal> candidates = new ArrayList<>();
	candidates.addAll(commandsStartingWith(prefix));

	String best = findLongestCommand(prefix);
	if (best != null) {
		context = context.drop(best.split(" ").length);
		CommandRegistration registration = commandRegistry.getRegistrations().get(best);
		CompletionContext argsContext = context.commandRegistration(registration);

		// current code
		//for (CompletionResolver resolver : completionResolvers) {
		//	List<CompletionProposal> resolved = resolver.apply(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)));
		//}
		// end of current code


		// new code
		final List<String> words = context.getWords().stream().filter(StringUtils::hasText).collect(Collectors.toList());
                String lastWord = words.isEmpty() ? null : words.get(words.size() - 1);

                List<CommandOption> matchedArgOptions = new ArrayList<>();
                if (lastWord != null) {
                    // last word used instead of first to check if matching args
                    matchedArgOptions.addAll(matchOptions(registration.getOptions(), lastWord));
                }
                if (matchedArgOptions.isEmpty()) {
                    // only add command options if last word did not match option
                    for (CompletionResolver resolver : completionResolvers) {
                        List<CompletionProposal> resolved = resolver.apply(argsContext);
                        candidates.addAll(resolved.stream().filter(cp -> !words.contains(cp.value())).collect(Collectors.toList()));
                    }
                }
		// end of new code

		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;
}
@jvalkeal jvalkeal added the status/duplicate There were an existing issue label Aug 16, 2022
@jvalkeal
Copy link
Contributor

Dup of #495. It's backported for next release 2.1.1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status/duplicate There were an existing issue
Projects
None yet
Development

No branches or pull requests

2 participants