Skip to content

Commit

Permalink
Completion proposal support for complete jline parameter
Browse files Browse the repository at this point in the history
- to allow completing single argument with multiple tab clicks,
  for example file paths.
- Backport #512
- Fixes #834
  • Loading branch information
MJ Gallego authored and jvalkeal committed Jul 25, 2023
1 parent 1350f61 commit aa74ea2
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public void complete(LineReader reader, ParsedLine line, List<Candidate> candida
p.description(),
null,
null,
true)
p.complete())
)
.forEach(candidates::add);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ public class CompletionProposal {
*/
private boolean dontQuote = false;

/**
* Whether the proposal cant be completed further. By setting complete to false then it will not append an space
* making it easier to continue tab completion
*/
private boolean complete = true;

public CompletionProposal(String value) {
this.value = this.displayText = value;
}
Expand Down Expand Up @@ -89,6 +95,16 @@ public CompletionProposal category(String category) {
return this;
}

public CompletionProposal complete(boolean complete) {
this.complete = complete;
return this;
}

public boolean complete() {
return complete;
}


public CompletionProposal dontQuote(boolean dontQuote) {
this.dontQuote = dontQuote;
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}

0 comments on commit aa74ea2

Please sign in to comment.