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:
@@ -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()
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -311,6 +311,39 @@ public class ShellTests {
|
||||
assertThat(proposals).containsExactlyInAnyOrder("--arg1");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldCompleteWithCorrectArgument() throws Exception {
|
||||
CommandRegistration registration1 = CommandRegistration.builder()
|
||||
.command("hello world")
|
||||
.withTarget()
|
||||
.method(this, "helloWorld")
|
||||
.and()
|
||||
.withOption()
|
||||
.longNames("arg1")
|
||||
.completion(ctx -> Arrays.asList("arg1Comp1").stream().map(CompletionProposal::new).collect(Collectors.toList()))
|
||||
.and()
|
||||
.withOption()
|
||||
.longNames("arg2")
|
||||
.completion(ctx -> Arrays.asList("arg2Comp1").stream().map(CompletionProposal::new).collect(Collectors.toList()))
|
||||
.and()
|
||||
.build();
|
||||
Map<String, CommandRegistration> registrations = new HashMap<>();
|
||||
registrations.put("hello world", registration1);
|
||||
when(commandRegistry.getRegistrations()).thenReturn(registrations);
|
||||
|
||||
List<String> proposals1 = shell.complete(new CompletionContext(Arrays.asList("hello", "world", "--arg1", ""), 3, "".length(), null, null))
|
||||
.stream().map(CompletionProposal::value).collect(Collectors.toList());
|
||||
assertThat(proposals1).containsExactlyInAnyOrder("--arg2", "arg1Comp1");
|
||||
|
||||
List<String> proposals2 = shell.complete(new CompletionContext(Arrays.asList("hello", "world", "--arg1", "xxx", "--arg2", ""), 5, "".length(), null, null))
|
||||
.stream().map(CompletionProposal::value).collect(Collectors.toList());
|
||||
assertThat(proposals2).containsExactlyInAnyOrder("arg2Comp1");
|
||||
|
||||
List<String> proposals3 = shell.complete(new CompletionContext(Arrays.asList("hello", "world", "--arg2", "xxx", "--arg1", ""), 5, "".length(), null, null))
|
||||
.stream().map(CompletionProposal::value).collect(Collectors.toList());
|
||||
assertThat(proposals3).containsExactlyInAnyOrder("arg1Comp1");
|
||||
}
|
||||
|
||||
private static class Exit extends RuntimeException {
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
/*
|
||||
* Copyright 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.
|
||||
* 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.completion;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.shell.CompletionContext;
|
||||
import org.springframework.shell.CompletionProposal;
|
||||
import org.springframework.shell.command.CommandRegistration;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class RegistrationOptionsCompletionResolverTests {
|
||||
|
||||
private final RegistrationOptionsCompletionResolver resolver = new RegistrationOptionsCompletionResolver();
|
||||
|
||||
@Test
|
||||
void completesAllOptions() {
|
||||
CommandRegistration registration = CommandRegistration.builder()
|
||||
.command("hello world")
|
||||
.withTarget()
|
||||
.consumer(ctx -> {})
|
||||
.and()
|
||||
.withOption()
|
||||
.longNames("arg1")
|
||||
.and()
|
||||
.withOption()
|
||||
.longNames("arg2")
|
||||
.and()
|
||||
.build();
|
||||
CompletionContext ctx = new CompletionContext(Arrays.asList("hello", "world", ""), 2, "".length(), registration, null);
|
||||
List<CompletionProposal> proposals = resolver.apply(ctx);
|
||||
assertThat(proposals).isNotNull();
|
||||
assertThat(proposals).hasSize(2);
|
||||
}
|
||||
|
||||
@Test
|
||||
void completesNonExistingOptions() {
|
||||
CommandRegistration registration = CommandRegistration.builder()
|
||||
.command("hello world")
|
||||
.withTarget()
|
||||
.consumer(ctx -> {})
|
||||
.and()
|
||||
.withOption()
|
||||
.longNames("arg1")
|
||||
.and()
|
||||
.withOption()
|
||||
.longNames("arg2")
|
||||
.and()
|
||||
.build();
|
||||
CompletionContext ctx = new CompletionContext(Arrays.asList("hello", "world", "--arg1", ""), 2, "".length(), registration, null);
|
||||
List<CompletionProposal> proposals = resolver.apply(ctx);
|
||||
assertThat(proposals).isNotNull();
|
||||
assertThat(proposals).hasSize(1);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user