Skip to content

Commit

Permalink
Special handle of "hidden" character in DiffHelper.
Browse files Browse the repository at this point in the history
Specifically, a span of hidden characters has to be handled as a unit:
Since its meaning is opaque, we have to emit all or none.

Also AttributedCharSequence.columnSubSequence should stop before newline.
  • Loading branch information
PerBothner committed Nov 12, 2016
1 parent 25d3a3d commit 2af23c1
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
4 changes: 3 additions & 1 deletion src/main/java/org/jline/utils/AttributedCharSequence.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ int styleCodeAt(int index) {
return styleAt(index).getStyle();
}

boolean isHidden(int index) {
public boolean isHidden(int index) {
return (styleCodeAt(index) & F_HIDDEN) != 0;
}

Expand Down Expand Up @@ -212,6 +212,8 @@ public AttributedString columnSubSequence(int start, int stop) {
int end = begin;
while (end < this.length()) {
int cp = codePointAt(end);
if (cp == '\n')
break;
int w = isHidden(end) ? 0 : WCWidth.wcwidth(cp);
if (col + w > stop) {
break;
Expand Down
30 changes: 25 additions & 5 deletions src/main/java/org/jline/utils/DiffHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,27 +69,48 @@ public String toString() {
* The computation is done on characters and their attributes expressed
* as ansi sequences.
*
* @param text1 the first line
* @param text2 the second line
* @param text1 the old line
* @param text2 the new line
* @return a list of Diff
*/
public static List<Diff> diff(AttributedString text1, AttributedString text2) {
int l1 = text1.length();
int l2 = text2.length();
int n = Math.min(l1, l2);
int commonStart = 0;
// Given a run of contiguous "hidden" characters (which are
// sequences of uninterrupted escape sequences) we always want to
// print either the entire run or none of it - never a part of it.
int startHiddenRange = -1;
while (commonStart < n
&& text1.charAt(commonStart) == text2.charAt(commonStart)
&& text1.styleAt(commonStart).equals(text2.styleAt(commonStart))) {
if (text1.isHidden(commonStart)) {
if (startHiddenRange < 0)
startHiddenRange = commonStart;
} else
startHiddenRange = -1;
commonStart++;
}
if (startHiddenRange >= 0
&& (commonStart == l1 || ! text1.isHidden(commonStart))
&& (commonStart == l2 || ! text2.isHidden(commonStart)))
commonStart = startHiddenRange;

startHiddenRange = -1;
int commonEnd = 0;
while (commonEnd < n - commonStart
&& text1.charAt(l1 - commonEnd - 1) == text2.charAt(l2 - commonEnd - 1)
&& text1.styleAt(l1 - commonEnd - 1).equals(text2.styleAt(l2 - commonEnd - 1))) {
if (text1.isHidden(l1 - commonEnd - 1)) {
if (startHiddenRange < 0)
startHiddenRange = commonEnd;
} else
startHiddenRange = -1;
commonEnd++;
}

if (startHiddenRange >= 0)
commonEnd = startHiddenRange;
LinkedList<Diff> diffs = new LinkedList<>();
if (commonStart > 0) {
diffs.add(new Diff(DiffHelper.Operation.EQUAL,
Expand All @@ -110,5 +131,4 @@ public static List<Diff> diff(AttributedString text1, AttributedString text2) {
return diffs;
}


}
}

0 comments on commit 2af23c1

Please sign in to comment.