summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorShawn O. Pearce <sop@google.com>2009-02-06 17:31:29 -0800
committerShawn O. Pearce <sop@google.com>2009-02-06 17:31:29 -0800
commitc2e936a81ce2eca1cadbf89aea7874bb8f7b7eae (patch)
tree6fed682b88d5266abe5ce8f23a61822614a7c092
parentb97a109a9cab8dbeb3aab0c89c818307470548df (diff)
Highlight common whitespace errors such as whitespace on end of line
Show trailing whitespace (space and/or tab after the end of line, but before the LF) by displaying it with a red background, so it stands out visually against the rest of the patch. Also show tab-after-space within the leading indentation for the line, as this is an abnormal way to indent a line of code that usually results in badly formatted code. Bug: GERRIT-23 Signed-off-by: Shawn O. Pearce <sop@google.com>
-rw-r--r--src/main/java/com/google/gerrit/client/patches/PatchUtil.java13
-rw-r--r--src/main/java/com/google/gerrit/client/patches/SideBySideTable.java10
-rw-r--r--src/main/java/com/google/gerrit/client/patches/UnifiedDiffTable.java10
-rw-r--r--src/main/java/com/google/gerrit/public/Gerrit.css4
4 files changed, 34 insertions, 3 deletions
diff --git a/src/main/java/com/google/gerrit/client/patches/PatchUtil.java b/src/main/java/com/google/gerrit/client/patches/PatchUtil.java
index 0f7f175a21..1e95950b3f 100644
--- a/src/main/java/com/google/gerrit/client/patches/PatchUtil.java
+++ b/src/main/java/com/google/gerrit/client/patches/PatchUtil.java
@@ -29,10 +29,15 @@ public class PatchUtil {
JsonUtil.bind(DETAIL_SVC, "rpc/PatchDetailService");
}
- public static String lineToHTML(final String src, final int lineLength) {
+ public static String lineToHTML(final String src, final int lineLength,
+ final boolean showWhiteSpaceErrors) {
final boolean hasTab = src.indexOf('\t') >= 0;
String brokenSrc = wrapLines(src, hasTab, lineLength);
String html = DomUtil.escape(brokenSrc);
+ if (showWhiteSpaceErrors) {
+ html = showTabAfterSpace(html);
+ html = showTrailingWhitespace(html);
+ }
if (brokenSrc != src) {
// If we had line breaks inserted into the source text we need
// to expand the line breaks into <br> tags in HTML, so the
@@ -81,4 +86,10 @@ public class PatchUtil {
private native static String expandLFs(String src)
/*-{ return src.replace(/\n/g, '<br>'); }-*/;
+
+ private native static String showTabAfterSpace(String src)
+ /*-{ return src.replace(/^( *\t)/, '<span class="gerrit-whitespaceerror">$1</span>'); }-*/;
+
+ private native static String showTrailingWhitespace(String src)
+ /*-{ return src.replace(/([ \t][ \t]*)(\r?\n?)$/, '<span class="gerrit-whitespaceerror">$1</span>$2'); }-*/;
}
diff --git a/src/main/java/com/google/gerrit/client/patches/SideBySideTable.java b/src/main/java/com/google/gerrit/client/patches/SideBySideTable.java
index 4a09d24897..d051b1bb67 100644
--- a/src/main/java/com/google/gerrit/client/patches/SideBySideTable.java
+++ b/src/main/java/com/google/gerrit/client/patches/SideBySideTable.java
@@ -311,8 +311,16 @@ public class SideBySideTable extends AbstractPatchContentTable {
nc.append(s.getType().name());
nc.append("\">");
if (!"".equals(s.getText())) {
+ boolean showWhitespaceErrors = false;
+ if (fileId == fileCnt - 1
+ && s.getType() == SideBySideLine.Type.INSERT) {
+ // Only show whitespace errors in the last column, and
+ // only if the line is introduced here.
+ //
+ showWhitespaceErrors = true;
+ }
nc.append(PatchUtil.lineToHTML(s.getText(),
- PatchUtil.DEFAULT_LINE_LENGTH));
+ PatchUtil.DEFAULT_LINE_LENGTH, showWhitespaceErrors));
} else {
nc.append("&nbsp;");
}
diff --git a/src/main/java/com/google/gerrit/client/patches/UnifiedDiffTable.java b/src/main/java/com/google/gerrit/client/patches/UnifiedDiffTable.java
index b2d6682c3d..642aa7aa25 100644
--- a/src/main/java/com/google/gerrit/client/patches/UnifiedDiffTable.java
+++ b/src/main/java/com/google/gerrit/client/patches/UnifiedDiffTable.java
@@ -110,7 +110,15 @@ public class UnifiedDiffTable extends AbstractPatchContentTable {
nc.append(line.getType().name());
nc.append("\">");
if (!"".equals(line.getText())) {
- nc.append(PatchUtil.lineToHTML(line.getText(), 0));
+ boolean showWhitespaceErrors = false;
+ switch (line.getType()) {
+ case POST_IMAGE:
+ // Only show whitespace errors if the error was introduced.
+ //
+ showWhitespaceErrors = true;
+ break;
+ }
+ nc.append(PatchUtil.lineToHTML(line.getText(), 0, showWhitespaceErrors));
} else {
nc.append("&nbsp;");
}
diff --git a/src/main/java/com/google/gerrit/public/Gerrit.css b/src/main/java/com/google/gerrit/public/Gerrit.css
index de3a0a11ba..72c2f45adb 100644
--- a/src/main/java/com/google/gerrit/public/Gerrit.css
+++ b/src/main/java/com/google/gerrit/public/Gerrit.css
@@ -53,6 +53,10 @@
font-size: 8pt;
}
+.gerrit-whitespaceerror {
+ background: red;
+}
+
.gerrit-preformat {
white-space: pre;
font-family: monospace;