Skip to content

Commit

Permalink
DefaultParser: enclosed candidate with quotes if it contains delimiter
Browse files Browse the repository at this point in the history
char (escapeChars=null)
  • Loading branch information
mattirn committed Nov 18, 2018
1 parent dc97839 commit 12ad62c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
23 changes: 19 additions & 4 deletions reader/src/main/java/org/jline/reader/impl/DefaultParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,7 @@ public String line() {
public CharSequence escape(CharSequence candidate, boolean complete) {
StringBuilder sb = new StringBuilder(candidate);
Predicate<Integer> needToBeEscaped;
String _openingQuote=null;
if (escapeChars != null) {
// Completion is protected by an opening quote:
// Delimiters (spaces) don't need to be escaped, nor do other quotes, but everything else does.
Expand All @@ -390,15 +391,29 @@ public CharSequence escape(CharSequence candidate, boolean complete) {
sb.insert(i++, escapeChars[0]);
}
}
} else if (openingQuote==null) {
for (int i = 0; i < sb.length(); i++) {
if (isDelimiterChar(sb, i)) {
_openingQuote="'";
break;
}
}

}
if (openingQuote != null) {
sb.insert(0, openingQuote);
if (complete) {
sb.append(openingQuote);
}
encloseQuotes(sb, openingQuote, complete);
} else if (_openingQuote != null) {
encloseQuotes(sb, _openingQuote, complete);
}
return sb;
}

private void encloseQuotes(StringBuilder sb, String openingQuote, boolean complete){
sb.insert(0, openingQuote);
if (complete) {
sb.append(openingQuote);
}
}

@Override
public int rawWordCursor() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,30 @@ public void test1() throws Exception {

@Test
public void escapeCharsNull() throws Exception {
DefaultParser dp = (DefaultParser)reader.getParser();
dp.setEscapeChars(null);
reader.setParser(dp);
reader.setCompleter(new StringsCompleter("foo bar"));

assertBuffer("'foo bar' ", new TestBuffer("f").tab());
assertBuffer("'foo bar' ", new TestBuffer("'f").tab());
// mrn 18/11/2018 IMHO the two bellow are wrong
assertBuffer("'foo bar' ", new TestBuffer("foo'b").tab());
assertBuffer("'foo bar' ", new TestBuffer("'bar'f").tab());
}

@Test
public void escapeChars() throws Exception {
DefaultParser dp = (DefaultParser)reader.getParser();
dp.setEscapeChars(null);
dp.setEscapeChars(new char[]{'\\'});
reader.setParser(dp);
reader.setCompleter(new StringsCompleter("foo bar", "bar"));

assertBuffer("foo bar ", new TestBuffer("f").tab());
assertBuffer("bar ", new TestBuffer("b").tab());
assertBuffer("foo\\ bar ", new TestBuffer("f").tab());
assertBuffer("'bar' ", new TestBuffer("'b").tab());
// mrn 18/11/2018 IMHO the two bellow are wrong
assertBuffer("foo\\ bar ", new TestBuffer("'bar'f").tab());
assertBuffer("'bar' ", new TestBuffer("bar'f").tab());
}

}

0 comments on commit 12ad62c

Please sign in to comment.