diff options
author | Shawn O. Pearce <sop@google.com> | 2010-03-09 18:46:00 -0800 |
---|---|---|
committer | Shawn O. Pearce <sop@google.com> | 2010-03-09 18:46:05 -0800 |
commit | 71b04c00b174b056ed2579683e2c1546d156b75a (patch) | |
tree | f67b96be8366422ba5780e5f70363e967bee38e6 | |
parent | 4a25883456711c2348885dba787e9947bb72ac07 (diff) |
Handle bare CR in the middle of a linev2.1.2-rc3
Prettify converts a bare CR in the middle of a line of text into a
<br/>, which we later convert back into an LF. This skews all of the
line numbers through the rest of the file, resulting in a confusing
situation (due to the unexpected LF) as we try to map the formatted
HTML back into the SparseFileContent object we use for rendering.
Highlight bare CRs as spaces with whitespace error spans around them,
before we feed the text into prettify. This ensures we won't get
back more lines than we fed in, so our formatting is still correct.
Change-Id: Ib2dc8b024a2211601a588fd0552c4963d5b6e028
Signed-off-by: Shawn O. Pearce <sop@google.com>
3 files changed, 43 insertions, 6 deletions
diff --git a/gerrit-prettify/src/main/java/com/google/gerrit/prettify/common/PrettifyConstants.java b/gerrit-prettify/src/main/java/com/google/gerrit/prettify/common/PrettifyConstants.java index 346688e767..e14063a1d1 100644 --- a/gerrit-prettify/src/main/java/com/google/gerrit/prettify/common/PrettifyConstants.java +++ b/gerrit-prettify/src/main/java/com/google/gerrit/prettify/common/PrettifyConstants.java @@ -22,4 +22,5 @@ public interface PrettifyConstants extends Constants { String wseTabAfterSpace(); String wseTrailingSpace(); + String wseBareCR(); } diff --git a/gerrit-prettify/src/main/java/com/google/gerrit/prettify/common/PrettifyConstants.properties b/gerrit-prettify/src/main/java/com/google/gerrit/prettify/common/PrettifyConstants.properties index 5cb864b234..d440c65c55 100644 --- a/gerrit-prettify/src/main/java/com/google/gerrit/prettify/common/PrettifyConstants.properties +++ b/gerrit-prettify/src/main/java/com/google/gerrit/prettify/common/PrettifyConstants.properties @@ -1,2 +1,3 @@ wseTabAfterSpace=Whitespace error: Tab after space wseTrailingSpace=Whitespace error: Trailing space at end of line +wseBareCR=Whitespace error: CR without LF diff --git a/gerrit-prettify/src/main/java/com/google/gerrit/prettify/common/PrettyFormatter.java b/gerrit-prettify/src/main/java/com/google/gerrit/prettify/common/PrettyFormatter.java index c5a8b2763d..4406477ca7 100644 --- a/gerrit-prettify/src/main/java/com/google/gerrit/prettify/common/PrettyFormatter.java +++ b/gerrit-prettify/src/main/java/com/google/gerrit/prettify/common/PrettyFormatter.java @@ -314,6 +314,11 @@ public abstract class PrettyFormatter implements SparseHtmlFile { b.append('\n'); } html = b; + + final String r = "<span class=\"wse\"" // + + " title=\"" + PrettifyConstants.C.wseBareCR() + "\"" // + + "> </span>$1"; + html = html.replaceAll("\r([^\n])", r); } if (settings.isShowWhiteSpaceErrors()) { @@ -363,7 +368,7 @@ public abstract class PrettyFormatter implements SparseHtmlFile { if (cmp < 0) { // index occurs before the edit. This is a line of context. // - buf.append(src.get(index)); + appendShowBareCR(buf, src.get(index), true); buf.append('\n'); continue; } @@ -378,10 +383,10 @@ public abstract class PrettyFormatter implements SparseHtmlFile { lastIdx = 0; } - final String line = src.get(index) + "\n"; + String line = src.get(index) + "\n"; for (int c = 0; c < line.length();) { if (charEdits.size() <= lastIdx) { - buf.append(line.substring(c)); + appendShowBareCR(buf, line.substring(c), false); break; } @@ -396,7 +401,8 @@ public abstract class PrettyFormatter implements SparseHtmlFile { final int cmnLen = Math.min(b, line.length()); buf.openSpan(); buf.setStyleName("wdc"); - buf.append(line.substring(c, cmnLen)); + appendShowBareCR(buf, line.substring(c, cmnLen), // + cmnLen == line.length() - 1); buf.closeSpan(); c = cmnLen; } @@ -405,7 +411,8 @@ public abstract class PrettyFormatter implements SparseHtmlFile { if (c < e && c < modLen) { buf.openSpan(); buf.setStyleName(side.getStyleName()); - buf.append(line.substring(c, modLen)); + appendShowBareCR(buf, line.substring(c, modLen), // + modLen == line.length() - 1); buf.closeSpan(); if (modLen == line.length()) { trailingEdits.add(index); @@ -420,13 +427,41 @@ public abstract class PrettyFormatter implements SparseHtmlFile { lastPos += line.length(); } else { - buf.append(src.get(index)); + appendShowBareCR(buf, src.get(index), true); buf.append('\n'); } } return buf; } + private void appendShowBareCR(SafeHtmlBuilder buf, String src, boolean end) { + while (!src.isEmpty()) { + int cr = src.indexOf('\r'); + if (cr < 0) { + buf.append(src); + return; + + } else if (end) { + if (cr == src.length() - 1) { + buf.append(src.substring(0, cr)); + return; + } + } else if (cr == src.length() - 2 && src.charAt(cr + 1) == '\n') { + buf.append(src.substring(0, cr)); + buf.append('\n'); + return; + } + + buf.append(src.substring(0, cr)); + buf.openSpan(); + buf.setStyleName("wse"); + buf.setAttribute("title", PrettifyConstants.C.wseBareCR()); + buf.nbsp(); + buf.closeSpan(); + src = src.substring(cr + 1); + } + } + private int compare(int index, Edit edit) { if (index < side.getBegin(edit)) { return -1; // index occurs before the edit. |