Skip to content

Commit

Permalink
FileNameCompleter catch and ignore all exceptions, fixes #453
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Oct 30, 2019
1 parent da89fc4 commit dd8098b
Showing 1 changed file with 37 additions and 33 deletions.
70 changes: 37 additions & 33 deletions builtins/src/main/java/org/jline/builtins/Completers.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2002-2018, the original author or authors.
* Copyright (c) 2002-2019, the original author or authors.
*
* This software is distributable under the BSD license. See the terms of the
* BSD license in the documentation provided with this software.
Expand Down Expand Up @@ -219,7 +219,7 @@ private boolean isTrue(Object result) {
public static class DirectoriesCompleter extends FileNameCompleter {

private final Supplier<Path> currentDir;
private final boolean forceSlash;
private final boolean forceSlash;

public DirectoriesCompleter(File currentDir) {
this(currentDir.toPath(), false);
Expand All @@ -235,7 +235,7 @@ public DirectoriesCompleter(Path currentDir) {

public DirectoriesCompleter(Path currentDir, boolean forceSlash) {
this.currentDir = () -> currentDir;
this.forceSlash = forceSlash;
this.forceSlash = forceSlash;
}

public DirectoriesCompleter(Supplier<Path> currentDir) {
Expand All @@ -255,7 +255,7 @@ protected Path getUserDir() {
@Override
protected String getSeparator() {
return forceSlash ? "/" : getUserDir().getFileSystem().getSeparator();
}
}

@Override
protected boolean accept(Path path) {
Expand All @@ -266,7 +266,7 @@ protected boolean accept(Path path) {
public static class FilesCompleter extends FileNameCompleter {

private final Supplier<Path> currentDir;
private final boolean forceSlash;
private final boolean forceSlash;

public FilesCompleter(File currentDir) {
this(currentDir.toPath(), false);
Expand Down Expand Up @@ -298,11 +298,11 @@ public FilesCompleter(Supplier<Path> currentDir, boolean forceSlash) {
protected Path getUserDir() {
return currentDir.get();
}

@Override
protected String getSeparator() {
return forceSlash ? "/" : getUserDir().getFileSystem().getSeparator();
}
}
}

/**
Expand Down Expand Up @@ -337,35 +337,39 @@ public void complete(LineReader reader, ParsedLine commandLine, final List<Candi
String curBuf;
String sep = getSeparator();
int lastSep = buffer.lastIndexOf(sep);
if (lastSep >= 0) {
curBuf = buffer.substring(0, lastSep + 1);
if (curBuf.startsWith("~")) {
if (curBuf.startsWith("~" + sep)) {
current = getUserHome().resolve(curBuf.substring(2));
try {
if (lastSep >= 0) {
curBuf = buffer.substring(0, lastSep + 1);
if (curBuf.startsWith("~")) {
if (curBuf.startsWith("~" + sep)) {
current = getUserHome().resolve(curBuf.substring(2));
} else {
current = getUserHome().getParent().resolve(curBuf.substring(1));
}
} else {
current = getUserHome().getParent().resolve(curBuf.substring(1));
current = getUserDir().resolve(curBuf);
}
} else {
current = getUserDir().resolve(curBuf);
curBuf = "";
current = getUserDir();
}
} else {
curBuf = "";
current = getUserDir();
}
try (DirectoryStream<Path> directory = Files.newDirectoryStream(current, this::accept)) {
directory.forEach(p -> {
String value = curBuf + p.getFileName().toString();
if (Files.isDirectory(p)) {
candidates.add(
new Candidate(value + (reader.isSet(LineReader.Option.AUTO_PARAM_SLASH) ? sep : ""),
getDisplay(reader.getTerminal(), p), null, null,
reader.isSet(LineReader.Option.AUTO_REMOVE_SLASH) ? sep : null, null, false));
} else {
candidates.add(new Candidate(value, getDisplay(reader.getTerminal(), p), null, null, null, null,
true));
}
});
} catch (IOException e) {
try (DirectoryStream<Path> directory = Files.newDirectoryStream(current, this::accept)) {
directory.forEach(p -> {
String value = curBuf + p.getFileName().toString();
if (Files.isDirectory(p)) {
candidates.add(
new Candidate(value + (reader.isSet(LineReader.Option.AUTO_PARAM_SLASH) ? sep : ""),
getDisplay(reader.getTerminal(), p), null, null,
reader.isSet(LineReader.Option.AUTO_REMOVE_SLASH) ? sep : null, null, false));
} else {
candidates.add(new Candidate(value, getDisplay(reader.getTerminal(), p), null, null, null, null,
true));
}
});
} catch (IOException e) {
// Ignore
}
} catch (Exception e) {
// Ignore
}
}
Expand All @@ -385,7 +389,7 @@ protected Path getUserDir() {
protected Path getUserHome() {
return Paths.get(System.getProperty("user.home"));
}

protected String getSeparator() {
return getUserDir().getFileSystem().getSeparator();
}
Expand Down

0 comments on commit dd8098b

Please sign in to comment.