Skip to content

Commit

Permalink
Use try-with-resources where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
turbanoff authored and mattirn committed Sep 27, 2021
1 parent 56c2a07 commit ba06531
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 157 deletions.
106 changes: 53 additions & 53 deletions builtins/src/main/java/org/jline/builtins/Less.java
Original file line number Diff line number Diff line change
Expand Up @@ -219,64 +219,64 @@ public Less(Terminal terminal, Path currentDir, Options opts, ConfigurationPath
}

private void parseConfig(Path file) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader(file.toFile()));
String line = reader.readLine();
while (line!= null) {
line = line.trim();
if (line.length() > 0 && !line.startsWith("#")) {
List<String> parts = Parser.split(line);
if (parts.get(0).equals("include")) {
if (parts.get(1).contains("*") || parts.get(1).contains("?")) {
PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher("glob:" + parts.get(1));
Files.find(Paths.get(new File(parts.get(1)).getParent()), Integer.MAX_VALUE, (path, f) -> pathMatcher.matches(path))
.forEach(syntaxFiles::add);
} else {
syntaxFiles.add(Paths.get(parts.get(1)));
}
} else if (parts.size() == 2
&& (parts.get(0).equals("set") || parts.get(0).equals("unset"))) {
String option = parts.get(1);
boolean val = parts.get(0).equals("set");
if (option.equals("QUIT-AT-EOF")) {
quitAtFirstEof = val;
} else if (option.equals("quit-at-eof")) {
quitAtSecondEof = val;
} else if (option.equals("quit-if-one-screen")) {
quitIfOneScreen = val;
} else if (option.equals("quiet") || option.equals("silent")) {
quiet = val;
} else if (option.equals("QUIET") || option.equals("SILENT")) {
veryQuiet = val;
} else if (option.equals("chop-long-lines")) {
chopLongLines = val;
} else if (option.equals("IGNORE-CASE")) {
ignoreCaseAlways = val;
} else if (option.equals("ignore-case")) {
ignoreCaseCond = val;
} else if (option.equals("LINE-NUMBERS")) {
printLineNumbers = val;
} else {
errorMessage = "Less config: Unknown or unsupported configuration option " + option;
}
} else if (parts.size() == 3 && parts.get(0).equals("set")) {
String option = parts.get(1);
String val = parts.get(2);
if (option.equals("tabs")) {
doTabs(val);
} else if (option.equals("historylog")) {
historyLog = val;
try (BufferedReader reader = new BufferedReader(new FileReader(file.toFile()))) {
String line = reader.readLine();
while (line != null) {
line = line.trim();
if (line.length() > 0 && !line.startsWith("#")) {
List<String> parts = Parser.split(line);
if (parts.get(0).equals("include")) {
if (parts.get(1).contains("*") || parts.get(1).contains("?")) {
PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher("glob:" + parts.get(1));
Files.find(Paths.get(new File(parts.get(1)).getParent()), Integer.MAX_VALUE, (path, f) -> pathMatcher.matches(path))
.forEach(syntaxFiles::add);
} else {
syntaxFiles.add(Paths.get(parts.get(1)));
}
} else if (parts.size() == 2
&& (parts.get(0).equals("set") || parts.get(0).equals("unset"))) {
String option = parts.get(1);
boolean val = parts.get(0).equals("set");
if (option.equals("QUIT-AT-EOF")) {
quitAtFirstEof = val;
} else if (option.equals("quit-at-eof")) {
quitAtSecondEof = val;
} else if (option.equals("quit-if-one-screen")) {
quitIfOneScreen = val;
} else if (option.equals("quiet") || option.equals("silent")) {
quiet = val;
} else if (option.equals("QUIET") || option.equals("SILENT")) {
veryQuiet = val;
} else if (option.equals("chop-long-lines")) {
chopLongLines = val;
} else if (option.equals("IGNORE-CASE")) {
ignoreCaseAlways = val;
} else if (option.equals("ignore-case")) {
ignoreCaseCond = val;
} else if (option.equals("LINE-NUMBERS")) {
printLineNumbers = val;
} else {
errorMessage = "Less config: Unknown or unsupported configuration option " + option;
}
} else if (parts.size() == 3 && parts.get(0).equals("set")) {
String option = parts.get(1);
String val = parts.get(2);
if (option.equals("tabs")) {
doTabs(val);
} else if (option.equals("historylog")) {
historyLog = val;
} else {
errorMessage = "Less config: Unknown or unsupported configuration option " + option;
}
} else if (parts.get(0).equals("bind") || parts.get(0).equals("unbind")) {
errorMessage = "Less config: Key bindings can not be changed!";
} else {
errorMessage = "Less config: Unknown or unsupported configuration option " + option;
errorMessage = "Less config: Bad configuration '" + line + "'";
}
} else if (parts.get(0).equals("bind") || parts.get(0).equals("unbind")) {
errorMessage = "Less config: Key bindings can not be changed!";
} else {
errorMessage = "Less config: Bad configuration '" + line + "'";
}
line = reader.readLine();
}
line = reader.readLine();
}
reader.close();
}

private void doTabs(String val) {
Expand Down
184 changes: 92 additions & 92 deletions builtins/src/main/java/org/jline/builtins/Nano.java
Original file line number Diff line number Diff line change
Expand Up @@ -1463,29 +1463,29 @@ public static SyntaxHighlighter build(Path nanorc, String syntaxName) {
SyntaxHighlighter out = new SyntaxHighlighter();
List<Path> syntaxFiles = new ArrayList<>();
try {
BufferedReader reader = new BufferedReader(new FileReader(nanorc.toFile()));
String line = reader.readLine();
while (line != null) {
line = line.trim();
if (line.length() > 0 && !line.startsWith("#")) {
List<String> parts = Parser.split(line);
if (parts.get(0).equals("include")) {
if (parts.get(1).contains("*") || parts.get(1).contains("?")) {
PathMatcher pathMatcher = FileSystems
.getDefault().getPathMatcher("glob:" + parts.get(1));
Files.find(
Paths.get(new File(parts.get(1)).getParent()),
Integer.MAX_VALUE,
(path, f) -> pathMatcher.matches(path))
.forEach(syntaxFiles::add);
} else {
syntaxFiles.add(Paths.get(parts.get(1)));
try (BufferedReader reader = new BufferedReader(new FileReader(nanorc.toFile()))) {
String line = reader.readLine();
while (line != null) {
line = line.trim();
if (line.length() > 0 && !line.startsWith("#")) {
List<String> parts = Parser.split(line);
if (parts.get(0).equals("include")) {
if (parts.get(1).contains("*") || parts.get(1).contains("?")) {
PathMatcher pathMatcher = FileSystems
.getDefault().getPathMatcher("glob:" + parts.get(1));
Files.find(
Paths.get(new File(parts.get(1)).getParent()),
Integer.MAX_VALUE,
(path, f) -> pathMatcher.matches(path))
.forEach(syntaxFiles::add);
} else {
syntaxFiles.add(Paths.get(parts.get(1)));
}
}
}
line = reader.readLine();
}
line = reader.readLine();
}
reader.close();
out = build(syntaxFiles, null, syntaxName);
} catch (Exception e) {
// ignore
Expand Down Expand Up @@ -2043,84 +2043,84 @@ public Nano(Terminal terminal, Path root, Options opts, ConfigurationPath config
}

private void parseConfig(Path file) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader(file.toFile()));
String line = reader.readLine();
while (line!= null) {
line = line.trim();
if (line.length() > 0 && !line.startsWith("#")) {
List<String> parts = Parser.split(line);
if (parts.get(0).equals("include")) {
if (parts.get(1).contains("*") || parts.get(1).contains("?")) {
PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher("glob:" + parts.get(1));
Files.find(Paths.get(new File(parts.get(1)).getParent()), Integer.MAX_VALUE, (path, f) -> pathMatcher.matches(path))
.forEach(syntaxFiles::add);
} else {
syntaxFiles.add(Paths.get(parts.get(1)));
}
} else if (parts.size() == 2
&& (parts.get(0).equals("set") || parts.get(0).equals("unset"))) {
String option = parts.get(1);
boolean val = parts.get(0).equals("set");
if (option.equals("linenumbers")) {
printLineNumbers = val;
} else if (option.equals("jumpyscrolling")) {
smoothScrolling = !val;
} else if (option.equals("smooth")) {
smoothScrolling = val;
} else if (option.equals("softwrap")) {
wrapping = val;
} else if (option.equals("mouse")) {
mouseSupport = val;
} else if (option.equals("emptyline")) {
oneMoreLine = val;
} else if (option.equals("morespace")) {
oneMoreLine = !val;
} else if (option.equals("constantshow")) {
constantCursor = val;
} else if (option.equals("quickblank")) {
quickBlank = val;
} else if (option.equals("atblanks")) {
atBlanks = val;
} else if (option.equals("suspend")) {
enableSuspension();
} else if (option.equals("view")) {
view = val;
} else if (option.equals("cutfromcursor")) {
cut2end = val;
} else if (option.equals("tempfile")) {
tempFile = val;
} else if (option.equals("tabstospaces")) {
tabsToSpaces = val;
} else if (option.equals("autoindent")) {
autoIndent = val;
} else {
errorMessage = "Nano config: Unknown or unsupported configuration option " + option;
}
} else if (parts.size() == 3 && parts.get(0).equals("set")) {
String option = parts.get(1);
String val = parts.get(2);
if (option.equals("quotestr")) {
quoteStr = val;
} else if (option.equals("punct")) {
punct = val;
} else if (option.equals("matchbrackets")) {
matchBrackets = val;
} else if (option.equals("brackets")) {
brackets = val;
} else if (option.equals("historylog")) {
historyLog = val;
try (BufferedReader reader = new BufferedReader(new FileReader(file.toFile()))) {
String line = reader.readLine();
while (line != null) {
line = line.trim();
if (line.length() > 0 && !line.startsWith("#")) {
List<String> parts = Parser.split(line);
if (parts.get(0).equals("include")) {
if (parts.get(1).contains("*") || parts.get(1).contains("?")) {
PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher("glob:" + parts.get(1));
Files.find(Paths.get(new File(parts.get(1)).getParent()), Integer.MAX_VALUE, (path, f) -> pathMatcher.matches(path))
.forEach(syntaxFiles::add);
} else {
syntaxFiles.add(Paths.get(parts.get(1)));
}
} else if (parts.size() == 2
&& (parts.get(0).equals("set") || parts.get(0).equals("unset"))) {
String option = parts.get(1);
boolean val = parts.get(0).equals("set");
if (option.equals("linenumbers")) {
printLineNumbers = val;
} else if (option.equals("jumpyscrolling")) {
smoothScrolling = !val;
} else if (option.equals("smooth")) {
smoothScrolling = val;
} else if (option.equals("softwrap")) {
wrapping = val;
} else if (option.equals("mouse")) {
mouseSupport = val;
} else if (option.equals("emptyline")) {
oneMoreLine = val;
} else if (option.equals("morespace")) {
oneMoreLine = !val;
} else if (option.equals("constantshow")) {
constantCursor = val;
} else if (option.equals("quickblank")) {
quickBlank = val;
} else if (option.equals("atblanks")) {
atBlanks = val;
} else if (option.equals("suspend")) {
enableSuspension();
} else if (option.equals("view")) {
view = val;
} else if (option.equals("cutfromcursor")) {
cut2end = val;
} else if (option.equals("tempfile")) {
tempFile = val;
} else if (option.equals("tabstospaces")) {
tabsToSpaces = val;
} else if (option.equals("autoindent")) {
autoIndent = val;
} else {
errorMessage = "Nano config: Unknown or unsupported configuration option " + option;
}
} else if (parts.size() == 3 && parts.get(0).equals("set")) {
String option = parts.get(1);
String val = parts.get(2);
if (option.equals("quotestr")) {
quoteStr = val;
} else if (option.equals("punct")) {
punct = val;
} else if (option.equals("matchbrackets")) {
matchBrackets = val;
} else if (option.equals("brackets")) {
brackets = val;
} else if (option.equals("historylog")) {
historyLog = val;
} else {
errorMessage = "Nano config: Unknown or unsupported configuration option " + option;
}
} else if (parts.get(0).equals("bind") || parts.get(0).equals("unbind")) {
errorMessage = "Nano config: Key bindings can not be changed!";
} else {
errorMessage = "Nano config: Unknown or unsupported configuration option " + option;
errorMessage = "Nano config: Bad configuration '" + line + "'";
}
} else if (parts.get(0).equals("bind") || parts.get(0).equals("unbind")) {
errorMessage = "Nano config: Key bindings can not be changed!";
} else {
errorMessage = "Nano config: Bad configuration '" + line + "'";
}
line = reader.readLine();
}
line = reader.readLine();
}
reader.close();
}

public void setRestricted(boolean restricted) {
Expand Down
6 changes: 3 additions & 3 deletions demo/src/main/java/org/jline/demo/Repl.java
Original file line number Diff line number Diff line change
Expand Up @@ -260,9 +260,9 @@ public static void main(String[] args) {
String root = file.getCanonicalPath().replace("classes", "").replaceAll("\\\\", "/"); // forward slashes works better also in windows!
File jnanorcFile = Paths.get(root, "jnanorc").toFile();
if (!jnanorcFile.exists()) {
FileWriter fw = new FileWriter(jnanorcFile);
fw.write("include " + root + "nanorc/*.nanorc\n");
fw.close();
try (FileWriter fw = new FileWriter(jnanorcFile)) {
fw.write("include " + root + "nanorc/*.nanorc\n");
}
}
//
// ScriptEngine and command registries
Expand Down

0 comments on commit ba06531

Please sign in to comment.