Simplify CodeMirror wrapper JSNI

LineCharacter (aka {from, to} objects) is the same as Pos in
CodeMirror.  The CodeMirror documentation suggests using a Pos
object in a number of function calls, but it can be unclear this
is the LineCharacter wrapper in Gerrit

Rename LineCharacter to Pos, and change its factory methods to
construct using CodeMirror.Pos(line, ch) so instances are of the
type CodeMirror expects internally.

Simplify accessor methods by dropping the get/set Java idiom and
use the field name as the method name. Argument overloading makes
it possible to identify between a get and a set variant. This makes
these lightweight JSNI wrappers feel more like an @AutoValue object
or raw JavaScript code that they are interacting with.

Change-Id: If73536652cc5559fcef5b2519cb686460a126ff5
This commit is contained in:
Shawn Pearce
2014-12-29 15:10:28 -05:00
parent 3c9816c3cc
commit 6cadc12269
22 changed files with 337 additions and 337 deletions

View File

@@ -32,7 +32,7 @@ import com.google.gwt.user.client.EventListener;
import net.codemirror.lib.CodeMirror; import net.codemirror.lib.CodeMirror;
import net.codemirror.lib.CodeMirror.LineClassWhere; import net.codemirror.lib.CodeMirror.LineClassWhere;
import net.codemirror.lib.Configuration; import net.codemirror.lib.Configuration;
import net.codemirror.lib.LineCharacter; import net.codemirror.lib.Pos;
import net.codemirror.lib.LineWidget; import net.codemirror.lib.LineWidget;
import net.codemirror.lib.TextMarker; import net.codemirror.lib.TextMarker;
@@ -217,20 +217,20 @@ class ChunkManager {
.set("className", DiffTable.style.diff()) .set("className", DiffTable.style.diff())
.set("readOnly", true); .set("readOnly", true);
LineCharacter last = CodeMirror.pos(0, 0); Pos last = Pos.create(0, 0);
for (Span span : Natives.asList(edits)) { for (Span span : Natives.asList(edits)) {
LineCharacter from = iter.advance(span.skip()); Pos from = iter.advance(span.skip());
LineCharacter to = iter.advance(span.mark()); Pos to = iter.advance(span.mark());
if (from.getLine() == last.getLine()) { if (from.line() == last.line()) {
markers.add(cm.markText(last, from, bg)); markers.add(cm.markText(last, from, bg));
} else { } else {
markers.add(cm.markText(CodeMirror.pos(from.getLine(), 0), from, bg)); markers.add(cm.markText(Pos.create(from.line(), 0), from, bg));
} }
markers.add(cm.markText(from, to, diff)); markers.add(cm.markText(from, to, diff));
last = to; last = to;
colorLines(cm, LineClassWhere.BACKGROUND, colorLines(cm, LineClassWhere.BACKGROUND,
DiffTable.style.diff(), DiffTable.style.diff(),
from.getLine(), to.getLine()); from.line(), to.line());
} }
} }
@@ -291,7 +291,7 @@ class ChunkManager {
return new Runnable() { return new Runnable() {
@Override @Override
public void run() { public void run() {
int line = cm.hasActiveLine() ? cm.getLineNumber(cm.getActiveLine()) : 0; int line = cm.hasActiveLine() ? cm.getLineNumber(cm.activeLine()) : 0;
int res = Collections.binarySearch( int res = Collections.binarySearch(
chunks, chunks,
new DiffChunkInfo(cm.side(), line, 0, false), new DiffChunkInfo(cm.side(), line, 0, false),
@@ -315,11 +315,11 @@ class ChunkManager {
DiffChunkInfo target = chunks.get(res); DiffChunkInfo target = chunks.get(res);
CodeMirror targetCm = host.getCmFromSide(target.getSide()); CodeMirror targetCm = host.getCmFromSide(target.getSide());
targetCm.setCursor(LineCharacter.create(target.getStart())); targetCm.setCursor(Pos.create(target.getStart()));
targetCm.focus(); targetCm.focus();
targetCm.scrollToY( targetCm.scrollToY(
targetCm.heightAtLine(target.getStart(), "local") - targetCm.heightAtLine(target.getStart(), "local") -
0.5 * cmB.getScrollbarV().getClientHeight()); 0.5 * cmB.scrollbarV().getClientHeight());
} }
}; };
} }

View File

@@ -58,8 +58,8 @@ abstract class CommentBox extends Composite {
if (range != null) { if (range != null) {
fromTo = FromTo.create(range); fromTo = FromTo.create(range);
rangeMarker = group.getCm().markText( rangeMarker = group.getCm().markText(
fromTo.getFrom(), fromTo.from(),
fromTo.getTo(), fromTo.to(),
Configuration.create() Configuration.create()
.set("className", DiffTable.style.range())); .set("className", DiffTable.style.range()));
} }
@@ -106,8 +106,8 @@ abstract class CommentBox extends Composite {
if (fromTo != null) { if (fromTo != null) {
if (highlight && rangeHighlightMarker == null) { if (highlight && rangeHighlightMarker == null) {
rangeHighlightMarker = group.getCm().markText( rangeHighlightMarker = group.getCm().markText(
fromTo.getFrom(), fromTo.from(),
fromTo.getTo(), fromTo.to(),
Configuration.create() Configuration.create()
.set("className", DiffTable.style.rangeHighlight())); .set("className", DiffTable.style.rangeHighlight()));
} else if (!highlight && rangeHighlightMarker != null) { } else if (!highlight && rangeHighlightMarker != null) {

View File

@@ -218,8 +218,8 @@ class CommentGroup extends Composite {
private void updateSelection() { private void updateSelection() {
if (cm.somethingSelected()) { if (cm.somethingSelected()) {
FromTo r = cm.getSelectedRange(); FromTo r = cm.getSelectedRange();
if (r.getTo().getLine() >= line) { if (r.to().line() >= line) {
cm.setSelection(r.getFrom(), r.getTo()); cm.setSelection(r.from(), r.to());
} }
} }
} }

View File

@@ -26,7 +26,7 @@ import com.google.gwt.core.client.JsArray;
import net.codemirror.lib.CodeMirror; import net.codemirror.lib.CodeMirror;
import net.codemirror.lib.CodeMirror.LineHandle; import net.codemirror.lib.CodeMirror.LineHandle;
import net.codemirror.lib.LineCharacter; import net.codemirror.lib.Pos;
import net.codemirror.lib.TextMarker.FromTo; import net.codemirror.lib.TextMarker.FromTo;
import java.util.ArrayList; import java.util.ArrayList;
@@ -92,7 +92,7 @@ class CommentManager {
// on either side of the editor pair. // on either side of the editor pair.
SortedMap<Integer, CommentGroup> map = map(src.side()); SortedMap<Integer, CommentGroup> map = map(src.side());
int line = src.hasActiveLine() int line = src.hasActiveLine()
? src.getLineNumber(src.getActiveLine()) + 1 ? src.getLineNumber(src.activeLine()) + 1
: 0; : 0;
if (dir == Direction.NEXT) { if (dir == Direction.NEXT) {
map = map.tailMap(line + 1); map = map.tailMap(line + 1);
@@ -115,8 +115,8 @@ class CommentManager {
CodeMirror cm = g.getCm(); CodeMirror cm = g.getCm();
double y = cm.heightAtLine(g.getLine() - 1, "local"); double y = cm.heightAtLine(g.getLine() - 1, "local");
cm.setCursor(LineCharacter.create(g.getLine() - 1)); cm.setCursor(Pos.create(g.getLine() - 1));
cm.scrollToY(y - 0.5 * cm.getScrollbarV().getClientHeight()); cm.scrollToY(y - 0.5 * cm.scrollbarV().getClientHeight());
cm.focus(); cm.focus();
} }
}; };
@@ -297,7 +297,7 @@ class CommentManager {
public void run() { public void run() {
if (cm.hasActiveLine()) { if (cm.hasActiveLine()) {
CommentGroup w = map(cm.side()).get( CommentGroup w = map(cm.side()).get(
cm.getLineNumber(cm.getActiveLine()) + 1); cm.getLineNumber(cm.activeLine()) + 1);
if (w != null) { if (w != null) {
w.openCloseLast(); w.openCloseLast();
} }
@@ -312,7 +312,7 @@ class CommentManager {
public void run() { public void run() {
if (cm.hasActiveLine()) { if (cm.hasActiveLine()) {
CommentGroup w = map(cm.side()).get( CommentGroup w = map(cm.side()).get(
cm.getLineNumber(cm.getActiveLine()) + 1); cm.getLineNumber(cm.activeLine()) + 1);
if (w != null) { if (w != null) {
w.openCloseAll(); w.openCloseAll();
} }
@@ -328,7 +328,7 @@ class CommentManager {
public void run() { public void run() {
String token = host.getToken(); String token = host.getToken();
if (cm.hasActiveLine()) { if (cm.hasActiveLine()) {
LineHandle handle = cm.getActiveLine(); LineHandle handle = cm.activeLine();
int line = cm.getLineNumber(handle) + 1; int line = cm.getLineNumber(handle) + 1;
token += "@" + (cm.side() == DisplaySide.A ? "a" : "") + line; token += "@" + (cm.side() == DisplaySide.A ? "a" : "") + line;
} }
@@ -348,13 +348,13 @@ class CommentManager {
} }
private void newDraft(CodeMirror cm) { private void newDraft(CodeMirror cm) {
int line = cm.getLineNumber(cm.getActiveLine()) + 1; int line = cm.getLineNumber(cm.activeLine()) + 1;
if (cm.somethingSelected()) { if (cm.somethingSelected()) {
FromTo fromTo = cm.getSelectedRange(); FromTo fromTo = cm.getSelectedRange();
LineCharacter end = fromTo.getTo(); Pos end = fromTo.to();
if (end.getCh() == 0) { if (end.ch() == 0) {
end.setLine(end.getLine() - 1); end.line(end.line() - 1);
end.setCh(cm.getLine(end.getLine()).length()); end.ch(cm.getLine(end.line()).length());
} }
addDraftBox(cm.side(), CommentInfo.create( addDraftBox(cm.side(), CommentInfo.create(

View File

@@ -16,7 +16,7 @@ package com.google.gerrit.client.diff;
import com.google.gwt.core.client.JavaScriptObject; import com.google.gwt.core.client.JavaScriptObject;
import net.codemirror.lib.LineCharacter; import net.codemirror.lib.Pos;
import net.codemirror.lib.TextMarker.FromTo; import net.codemirror.lib.TextMarker.FromTo;
public class CommentRange extends JavaScriptObject { public class CommentRange extends JavaScriptObject {
@@ -31,11 +31,11 @@ public class CommentRange extends JavaScriptObject {
return null; return null;
} }
LineCharacter from = fromTo.getFrom(); Pos from = fromTo.from();
LineCharacter to = fromTo.getTo(); Pos to = fromTo.to();
return create( return create(
from.getLine() + 1, from.getCh(), from.line() + 1, from.ch(),
to.getLine() + 1, to.getCh()); to.line() + 1, to.ch());
} }
public final native int start_line() /*-{ return this.start_line; }-*/; public final native int start_line() /*-{ return this.start_line; }-*/;

View File

@@ -243,7 +243,7 @@ class DraftBox extends CommentBox {
private void restoreSelection() { private void restoreSelection() {
if (getFromTo() != null && comment.in_reply_to() == null) { if (getFromTo() != null && comment.in_reply_to() == null) {
getCm().setSelection(getFromTo().getFrom(), getFromTo().getTo()); getCm().setSelection(getFromTo().from(), getFromTo().to());
} }
} }

View File

@@ -16,7 +16,7 @@ package com.google.gerrit.client.diff;
import com.google.gwt.core.client.JsArrayString; import com.google.gwt.core.client.JsArrayString;
import net.codemirror.lib.LineCharacter; import net.codemirror.lib.Pos;
/** An iterator for intraline edits */ /** An iterator for intraline edits */
class EditIterator { class EditIterator {
@@ -30,13 +30,13 @@ class EditIterator {
startLine = start; startLine = start;
} }
LineCharacter advance(int numOfChar) { Pos advance(int numOfChar) {
numOfChar = adjustForNegativeDelta(numOfChar); numOfChar = adjustForNegativeDelta(numOfChar);
while (line < lines.length()) { while (line < lines.length()) {
int len = lines.get(line).length() - pos + 1; // + 1 for LF int len = lines.get(line).length() - pos + 1; // + 1 for LF
if (numOfChar < len) { if (numOfChar < len) {
LineCharacter at = LineCharacter.create( Pos at = Pos.create(
startLine + line, startLine + line,
numOfChar + pos); numOfChar + pos);
pos += numOfChar; pos += numOfChar;
@@ -48,7 +48,7 @@ class EditIterator {
pos = 0; pos = 0;
if (numOfChar == 0) { if (numOfChar == 0) {
return LineCharacter.create(startLine + line, 0); return Pos.create(startLine + line, 0);
} }
} }

View File

@@ -43,9 +43,9 @@ class ScrollSynchronizer {
} }
private void updateScreenHeader(ScrollInfo si) { private void updateScreenHeader(ScrollInfo si) {
if (si.getTop() == 0 && !diffTable.isHeaderVisible()) { if (si.top() == 0 && !diffTable.isHeaderVisible()) {
diffTable.setHeaderVisible(true); diffTable.setHeaderVisible(true);
} else if (si.getTop() > 0.5 * si.getClientHeight() } else if (si.top() > 0.5 * si.clientHeight()
&& diffTable.isHeaderVisible()) { && diffTable.isHeaderVisible()) {
diffTable.setHeaderVisible(false); diffTable.setHeaderVisible(false);
} }
@@ -73,7 +73,7 @@ class ScrollSynchronizer {
} }
void sync() { void sync() {
dst.scrollToY(align(src.getScrollInfo().getTop())); dst.scrollToY(align(src.getScrollInfo().top()));
} }
@Override @Override
@@ -85,7 +85,7 @@ class ScrollSynchronizer {
if (active == this) { if (active == this) {
ScrollInfo si = src.getScrollInfo(); ScrollInfo si = src.getScrollInfo();
updateScreenHeader(si); updateScreenHeader(si);
dst.scrollTo(si.getLeft(), align(si.getTop())); dst.scrollTo(si.left(), align(si.top()));
state = 0; state = 0;
} }
} }
@@ -94,7 +94,7 @@ class ScrollSynchronizer {
switch (state) { switch (state) {
case 0: case 0:
state = 1; state = 1;
dst.scrollToY(align(src.getScrollInfo().getTop())); dst.scrollToY(align(src.getScrollInfo().top()));
break; break;
case 1: case 1:
state = 2; state = 2;

View File

@@ -17,6 +17,7 @@ package com.google.gerrit.client.diff;
import com.google.gwt.resources.client.CssResource; import com.google.gwt.resources.client.CssResource;
import net.codemirror.lib.CodeMirror; import net.codemirror.lib.CodeMirror;
import net.codemirror.lib.Pos;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@@ -84,7 +85,7 @@ class Scrollbar {
private ScrollbarAnnotation diff(CodeMirror cm, int s, int n) { private ScrollbarAnnotation diff(CodeMirror cm, int s, int n) {
ScrollbarAnnotation a = new ScrollbarAnnotation(cm); ScrollbarAnnotation a = new ScrollbarAnnotation(cm);
a.at(CodeMirror.pos(s), CodeMirror.pos(s + n)); a.at(Pos.create(s), Pos.create(s + n));
diff.add(a); diff.add(a);
return a; return a;
} }

View File

@@ -23,15 +23,15 @@ import com.google.gwt.user.client.ui.Widget;
import net.codemirror.lib.CodeMirror; import net.codemirror.lib.CodeMirror;
import net.codemirror.lib.CodeMirror.RegisteredHandler; import net.codemirror.lib.CodeMirror.RegisteredHandler;
import net.codemirror.lib.LineCharacter; import net.codemirror.lib.Pos;
/** Displayed on the vertical scrollbar to place a chunk or comment. */ /** Displayed on the vertical scrollbar to place a chunk or comment. */
class ScrollbarAnnotation extends Widget implements ClickHandler { class ScrollbarAnnotation extends Widget implements ClickHandler {
private final CodeMirror cm; private final CodeMirror cm;
private CodeMirror cmB; private CodeMirror cmB;
private RegisteredHandler refresh; private RegisteredHandler refresh;
private LineCharacter from; private Pos from;
private LineCharacter to; private Pos to;
private double scale; private double scale;
ScrollbarAnnotation(CodeMirror cm) { ScrollbarAnnotation(CodeMirror cm) {
@@ -47,10 +47,10 @@ class ScrollbarAnnotation extends Widget implements ClickHandler {
} }
void at(int line) { void at(int line) {
at(CodeMirror.pos(line), CodeMirror.pos(line + 1)); at(Pos.create(line), Pos.create(line + 1));
} }
void at(LineCharacter from, LineCharacter to) { void at(Pos from, Pos to) {
this.from = from; this.from = from;
this.to = to; this.to = to;
} }
@@ -101,8 +101,8 @@ class ScrollbarAnnotation extends Widget implements ClickHandler {
public void onClick(ClickEvent event) { public void onClick(ClickEvent event) {
event.stopPropagation(); event.stopPropagation();
int line = from.getLine(); int line = from.line();
int h = to.getLine() - line; int h = to.line() - line;
if (h > 5) { if (h > 5) {
// Map click inside of the annotation to the relative position // Map click inside of the annotation to the relative position
// within the region covered by the annotation. // within the region covered by the annotation.
@@ -111,7 +111,7 @@ class ScrollbarAnnotation extends Widget implements ClickHandler {
} }
double y = cm.heightAtLine(line, "local"); double y = cm.heightAtLine(line, "local");
double viewport = cm.getScrollInfo().getClientHeight(); double viewport = cm.getScrollInfo().clientHeight();
cm.setCursor(from); cm.setCursor(from);
cm.scrollTo(0, y - 0.5 * viewport); cm.scrollTo(0, y - 0.5 * viewport);
cm.focus(); cm.focus();

View File

@@ -75,8 +75,8 @@ import net.codemirror.lib.CodeMirror.LineClassWhere;
import net.codemirror.lib.CodeMirror.LineHandle; import net.codemirror.lib.CodeMirror.LineHandle;
import net.codemirror.lib.Configuration; import net.codemirror.lib.Configuration;
import net.codemirror.lib.KeyMap; import net.codemirror.lib.KeyMap;
import net.codemirror.lib.LineCharacter;
import net.codemirror.lib.ModeInjector; import net.codemirror.lib.ModeInjector;
import net.codemirror.lib.Pos;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
@@ -294,10 +294,10 @@ public class SideBySide2 extends Screen {
if (cm.lineAtHeight(height - 20) < line) { if (cm.lineAtHeight(height - 20) < line) {
cm.scrollToY(cm.heightAtLine(line, "local") - 0.5 * height); cm.scrollToY(cm.heightAtLine(line, "local") - 0.5 * height);
} }
cm.setCursor(LineCharacter.create(line)); cm.setCursor(Pos.create(line));
cm.focus(); cm.focus();
} else { } else {
cmA.setCursor(LineCharacter.create(0)); cmA.setCursor(Pos.create(0));
cmA.focus(); cmA.focus();
} }
if (Gerrit.isSignedIn() && prefs.autoReview()) { if (Gerrit.isSignedIn() && prefs.autoReview()) {
@@ -393,19 +393,19 @@ public class SideBySide2 extends Screen {
.on("Space", new Runnable() { .on("Space", new Runnable() {
@Override @Override
public void run() { public void run() {
CodeMirror.handleVimKey(cm, "<C-d>"); cm.vim().handleKey("<C-d>");
} }
}) })
.on("Shift-Space", new Runnable() { .on("Shift-Space", new Runnable() {
@Override @Override
public void run() { public void run() {
CodeMirror.handleVimKey(cm, "<C-u>"); cm.vim().handleKey("<C-u>");
} }
}) })
.on("Ctrl-F", new Runnable() { .on("Ctrl-F", new Runnable() {
@Override @Override
public void run() { public void run() {
CodeMirror.handleVimKey(cm, "/"); cm.vim().handleKey("/");
} }
}) })
.on("Ctrl-A", new Runnable() { .on("Ctrl-A", new Runnable() {
@@ -424,10 +424,8 @@ public class SideBySide2 extends Screen {
private InsertCommentBubble bubble; private InsertCommentBubble bubble;
@Override @Override
public void handle(CodeMirror cm, LineCharacter anchor, LineCharacter head) { public void handle(CodeMirror cm, Pos anchor, Pos head) {
if (anchor == head if (anchor.equals(head)) {
|| (anchor.getLine() == head.getLine()
&& anchor.getCh() == head.getCh())) {
if (bubble != null) { if (bubble != null) {
bubble.setVisible(false); bubble.setVisible(false);
} }
@@ -440,10 +438,10 @@ public class SideBySide2 extends Screen {
bubble.position(cm.charCoords(head, "local")); bubble.position(cm.charCoords(head, "local"));
} }
private void init(LineCharacter anchor) { private void init(Pos anchor) {
bubble = new InsertCommentBubble(commentManager, cm); bubble = new InsertCommentBubble(commentManager, cm);
add(bubble); add(bubble);
cm.addWidget(anchor, bubble.getElement(), false); cm.addWidget(anchor, bubble.getElement());
} }
}; };
} }
@@ -551,8 +549,8 @@ public class SideBySide2 extends Screen {
diffTable.addStyleName(DiffTable.style.showLineNumbers()); diffTable.addStyleName(DiffTable.style.showLineNumbers());
} }
cmA = newCM(diff.meta_a(), diff.text_a(), DisplaySide.A, diffTable.cmA); cmA = newCM(diff.meta_a(), diff.text_a(), diffTable.cmA).side(DisplaySide.A);
cmB = newCM(diff.meta_b(), diff.text_b(), DisplaySide.B, diffTable.cmB); cmB = newCM(diff.meta_b(), diff.text_b(), diffTable.cmB).side(DisplaySide.B);
chunkManager = new ChunkManager(this, cmA, cmB, diffTable.scrollbar); chunkManager = new ChunkManager(this, cmA, cmB, diffTable.scrollbar);
skipManager = new SkipManager(this, commentManager); skipManager = new SkipManager(this, commentManager);
@@ -560,8 +558,8 @@ public class SideBySide2 extends Screen {
columnMarginB = DOM.createDiv(); columnMarginB = DOM.createDiv();
columnMarginA.setClassName(DiffTable.style.columnMargin()); columnMarginA.setClassName(DiffTable.style.columnMargin());
columnMarginB.setClassName(DiffTable.style.columnMargin()); columnMarginB.setClassName(DiffTable.style.columnMargin());
cmA.getMoverElement().appendChild(columnMarginA); cmA.mover().appendChild(columnMarginA);
cmB.getMoverElement().appendChild(columnMarginB); cmB.mover().appendChild(columnMarginB);
if (prefs.renderEntireFile() && !canEnableRenderEntireFile(prefs)) { if (prefs.renderEntireFile() && !canEnableRenderEntireFile(prefs)) {
// CodeMirror is too slow to layout an entire huge file. // CodeMirror is too slow to layout an entire huge file.
@@ -636,18 +634,14 @@ public class SideBySide2 extends Screen {
private CodeMirror newCM( private CodeMirror newCM(
DiffInfo.FileMeta meta, DiffInfo.FileMeta meta,
String contents, String contents,
DisplaySide side,
Element parent) { Element parent) {
String mode = fileSize == FileSize.SMALL return CodeMirror.create(parent, Configuration.create()
? getContentType(meta)
: null;
return CodeMirror.create(side, parent, Configuration.create()
.set("readOnly", true) .set("readOnly", true)
.set("cursorBlinkRate", 0) .set("cursorBlinkRate", 0)
.set("cursorHeight", 0.85) .set("cursorHeight", 0.85)
.set("lineNumbers", prefs.showLineNumbers()) .set("lineNumbers", prefs.showLineNumbers())
.set("tabSize", prefs.tabSize()) .set("tabSize", prefs.tabSize())
.set("mode", mode) .set("mode", fileSize == FileSize.SMALL ? getContentType(meta) : null)
.set("lineWrapping", false) .set("lineWrapping", false)
.set("scrollbarStyle", "overlay") .set("scrollbarStyle", "overlay")
.set("styleSelectedText", true) .set("styleSelectedText", true)
@@ -706,7 +700,7 @@ public class SideBySide2 extends Screen {
e.appendChild(pre); e.appendChild(pre);
} }
cmB.getMeasureElement().appendChild(p); cmB.measure().appendChild(p);
lineHeightPx = ((double) p.getOffsetHeight()) / lines; lineHeightPx = ((double) p.getOffsetHeight()) / lines;
p.removeFromParent(); p.removeFromParent();
} }
@@ -724,11 +718,11 @@ public class SideBySide2 extends Screen {
e.getStyle().setDisplay(Style.Display.INLINE_BLOCK); e.getStyle().setDisplay(Style.Display.INLINE_BLOCK);
e.setInnerText(s.toString()); e.setInnerText(s.toString());
cmA.getMeasureElement().appendChild(e); cmA.measure().appendChild(e);
double a = ((double) e.getOffsetWidth()) / len; double a = ((double) e.getOffsetWidth()) / len;
e.removeFromParent(); e.removeFromParent();
cmB.getMeasureElement().appendChild(e); cmB.measure().appendChild(e);
double b = ((double) e.getOffsetWidth()) / len; double b = ((double) e.getOffsetWidth()) / len;
e.removeFromParent(); e.removeFromParent();
charWidthPx = Math.max(a, b); charWidthPx = Math.max(a, b);
@@ -817,10 +811,10 @@ public class SideBySide2 extends Screen {
private void clearActiveLine(CodeMirror cm) { private void clearActiveLine(CodeMirror cm) {
if (cm.hasActiveLine()) { if (cm.hasActiveLine()) {
LineHandle activeLine = cm.getActiveLine(); LineHandle activeLine = cm.activeLine();
cm.removeLineClass(activeLine, cm.removeLineClass(activeLine,
LineClassWhere.WRAP, DiffTable.style.activeLine()); LineClassWhere.WRAP, DiffTable.style.activeLine());
cm.setActiveLine(null); cm.activeLine(null);
} }
} }
@@ -840,22 +834,22 @@ public class SideBySide2 extends Screen {
operation(new Runnable() { operation(new Runnable() {
@Override @Override
public void run() { public void run() {
LineHandle handle = cm.getLineHandleVisualStart( LineHandle handle =
cm.getCursor("end").getLine()); cm.getLineHandleVisualStart(cm.getCursor("end").line());
if (cm.hasActiveLine() && cm.getActiveLine().equals(handle)) { if (cm.hasActiveLine() && cm.activeLine().equals(handle)) {
return; return;
} }
clearActiveLine(cm); clearActiveLine(cm);
clearActiveLine(other); clearActiveLine(other);
cm.setActiveLine(handle); cm.activeLine(handle);
cm.addLineClass( cm.addLineClass(
handle, LineClassWhere.WRAP, DiffTable.style.activeLine()); handle, LineClassWhere.WRAP, DiffTable.style.activeLine());
LineOnOtherInfo info = LineOnOtherInfo info =
lineOnOther(cm.side(), cm.getLineNumber(handle)); lineOnOther(cm.side(), cm.getLineNumber(handle));
if (info.isAligned()) { if (info.isAligned()) {
LineHandle oLineHandle = other.getLineHandle(info.getLine()); LineHandle oLineHandle = other.getLineHandle(info.getLine());
other.setActiveLine(oLineHandle); other.activeLine(oLineHandle);
other.addLineClass(oLineHandle, LineClassWhere.WRAP, other.addLineClass(oLineHandle, LineClassWhere.WRAP,
DiffTable.style.activeLine()); DiffTable.style.activeLine());
} }
@@ -878,8 +872,8 @@ public class SideBySide2 extends Screen {
&& !clickEvent.getCtrlKey() && !clickEvent.getCtrlKey()
&& !clickEvent.getShiftKey()) { && !clickEvent.getShiftKey()) {
if (!(cm.hasActiveLine() && if (!(cm.hasActiveLine() &&
cm.getLineNumber(cm.getActiveLine()) == line)) { cm.getLineNumber(cm.activeLine()) == line)) {
cm.setCursor(LineCharacter.create(line)); cm.setCursor(Pos.create(line));
} }
Scheduler.get().scheduleDeferred(new ScheduledCommand() { Scheduler.get().scheduleDeferred(new ScheduledCommand() {
@Override @Override
@@ -929,9 +923,9 @@ public class SideBySide2 extends Screen {
@Override @Override
public void run() { public void run() {
if (cmSrc.hasActiveLine()) { if (cmSrc.hasActiveLine()) {
cmDst.setCursor(LineCharacter.create(lineOnOther( cmDst.setCursor(Pos.create(lineOnOther(
sideSrc, sideSrc,
cmSrc.getLineNumber(cmSrc.getActiveLine())).getLine())); cmSrc.getLineNumber(cmSrc.activeLine())).getLine()));
} }
cmDst.focus(); cmDst.focus();
} }
@@ -942,8 +936,8 @@ public class SideBySide2 extends Screen {
return new Runnable() { return new Runnable() {
@Override @Override
public void run() { public void run() {
if (cm.hasVimSearchHighlight()) { if (cm.vim().hasSearchHighlight()) {
CodeMirror.handleVimKey(cm, "N"); cm.vim().handleKey("N");
} else { } else {
commentManager.commentNav(cm, Direction.NEXT).run(); commentManager.commentNav(cm, Direction.NEXT).run();
} }
@@ -955,8 +949,8 @@ public class SideBySide2 extends Screen {
return new Runnable() { return new Runnable() {
@Override @Override
public void run() { public void run() {
if (cm.hasVimSearchHighlight()) { if (cm.vim().hasSearchHighlight()) {
CodeMirror.handleVimKey(cm, "n"); cm.vim().handleKey("n");
} else { } else {
chunkManager.diffChunkNav(cm, Direction.NEXT).run(); chunkManager.diffChunkNav(cm, Direction.NEXT).run();
} }

View File

@@ -30,6 +30,7 @@ import com.google.gwt.user.client.ui.HTMLPanel;
import net.codemirror.lib.CodeMirror; import net.codemirror.lib.CodeMirror;
import net.codemirror.lib.Configuration; import net.codemirror.lib.Configuration;
import net.codemirror.lib.LineWidget; import net.codemirror.lib.LineWidget;
import net.codemirror.lib.Pos;
import net.codemirror.lib.TextMarker; import net.codemirror.lib.TextMarker;
import net.codemirror.lib.TextMarker.FromTo; import net.codemirror.lib.TextMarker.FromTo;
@@ -95,8 +96,8 @@ class SkipBar extends Composite {
} }
textMarker = cm.markText( textMarker = cm.markText(
CodeMirror.pos(start, 0), Pos.create(start, 0),
CodeMirror.pos(end), Pos.create(end),
Configuration.create() Configuration.create()
.set("collapsed", true) .set("collapsed", true)
.set("inclusiveLeft", true) .set("inclusiveLeft", true)
@@ -137,9 +138,9 @@ class SkipBar extends Composite {
private void expandSideBefore(int cnt) { private void expandSideBefore(int cnt) {
FromTo range = textMarker.find(); FromTo range = textMarker.find();
int oldStart = range.getFrom().getLine(); int oldStart = range.from().line();
int newStart = oldStart + cnt; int newStart = oldStart + cnt;
int end = range.getTo().getLine(); int end = range.to().line();
clearMarkerAndWidget(); clearMarkerAndWidget();
collapse(newStart, end, true); collapse(newStart, end, true);
updateSelection(); updateSelection();
@@ -152,8 +153,8 @@ class SkipBar extends Composite {
private void expandAfter() { private void expandAfter() {
FromTo range = textMarker.find(); FromTo range = textMarker.find();
int start = range.getFrom().getLine(); int start = range.from().line();
int oldEnd = range.getTo().getLine(); int oldEnd = range.to().line();
int newEnd = oldEnd - NUM_ROWS_TO_EXPAND; int newEnd = oldEnd - NUM_ROWS_TO_EXPAND;
boolean attach = start == 0; boolean attach = start == 0;
if (attach) { if (attach) {
@@ -168,7 +169,7 @@ class SkipBar extends Composite {
private void updateSelection() { private void updateSelection() {
if (cm.somethingSelected()) { if (cm.somethingSelected()) {
FromTo sel = cm.getSelectedRange(); FromTo sel = cm.getSelectedRange();
cm.setSelection(sel.getFrom(), sel.getTo()); cm.setSelection(sel.from(), sel.to());
} }
} }

View File

@@ -32,153 +32,146 @@ public class CodeMirror extends JavaScriptObject {
Loader.initLibrary(cb); Loader.initLibrary(cb);
} }
public static native CodeMirror create( public static native CodeMirror create(Element p, Configuration cfg) /*-{
DisplaySide side, return $wnd.CodeMirror(p, cfg);
Element parent,
Configuration cfg) /*-{
var m = $wnd.CodeMirror(parent, cfg);
m._sbs2_side = side;
return m;
}-*/; }-*/;
public static CodeMirror create(Element parent, Configuration cfg) {
return create(null, parent, cfg);
}
public final native void setOption(String option, boolean value) /*-{ public final native void setOption(String option, boolean value) /*-{
this.setOption(option, value); this.setOption(option, value)
}-*/; }-*/;
public final native void setOption(String option, double value) /*-{ public final native void setOption(String option, double value) /*-{
this.setOption(option, value); this.setOption(option, value)
}-*/; }-*/;
public final native void setOption(String option, String value) /*-{ public final native void setOption(String option, String value) /*-{
this.setOption(option, value); this.setOption(option, value)
}-*/; }-*/;
public final native void setOption(String option, JavaScriptObject val) /*-{ public final native void setOption(String option, JavaScriptObject val) /*-{
this.setOption(option, val); this.setOption(option, val)
}-*/; }-*/;
public final native String getStringOption(String o) /*-{ return this.getOption(o) }-*/; public final native String getStringOption(String o) /*-{ return this.getOption(o) }-*/;
public final native void setValue(String v) /*-{ this.setValue(v); }-*/;
public final native void setWidth(double w) /*-{ this.setSize(w, null); }-*/; public final native String getValue() /*-{ return this.getValue() }-*/;
public final native void setWidth(String w) /*-{ this.setSize(w, null); }-*/; public final native void setValue(String v) /*-{ this.setValue(v) }-*/;
public final native void setHeight(double h) /*-{ this.setSize(null, h); }-*/;
public final native void setHeight(String h) /*-{ this.setSize(null, h); }-*/; public final native void setWidth(double w) /*-{ this.setSize(w, null) }-*/;
public final native void setWidth(String w) /*-{ this.setSize(w, null) }-*/;
public final native void setHeight(double h) /*-{ this.setSize(null, h) }-*/;
public final native void setHeight(String h) /*-{ this.setSize(null, h) }-*/;
public final native String getLine(int n) /*-{ return this.getLine(n) }-*/; public final native String getLine(int n) /*-{ return this.getLine(n) }-*/;
public final native double barHeight() /*-{ return this.display.barHeight }-*/; public final native double barHeight() /*-{ return this.display.barHeight }-*/;
public final native double barWidth() /*-{ return this.display.barWidth }-*/; public final native double barWidth() /*-{ return this.display.barWidth }-*/;
public final native int lastLine() /*-{ return this.lastLine() }-*/; public final native int lastLine() /*-{ return this.lastLine() }-*/;
public final native void refresh() /*-{ this.refresh() }-*/;
public final native void refresh() /*-{ this.refresh(); }-*/; public final native TextMarker markText(Pos from, Pos to,
public final native Element getWrapperElement() /*-{ return this.getWrapperElement(); }-*/;
public final native TextMarker markText(LineCharacter from, LineCharacter to,
Configuration options) /*-{ Configuration options) /*-{
return this.markText(from, to, options); return this.markText(from, to, options)
}-*/; }-*/;
public enum LineClassWhere { public enum LineClassWhere {
TEXT, BACKGROUND, WRAP TEXT { @Override String value() { return "text"; } },
BACKGROUND { @Override String value() { return "background"; } },
WRAP { @Override String value() { return "wrap"; } };
abstract String value();
} }
public final void addLineClass(int line, LineClassWhere where, public final void addLineClass(int line, LineClassWhere where,
String className) { String className) {
addLineClassNative(line, where.name().toLowerCase(), className); addLineClassNative(line, where.value(), className);
} }
private final native void addLineClassNative(int line, String where, private final native void addLineClassNative(int line, String where,
String lineClass) /*-{ String lineClass) /*-{
this.addLineClass(line, where, lineClass); this.addLineClass(line, where, lineClass)
}-*/; }-*/;
public final void addLineClass(LineHandle line, LineClassWhere where, public final void addLineClass(LineHandle line, LineClassWhere where,
String className) { String className) {
addLineClassNative(line, where.name().toLowerCase(), className); addLineClassNative(line, where.value(), className);
} }
private final native void addLineClassNative(LineHandle line, String where, private final native void addLineClassNative(LineHandle line, String where,
String lineClass) /*-{ String lineClass) /*-{
this.addLineClass(line, where, lineClass); this.addLineClass(line, where, lineClass)
}-*/; }-*/;
public final void removeLineClass(int line, LineClassWhere where, public final void removeLineClass(int line, LineClassWhere where,
String className) { String className) {
removeLineClassNative(line, where.name().toLowerCase(), className); removeLineClassNative(line, where.value(), className);
} }
private final native void removeLineClassNative(int line, String where, private final native void removeLineClassNative(int line, String where,
String lineClass) /*-{ String lineClass) /*-{
this.removeLineClass(line, where, lineClass); this.removeLineClass(line, where, lineClass)
}-*/; }-*/;
public final void removeLineClass(LineHandle line, LineClassWhere where, public final void removeLineClass(LineHandle line, LineClassWhere where,
String className) { String className) {
removeLineClassNative(line, where.name().toLowerCase(), className); removeLineClassNative(line, where.value(), className);
} }
private final native void removeLineClassNative(LineHandle line, String where, private final native void removeLineClassNative(LineHandle line, String where,
String lineClass) /*-{ String lineClass) /*-{
this.removeLineClass(line, where, lineClass); this.removeLineClass(line, where, lineClass)
}-*/; }-*/;
public final native void addWidget(LineCharacter pos, Element node, public final native void addWidget(Pos pos, Element node) /*-{
boolean scrollIntoView) /*-{ this.addWidget(pos, node, false)
this.addWidget(pos, node, scrollIntoView);
}-*/; }-*/;
public final native LineWidget addLineWidget(int line, Element node, public final native LineWidget addLineWidget(int line, Element node,
Configuration options) /*-{ Configuration options) /*-{
return this.addLineWidget(line, node, options); return this.addLineWidget(line, node, options)
}-*/; }-*/;
public final native int lineAtHeight(double height) /*-{ public final native int lineAtHeight(double height) /*-{
return this.lineAtHeight(height); return this.lineAtHeight(height)
}-*/; }-*/;
public final native int lineAtHeight(double height, String mode) /*-{ public final native int lineAtHeight(double height, String mode) /*-{
return this.lineAtHeight(height, mode); return this.lineAtHeight(height, mode)
}-*/; }-*/;
public final native double heightAtLine(int line) /*-{ public final native double heightAtLine(int line) /*-{
return this.heightAtLine(line); return this.heightAtLine(line)
}-*/; }-*/;
public final native double heightAtLine(int line, String mode) /*-{ public final native double heightAtLine(int line, String mode) /*-{
return this.heightAtLine(line, mode); return this.heightAtLine(line, mode)
}-*/; }-*/;
public final native Rect charCoords(LineCharacter pos, String mode) /*-{ public final native Rect charCoords(Pos pos, String mode) /*-{
return this.charCoords(pos, mode); return this.charCoords(pos, mode)
}-*/; }-*/;
public final native CodeMirrorDoc getDoc() /*-{ public final native CodeMirrorDoc getDoc() /*-{
return this.getDoc(); return this.getDoc()
}-*/; }-*/;
public final native void scrollTo(double x, double y) /*-{ public final native void scrollTo(double x, double y) /*-{
this.scrollTo(x, y); this.scrollTo(x, y)
}-*/; }-*/;
public final native void scrollToY(double y) /*-{ public final native void scrollToY(double y) /*-{
this.scrollTo(null, y); this.scrollTo(null, y)
}-*/; }-*/;
public final native ScrollInfo getScrollInfo() /*-{ public final native ScrollInfo getScrollInfo() /*-{
return this.getScrollInfo(); return this.getScrollInfo()
}-*/; }-*/;
public final native Viewport getViewport() /*-{ public final native Viewport getViewport() /*-{
return this.getViewport(); return this.getViewport()
}-*/; }-*/;
public final native void operation(Runnable thunk) /*-{ public final native void operation(Runnable thunk) /*-{
this.operation(function() { this.operation(function() {
thunk.@java.lang.Runnable::run()(); thunk.@java.lang.Runnable::run()();
}); })
}-*/; }-*/;
public final native void off(String event, RegisteredHandler h) /*-{ public final native void off(String event, RegisteredHandler h) /*-{
@@ -194,120 +187,112 @@ public class CodeMirror extends JavaScriptObject {
public final native void on(String event, EventHandler handler) /*-{ public final native void on(String event, EventHandler handler) /*-{
this.on(event, $entry(function(cm, e) { this.on(event, $entry(function(cm, e) {
handler.@net.codemirror.lib.CodeMirror.EventHandler::handle( handler.@net.codemirror.lib.CodeMirror.EventHandler::handle(
Lnet/codemirror/lib/CodeMirror;Lcom/google/gwt/dom/client/NativeEvent;)(cm, e); Lnet/codemirror/lib/CodeMirror;
})); Lcom/google/gwt/dom/client/NativeEvent;)(cm, e);
}))
}-*/; }-*/;
public final native void on(String event, RenderLineHandler handler) /*-{ public final native void on(String event, RenderLineHandler handler) /*-{
this.on(event, $entry(function(cm, h, ele) { this.on(event, $entry(function(cm, h, e) {
handler.@net.codemirror.lib.CodeMirror.RenderLineHandler::handle( handler.@net.codemirror.lib.CodeMirror.RenderLineHandler::handle(
Lnet/codemirror/lib/CodeMirror;Lnet/codemirror/lib/CodeMirror$LineHandle; Lnet/codemirror/lib/CodeMirror;
Lcom/google/gwt/dom/client/Element;)(cm, h, ele); Lnet/codemirror/lib/CodeMirror$LineHandle;
})); Lcom/google/gwt/dom/client/Element;)(cm, h, e);
}))
}-*/; }-*/;
public final native void on(String event, GutterClickHandler handler) /*-{ public final native void on(String event, GutterClickHandler handler) /*-{
this.on(event, $entry(function(cm, l, g, e) { this.on(event, $entry(function(cm, l, g, e) {
handler.@net.codemirror.lib.CodeMirror.GutterClickHandler::handle( handler.@net.codemirror.lib.CodeMirror.GutterClickHandler::handle(
Lnet/codemirror/lib/CodeMirror;ILjava/lang/String; Lnet/codemirror/lib/CodeMirror;
I
Ljava/lang/String;
Lcom/google/gwt/dom/client/NativeEvent;)(cm, l, g, e); Lcom/google/gwt/dom/client/NativeEvent;)(cm, l, g, e);
})); }))
}-*/; }-*/;
public final native void on(String event, BeforeSelectionChangeHandler handler) /*-{ public final native void on(String event, BeforeSelectionChangeHandler handler) /*-{
this.on(event, $entry(function(cm, o) { this.on(event, $entry(function(cm, o) {
var e = o.ranges[o.ranges.length-1]; var e = o.ranges[o.ranges.length-1];
handler.@net.codemirror.lib.CodeMirror.BeforeSelectionChangeHandler::handle( handler.@net.codemirror.lib.CodeMirror.BeforeSelectionChangeHandler::handle(
Lnet/codemirror/lib/CodeMirror;Lnet/codemirror/lib/LineCharacter; Lnet/codemirror/lib/CodeMirror;
Lnet/codemirror/lib/LineCharacter;)(cm,e.anchor,e.head); Lnet/codemirror/lib/Pos;
})); Lnet/codemirror/lib/Pos;)(cm, e.anchor, e.head);
}))
}-*/; }-*/;
public final native LineCharacter getCursor() /*-{ public final native void setCursor(Pos p) /*-{ this.setCursor(p) }-*/;
return this.getCursor(); public final native Pos getCursor() /*-{ return this.getCursor() }-*/;
}-*/; public final native Pos getCursor(String start) /*-{
return this.getCursor(start)
public final native LineCharacter getCursor(String start) /*-{
return this.getCursor(start);
}-*/; }-*/;
public final FromTo getSelectedRange() { public final FromTo getSelectedRange() {
return FromTo.create(getCursor("start"), getCursor("end")); return FromTo.create(getCursor("start"), getCursor("end"));
} }
public final native void setSelection(LineCharacter anchor) /*-{ public final native void setSelection(Pos p) /*-{ this.setSelection(p) }-*/;
this.setSelection(anchor); public final native void setSelection(Pos anchor, Pos head) /*-{
}-*/; this.setSelection(anchor, head)
public final native void setSelection(LineCharacter anchor, LineCharacter head) /*-{
this.setSelection(anchor, head);
}-*/;
public final native void setCursor(LineCharacter lineCh) /*-{
this.setCursor(lineCh);
}-*/; }-*/;
public final native boolean somethingSelected() /*-{ public final native boolean somethingSelected() /*-{
return this.somethingSelected(); return this.somethingSelected()
}-*/; }-*/;
public final native boolean hasActiveLine() /*-{ public final native boolean hasActiveLine() /*-{
return !!this.state.activeLine; return !!this.state.activeLine
}-*/; }-*/;
public final native LineHandle getActiveLine() /*-{ public final native LineHandle activeLine() /*-{
return this.state.activeLine; return this.state.activeLine
}-*/; }-*/;
public final native void setActiveLine(LineHandle line) /*-{ public final native void activeLine(LineHandle line) /*-{
this.state.activeLine = line; this.state.activeLine = line
}-*/; }-*/;
public final native void addKeyMap(KeyMap map) /*-{ this.addKeyMap(map); }-*/; public final native void addKeyMap(KeyMap map) /*-{ this.addKeyMap(map) }-*/;
public final native void removeKeyMap(KeyMap map) /*-{ this.removeKeyMap(map); }-*/; public final native void removeKeyMap(KeyMap map) /*-{ this.removeKeyMap(map) }-*/;
public static final native LineCharacter pos(int line, int ch) /*-{
return $wnd.CodeMirror.Pos(line, ch);
}-*/;
public static final native LineCharacter pos(int line) /*-{
return $wnd.CodeMirror.Pos(line);
}-*/;
public final native LineHandle getLineHandle(int line) /*-{ public final native LineHandle getLineHandle(int line) /*-{
return this.getLineHandle(line); return this.getLineHandle(line)
}-*/; }-*/;
public final native LineHandle getLineHandleVisualStart(int line) /*-{ public final native LineHandle getLineHandleVisualStart(int line) /*-{
return this.getLineHandleVisualStart(line); return this.getLineHandleVisualStart(line)
}-*/; }-*/;
public final native int getLineNumber(LineHandle handle) /*-{ public final native int getLineNumber(LineHandle handle) /*-{
return this.getLineNumber(handle); return this.getLineNumber(handle)
}-*/; }-*/;
public final native void focus() /*-{ public final native void focus() /*-{
this.focus(); this.focus()
}-*/;
public final native Element getWrapperElement() /*-{
return this.getWrapperElement()
}-*/; }-*/;
public final native Element getGutterElement() /*-{ public final native Element getGutterElement() /*-{
return this.getGutterElement(); return this.getGutterElement()
}-*/; }-*/;
public final native Element getSizer() /*-{ public final native Element sizer() /*-{
return this.display.sizer; return this.display.sizer
}-*/; }-*/;
public final native Element getMoverElement() /*-{ public final native Element mover() /*-{
return this.display.mover; return this.display.mover
}-*/; }-*/;
public final native Element getMeasureElement() /*-{ public final native Element measure() /*-{
return this.display.measure; return this.display.measure
}-*/; }-*/;
public final native Element getScrollbarV() /*-{ public final native Element scrollbarV() /*-{
return this.display.scrollbarV; return this.display.scrollbarV
}-*/; }-*/;
public static final native KeyMap cloneKeyMap(String name) /*-{ public static final native KeyMap cloneKeyMap(String name) /*-{
@@ -320,36 +305,31 @@ public class CodeMirror extends JavaScriptObject {
}-*/; }-*/;
public final native void execCommand(String cmd) /*-{ public final native void execCommand(String cmd) /*-{
this.execCommand(cmd); this.execCommand(cmd)
}-*/; }-*/;
public static final native void addKeyMap(String name, KeyMap km) /*-{ public static final native void addKeyMap(String name, KeyMap km) /*-{
$wnd.CodeMirror.keyMap[name] = km; $wnd.CodeMirror.keyMap[name] = km
}-*/; }-*/;
public static final native void handleVimKey(CodeMirror cm, String key) /*-{ public final native Vim vim() /*-{
$wnd.CodeMirror.Vim.handleKey(cm, key); return this;
}-*/;
public static final native void mapVimKey(String alias, String actual) /*-{
$wnd.CodeMirror.Vim.map(alias, actual);
}-*/;
public final native boolean hasVimSearchHighlight() /*-{
return this.state.vim && this.state.vim.searchState_ &&
!!this.state.vim.searchState_.getOverlay();
}-*/; }-*/;
public final native DisplaySide side() /*-{ return this._sbs2_side }-*/; public final native DisplaySide side() /*-{ return this._sbs2_side }-*/;
public final native CodeMirror side(DisplaySide side) /*-{
this._sbs2_side = side;
return this;
}-*/;
protected CodeMirror() { protected CodeMirror() {
} }
public static class Viewport extends JavaScriptObject { public static class Viewport extends JavaScriptObject {
public final native int getFrom() /*-{ return this.from; }-*/; public final native int from() /*-{ return this.from }-*/;
public final native int getTo() /*-{ return this.to; }-*/; public final native int to() /*-{ return this.to }-*/;
public final boolean contains(int line) { public final boolean contains(int line) {
return getFrom() <= line && line < getTo(); return from() <= line && line < to();
} }
protected Viewport() { protected Viewport() {
@@ -380,10 +360,6 @@ public class CodeMirror extends JavaScriptObject {
} }
public interface BeforeSelectionChangeHandler { public interface BeforeSelectionChangeHandler {
public void handle(CodeMirror instance, LineCharacter anchor, LineCharacter head); public void handle(CodeMirror instance, Pos anchor, Pos head);
} }
public final native String getValue() /*-{
return this.getValue();
}-*/;
} }

View File

@@ -20,11 +20,11 @@ import com.google.gwt.core.client.JavaScriptObject;
public class CodeMirrorDoc extends JavaScriptObject { public class CodeMirrorDoc extends JavaScriptObject {
public final native void replaceRange(String replacement, public final native void replaceRange(String replacement,
LineCharacter from, LineCharacter to) /*-{ Pos from, Pos to) /*-{
this.replaceRange(replacement, from, to); this.replaceRange(replacement, from, to);
}-*/; }-*/;
public final native void insertText(String insertion, LineCharacter at) /*-{ public final native void insertText(String insertion, Pos at) /*-{
this.replaceRange(insertion, at); this.replaceRange(insertion, at);
}-*/; }-*/;

View File

@@ -1,44 +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.JavaScriptObject;
/** {line, ch} objects used within CodeMirror. */
public class LineCharacter extends JavaScriptObject {
public static LineCharacter create(int line, int ch) {
return createImpl(line, ch);
}
public static LineCharacter create(int line) {
return createImpl(line, 0);
}
private static LineCharacter createImpl(int line, int ch) {
LineCharacter lineCh = createObject().cast();
lineCh.setLine(line);
lineCh.setCh(ch);
return lineCh;
}
public final native void setLine(int line) /*-{ this.line = line; }-*/;
public final native void setCh(int ch) /*-{ this.ch = ch; }-*/;
public final native int getLine() /*-{ return this.line; }-*/;
public final native int getCh() /*-{ return this.ch; }-*/;
protected LineCharacter() {
}
}

View File

@@ -18,17 +18,13 @@ import com.google.gwt.core.client.JavaScriptObject;
/** LineWidget objects used within CodeMirror. */ /** LineWidget objects used within CodeMirror. */
public class LineWidget extends JavaScriptObject { public class LineWidget extends JavaScriptObject {
public static LineWidget create() { public final native void clear() /*-{ this.clear() }-*/;
return createObject().cast(); public final native void changed() /*-{ this.changed() }-*/;
}
public final native void clear() /*-{ this.clear(); }-*/;
public final native void changed() /*-{ this.changed(); }-*/;
public final native void onRedraw(Runnable thunk) /*-{ public final native void onRedraw(Runnable thunk) /*-{
this.on("redraw", $entry(function() { this.on("redraw", $entry(function() {
thunk.@java.lang.Runnable::run()(); thunk.@java.lang.Runnable::run()();
})); }))
}-*/; }-*/;
public final native void onFirstRedraw(Runnable thunk) /*-{ public final native void onFirstRedraw(Runnable thunk) /*-{

View File

@@ -41,7 +41,7 @@ class Loader {
injectScript(Lib.I.js().getSafeUri(), new GerritCallback<Void>(){ injectScript(Lib.I.js().getSafeUri(), new GerritCallback<Void>(){
@Override @Override
public void onSuccess(Void result) { public void onSuccess(Void result) {
initVimKeys(); Vim.initKeyMap();
cb.onSuccess(null); cb.onSuccess(null);
} }
}); });
@@ -87,28 +87,6 @@ class Loader {
.cast(); .cast();
} }
private static void initVimKeys() {
// TODO: Better custom keybindings, remove temporary navigation hacks.
KeyMap km = CodeMirror.cloneKeyMap("vim");
for (String s : new String[] {
"A", "C", "I", "O", "R", "U",
"Ctrl-C", "Ctrl-O", "Ctrl-P", "Ctrl-S",
"Ctrl-F", "Ctrl-B", "Ctrl-R"}) {
km.remove(s);
}
for (int i = 0; i <= 9; i++) {
km.remove("Ctrl-" + i);
}
CodeMirror.addKeyMap("vim_ro", km);
CodeMirror.mapVimKey("j", "gj");
CodeMirror.mapVimKey("k", "gk");
CodeMirror.mapVimKey("Down", "gj");
CodeMirror.mapVimKey("Up", "gk");
CodeMirror.mapVimKey("<PageUp>", "<C-u>");
CodeMirror.mapVimKey("<PageDown>", "<C-d>");
}
private static void error(Exception e) { private static void error(Exception e) {
Logger log = Logger.getLogger("net.codemirror"); Logger log = Logger.getLogger("net.codemirror");
log.log(Level.SEVERE, "Cannot load portions of CodeMirror", e); log.log(Level.SEVERE, "Cannot load portions of CodeMirror", e);

View File

@@ -0,0 +1,41 @@
// 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.JavaScriptObject;
/** Pos (or {line, ch}) objects used within CodeMirror. */
public class Pos extends JavaScriptObject {
public static final native Pos create(int line) /*-{
return $wnd.CodeMirror.Pos(line)
}-*/;
public static final native Pos create(int line, int ch) /*-{
return $wnd.CodeMirror.Pos(line, ch)
}-*/;
public final native void line(int l) /*-{ this.line = l }-*/;
public final native void ch(int c) /*-{ this.ch = c }-*/;
public final native int line() /*-{ return this.line }-*/;
public final native int ch() /*-{ return this.ch || 0 }-*/;
public final boolean equals(Pos o) {
return this == o || (line() == o.line() && ch() == o.ch());
}
protected Pos() {
}
}

View File

@@ -18,20 +18,20 @@ import com.google.gwt.core.client.JavaScriptObject;
/** Returned by {@link CodeMirror#getScrollInfo()}. */ /** Returned by {@link CodeMirror#getScrollInfo()}. */
public class ScrollInfo extends JavaScriptObject { public class ScrollInfo extends JavaScriptObject {
public final native double getLeft() /*-{ return this.left; }-*/; public final native double left() /*-{ return this.left }-*/;
public final native double getTop() /*-{ return this.top; }-*/; public final native double top() /*-{ return this.top }-*/;
/** /**
* Pixel height of the full content being scrolled. This may only be an * Pixel height of the full content being scrolled. This may only be an
* estimate given by CodeMirror. Line widgets further down in the document may * estimate given by CodeMirror. Line widgets further down in the document may
* not be measured, so line heights can be incorrect until drawn. * not be measured, so line heights can be incorrect until drawn.
*/ */
public final native double getHeight() /*-{ return this.height; }-*/; public final native double height() /*-{ return this.height }-*/;
public final native double getWidth() /*-{ return this.width; }-*/; public final native double width() /*-{ return this.width }-*/;
/** Visible height of the viewport, excluding scrollbars. */ /** Visible height of the viewport, excluding scrollbars. */
public final native double getClientHeight() /*-{ return this.clientHeight; }-*/; public final native double clientHeight() /*-{ return this.clientHeight }-*/;
public final native double getClientWidth() /*-{ return this.clientWidth; }-*/; public final native double clientWidth() /*-{ return this.clientWidth }-*/;
protected ScrollInfo() { protected ScrollInfo() {
} }

View File

@@ -19,10 +19,6 @@ import com.google.gwt.core.client.JavaScriptObject;
/** Object that represents a text marker within CodeMirror */ /** Object that represents a text marker within CodeMirror */
public class TextMarker extends JavaScriptObject { public class TextMarker extends JavaScriptObject {
public static TextMarker create() {
return createObject().cast();
}
public final native void clear() /*-{ this.clear(); }-*/; public final native void clear() /*-{ this.clear(); }-*/;
public final native void changed() /*-{ this.changed(); }-*/; public final native void changed() /*-{ this.changed(); }-*/;
public final native FromTo find() /*-{ return this.find(); }-*/; public final native FromTo find() /*-{ return this.find(); }-*/;
@@ -33,24 +29,21 @@ public class TextMarker extends JavaScriptObject {
} }
public static class FromTo extends JavaScriptObject { public static class FromTo extends JavaScriptObject {
public static FromTo create(LineCharacter from, LineCharacter to) { public static final native FromTo create(Pos f, Pos t) /*-{
FromTo fromTo = createObject().cast(); return {from: f, to: t}
fromTo.setFrom(from); }-*/;
fromTo.setTo(to);
return fromTo;
}
public static FromTo create(CommentRange range) { public static FromTo create(CommentRange range) {
return create( return create(
LineCharacter.create(range.start_line() - 1, range.start_character()), Pos.create(range.start_line() - 1, range.start_character()),
LineCharacter.create(range.end_line() - 1, range.end_character())); Pos.create(range.end_line() - 1, range.end_character()));
} }
public final native LineCharacter getFrom() /*-{ return this.from; }-*/; public final native Pos from() /*-{ return this.from }-*/;
public final native LineCharacter getTo() /*-{ return this.to; }-*/; public final native Pos to() /*-{ return this.to }-*/;
public final native void setFrom(LineCharacter from) /*-{ this.from = from; }-*/; public final native void from(Pos f) /*-{ this.from = f }-*/;
public final native void setTo(LineCharacter to) /*-{ this.to = to; }-*/; public final native void to(Pos t) /*-{ this.to = t }-*/;
protected FromTo() { protected FromTo() {
} }

View File

@@ -0,0 +1,64 @@
// 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.lib;
import com.google.gwt.core.client.JavaScriptObject;
/**
* Glue around the Vim emulation for {@link CodeMirror}.
*
* As an instance {@code this} is actually the {@link CodeMirror} object. Class
* Vim is providing a new namespace for Vim related methods that are associated
* with an editor.
*/
public class Vim extends JavaScriptObject {
static void initKeyMap() {
// TODO: Better custom keybindings, remove temporary navigation hacks.
KeyMap km = CodeMirror.cloneKeyMap("vim");
for (String s : new String[] {
"A", "C", "I", "O", "R", "U",
"Ctrl-C", "Ctrl-O", "Ctrl-P", "Ctrl-S",
"Ctrl-F", "Ctrl-B", "Ctrl-R"}) {
km.remove(s);
}
for (int i = 0; i <= 9; i++) {
km.remove("Ctrl-" + i);
}
CodeMirror.addKeyMap("vim_ro", km);
mapKey("j", "gj");
mapKey("k", "gk");
mapKey("Down", "gj");
mapKey("Up", "gk");
mapKey("<PageUp>", "<C-u>");
mapKey("<PageDown>", "<C-d>");
}
public static final native void mapKey(String alias, String actual) /*-{
$wnd.CodeMirror.Vim.map(alias, actual)
}-*/;
public final native void handleKey(String key) /*-{
$wnd.CodeMirror.Vim.handleKey(this, key)
}-*/;
public final native boolean hasSearchHighlight() /*-{
var v = this.state.vim;
return v && v.searchState_ && !!v.searchState_.getOverlay();
}-*/;
protected Vim() {
}
}

View File

@@ -22,7 +22,7 @@ import com.google.gwt.core.client.JsArrayString;
import com.googlecode.gwt.test.GwtModule; import com.googlecode.gwt.test.GwtModule;
import com.googlecode.gwt.test.GwtTest; import com.googlecode.gwt.test.GwtTest;
import net.codemirror.lib.LineCharacter; import net.codemirror.lib.Pos;
import org.junit.Before; import org.junit.Before;
import org.junit.Ignore; import org.junit.Ignore;
@@ -35,8 +35,8 @@ import org.junit.Test;
public class EditIteratorTest extends GwtTest { public class EditIteratorTest extends GwtTest {
private JsArrayString lines; private JsArrayString lines;
private void assertLineChsEqual(LineCharacter a, LineCharacter b) { private void assertLineChsEqual(Pos a, Pos b) {
assertEquals(a.getLine() + "," + a.getCh(), b.getLine() + "," + b.getCh()); assertEquals(a.line() + "," + a.ch(), b.line() + "," + b.ch());
} }
@Before @Before
@@ -50,57 +50,57 @@ public class EditIteratorTest extends GwtTest {
@Test @Test
public void testNegativeAdvance() { public void testNegativeAdvance() {
EditIterator i = new EditIterator(lines, 0); EditIterator i = new EditIterator(lines, 0);
assertLineChsEqual(LineCharacter.create(1, 1), i.advance(5)); assertLineChsEqual(Pos.create(1, 1), i.advance(5));
assertLineChsEqual(LineCharacter.create(0, 3), i.advance(-2)); assertLineChsEqual(Pos.create(0, 3), i.advance(-2));
} }
@Test @Test
public void testNoAdvance() { public void testNoAdvance() {
EditIterator iter = new EditIterator(lines, 0); EditIterator iter = new EditIterator(lines, 0);
assertLineChsEqual(LineCharacter.create(0), iter.advance(0)); assertLineChsEqual(Pos.create(0), iter.advance(0));
} }
@Test @Test
public void testSimpleAdvance() { public void testSimpleAdvance() {
EditIterator iter = new EditIterator(lines, 0); EditIterator iter = new EditIterator(lines, 0);
assertLineChsEqual(LineCharacter.create(0, 1), iter.advance(1)); assertLineChsEqual(Pos.create(0, 1), iter.advance(1));
} }
@Test @Test
public void testEndsBeforeNewline() { public void testEndsBeforeNewline() {
EditIterator iter = new EditIterator(lines, 0); EditIterator iter = new EditIterator(lines, 0);
assertLineChsEqual(LineCharacter.create(0, 3), iter.advance(3)); assertLineChsEqual(Pos.create(0, 3), iter.advance(3));
} }
@Test @Test
public void testEndsOnNewline() { public void testEndsOnNewline() {
EditIterator iter = new EditIterator(lines, 0); EditIterator iter = new EditIterator(lines, 0);
assertLineChsEqual(LineCharacter.create(1), iter.advance(4)); assertLineChsEqual(Pos.create(1), iter.advance(4));
} }
@Test @Test
public void testAcrossNewline() { public void testAcrossNewline() {
EditIterator iter = new EditIterator(lines, 0); EditIterator iter = new EditIterator(lines, 0);
assertLineChsEqual(LineCharacter.create(1, 1), iter.advance(5)); assertLineChsEqual(Pos.create(1, 1), iter.advance(5));
} }
@Test @Test
public void testContinueFromBeforeNewline() { public void testContinueFromBeforeNewline() {
EditIterator iter = new EditIterator(lines, 0); EditIterator iter = new EditIterator(lines, 0);
iter.advance(3); iter.advance(3);
assertLineChsEqual(LineCharacter.create(2, 2), iter.advance(7)); assertLineChsEqual(Pos.create(2, 2), iter.advance(7));
} }
@Test @Test
public void testContinueFromAfterNewline() { public void testContinueFromAfterNewline() {
EditIterator iter = new EditIterator(lines, 0); EditIterator iter = new EditIterator(lines, 0);
iter.advance(4); iter.advance(4);
assertLineChsEqual(LineCharacter.create(2, 2), iter.advance(6)); assertLineChsEqual(Pos.create(2, 2), iter.advance(6));
} }
@Test @Test
public void testAcrossMultipleLines() { public void testAcrossMultipleLines() {
EditIterator iter = new EditIterator(lines, 0); EditIterator iter = new EditIterator(lines, 0);
assertLineChsEqual(LineCharacter.create(2, 2), iter.advance(10)); assertLineChsEqual(Pos.create(2, 2), iter.advance(10));
} }
} }