Completion proposal support for complete jline parameter

- to allow completing single argument with multiple tab clicks,
  for example file paths.
- Backport #512
- Fixes #835
This commit is contained in:
MJ Gallego
2023-04-15 16:04:55 +02:00
committed by Janne Valkealahti
parent e2da0d0907
commit 2cb0f115c6
3 changed files with 34 additions and 14 deletions

View File

@@ -41,19 +41,23 @@ public class FileValueProvider implements ValueProvider {
@Override
public List<CompletionProposal> complete(CompletionContext completionContext) {
String input = completionContext.currentWordUpToCursor();
int lastSlash = input.lastIndexOf(File.separatorChar);
Path dir = lastSlash > -1 ? Paths.get(input.substring(0, lastSlash+1)) : Paths.get("");
String prefix = input.substring(lastSlash + 1, input.length());
String input = completionContext.currentWordUpToCursor();
int lastSlash = input.lastIndexOf(File.separatorChar);
Path dir = lastSlash > -1 ? Paths.get(input.substring(0, lastSlash + 1)) : Paths.get("");
String prefix = input.substring(lastSlash + 1);
try {
return Files
.find(dir, 1, (p, a) -> p.getFileName() != null && p.getFileName().toString().startsWith(prefix),
FOLLOW_LINKS)
.map(p -> new CompletionProposal(p.toString()))
.collect(Collectors.toList());
} catch (IOException e) {
throw new UncheckedIOException(e);
}
try {
return Files
.find(dir, 1, (p, a) -> p.getFileName() != null && p.getFileName().toString().startsWith(prefix),
FOLLOW_LINKS)
.map(p -> {
boolean directory = Files.isDirectory(p);
String value = p.toString() + (directory ? File.separatorChar : "");
return new CompletionProposal(value).complete(!directory);
})
.collect(Collectors.toList());
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
}