Apply correct completion

- This commit fixes two issues.
- Firstly complete with correct option as existing bug was
  to wrongly always complete with first option which used
  wrong provider.
- Secondly filter out duplicate option proposals giving better
  result when options is already in place.
- Fixes #495
This commit is contained in:
Janne Valkealahti
2022-08-04 08:55:58 +01:00
parent 23194d05e2
commit a143d25ffc
5 changed files with 220 additions and 4 deletions

View File

@@ -288,8 +288,8 @@ public class Shell {
// Try to complete arguments
List<CommandOption> matchedArgOptions = new ArrayList<>();
if (argsContext.getWords().size() > 0) {
matchedArgOptions.addAll(matchOptions(registration.getOptions(), argsContext.getWords().get(0)));
if (argsContext.getWords().size() > 0 && argsContext.getWordIndex() > 0 && argsContext.getWords().size() > argsContext.getWordIndex()) {
matchedArgOptions.addAll(matchOptions(registration.getOptions(), argsContext.getWords().get(argsContext.getWordIndex() - 1)));
}
List<CompletionProposal> argProposals = matchedArgOptions.stream()

View File

@@ -38,11 +38,15 @@ public class RegistrationOptionsCompletionResolver implements CompletionResolver
List<CompletionProposal> candidates = new ArrayList<>();
context.getCommandRegistration().getOptions().stream()
.flatMap(o -> Stream.of(o.getLongNames()))
.map(ln -> new CompletionProposal("--" + ln))
.map(ln -> "--" + ln)
.filter(ln -> !context.getWords().contains(ln))
.map(CompletionProposal::new)
.forEach(candidates::add);
context.getCommandRegistration().getOptions().stream()
.flatMap(o -> Stream.of(o.getShortNames()))
.map(ln -> new CompletionProposal("-" + ln))
.map(ln -> "-" + ln)
.filter(ln -> !context.getWords().contains(ln))
.map(CompletionProposal::new)
.forEach(candidates::add);
return candidates;
}