Skip to content

Commit

Permalink
Upgrade CodeMirror
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrea Bonomi committed Jun 21, 2024
1 parent dab2d6c commit bf1e5f2
Show file tree
Hide file tree
Showing 11 changed files with 78 additions and 62 deletions.
13 changes: 5 additions & 8 deletions airflow_code_editor/static/addon/lint/lint.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@

function position(e) {
if (!tt.parentNode) return CodeMirror.off(document, "mousemove", position);
tt.style.top = Math.max(0, e.clientY - tt.offsetHeight - 5) + "px";
tt.style.left = (e.clientX + 5) + "px";
var top = Math.max(0, e.clientY - tt.offsetHeight - 5);
var left = Math.max(0, Math.min(e.clientX + 5, tt.ownerDocument.defaultView.innerWidth - tt.offsetWidth));
tt.style.top = top + "px"
tt.style.left = left + "px";
}
CodeMirror.on(document, "mousemove", position);
position(e);
Expand Down Expand Up @@ -199,10 +201,6 @@
var anns = annotations[line];
if (!anns) continue;

// filter out duplicate messages
var message = [];
anns = anns.filter(function(item) { return message.indexOf(item.message) > -1 ? false : message.push(item.message) });

var maxSeverity = null;
var tipLabel = state.hasGutter && document.createDocumentFragment();

Expand All @@ -220,9 +218,8 @@
__annotation: ann
}));
}
// use original annotations[line] to show multiple messages
if (state.hasGutter)
cm.setGutterMarker(line, GUTTER_ID, makeMarker(cm, tipLabel, maxSeverity, annotations[line].length > 1,
cm.setGutterMarker(line, GUTTER_ID, makeMarker(cm, tipLabel, maxSeverity, anns.length > 1,
options.tooltips));

if (options.highlightLines)
Expand Down
78 changes: 44 additions & 34 deletions airflow_code_editor/static/codemirror.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,14 @@
} while (child = child.parentNode)
}

