summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorShawn O. Pearce <sop@google.com>2009-07-18 17:23:06 -0700
committerShawn O. Pearce <sop@google.com>2009-07-18 17:23:06 -0700
commit47575135d1a33164ec1f5fc1d5a7947d4bc33c2c (patch)
treef0dd6d81a3fbf76e0328b68c4e3485c81d31e8c3
parenta5317ec8b477f9ad4a09a958e42d5180349b22f0 (diff)
Apply syntax highlighting when showing file content
Bug: 228 Signed-off-by: Shawn O. Pearce <sop@google.com>
-rw-r--r--src/main/java/com/google/gerrit/client/patches/AbstractPatchContentTable.java12
-rw-r--r--src/main/java/com/google/gerrit/client/patches/PatchUtil.java19
-rw-r--r--src/main/java/com/google/gerrit/client/patches/SideBySideTable.java4
-rw-r--r--src/main/java/com/google/gerrit/client/patches/UnifiedDiffTable.java3
-rw-r--r--src/main/java/com/google/gerrit/public/gerrit.css4
-rw-r--r--src/main/java/com/google/gwtexpui/safehtml/client/Prettify.java36
6 files changed, 63 insertions, 15 deletions
diff --git a/src/main/java/com/google/gerrit/client/patches/AbstractPatchContentTable.java b/src/main/java/com/google/gerrit/client/patches/AbstractPatchContentTable.java
index da373c3561..0eec7629e0 100644
--- a/src/main/java/com/google/gerrit/client/patches/AbstractPatchContentTable.java
+++ b/src/main/java/com/google/gerrit/client/patches/AbstractPatchContentTable.java
@@ -42,6 +42,7 @@ import com.google.gwt.user.client.ui.Widget;
import com.google.gwtexpui.globalkey.client.GlobalKey;
import com.google.gwtexpui.globalkey.client.KeyCommand;
import com.google.gwtexpui.globalkey.client.KeyCommandSet;
+import com.google.gwtexpui.safehtml.client.SafeHtml;
import java.sql.Timestamp;
import java.util.ArrayList;
@@ -55,6 +56,7 @@ public abstract class AbstractPatchContentTable extends NavigationTable<Object>
protected PatchSet.Id idSideA;
protected PatchSet.Id idSideB;
protected boolean onlyOneHunk;
+ protected String formatLanguage;
private final Timestamp aged =
new Timestamp(System.currentTimeMillis() - AGE);
@@ -149,6 +151,11 @@ public abstract class AbstractPatchContentTable extends NavigationTable<Object>
patchKey = k;
idSideA = a;
idSideB = b;
+
+ final String pathName = patchKey.get();
+ int ext = pathName.lastIndexOf('.');
+ formatLanguage = ext > 0 ? pathName.substring(ext + 1).toLowerCase() : null;
+
render(s);
}
@@ -178,6 +185,11 @@ public abstract class AbstractPatchContentTable extends NavigationTable<Object>
}
}
+ protected SafeHtml lineToSafeHtml(String text, boolean showWhitespaceErrors) {
+ return PatchUtil.lineToSafeHtml(text, PatchUtil.DEFAULT_LINE_LENGTH,
+ showWhitespaceErrors, formatLanguage);
+ }
+
private boolean isChunk(final int row) {
final Object o = getRowItem(row);
if (!onlyOneHunk && o instanceof PatchLine) {
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 5aff868c65..c5d9238470 100644
--- a/src/main/java/com/google/gerrit/client/patches/PatchUtil.java
+++ b/src/main/java/com/google/gerrit/client/patches/PatchUtil.java
@@ -15,6 +15,7 @@
package com.google.gerrit.client.patches;
import com.google.gwt.core.client.GWT;
+import com.google.gwtexpui.safehtml.client.Prettify;
import com.google.gwtexpui.safehtml.client.SafeHtml;
import com.google.gwtexpui.safehtml.client.SafeHtmlBuilder;
import com.google.gwtjsonrpc.client.JsonUtil;
@@ -31,26 +32,28 @@ public class PatchUtil {
}
public static SafeHtml lineToSafeHtml(final String src, final int lineLength,
- final boolean showWhiteSpaceErrors) {
+ final boolean showWhiteSpaceErrors, final String languageType) {
final boolean hasTab = src.indexOf('\t') >= 0;
String brokenSrc = wrapLines(src, hasTab, lineLength);
+ final boolean hasLFs = brokenSrc != src;
SafeHtml html = new SafeHtmlBuilder().append(brokenSrc);
if (showWhiteSpaceErrors) {
html = showTabAfterSpace(html);
html = showTrailingWhitespace(html);
}
- if (brokenSrc != src) {
+ if (hasTab) {
+ // We had at least one horizontal tab, so we should expand it out.
+ //
+ html = expandTabs(html);
+ }
+ html = Prettify.prettify(html, languageType);
+ if (hasLFs) {
// If we had line breaks inserted into the source text we need
// to expand the line breaks into <br> tags in HTML, so the
// line will wrap around.
//
html = expandLFs(html);
}
- if (hasTab) {
- // We had at least one horizontal tab, so we should expand it out.
- //
- html = expandTabs(html);
- }
return html;
}
@@ -85,7 +88,7 @@ public class PatchUtil {
private static SafeHtml expandTabs(SafeHtml src) {
return src
.replaceAll("\t",
- "<span title=\"Visual Tab\" class=\"gerrit-visualtab\">&raquo;</span>\t");
+ "<span title=\"Visual Tab\" class=\"gerrit-visualtab\"> </span>\t");
}
private static SafeHtml expandLFs(SafeHtml src) {
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 e41b9a8da6..ca01c97f12 100644
--- a/src/main/java/com/google/gerrit/client/patches/SideBySideTable.java
+++ b/src/main/java/com/google/gerrit/client/patches/SideBySideTable.java
@@ -22,6 +22,7 @@ import static com.google.gerrit.client.patches.PatchLine.Type.REPLACE;
import com.google.gerrit.client.data.PatchScript;
import com.google.gerrit.client.data.SparseFileContent;
import com.google.gerrit.client.reviewdb.PatchLineComment;
+import com.google.gwtexpui.safehtml.client.SafeHtml;
import com.google.gwtexpui.safehtml.client.SafeHtmlBuilder;
import java.util.ArrayList;
@@ -248,8 +249,7 @@ public class SideBySideTable extends AbstractPatchContentTable {
m.addStyleName("FileLine");
m.addStyleName("FileLine-" + type.name());
final String text = src.get(i);
- final boolean ws = type == INSERT;
- m.append(PatchUtil.lineToSafeHtml(text, PatchUtil.DEFAULT_LINE_LENGTH, ws));
+ m.append(lineToSafeHtml(text, type == INSERT));
m.closeTd();
}
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 24c09e23ce..46785b6012 100644
--- a/src/main/java/com/google/gerrit/client/patches/UnifiedDiffTable.java
+++ b/src/main/java/com/google/gerrit/client/patches/UnifiedDiffTable.java
@@ -281,7 +281,6 @@ public class UnifiedDiffTable extends AbstractPatchContentTable {
private void appendLineText(final SafeHtmlBuilder m,
final PatchLine.Type type, final SparseFileContent src, final int i) {
- final int len = PatchUtil.DEFAULT_LINE_LENGTH;
final String text = src.get(i);
m.openTd();
m.addStyleName("DiffText");
@@ -297,7 +296,7 @@ public class UnifiedDiffTable extends AbstractPatchContentTable {
m.append("+");
break;
}
- m.append(PatchUtil.lineToSafeHtml(text, len, false));
+ m.append(lineToSafeHtml(text, type == PatchLine.Type.INSERT));
m.closeTd();
}
diff --git a/src/main/java/com/google/gerrit/public/gerrit.css b/src/main/java/com/google/gerrit/public/gerrit.css
index 76a6c48d52..b0705bade1 100644
--- a/src/main/java/com/google/gerrit/public/gerrit.css
+++ b/src/main/java/com/google/gerrit/public/gerrit.css
@@ -48,9 +48,7 @@
}
.gerrit-visualtab {
- color: red;
- font-weight: bold;
- font-size: 8pt;
+ border: 1px dotted red;
}
.gerrit-whitespaceerror {
diff --git a/src/main/java/com/google/gwtexpui/safehtml/client/Prettify.java b/src/main/java/com/google/gwtexpui/safehtml/client/Prettify.java
new file mode 100644
index 0000000000..da382f0735
--- /dev/null
+++ b/src/main/java/com/google/gwtexpui/safehtml/client/Prettify.java
@@ -0,0 +1,36 @@
+// Copyright (C) 2008 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.google.gwtexpui.safehtml.client;
+
+public class Prettify {
+ private static boolean loaded = isLoaded();
+
+ private static native boolean isLoaded()
+ /*-{ return $wnd['prettyPrintOne'] != null }-*/;
+
+ public static SafeHtml prettify(SafeHtml src, String srcType) {
+ if (loaded) {
+ src = src.replaceAll("&#39;", "'");
+ src = SafeHtml.asis(prettifyNative(src.asString(), srcType));
+ }
+ return src;
+ }
+
+ private static native String prettifyNative(String srcText, String srcType)
+ /*-{ return $wnd.prettyPrintOne(srcText, srcType); }-*/;
+
+ private Prettify() {
+ }
+}