Skip to content

Option for auto-completing single argument without spaces insertion (complete support) #715

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -16,6 +16,9 @@

package org.springframework.shell.standard;

import org.springframework.shell.CompletionContext;
import org.springframework.shell.CompletionProposal;

import java.io.File;
import java.io.IOException;
import java.io.UncheckedIOException;
Expand All @@ -25,9 +28,6 @@
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.shell.CompletionContext;
import org.springframework.shell.CompletionProposal;

import static java.nio.file.FileVisitOption.FOLLOW_LINKS;

/**
Expand All @@ -39,21 +39,25 @@
*/
public class FileValueProvider implements ValueProvider {

@Override
public List<CompletionProposal> complete(CompletionContext completionContext) {
@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());
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());
.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);
}
}
}
}