Skip to content

Commit

Permalink
fixed middle quoted word escape rules
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Dec 9, 2018
1 parent 4244c6a commit b8dd35d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
21 changes: 16 additions & 5 deletions reader/src/main/java/org/jline/reader/impl/DefaultParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -371,13 +371,27 @@ public CharSequence escape(CharSequence candidate, boolean complete) {
StringBuilder sb = new StringBuilder(candidate);
Predicate<Integer> needToBeEscaped;
String quote = openingQuote;
boolean middleQuotes = false;
if (openingQuote==null) {
for (int i=0; i < sb.length(); i++) {
if (isQuoteChar(sb, i)) {
middleQuotes = true;
break;
}
}
}
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.
// Also, close the quote at the end
if (openingQuote != null) {
needToBeEscaped = i -> isRawEscapeChar(sb.charAt(i)) || String.valueOf(sb.charAt(i)).equals(openingQuote);
}
// Completion is protected by middle quotes:
// Delimiters (spaces) don't need to be escaped, nor do quotes, but everything else does.
else if (middleQuotes) {
needToBeEscaped = i -> isRawEscapeChar(sb.charAt(i));
}
// No quote protection, need to escape everything: delimiter chars (spaces), quote chars
// and escapes themselves
else {
Expand All @@ -388,14 +402,11 @@ public CharSequence escape(CharSequence candidate, boolean complete) {
sb.insert(i++, escapeChars[0]);
}
}
} else if (openingQuote == null) {
} else if (openingQuote == null && !middleQuotes) {
for (int i = 0; i < sb.length(); i++) {
if (isQuoteChar(sb, i)) {
quote = null;
break;
}
if (isDelimiterChar(sb, i)) {
quote = "'";
break;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public void middleQuotesEscapeChars() throws Exception {
reader.setParser(dp);
reader.setCompleter(new StringsCompleter("/foo?name='foo bar'","/foo?name='foo qux'"));

assertBuffer("/foo?name=\\'foo\\ ", new TestBuffer("/f").tab());
assertBuffer("/foo?name=\\'foo\\ bar\\' ", new TestBuffer("/foo?name='foo b").tab());
assertBuffer("/foo?name='foo ", new TestBuffer("/f").tab());
assertBuffer("/foo?name='foo bar' ", new TestBuffer("/foo?name='foo b").tab());
}
}

0 comments on commit b8dd35d

Please # to comment.