Skip to content

Commit c7127e3

Browse files
committed
Add support for command completion
1 parent d893a3d commit c7127e3

File tree

3 files changed

+58
-3
lines changed

3 files changed

+58
-3
lines changed

spring-shell-autoconfigure/src/main/java/org/springframework/shell/boot/JLineShellAutoConfiguration.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import org.springframework.shell.core.command.Command;
4949
import org.springframework.shell.core.command.CommandRegistry;
5050
import org.springframework.shell.core.config.UserConfigPathProvider;
51+
import org.springframework.shell.jline.CommandCompleter;
5152
import org.springframework.shell.jline.ExtendedDefaultParser;
5253
import org.springframework.shell.jline.JLineInputProvider;
5354
import org.springframework.shell.jline.PromptProvider;
@@ -88,10 +89,12 @@ public void onContextClosedEvent(ContextClosedEvent event) throws IOException {
8889
}
8990

9091
@Bean
91-
public LineReader lineReader(Terminal terminal, Parser parser, CommandRegistry commandRegistry) {
92+
public LineReader lineReader(Terminal terminal, Parser parser, CommandCompleter commandCompleter,
93+
CommandRegistry commandRegistry) {
9294
LineReaderBuilder lineReaderBuilder = LineReaderBuilder.builder()
9395
.terminal(terminal)
9496
.appName("Spring Shell")
97+
.completer(commandCompleter)
9598
.history(jLineHistory)
9699
.highlighter(new Highlighter() {
97100

@@ -179,6 +182,12 @@ public Parser parser() {
179182
return parser;
180183
}
181184

185+
@Bean
186+
@ConditionalOnMissingBean
187+
public CommandCompleter commandCompleter(CommandRegistry commandRegistry) {
188+
return new CommandCompleter(commandRegistry);
189+
}
190+
182191
@Configuration
183192
@ConditionalOnMissingBean(org.jline.reader.History.class)
184193
public static class JLineHistoryConfiguration {
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package org.springframework.shell.jline;
2+
3+
import java.util.List;
4+
import java.util.Set;
5+
6+
import org.jline.reader.Candidate;
7+
import org.jline.reader.Completer;
8+
import org.jline.reader.LineReader;
9+
import org.jline.reader.ParsedLine;
10+
11+
import org.springframework.shell.core.command.Command;
12+
import org.springframework.shell.core.command.CommandRegistry;
13+
14+
/**
15+
* A JLine {@link Completer} that completes command names from a {@link CommandRegistry}.
16+
*
17+
* @author Mahmoud Ben Hassine
18+
* @since 4.0.0
19+
*/
20+
public class CommandCompleter implements Completer {
21+
22+
private final CommandRegistry commandRegistry;
23+
24+
/**
25+
* Create a new {@link CommandCompleter} instance.
26+
* @param commandRegistry the command registry
27+
*/
28+
public CommandCompleter(CommandRegistry commandRegistry) {
29+
this.commandRegistry = commandRegistry;
30+
}
31+
32+
@Override
33+
public void complete(LineReader reader, ParsedLine line, List<Candidate> candidates) {
34+
Set<Command> commands = this.commandRegistry.getCommands();
35+
for (Command command : commands) {
36+
candidates.add(new Candidate(command.getName(), command.getName() + ": " + command.getDescription(),
37+
command.getGroup(), command.getHelp(), null, null, true));
38+
}
39+
}
40+
41+
}

spring-shell-jline/src/main/java/org/springframework/shell/jline/DefaultJLineShellConfiguration.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,13 @@ public JLineInputProvider inputProvider(LineReader lineReader) {
2828
}
2929

3030
@Bean
31-
public LineReader lineReader(Terminal terminal) {
32-
return LineReaderBuilder.builder().terminal(terminal).build();
31+
public LineReader lineReader(Terminal terminal, CommandCompleter commandCompleter) {
32+
return LineReaderBuilder.builder().terminal(terminal).completer(commandCompleter).build();
33+
}
34+
35+
@Bean
36+
public CommandCompleter commandCompleter(CommandRegistry commandRegistry) {
37+
return new CommandCompleter(commandRegistry);
3338
}
3439

3540
@Bean

0 commit comments

Comments
 (0)