Skip to content

Commit

Permalink
Improved PipelineCompleter
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Jun 30, 2020
1 parent c0f0e79 commit 99e5efe
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ public Completer completer() {
completers.add(customAggregateCompleter);
if (consoleId != null) {
completers.addAll(consoleEngine().scriptCompleters());
completers.add(new PipelineCompleter().doCompleter());
completers.add(new PipelineCompleter(workDir, pipeName, names).doCompleter());
}
return new AggregateCompleter(completers);
}
Expand Down Expand Up @@ -868,6 +868,12 @@ private boolean isEnclosed() {
return round == 0 && curly == 0 && square == 0 && !quoted && !doubleQuoted;
}

public boolean isEnclosed(String arg) {
reset();
next(arg);
return isEnclosed();
}

private void enclosedArgs(List<String> words) {
args = new ArrayList<>();
reset();
Expand Down Expand Up @@ -1595,9 +1601,16 @@ private int registryId(String command) {
return -1;
}

private class PipelineCompleter implements Completer {
private static class PipelineCompleter implements Completer {
private NamesAndValues names;
private Supplier<Path> workDir;
private Map<Pipe, String> pipeName = new HashMap<>();

public PipelineCompleter() {}
public PipelineCompleter(Supplier<Path> workDir, Map<Pipe, String> pipeName, NamesAndValues names) {
this.workDir = workDir;
this.pipeName = pipeName;
this.names = names;
}

public Completer doCompleter() {
ArgumentCompleter out = new ArgumentCompleter(this);
Expand All @@ -1609,15 +1622,19 @@ public Completer doCompleter() {
public void complete(LineReader reader, ParsedLine commandLine, List<Candidate> candidates) {
assert commandLine != null;
assert candidates != null;
if (commandLine.wordIndex() < 2 || !names.hasPipes(commandLine.words())) {
ArgsParser ap = new ArgsParser(reader.getParser());
ap.parse(commandLine.line().substring(0, commandLine.cursor()));
List<String> args = ap.args();
if (args.size() < 2 || !names.hasPipes(args)) {
return;
}
boolean enclosed = ap.isEnclosed(args.get(args.size() - 1));
String pWord = commandLine.words().get(commandLine.wordIndex() - 1);
if (pWord.equals(pipeName.get(Pipe.NAMED))) {
if (enclosed && pWord.equals(pipeName.get(Pipe.NAMED))) {
for (String name : names.namedPipes()) {
candidates.add(new Candidate(name, name, null, null, null, null, true));
}
} else if (pWord.equals(">") || pWord.equals(">>")) {
} else if (enclosed && pWord.equals(">") || pWord.equals(">>")) {
Completer c = new FilesCompleter(workDir);
c.complete(reader, commandLine, candidates);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ public ParsedLine parse(final String line, final int cursor, ParseContext contex
rawWordLength = rawWordCursor;
}

if (context != ParseContext.COMPLETE) {
if (context != ParseContext.COMPLETE && context != ParseContext.SPLIT_LINE) {
if (eofOnEscapedNewLine && isEscapeChar(line, line.length() - 1)) {
throw new EOFError(-1, -1, "Escaped new line", "newline");
}
Expand Down

0 comments on commit 99e5efe

Please sign in to comment.