Use CodeMirror meta.js to describe modes

Instead of writing out the mode_map table by hand, compile and include
meta.js from the CodeMirror distribution.  This script lists all modes
in simple JavaScript objects.

An advantage of this format is the name property is human readable,
and does not require munging MIME type strings.  Adding new modes now
changes only 2 files in the client (Modes and ModeInfo).

Change-Id: Ifa0c7af77681f72b8210fca83a7af6a9ef1c6541
This commit is contained in:
Shawn Pearce 2014-12-30 23:41:57 -05:00
parent 8deb1b43d4
commit 60a77b0562
10 changed files with 314 additions and 412 deletions

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,8 @@ 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 java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
import net.codemirror.mode.ModeInfo;
import net.codemirror.mode.ModeInjector;
/** Displays current diff preferences. */
class PreferencesBox extends Composite {
@ -461,49 +459,25 @@ 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);
}
ModeInfo m = ModeInfo.findModeByMIME(modeType);
if (m != null) {
for (int i = 0; i < mode.getItemCount(); i++) {
if (mode.getValue(i).equals(modeType)) {
if (mode.getValue(i).equals(m.mime())) {
mode.setSelectedIndex(i);
return;
}
}
}
}
mode.setSelectedIndex(0);
}

View File

@ -75,8 +75,8 @@ 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.ModeInjector;
import java.util.ArrayList;
import java.util.Collections;

View File

@ -39,7 +39,7 @@ import com.google.gwtexpui.globalkey.client.GlobalKey;
import net.codemirror.lib.CodeMirror;
import net.codemirror.lib.Configuration;
import net.codemirror.lib.ModeInjector;
import net.codemirror.mode.ModeInjector;
public class EditScreen extends Screen {

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,169 @@
// 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;
/** 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
}-*/;
/** Lookup 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);
}
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] }-*/;
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,127 @@
// 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;
}
public static String getContentType(String mode) {
if (canLoad(mode)) {
return mode;
}
ModeInfo m = ModeInfo.findModeByMIME(mode);
return m != null ? m.mime() : mode;
}
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

@ -15,6 +15,7 @@ CM_THEMES = [
CM_JS = [
'lib/codemirror.js',
'mode/meta.js',
'keymap/vim.js',
'addon/dialog/dialog.js',
'addon/scroll/simplescrollbars.js',