diff options
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. |