function activeElt(doc) {
function activeElt(rootNode) {
// IE and Edge may throw an "Unspecified Error" when accessing document.activeElement.
// IE < 10 will throw when accessed while the page is loading or in an iframe.
// IE > 9 and Edge will throw when accessed in an iframe if document.body is unavailable.
var doc = rootNode.ownerDocument || rootNode;
var activeElement;
try {
activeElement = doc.activeElement;
activeElement = rootNode.activeElement;
} catch(e) {
activeElement = doc.body || null;
}
Expand Down Expand Up @@ -146,6 +147,15 @@

function doc(cm) { return cm.display.wrapper.ownerDocument }

function root(cm) {
return rootNode(cm.display.wrapper)
}

function rootNode(element) {
// Detect modern browsers (2017+).
return element.getRootNode ? element.getRootNode() : element.ownerDocument
}

function win(cm) { return doc(cm).defaultView }

function bind(f) {
Expand Down Expand Up @@ -3900,7 +3910,7 @@
cm.display.maxLineChanged = false;
}

var takeFocus = op.focus && op.focus == activeElt(doc(cm));
var takeFocus = op.focus && op.focus == activeElt(root(cm));
if (op.preparedSelection)
{ cm.display.input.showSelection(op.preparedSelection, takeFocus); }
if (op.updatedDisplay || op.startHeight != cm.doc.height)
Expand Down Expand Up @@ -4077,7 +4087,7 @@

function selectionSnapshot(cm) {
if (cm.hasFocus()) { return null }
var active = activeElt(doc(cm));
var active = activeElt(root(cm));
if (!active || !contains(cm.display.lineDiv, active)) { return null }
var result = {activeElt: active};
if (window.getSelection) {
Expand All @@ -4093,7 +4103,7 @@
}

function restoreSelection(snapshot) {
if (!snapshot || !snapshot.activeElt || snapshot.activeElt == activeElt(snapshot.activeElt.ownerDocument)) { return }
if (!snapshot || !snapshot.activeElt || snapshot.activeElt == activeElt(rootNode(snapshot.activeElt))) { return }
snapshot.activeElt.focus();
if (!/^(INPUT|TEXTAREA)$/.test(snapshot.activeElt.nodeName) &&
snapshot.anchorNode && contains(document.body, snapshot.anchorNode) && contains(document.body, snapshot.focusNode)) {
Expand Down Expand Up @@ -7264,7 +7274,7 @@
function onKeyDown(e) {
var cm = this;
if (e.target && e.target != cm.display.input.getField()) { return }
cm.curOp.focus = activeElt(doc(cm));
cm.curOp.focus = activeElt(root(cm));
if (signalDOMEvent(cm, e)) { return }
// IE does strange things with escape.
if (ie && ie_version < 11 && e.keyCode == 27) { e.returnValue = false; }
Expand Down Expand Up @@ -7426,7 +7436,7 @@

function leftButtonDown(cm, pos, repeat, event) {
if (ie) { setTimeout(bind(ensureFocus, cm), 0); }
else { cm.curOp.focus = activeElt(doc(cm)); }
else { cm.curOp.focus = activeElt(root(cm)); }

var behavior = configureMouse(cm, repeat, event);

Expand Down Expand Up @@ -7496,19 +7506,19 @@
// Normal selection, as opposed to text dragging.
function leftButtonSelect(cm, event, start, behavior) {
if (ie) { delayBlurEvent(cm); }
var display = cm.display, doc$1 = cm.doc;
var display = cm.display, doc = cm.doc;
e_preventDefault(event);

var ourRange, ourIndex, startSel = doc$1.sel, ranges = startSel.ranges;
var ourRange, ourIndex, startSel = doc.sel, ranges = startSel.ranges;
if (behavior.addNew && !behavior.extend) {
ourIndex = doc$1.sel.contains(start);
ourIndex = doc.sel.contains(start);
if (ourIndex > -1)
{ ourRange = ranges[ourIndex]; }
else
{ ourRange = new Range(start, start); }
} else {
ourRange = doc$1.sel.primary();
ourIndex = doc$1.sel.primIndex;
ourRange = doc.sel.primary();
ourIndex = doc.sel.primIndex;
}

if (behavior.unit == "rectangle") {
Expand All @@ -7525,18 +7535,18 @@

if (!behavior.addNew) {
ourIndex = 0;
setSelection(doc$1, new Selection([ourRange], 0), sel_mouse);
startSel = doc$1.sel;
setSelection(doc, new Selection([ourRange], 0), sel_mouse);
startSel = doc.sel;
} else if (ourIndex == -1) {
ourIndex = ranges.length;
setSelection(doc$1, normalizeSelection(cm, ranges.concat([ourRange]), ourIndex),
setSelection(doc, normalizeSelection(cm, ranges.concat([ourRange]), ourIndex),
{scroll: false, origin: "*mouse"});
} else if (ranges.length > 1 && ranges[ourIndex].empty() && behavior.unit == "char" && !behavior.extend) {
setSelection(doc$1, normalizeSelection(cm, ranges.slice(0, ourIndex).concat(ranges.slice(ourIndex + 1)), 0),
setSelection(doc, normalizeSelection(cm, ranges.slice(0, ourIndex).concat(ranges.slice(ourIndex + 1)), 0),
{scroll: false, origin: "*mouse"});
startSel = doc$1.sel;
startSel = doc.sel;
} else {
replaceOneSelection(doc$1, ourIndex, ourRange, sel_mouse);
replaceOneSelection(doc, ourIndex, ourRange, sel_mouse);
}

var lastPos = start;
Expand All @@ -7546,19 +7556,19 @@

if (behavior.unit == "rectangle") {
var ranges = [], tabSize = cm.options.tabSize;
var startCol = countColumn(getLine(doc$1, start.line).text, start.ch, tabSize);
var posCol = countColumn(getLine(doc$1, pos.line).text, pos.ch, tabSize);
var startCol = countColumn(getLine(doc, start.line).text, start.ch, tabSize);
var posCol = countColumn(getLine(doc, pos.line).text, pos.ch, tabSize);
var left = Math.min(startCol, posCol), right = Math.max(startCol, posCol);
for (var line = Math.min(start.line, pos.line), end = Math.min(cm.lastLine(), Math.max(start.line, pos.line));
line <= end; line++) {
var text = getLine(doc$1, line).text, leftPos = findColumn(text, left, tabSize);
var text = getLine(doc, line).text, leftPos = findColumn(text, left, tabSize);
if (left == right)
{ ranges.push(new Range(Pos(line, leftPos), Pos(line, leftPos))); }
else if (text.length > leftPos)
{ ranges.push(new Range(Pos(line, leftPos), Pos(line, findColumn(text, right, tabSize)))); }
}
if (!ranges.length) { ranges.push(new Range(start, start)); }
setSelection(doc$1, normalizeSelection(cm, startSel.ranges.slice(0, ourIndex).concat(ranges), ourIndex),
setSelection(doc, normalizeSelection(cm, startSel.ranges.slice(0, ourIndex).concat(ranges), ourIndex),
{origin: "*mouse", scroll: false});
cm.scrollIntoView(pos);
} else {
Expand All @@ -7573,8 +7583,8 @@
anchor = maxPos(oldRange.to(), range.head);
}
var ranges$1 = startSel.ranges.slice(0);
ranges$1[ourIndex] = bidiSimplify(cm, new Range(clipPos(doc$1, anchor), head));
setSelection(doc$1, normalizeSelection(cm, ranges$1, ourIndex), sel_mouse);
ranges$1[ourIndex] = bidiSimplify(cm, new Range(clipPos(doc, anchor), head));
setSelection(doc, normalizeSelection(cm, ranges$1, ourIndex), sel_mouse);
}
}

Expand All @@ -7590,9 +7600,9 @@
var cur = posFromMouse(cm, e, true, behavior.unit == "rectangle");
if (!cur) { return }
if (cmp(cur, lastPos) != 0) {
cm.curOp.focus = activeElt(doc(cm));
cm.curOp.focus = activeElt(root(cm));
extendTo(cur);
var visible = visibleLines(display, doc$1);
var visible = visibleLines(display, doc);
if (cur.line >= visible.to || cur.line < visible.from)
{ setTimeout(operation(cm, function () {if (counter == curCount) { extend(e); }}), 150); }
} else {
Expand All @@ -7617,7 +7627,7 @@
}
off(display.wrapper.ownerDocument, "mousemove", move);
off(display.wrapper.ownerDocument, "mouseup", up);
doc$1.history.lastSelOrigin = null;
doc.history.lastSelOrigin = null;
}

var move = operation(cm, function (e) {
Expand Down Expand Up @@ -8617,7 +8627,7 @@

signal(this, "overwriteToggle", this, this.state.overwrite);
},
hasFocus: function() { return this.display.input.getField() == activeElt(doc(this)) },
hasFocus: function() { return this.display.input.getField() == activeElt(root(this)) },
isReadOnly: function() { return !!(this.options.readOnly || this.doc.cantEdit) },

scrollTo: methodOp(function (x, y) { scrollToCoords(this, x, y); }),
Expand Down Expand Up @@ -8899,7 +8909,7 @@
disableBrowserMagic(te);
cm.display.lineSpace.insertBefore(kludge, cm.display.lineSpace.firstChild);
te.value = lastCopied.text.join("\n");
var hadFocus = activeElt(div.ownerDocument);
var hadFocus = activeElt(rootNode(div));
selectInput(te);
setTimeout(function () {
cm.display.lineSpace.removeChild(kludge);
Expand All @@ -8922,7 +8932,7 @@

ContentEditableInput.prototype.prepareSelection = function () {
var result = prepareSelection(this.cm, false);
result.focus = activeElt(this.div.ownerDocument) == this.div;
result.focus = activeElt(rootNode(this.div)) == this.div;
return result
};

Expand Down Expand Up @@ -9018,7 +9028,7 @@

ContentEditableInput.prototype.focus = function () {
if (this.cm.options.readOnly != "nocursor") {
if (!this.selectionInEditor() || activeElt(this.div.ownerDocument) != this.div)
if (!this.selectionInEditor() || activeElt(rootNode(this.div)) != this.div)
{ this.showSelection(this.prepareSelection(), true); }
this.div.focus();
}
Expand Down Expand Up @@ -9526,7 +9536,7 @@
TextareaInput.prototype.supportsTouch = function () { return false };

TextareaInput.prototype.focus = function () {
if (this.cm.options.readOnly != "nocursor" && (!mobile || activeElt(this.textarea.ownerDocument) != this.textarea)) {
if (this.cm.options.readOnly != "nocursor" && (!mobile || activeElt(rootNode(this.textarea)) != this.textarea)) {
try { this.textarea.focus(); }
catch (e) {} // IE8 will throw if the textarea is display: none or not in DOM
}
Expand Down Expand Up @@ -9733,7 +9743,7 @@
// Set autofocus to true if this textarea is focused, or if it has
// autofocus and no other element is focused.
if (options.autofocus == null) {
var hasFocus = activeElt(textarea.ownerDocument);
var hasFocus = activeElt(rootNode(textarea));
options.autofocus = hasFocus == textarea ||
textarea.getAttribute("autofocus") != null && hasFocus == document.body;
}
Expand Down Expand Up @@ -9867,7 +9877,7 @@

addLegacyProps(CodeMirror);

CodeMirror.version = "5.65.13";
CodeMirror.version = "5.65.16";

return CodeMirror;

Expand Down
3 changes: 2 additions & 1 deletion airflow_code_editor/static/mode/clike/clike.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,8 @@ CodeMirror.defineMode("clike", function(config, parserConfig) {
},

indent: function(state, textAfter) {
if (state.tokenize != tokenBase && state.tokenize != null || state.typeAtEndOfLine) return CodeMirror.Pass;
if (state.tokenize != tokenBase && state.tokenize != null || state.typeAtEndOfLine && isTopScope(state.context))
return CodeMirror.Pass;
var ctx = state.context, firstChar = textAfter && textAfter.charAt(0);
var closing = firstChar == ctx.type;
if (ctx.type == "statement" && firstChar == "}") ctx = ctx.prev;
Expand Down
5 changes: 5 additions & 0 deletions airflow_code_editor/static/mode/clike/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,9 @@
"[type StringBuffer];",
"[type StringBuilder];",
"[type Void];");

MTJAVA("indent",
"[keyword public] [keyword class] [def A] [keyword extends] [variable B]",
"{",
" [variable c]()")
})();
2 changes: 1 addition & 1 deletion airflow_code_editor/static/mode/dart/dart.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"implements mixin get native set typedef with enum throw rethrow assert break case " +
"continue default in return new deferred async await covariant try catch finally " +
"do else for if switch while import library export part of show hide is as extension " +
"on yield late required sealed base interface when inline").split(" ");
"on yield late required sealed base interface when").split(" ");
var blockKeywords = "try catch finally do else for if switch while".split(" ");
var atoms = "true false null".split(" ");
var builtins = "void bool num int double dynamic var String Null Never".split(" ");
Expand Down
24 changes: 12 additions & 12 deletions airflow_code_editor/static/mode/dart/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,33 +29,33 @@ <h2>Dart mode</h2>
import 'dart:math' show Random;

void main() {
print(new Die(n: 12).roll());
print(Die(n: 12).roll());
}

// Define a class.
class Die {
// Define a class variable.
static Random shaker = new Random();
static final Random shaker = Random();

// Define instance variables.
int sides, value;

// Define a method using shorthand syntax.
String toString() => '$value';
final int sides;
int? lastRoll;

// Define a constructor.
Die({int n: 6}) {
if (4 <= n && n <= 20) {
sides = n;
} else {
Die({int n = 6}) : sides = n {
if (4 > n || n > 20) {
// Support for errors and exceptions.
throw new ArgumentError(/* */);
throw ArgumentError(/* */);
}
}

// Define a method using shorthand syntax.
@override
String toString() => '$lastRoll';

// Define an instance method.
int roll() {
return value = shaker.nextInt(sides) + 1;
return lastRoll = shaker.nextInt(sides) + 1;
}
}
</textarea>
Expand Down
6 changes: 3 additions & 3 deletions airflow_code_editor/static/mode/go/go.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ CodeMirror.defineMode("go", function(config) {
}
if (/[\d\.]/.test(ch)) {
if (ch == ".") {
stream.match(/^[0-9]+([eE][\-+]?[0-9]+)?/);
stream.match(/^[0-9_]+([eE][\-+]?[0-9_]+)?/);
} else if (ch == "0") {
stream.match(/^[xX][0-9a-fA-F]+/) || stream.match(/^0[0-7]+/);
stream.match(/^[xX][0-9a-fA-F_]+/) || stream.match(/^[0-7_]+/);
} else {
stream.match(/^[0-9]*\.?[0-9]*([eE][\-+]?[0-9]+)?/);
stream.match(/^[0-9_]*\.?[0-9_]*([eE][\-+]?[0-9_]+)?/);
}
return "number";
}
Expand Down
3 changes: 2 additions & 1 deletion airflow_code_editor/static/mode/jsx/jsx.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@
}

function jsToken(stream, state, cx) {
if (stream.peek() == "<" && jsMode.expressionAllowed(stream, cx.state)) {
if (stream.peek() == "<" && !stream.match(/^<([^<>]|<[^>]*>)+,\s*>/, false) &&
jsMode.expressionAllowed(stream, cx.state)) {
state.context = new Context(CodeMirror.startState(xmlMode, jsMode.indent(cx.state, "", "")),
xmlMode, 0, state.context)
jsMode.skipExpression(cx.state)
Expand Down
2 changes: 2 additions & 0 deletions airflow_code_editor/static/mode/jsx/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,6 @@
"[bracket&tag <][tag MyComponent] [attribute foo]=[string \"bar\"] [bracket&tag />]; [comment //ok]",
"[bracket&tag <][tag MyComponent] [attribute foo]={[number 0]} [bracket&tag />]; [comment //error]")

TS("tsx_react_generics",
"[variable x] [operator =] [operator <] [variable T],[operator >] ([def v]: [type T]) [operator =>] [variable-2 v];")
})()
2 changes: 1 addition & 1 deletion airflow_code_editor/static/mode/nsis/nsis.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ CodeMirror.defineSimpleMode("nsis",{
{ regex: /`(?:[^\\`]|\\.)*`?/, token: "string" },

// Compile Time Commands
{regex: /^\s*(?:\!(addincludedir|addplugindir|appendfile|cd|define|delfile|echo|error|execute|finalize|getdllversion|gettlbversion|include|insertmacro|macro|macroend|makensis|packhdr|pragma|searchparse|searchreplace|system|tempfile|undef|uninstfinalize|verbose|warning))\b/i, token: "keyword"},
{regex: /^\s*(?:\!(addincludedir|addplugindir|appendfile|assert|cd|define|delfile|echo|error|execute|finalize|getdllversion|gettlbversion|include|insertmacro|macro|macroend|makensis|packhdr|pragma|searchparse|searchreplace|system|tempfile|undef|uninstfinalize|verbose|warning))\b/i, token: "keyword"},

// Conditional Compilation
{regex: /^\s*(?:\!(if(?:n?def)?|ifmacron?def|macro))\b/i, token: "keyword", indent: true},
Expand Down
Loading

0 comments on commit bf1e5f2

Please sign in to comment.