Skip to content

Commit

Permalink
Escaped new lines should not be copied into the resulting buffer, fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet committed Dec 5, 2016
1 parent a2919c5 commit 1970ed7
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
13 changes: 13 additions & 0 deletions src/main/java/org/jline/reader/impl/DefaultParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public class DefaultParser implements Parser {

private boolean eofOnUnclosedQuote;

private boolean eofOnEscapedNewLine;

public void setQuoteChars(final char[] chars) {
this.quoteChars = chars;
}
Expand All @@ -50,6 +52,14 @@ public boolean isEofOnUnclosedQuote() {
return eofOnUnclosedQuote;
}

public void setEofOnEscapedNewLine(boolean eofOnEscapedNewLine) {
this.eofOnEscapedNewLine = eofOnEscapedNewLine;
}

public boolean isEofOnEscapedNewLine() {
return eofOnEscapedNewLine;
}

public ParsedLine parse(final String line, final int cursor, ParseContext context) {
List<String> words = new LinkedList<>();
StringBuilder current = new StringBuilder();
Expand Down Expand Up @@ -103,6 +113,9 @@ public ParsedLine parse(final String line, final int cursor, ParseContext contex
wordCursor = words.get(words.size() - 1).length();
}

if (eofOnEscapedNewLine && isEscapeChar(line, line.length() - 1)) {
throw new EOFError(-1, -1, "Escaped new line", "newline");
}
if (eofOnUnclosedQuote && quoteStart >= 0) {
throw new EOFError(-1, -1, "Missing closing quote", line.charAt(quoteStart) == '\''
? "quote" : "dquote");
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/org/jline/reader/impl/LineReaderImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -801,7 +801,9 @@ protected String finishBuffer() {
char ch = str.charAt(i);
if (escaped) {
escaped = false;
sb.append(ch);
if (ch != '\n') {
sb.append(ch);
}
} else if (ch == '\\') {
escaped = true;
} else {
Expand Down
11 changes: 11 additions & 0 deletions src/test/java/org/jline/reader/impl/EditLineTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -200,4 +200,15 @@ public void testAbortPartialBuffer() throws Exception {
assertConsoleOutputContains("\n");
assertBeeped();
}

@Test
public void testEscapeNewLine() throws Exception {
boolean prev = ((DefaultParser) reader.getParser()).isEofOnEscapedNewLine();
((DefaultParser) reader.getParser()).setEofOnEscapedNewLine(true);
try {
assertLine("echo foobar", new TestBuffer("echo foo\\").enter().append("bar").enter());
} finally {
((DefaultParser) reader.getParser()).setEofOnEscapedNewLine(prev);
}
}
}

0 comments on commit 1970ed7

Please sign in to comment.