Merge changes from topic 'cm4'

* changes:
  SideBySide2: Highlight search results on scrollbar
  SideBySide2: Fix wrong mode selection after JS loading
  Dynamically load CodeMirror themes
  Simplify CodeMirror build by splitting out addons
  Fallback language detection with meta.js extensions
  Use CodeMirror meta.js to describe modes
This commit is contained in:
Shawn Pearce 2015-01-05 18:42:29 +00:00 committed by Gerrit Code Review
commit bd70bdbd5c
17 changed files with 562 additions and 491 deletions

View File

@ -20,6 +20,7 @@ public enum Theme {
ECLIPSE,
ELEGANT,
NEAT,
// Dark themes
MIDNIGHT,
NIGHT,

View File

@ -27,6 +27,7 @@ import com.google.gerrit.client.account.AccountApi;
import com.google.gerrit.client.account.DiffPreferences;
import com.google.gerrit.client.patches.PatchUtil;
import com.google.gerrit.client.rpc.GerritCallback;
import com.google.gerrit.client.rpc.Natives;
import com.google.gerrit.client.ui.NpIntTextBox;
import com.google.gerrit.extensions.common.Theme;
import com.google.gerrit.reviewdb.client.AccountDiffPreference;
@ -53,11 +54,11 @@ import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.ToggleButton;
import net.codemirror.lib.ModeInjector;
import net.codemirror.mode.ModeInfo;
import net.codemirror.mode.ModeInjector;
import net.codemirror.theme.ThemeLoader;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
import java.util.Objects;
/** Displays current diff preferences. */
class PreferencesBox extends Composite {
@ -346,15 +347,15 @@ class PreferencesBox extends Composite {
@UiHandler("mode")
void onMode(@SuppressWarnings("unused") ChangeEvent e) {
String m = mode.getValue(mode.getSelectedIndex());
final String mode = m != null && !m.isEmpty() ? m : null;
final String mode = getSelectedMode();
prefs.syntaxHighlighting(true);
syntaxHighlighting.setValue(true, false);
new ModeInjector().add(mode).inject(new GerritCallback<Void>() {
@Override
public void onSuccess(Void result) {
if (prefs.syntaxHighlighting() && view.isAttached()) {
if (prefs.syntaxHighlighting()
&& Objects.equals(mode, getSelectedMode())
&& view.isAttached()) {
view.operation(new Runnable() {
@Override
public void run() {
@ -367,6 +368,11 @@ class PreferencesBox extends Composite {
});
}
private String getSelectedMode() {
String m = mode.getValue(mode.getSelectedIndex());
return m != null && !m.isEmpty() ? m : null;
}
@UiHandler("whitespaceErrors")
void onWhitespaceErrors(ValueChangeEvent<Boolean> e) {
prefs.showWhitespaceErrors(e.getValue());
@ -388,18 +394,30 @@ class PreferencesBox extends Composite {
@UiHandler("theme")
void onTheme(@SuppressWarnings("unused") ChangeEvent e) {
prefs.theme(Theme.valueOf(theme.getValue(theme.getSelectedIndex())));
view.setThemeStyles(prefs.theme().isDark());
view.operation(new Runnable() {
final Theme newTheme = getSelectedTheme();
prefs.theme(newTheme);
ThemeLoader.loadTheme(newTheme, new GerritCallback<Void>() {
@Override
public void run() {
String t = prefs.theme().name().toLowerCase();
view.getCmFromSide(DisplaySide.A).setOption("theme", t);
view.getCmFromSide(DisplaySide.B).setOption("theme", t);
public void onSuccess(Void result) {
view.operation(new Runnable() {
@Override
public void run() {
if (getSelectedTheme() == newTheme && isAttached()) {
String t = newTheme.name().toLowerCase();
view.getCmFromSide(DisplaySide.A).setOption("theme", t);
view.getCmFromSide(DisplaySide.B).setOption("theme", t);
view.setThemeStyles(newTheme.isDark());
}
}
});
}
});
}
private Theme getSelectedTheme() {
return Theme.valueOf(theme.getValue(theme.getSelectedIndex()));
}
@UiHandler("apply")
void onApply(@SuppressWarnings("unused") ClickEvent e) {
close();
@ -461,46 +479,22 @@ class PreferencesBox extends Composite {
IGNORE_ALL_SPACE.name());
}
private static final Map<String, String> NAME_TO_MODE;
private static final Map<String, String> NORMALIZED_MODES;
static {
NAME_TO_MODE = new TreeMap<>();
NORMALIZED_MODES = new HashMap<>();
for (String type : ModeInjector.getKnownMimeTypes()) {
String name = type;
if (name.startsWith("text/x-")) {
name = name.substring("text/x-".length());
} else if (name.startsWith("text/")) {
name = name.substring("text/".length());
} else if (name.startsWith("application/")) {
name = name.substring("application/".length());
}
String normalized = NAME_TO_MODE.get(name);
if (normalized == null) {
normalized = type;
NAME_TO_MODE.put(name, normalized);
}
NORMALIZED_MODES.put(type, normalized);
}
}
private void initMode() {
mode.addItem("", "");
for (Map.Entry<String, String> e : NAME_TO_MODE.entrySet()) {
mode.addItem(e.getKey(), e.getValue());
for (ModeInfo m : Natives.asList(ModeInfo.all())) {
mode.addItem(m.name(), m.mime());
}
}
private void setMode(String modeType) {
if (modeType != null && !modeType.isEmpty()) {
if (NORMALIZED_MODES.containsKey(modeType)) {
modeType = NORMALIZED_MODES.get(modeType);
}
for (int i = 0; i < mode.getItemCount(); i++) {
if (mode.getValue(i).equals(modeType)) {
mode.setSelectedIndex(i);
return;
ModeInfo m = ModeInfo.findModeByMIME(modeType);
if (m != null) {
for (int i = 0; i < mode.getItemCount(); i++) {
if (mode.getValue(i).equals(m.mime())) {
mode.setSelectedIndex(i);
return;
}
}
}
}
@ -519,26 +513,8 @@ class PreferencesBox extends Composite {
}
private void initTheme() {
theme.addItem(
Theme.DEFAULT.name().toLowerCase(),
Theme.DEFAULT.name());
theme.addItem(
Theme.ECLIPSE.name().toLowerCase(),
Theme.ECLIPSE.name());
theme.addItem(
Theme.ELEGANT.name().toLowerCase(),
Theme.ELEGANT.name());
theme.addItem(
Theme.NEAT.name().toLowerCase(),
Theme.NEAT.name());
theme.addItem(
Theme.MIDNIGHT.name().toLowerCase(),
Theme.MIDNIGHT.name());
theme.addItem(
Theme.NIGHT.name().toLowerCase(),
Theme.NIGHT.name());
theme.addItem(
Theme.TWILIGHT.name().toLowerCase(),
Theme.TWILIGHT.name());
for (Theme t : Theme.values()) {
theme.addItem(t.name().toLowerCase(), t.name());
}
}
}

View File

@ -75,8 +75,10 @@ import net.codemirror.lib.CodeMirror.LineClassWhere;
import net.codemirror.lib.CodeMirror.LineHandle;
import net.codemirror.lib.Configuration;
import net.codemirror.lib.KeyMap;
import net.codemirror.lib.ModeInjector;
import net.codemirror.lib.Pos;
import net.codemirror.mode.ModeInfo;
import net.codemirror.mode.ModeInjector;
import net.codemirror.theme.ThemeLoader;
import java.util.ArrayList;
import java.util.Collections;
@ -174,7 +176,10 @@ public class SideBySide2 extends Screen {
CallbackGroup cmGroup = new CallbackGroup();
CodeMirror.initLibrary(cmGroup.add(CallbackGroup.<Void> emptyCallback()));
final CallbackGroup group = new CallbackGroup();
final AsyncCallback<Void> themeCallback =
group.add(CallbackGroup.<Void> emptyCallback());
final AsyncCallback<Void> modeInjectorCb =
group.add(CallbackGroup.<Void> emptyCallback());
@ -188,6 +193,9 @@ public class SideBySide2 extends Screen {
public void onSuccess(DiffInfo diffInfo) {
diff = diffInfo;
fileSize = bucketFileSize(diffInfo);
// Load theme after CM library to ensure theme can override CSS.
ThemeLoader.loadTheme(prefs.theme(), themeCallback);
if (prefs.syntaxHighlighting()) {
if (fileSize.compareTo(FileSize.SMALL) > 0) {
modeInjectorCb.onSuccess(null);
@ -991,11 +999,12 @@ public class SideBySide2 extends Screen {
}
private String getContentType(DiffInfo.FileMeta meta) {
return prefs.syntaxHighlighting()
&& meta != null
&& meta.content_type() != null
? ModeInjector.getContentType(meta.content_type())
: null;
if (prefs.syntaxHighlighting() && meta != null
&& meta.content_type() != null) {
ModeInfo m = ModeInfo.findMode(meta.content_type(), path);
return m != null ? m.mime() : null;
}
return null;
}
private void injectMode(DiffInfo diffInfo, AsyncCallback<Void> cb) {

View File

@ -45,7 +45,8 @@ import com.google.gwtexpui.safehtml.client.SafeHtml;
import net.codemirror.lib.CodeMirror;
import net.codemirror.lib.Configuration;
import net.codemirror.lib.ModeInjector;
import net.codemirror.mode.ModeInfo;
import net.codemirror.mode.ModeInjector;
public class EditScreen extends Screen {
interface Binder extends UiBinder<HTMLPanel, EditScreen> {}
@ -92,7 +93,8 @@ public class EditScreen extends Screen {
cmGroup.add(new GerritCallback<String>() {
@Override
public void onSuccess(String result) {
type = result;
ModeInfo mode = ModeInfo.findMode(result, path);
type = mode != null ? mode.mime() : null;
injectMode(result, modeInjectorCb);
}
}));
@ -174,6 +176,6 @@ public class EditScreen extends Screen {
.set("styleSelectedText", true)
.set("showTrailingSpace", true)
.set("keyMap", "default")
.set("mode", ModeInjector.getContentType(type));
.set("mode", type);
}
}

View File

@ -20,4 +20,5 @@
<source path='lib'/>
<source path='keymap'/>
<source path='mode'/>
<source path='theme'/>
</module>

View File

@ -23,10 +23,10 @@ import com.google.gwt.resources.client.ExternalTextResource;
interface Lib extends ClientBundle {
static final Lib I = GWT.create(Lib.class);
@Source("cm4.css")
@Source("cm.css")
ExternalTextResource css();
@Source("cm4.js")
@Source("cm.js")
@DoNotEmbed
DataResource js();
}

View File

@ -29,7 +29,7 @@ import com.google.gwt.user.client.rpc.AsyncCallback;
import java.util.logging.Level;
import java.util.logging.Logger;
class Loader {
public class Loader {
private static native boolean isLibLoaded()
/*-{ return $wnd.hasOwnProperty('CodeMirror'); }-*/;
@ -66,7 +66,7 @@ class Loader {
}
}
static void injectScript(SafeUri js, final AsyncCallback<Void> callback) {
public static void injectScript(SafeUri js, final AsyncCallback<Void> callback) {
final ScriptElement[] script = new ScriptElement[1];
script[0] = ScriptInjector.fromUrl(js.asString())
.setWindow(ScriptInjector.TOP_WINDOW)

View File

@ -1,212 +0,0 @@
// Copyright (C) 2013 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 net.codemirror.lib;
import com.google.gwt.core.client.JsArrayString;
import com.google.gwt.resources.client.DataResource;
import com.google.gwt.safehtml.shared.SafeUri;
import com.google.gwt.user.client.rpc.AsyncCallback;
import net.codemirror.mode.Modes;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
public class ModeInjector {
/** Map of server content type to CodeMiror mode or content type. */
private static final Map<String, String> mimeAlias;
/** Map of content type "text/x-java" to mode name "clike". */
private static final Map<String, String> mimeModes;
/** Map of names such as "clike" to URI for code download. */
private static final Map<String, SafeUri> modeUris;
static {
DataResource[] all = {
Modes.I.clike(),
Modes.I.clojure(),
Modes.I.coffeescript(),
Modes.I.commonlisp(),
Modes.I.css(),
Modes.I.d(),
Modes.I.dart(),
Modes.I.diff(),
Modes.I.dockerfile(),
Modes.I.dtd(),
Modes.I.erlang(),
Modes.I.gas(),
Modes.I.gerrit_commit(),
Modes.I.gfm(),
Modes.I.groovy(),
Modes.I.haskell(),
Modes.I.htmlmixed(),
Modes.I.javascript(),
Modes.I.lua(),
Modes.I.markdown(),
Modes.I.perl(),
Modes.I.php(),
Modes.I.pig(),
Modes.I.properties(),
Modes.I.python(),
Modes.I.r(),
Modes.I.rst(),
Modes.I.ruby(),
Modes.I.scheme(),
Modes.I.shell(),
Modes.I.smalltalk(),
Modes.I.soy(),
Modes.I.sql(),
Modes.I.stex(),
Modes.I.velocity(),
Modes.I.verilog(),
Modes.I.xml(),
Modes.I.yaml(),
};
mimeAlias = new HashMap<>();
mimeModes = new HashMap<>();
modeUris = new HashMap<>();
for (DataResource m : all) {
modeUris.put(m.getName(), m.getSafeUri());
}
parseModeMap();
}
private static void parseModeMap() {
String mode = null;
for (String line : Modes.I.mode_map().getText().split("\n")) {
int eq = line.indexOf('=');
if (0 < eq) {
mimeAlias.put(
line.substring(0, eq).trim(),
line.substring(eq + 1).trim());
} else if (line.endsWith(":")) {
String n = line.substring(0, line.length() - 1);
if (modeUris.containsKey(n)) {
mode = n;
}
} else if (mode != null && line.contains("/")) {
mimeModes.put(line, mode);
} else {
mode = null;
}
}
}
public static String getContentType(String mode) {
String real = mode != null ? mimeAlias.get(mode) : null;
return real != null ? real : mode;
}
public static Collection<String> getKnownMimeTypes() {
return mimeModes.keySet();
}
private static native boolean isModeLoaded(String n)
/*-{ return $wnd.CodeMirror.modes.hasOwnProperty(n); }-*/;
private static native boolean isMimeLoaded(String n)
/*-{ return $wnd.CodeMirror.mimeModes.hasOwnProperty(n); }-*/;
private static native JsArrayString getDependencies(String n)
/*-{ return $wnd.CodeMirror.modes[n].dependencies || []; }-*/;
private final Set<String> loading = new HashSet<>(4);
private int pending;
private AsyncCallback<Void> appCallback;
public ModeInjector add(String name) {
if (name == null || isModeLoaded(name) || isMimeLoaded(name)) {
return this;
}
String mode = mimeModes.get(name);
if (mode == null) {
mode = name;
}
SafeUri uri = modeUris.get(mode);
if (uri == null) {
Logger.getLogger("net.codemirror").log(
Level.WARNING,
"CodeMirror mode " + mode + " not configured.");
return this;
}
loading.add(mode);
return this;
}
public void inject(AsyncCallback<Void> appCallback) {
this.appCallback = appCallback;
for (String mode : loading) {
beginLoading(mode);
}
if (pending == 0) {
appCallback.onSuccess(null);
}
}
private void beginLoading(final String mode) {
pending++;
Loader.injectScript(
modeUris.get(mode),
new AsyncCallback<Void>() {
@Override
public void onSuccess(Void result) {
pending--;
ensureDependenciesAreLoaded(mode);
if (pending == 0) {
appCallback.onSuccess(null);
}
}
@Override
public void onFailure(Throwable caught) {
if (--pending == 0) {
appCallback.onFailure(caught);
}
}
});
}
private void ensureDependenciesAreLoaded(String mode) {
JsArrayString deps = getDependencies(mode);
for (int i = 0; i < deps.length(); i++) {
String d = deps.get(i);
if (loading.contains(d) || isModeLoaded(d)) {
continue;
}
SafeUri uri = modeUris.get(d);
if (uri == null) {
Logger.getLogger("net.codemirror").log(
Level.SEVERE,
"CodeMirror mode " + mode + " needs " + d);
continue;
}
loading.add(d);
beginLoading(d);
}
}
}

View File

@ -0,0 +1,197 @@
// Copyright (C) 2014 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 net.codemirror.mode;
import com.google.gerrit.client.rpc.NativeMap;
import com.google.gerrit.client.rpc.Natives;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.core.client.JsArray;
import com.google.gwt.core.client.JsArrayString;
import com.google.gwt.resources.client.DataResource;
import com.google.gwt.safehtml.shared.SafeUri;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
/** Description of a CodeMirror language mode. */
public class ModeInfo extends JavaScriptObject {
private static NativeMap<ModeInfo> byMime;
private static NativeMap<ModeInfo> byExt;
/** Map of names such as "clike" to URI for code download. */
private static final Map<String, SafeUri> modeUris = new HashMap<>();
static {
indexModes(new DataResource[] {
Modes.I.clike(),
Modes.I.clojure(),
Modes.I.coffeescript(),
Modes.I.commonlisp(),
Modes.I.css(),
Modes.I.d(),
Modes.I.dart(),
Modes.I.diff(),
Modes.I.dockerfile(),
Modes.I.dtd(),
Modes.I.erlang(),
Modes.I.gas(),
Modes.I.gerrit_commit(),
Modes.I.gfm(),
Modes.I.groovy(),
Modes.I.haskell(),
Modes.I.htmlmixed(),
Modes.I.javascript(),
Modes.I.lua(),
Modes.I.markdown(),
Modes.I.perl(),
Modes.I.php(),
Modes.I.pig(),
Modes.I.properties(),
Modes.I.python(),
Modes.I.r(),
Modes.I.rst(),
Modes.I.ruby(),
Modes.I.scheme(),
Modes.I.shell(),
Modes.I.smalltalk(),
Modes.I.soy(),
Modes.I.sql(),
Modes.I.stex(),
Modes.I.velocity(),
Modes.I.verilog(),
Modes.I.xml(),
Modes.I.yaml(),
});
alias("application/x-httpd-php-open", "application/x-httpd-php");
alias("application/x-javascript", "application/javascript");
alias("application/x-shellscript", "text/x-sh");
alias("application/x-tcl", "text/x-tcl");
alias("text/typescript", "application/typescript");
alias("text/x-c", "text/x-csrc");
alias("text/x-c++hdr", "text/x-c++src");
alias("text/x-chdr", "text/x-csrc");
alias("text/x-h", "text/x-csrc");
alias("text/x-ini", "text/x-properties");
alias("text/x-java-source", "text/x-java");
alias("text/x-php", "application/x-httpd-php");
alias("text/x-scripttcl", "text/x-tcl");
}
/** All supported modes. */
public static native JsArray<ModeInfo> all() /*-{
return $wnd.CodeMirror.modeInfo
}-*/;
private static native void setAll(JsArray<ModeInfo> m) /*-{
$wnd.CodeMirror.modeInfo = m
}-*/;
/** Look up mode by primary or alternate MIME types. */
public static ModeInfo findModeByMIME(String mime) {
return byMime.get(mime);
}
public static SafeUri getModeScriptUri(String mode) {
return modeUris.get(mode);
}
/** Look up mode by MIME type or file extension from a path. */
public static ModeInfo findMode(String mime, String path) {
ModeInfo m = byMime.get(mime);
if (m != null) {
return m;
}
int s = path.lastIndexOf('/');
int d = path.lastIndexOf('.');
if (d == -1 || s > d) {
return null; // punt on "foo.src/bar" type paths.
}
if (byExt == null) {
byExt = NativeMap.create();
for (ModeInfo mode : Natives.asList(all())) {
for (String ext : Natives.asList(mode.ext())) {
byExt.put(ext, mode);
}
}
}
return byExt.get(path.substring(d + 1));
}
private static void alias(String serverMime, String toMime) {
ModeInfo mode = byMime.get(toMime);
if (mode != null) {
byMime.put(serverMime, mode);
}
}
private static void indexModes(DataResource[] all) {
for (DataResource r : all) {
modeUris.put(r.getName(), r.getSafeUri());
}
JsArray<ModeInfo> modeList = all();
modeList.push(gerrit_commit());
byMime = NativeMap.create();
JsArray<ModeInfo> filtered = JsArray.createArray().cast();
for (ModeInfo m : Natives.asList(modeList)) {
if (modeUris.containsKey(m.mode())) {
filtered.push(m);
for (String mimeType : Natives.asList(m.mimes())) {
byMime.put(mimeType, m);
}
byMime.put(m.mode(), m);
}
}
Collections.sort(Natives.asList(filtered), new Comparator<ModeInfo>() {
@Override
public int compare(ModeInfo a, ModeInfo b) {
return a.name().toLowerCase().compareTo(b.name().toLowerCase());
}
});
setAll(filtered);
}
/** Human readable name of the mode, such as "C++". */
public final native String name() /*-{ return this.name }-*/;
/** Internal CodeMirror name for {@code mode.js} file to load. */
public final native String mode() /*-{ return this.mode }-*/;
/** Primary MIME type to activate this mode. */
public final native String mime() /*-{ return this.mime }-*/;
/** Primary and additional MIME types that activate this mode. */
public final native JsArrayString mimes()
/*-{ return this.mimes || [this.mime] }-*/;
private final native JsArrayString ext()
/*-{ return this.ext || [] }-*/;
protected ModeInfo() {
}
private static native ModeInfo gerrit_commit() /*-{
return {name: "Git Commit Message",
mime: "text/x-gerrit-commit-message",
mode: "gerrit_commit"}
}-*/;
}

View File

@ -0,0 +1,118 @@
// Copyright (C) 2013 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 net.codemirror.mode;
import com.google.gwt.core.client.JsArrayString;
import com.google.gwt.user.client.rpc.AsyncCallback;
import net.codemirror.lib.Loader;
import java.util.HashSet;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
public class ModeInjector {
private static boolean canLoad(String mode) {
return ModeInfo.getModeScriptUri(mode) != null;
}
private static native boolean isModeLoaded(String n)
/*-{ return $wnd.CodeMirror.modes.hasOwnProperty(n); }-*/;
private static native boolean isMimeLoaded(String n)
/*-{ return $wnd.CodeMirror.mimeModes.hasOwnProperty(n); }-*/;
private static native JsArrayString getDependencies(String n)
/*-{ return $wnd.CodeMirror.modes[n].dependencies || []; }-*/;
private final Set<String> loading = new HashSet<>(4);
private int pending;
private AsyncCallback<Void> appCallback;
public ModeInjector add(String name) {
if (name == null || isModeLoaded(name) || isMimeLoaded(name)) {
return this;
}
ModeInfo m = ModeInfo.findModeByMIME(name);
if (m != null) {
name = m.mode();
}
if (!canLoad(name)) {
Logger.getLogger("net.codemirror").log(
Level.WARNING,
"CodeMirror mode " + name + " not configured.");
return this;
}
loading.add(name);
return this;
}
public void inject(AsyncCallback<Void> appCallback) {
this.appCallback = appCallback;
for (String mode : loading) {
beginLoading(mode);
}
if (pending == 0) {
appCallback.onSuccess(null);
}
}
private void beginLoading(final String mode) {
pending++;
Loader.injectScript(
ModeInfo.getModeScriptUri(mode),
new AsyncCallback<Void>() {
@Override
public void onSuccess(Void result) {
pending--;
ensureDependenciesAreLoaded(mode);
if (pending == 0) {
appCallback.onSuccess(null);
}
}
@Override
public void onFailure(Throwable caught) {
if (--pending == 0) {
appCallback.onFailure(caught);
}
}
});
}
private void ensureDependenciesAreLoaded(String mode) {
JsArrayString deps = getDependencies(mode);
for (int i = 0; i < deps.length(); i++) {
String d = deps.get(i);
if (loading.contains(d) || isModeLoaded(d)) {
continue;
}
if (!canLoad(d)) {
Logger.getLogger("net.codemirror").log(
Level.SEVERE,
"CodeMirror mode " + d + " needs " + d);
continue;
}
loading.add(d);
beginLoading(d);
}
}
}

View File

@ -18,12 +18,10 @@ import com.google.gwt.core.client.GWT;
import com.google.gwt.resources.client.ClientBundle;
import com.google.gwt.resources.client.DataResource;
import com.google.gwt.resources.client.DataResource.DoNotEmbed;
import com.google.gwt.resources.client.TextResource;
public interface Modes extends ClientBundle {
public static final Modes I = GWT.create(Modes.class);
@Source("mode_map") TextResource mode_map();
@Source("clike.js") @DoNotEmbed DataResource clike();
@Source("clojure.js") @DoNotEmbed DataResource clojure();
@Source("coffeescript.js") @DoNotEmbed DataResource coffeescript();
@ -64,5 +62,5 @@ public interface Modes extends ClientBundle {
@Source("xml.js") @DoNotEmbed DataResource xml();
@Source("yaml.js") @DoNotEmbed DataResource yaml();
// When adding a resource, update static initializer in ModeInjector.
// When adding a resource, update static initializer in ModeInfo.
}

View File

@ -1,155 +0,0 @@
clike:
text/x-c
text/x-chdr
text/x-csharp
text/x-csrc
text/x-c++src
text/x-c++hdr
text/x-java
text/x-objectivec
text/x-scala
x-shader/x-fragment
x-shader/x-vertex
clojure:
text/x-clojure
coffeescript:
text/x-coffeescript
commonlisp:
text/x-common-lisp
css:
text/css
text/x-scss
d:
text/x-d
dart:
application/dart
diff:
text/x-diff
dockerfile:
text/x-dockerfile
dtd:
application/xml-dtd
erlang:
text/x-erlang
gas:
text/x-gas
gerrit_commit:
text/x-gerrit-commit-message
gfm:
text/x-github-markdown
go:
text/x-go
groovy:
text/x-groovy
haskell:
text/x-haskell
htmlmixed:
text/html
javascript:
text/javascript
text/ecmascript
application/javascript
application/ecmascript
application/json
application/x-json
text/typescript
application/typescript
lua:
text/x-lua
markdown:
text/x-markdown
perl:
text/x-perl
properties:
text/x-ini
text/x-properties
perl:
text/x-perl
php:
application/x-httpd-php
application/x-httpd-php-open
text/x-php
pig:
text/x-pig
python:
text/x-python
r:
text/r-src
rst:
text/x-rst
ruby:
text/x-ruby
scheme:
text/x-scheme
shell:
text/x-sh
application/x-shellscript
smalltalk:
text/x-stsrc
soy:
text/x-soy
sql:
text/x-sql
text/x-mariadb
text/x-mysql
text/x-plsql
stex:
text/x-stex
tcl:
text/x-tcl
velocity:
text/velocity
verilog:
text/x-verilog
xml:
text/xml
application/xml
yaml:
text/x-yaml
application/x-javascript = application/javascript
application/x-shellscript = text/x-sh
application/x-tcl = text/x-tcl
text/x-h = text/x-c++hdr
text/x-java-source = text/x-java
text/x-scripttcl = text/x-tcl

View File

@ -0,0 +1,83 @@
// Copyright (C) 2014 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 net.codemirror.theme;
import com.google.gerrit.extensions.common.Theme;
import com.google.gwt.dom.client.StyleInjector;
import com.google.gwt.resources.client.ExternalTextResource;
import com.google.gwt.resources.client.ResourceCallback;
import com.google.gwt.resources.client.ResourceException;
import com.google.gwt.resources.client.TextResource;
import com.google.gwt.user.client.rpc.AsyncCallback;
import java.util.EnumSet;
/** Dynamically loads a known CodeMirror theme's CSS */
public class ThemeLoader {
private static final ExternalTextResource[] THEMES = {
Themes.I.eclipse(),
Themes.I.elegant(),
Themes.I.midnight(),
Themes.I.neat(),
Themes.I.night(),
Themes.I.twilight(),
};
private static final EnumSet<Theme> loaded = EnumSet.of(Theme.DEFAULT);
public static final void loadTheme(final Theme theme,
final AsyncCallback<Void> cb) {
if (loaded.contains(theme)) {
cb.onSuccess(null);
return;
}
ExternalTextResource resource = findTheme(theme);
if (resource == null) {
cb.onFailure(new Exception("unknown theme " + theme));
return;
}
try {
resource.getText(new ResourceCallback<TextResource>() {
@Override
public void onSuccess(TextResource resource) {
StyleInjector.inject(resource.getText());
loaded.add(theme);
cb.onSuccess(null);
}
@Override
public void onError(ResourceException e) {
cb.onFailure(e);
}
});
} catch (ResourceException e) {
cb.onFailure(e);
}
}
private static final ExternalTextResource findTheme(Theme theme) {
for (ExternalTextResource r : THEMES) {
if (theme.name().toLowerCase().equals(r.getName())) {
return r;
}
}
return null;
}
private ThemeLoader() {
}
}

View File

@ -0,0 +1,34 @@
// Copyright (C) 2014 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 net.codemirror.theme;
import com.google.gwt.core.client.GWT;
import com.google.gwt.resources.client.ClientBundle;
import com.google.gwt.resources.client.ExternalTextResource;
public interface Themes extends ClientBundle {
public static final Themes I = GWT.create(Themes.class);
@Source("eclipse.css") ExternalTextResource eclipse();
@Source("elegant.css") ExternalTextResource elegant();
@Source("midnight.css") ExternalTextResource midnight();
@Source("neat.css") ExternalTextResource neat();
@Source("night.css") ExternalTextResource night();
@Source("twilight.css") ExternalTextResource twilight();
// When adding a resource, update:
// - static initializer in ThemeLoader
// - enum value in com.google.gerrit.extensions.common.Theme
}

View File

@ -2,8 +2,8 @@ include_defs('//lib/maven.defs')
include_defs('//lib/codemirror/cm.defs')
include_defs('//lib/codemirror/closure.defs')
VERSION = '4.10'
SHA1 = '577730c11277bf96ce637b183409a2865593f76b'
VERSION = 'd0a2ddaa04'
SHA1 = '1df573141fcceec039d0260d2d66a5b15d663f9a'
URL = GERRIT + 'net/codemirror/codemirror-%s.zip' % VERSION
ZIP = 'codemirror-%s.zip' % VERSION
@ -19,18 +19,29 @@ CLOSURE_COMPILER_ARGS = [
genrule(
name = 'css',
cmd = ';'.join([
':>$OUT',
"echo '/** @license' >>$OUT",
"echo '/** @license' >$OUT",
'unzip -p $(location :zip) %s/LICENSE >>$OUT' % TOP,
"echo '*/' >>$OUT",
] +
['unzip -p $(location :zip) %s/%s >>$OUT' % (TOP, n)
for n in CM_CSS + CM_THEMES]
for n in CM_CSS]
),
deps = [':zip'],
out = 'cm4.css',
out = 'cm.css',
)
for n in CM_THEMES:
genrule(
name = 'theme_%s' % n,
cmd = ';'.join([
"echo '/** @license' >$OUT",
'unzip -p $(location :zip) %s/LICENSE >>$OUT' % TOP,
"echo '*/' >>$OUT",
'unzip -p $(location :zip) %s/theme/%s.css >>$OUT' % (TOP, n)
]
),
out = 'theme_%s.css' % n,
)
genrule(
name = 'cm-verbose',
cmd = ';'.join([
@ -38,17 +49,18 @@ genrule(
'unzip -p $(location :zip) %s/LICENSE >>$OUT' % TOP,
"echo '*/' >>$OUT",
] +
['unzip -p $(location :zip) %s/%s >>$OUT' % (TOP, n) for n in CM_JS]
['unzip -p $(location :zip) %s/%s >>$OUT' % (TOP, n) for n in CM_JS] +
['unzip -p $(location :zip) %s/addon/%s >>$OUT' % (TOP, n)
for n in CM_ADDONS]
),
deps = [':zip'],
out = 'cm4-verbose.js',
out = 'cm-verbose.js',
)
js_minify(
name = 'js',
generated = [':cm-verbose'],
compiler_args = CLOSURE_COMPILER_ARGS,
out = 'cm4.js'
out = 'cm.js'
)
for n in CM_MODES:
@ -60,7 +72,6 @@ for n in CM_MODES:
"echo '*/' >>$OUT",
'unzip -p $(location :zip) %s/mode/%s/%s.js >>$OUT' % (TOP, n, n),
]),
deps = [':zip'],
out = 'mode_%s_src.js' %n,
)
js_minify(
@ -84,18 +95,14 @@ genrule(
name = 'jar',
cmd = ';'.join([
'cd $TMP',
'mkdir -p net/codemirror/{lib,mode}',
'mkdir -p net/codemirror/{lib,mode,theme}',
'cp $(location :css) net/codemirror/lib',
'cp $(location :js) net/codemirror/lib']
+ ['cp $(location :mode_%s_js) net/codemirror/mode/%s.js' % (n, n)
for n in CM_MODES]
+ ['zip -qr $OUT net/codemirror/{lib,mode}']),
deps = [
':css',
':js',
':js_minifier',
':zip',
] + [':mode_%s_js' % n for n in CM_MODES],
+ ['cp $(location :theme_%s) net/codemirror/theme/%s.css' % (n, n)
for n in CM_THEMES]
+ ['zip -qr $OUT net/codemirror/{lib,mode,theme}']),
out = 'codemirror.jar',
)
@ -105,7 +112,6 @@ genrule(
' -o $OUT' +
' -u ' + URL +
' -v ' + SHA1,
deps = ['//tools:download_file'],
out = ZIP,
)

View File

@ -2,30 +2,43 @@ CM_CSS = [
'lib/codemirror.css',
'addon/dialog/dialog.css',
'addon/scroll/simplescrollbars.css',
]
CM_THEMES = [
'theme/eclipse.css',
'theme/elegant.css',
'theme/midnight.css',
'theme/neat.css',
'theme/night.css',
'theme/twilight.css',
'addon/search/matchesonscrollbar.css',
]
CM_JS = [
'lib/codemirror.js',
'mode/meta.js',
'keymap/vim.js',
'addon/dialog/dialog.js',
'addon/scroll/simplescrollbars.js',
'addon/search/searchcursor.js',
'addon/search/search.js',
'addon/selection/mark-selection.js',
'addon/edit/trailingspace.js',
'addon/mode/overlay.js',
'addon/mode/simple.js',
]
CM_ADDONS = [
'dialog/dialog.js',
'edit/trailingspace.js',
'scroll/annotatescrollbar.js',
'scroll/simplescrollbars.js',
'search/matchesonscrollbar.js',
'search/searchcursor.js',
'search/search.js',
'selection/mark-selection.js',
'mode/overlay.js',
'mode/simple.js',
]
# Available themes must be enumerated here,
# in gerrit-extension-api/src/main/java/com/google/gerrit/extensions/common/Theme.java,
# in gerrit-gwtui/src/main/java/net/codemirror/theme/Themes.java
CM_THEMES = [
'eclipse',
'elegant',
'midnight',
'neat',
'night',
'twilight',
]
# Available modes must be enumerated here,
# in gerrit-gwtui/src/main/java/net/codemirror/mode/Modes.java,
# and in CodeMirror's own mode/meta.js script.
CM_MODES = [
'clike',
'clojure',

@ -1 +1 @@
Subproject commit 80373ef2e4c1978b8c9a68a12c05ec05c4063f8a
Subproject commit 39ffdc20f021484c1ce1dca2ae9f00fa10a482f4