Skip to content

Commit

Permalink
Upgrade CodeMirror
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrea Bonomi committed Feb 9, 2022
1 parent bd85ac9 commit 613e8d8
Show file tree
Hide file tree
Showing 13 changed files with 87 additions and 73 deletions.
93 changes: 51 additions & 42 deletions airflow_code_editor/static/addon/fold/brace-fold.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,58 +11,67 @@
})(function(CodeMirror) {
"use strict";

CodeMirror.registerHelper("fold", "brace", function(cm, start) {
var line = start.line, lineText = cm.getLine(line);
function bracketFolding(pairs) {
return function(cm, start) {
var line = start.line, lineText = cm.getLine(line);

function findOpening(openCh) {
var tokenType;
for (var at = start.ch, pass = 0;;) {
var found = at <= 0 ? -1 : lineText.lastIndexOf(openCh, at - 1);
if (found == -1) {
if (pass == 1) break;
pass = 1;
at = lineText.length;
continue;
function findOpening(pair) {
var tokenType;
for (var at = start.ch, pass = 0;;) {
var found = at <= 0 ? -1 : lineText.lastIndexOf(pair[0], at - 1);
if (found == -1) {
if (pass == 1) break;
pass = 1;
at = lineText.length;
continue;
}
if (pass == 1 && found < start.ch) break;
tokenType = cm.getTokenTypeAt(CodeMirror.Pos(line, found + 1));
if (!/^(comment|string)/.test(tokenType)) return {ch: found + 1, tokenType: tokenType, pair: pair};
at = found - 1;
}
if (pass == 1 && found < start.ch) break;
tokenType = cm.getTokenTypeAt(CodeMirror.Pos(line, found + 1));
if (!/^(comment|string)/.test(tokenType)) return {ch: found + 1, tokenType: tokenType};
at = found - 1;
}
}

function findRange(startToken, endToken, found) {
var count = 1, lastLine = cm.lastLine(), end, startCh = found.ch, endCh
outer: for (var i = line; i <= lastLine; ++i) {
var text = cm.getLine(i), pos = i == line ? startCh : 0;
for (;;) {
var nextOpen = text.indexOf(startToken, pos), nextClose = text.indexOf(endToken, pos);
if (nextOpen < 0) nextOpen = text.length;
if (nextClose < 0) nextClose = text.length;
pos = Math.min(nextOpen, nextClose);
if (pos == text.length) break;
if (cm.getTokenTypeAt(CodeMirror.Pos(i, pos + 1)) == found.tokenType) {
if (pos == nextOpen) ++count;
else if (!--count) { end = i; endCh = pos; break outer; }
function findRange(found) {
var count = 1, lastLine = cm.lastLine(), end, startCh = found.ch, endCh
outer: for (var i = line; i <= lastLine; ++i) {
var text = cm.getLine(i), pos = i == line ? startCh : 0;
for (;;) {
var nextOpen = text.indexOf(found.pair[0], pos), nextClose = text.indexOf(found.pair[1], pos);
if (nextOpen < 0) nextOpen = text.length;
if (nextClose < 0) nextClose = text.length;
pos = Math.min(nextOpen, nextClose);
if (pos == text.length) break;
if (cm.getTokenTypeAt(CodeMirror.Pos(i, pos + 1)) == found.tokenType) {
if (pos == nextOpen) ++count;
else if (!--count) { end = i; endCh = pos; break outer; }
}
++pos;
}
++pos;
}
}

if (end == null || line == end) return null
return {from: CodeMirror.Pos(line, startCh),
to: CodeMirror.Pos(end, endCh)};
}
if (end == null || line == end) return null
return {from: CodeMirror.Pos(line, startCh),
to: CodeMirror.Pos(end, endCh)};
}

var startBrace = findOpening("{"), startBracket = findOpening("[")
if (startBrace && (!startBracket || startBracket.ch > startBrace.ch)) {
return findRange("{", "}", startBrace) || (startBracket && findRange("[", "]", startBracket))
} else if (startBracket) {
return findRange("[", "]", startBracket) || (startBrace && findRange("{", "}", startBrace))
} else {
var found = []
for (var i = 0; i < pairs.length; i++) {
var open = findOpening(pairs[i])
if (open) found.push(open)
}
found.sort(function(a, b) { return a.ch - b.ch })
for (var i = 0; i < found.length; i++) {
var range = findRange(found[i])
if (range) return range
}
return null
}
});
}

CodeMirror.registerHelper("fold", "brace", bracketFolding([["{", "}"], ["[", "]"]]));

CodeMirror.registerHelper("fold", "brace-paren", bracketFolding([["{", "}"], ["[", "]"], ["(", ")"]]));

CodeMirror.registerHelper("fold", "import", function(cm, start) {
function hasImport(line) {
Expand Down
8 changes: 5 additions & 3 deletions airflow_code_editor/static/codemirror.js
Original file line number Diff line number Diff line change
Expand Up @@ -2585,9 +2585,11 @@
}

function widgetTopHeight(lineObj) {
var ref = visualLine(lineObj);
var widgets = ref.widgets;
var height = 0;
if (lineObj.widgets) { for (var i = 0; i < lineObj.widgets.length; ++i) { if (lineObj.widgets[i].above)
{ height += widgetHeight(lineObj.widgets[i]); } } }
if (widgets) { for (var i = 0; i < widgets.length; ++i) { if (widgets[i].above)
{ height += widgetHeight(widgets[i]); } } }
return height
}

Expand Down Expand Up @@ -9840,7 +9842,7 @@

addLegacyProps(CodeMirror);

CodeMirror.version = "5.65.0";
CodeMirror.version = "5.65.1";

return CodeMirror;

Expand Down
1 change: 1 addition & 0 deletions airflow_code_editor/static/css/codemirror.css
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ div.CodeMirror span.CodeMirror-nonmatchingbracket {color: #a22;}
height: 100%;
outline: none; /* Prevent dragging from highlighting the element */
position: relative;
z-index: 0;
}
.CodeMirror-sizer {
position: relative;
Expand Down
1 change: 1 addition & 0 deletions airflow_code_editor/static/mode/commonlisp/commonlisp.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ CodeMirror.defineMode("commonlisp", function (config) {

closeBrackets: {pairs: "()[]{}\"\""},
lineComment: ";;",
fold: "brace-paren",
blockCommentStart: "#|",
blockCommentEnd: "|#"
};
Expand Down
6 changes: 3 additions & 3 deletions airflow_code_editor/static/mode/css/css.js
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,7 @@ CodeMirror.defineMode("css", function(config, parserConfig) {
"cell", "center", "checkbox", "circle", "cjk-decimal", "cjk-earthly-branch",
"cjk-heavenly-stem", "cjk-ideographic", "clear", "clip", "close-quote",
"col-resize", "collapse", "color", "color-burn", "color-dodge", "column", "column-reverse",
"compact", "condensed", "contain", "content", "contents",
"compact", "condensed", "conic-gradient", "contain", "content", "contents",
"content-box", "context-menu", "continuous", "contrast", "copy", "counter", "counters", "cover", "crop",
"cross", "crosshair", "cubic-bezier", "currentcolor", "cursive", "cyclic", "darken", "dashed", "decimal",
"decimal-leading-zero", "default", "default-button", "dense", "destination-atop",
Expand Down Expand Up @@ -687,8 +687,8 @@ CodeMirror.defineMode("css", function(config, parserConfig) {
"pointer", "polygon", "portrait", "pre", "pre-line", "pre-wrap", "preserve-3d",
"progress", "push-button", "radial-gradient", "radio", "read-only",
"read-write", "read-write-plaintext-only", "rectangle", "region",
"relative", "repeat", "repeating-linear-gradient",
"repeating-radial-gradient", "repeat-x", "repeat-y", "reset", "reverse",
"relative", "repeat", "repeating-linear-gradient", "repeating-radial-gradient",
"repeating-conic-gradient", "repeat-x", "repeat-y", "reset", "reverse",
"rgb", "rgba", "ridge", "right", "rotate", "rotate3d", "rotateX", "rotateY",
"rotateZ", "round", "row", "row-resize", "row-reverse", "rtl", "run-in", "running",
"s-resize", "sans-serif", "saturate", "saturation", "scale", "scale3d", "scaleX", "scaleY", "scaleZ", "screen",
Expand Down
2 changes: 1 addition & 1 deletion airflow_code_editor/static/mode/fortran/fortran.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ CodeMirror.defineMode("fortran", function() {
"c_short", "c_signed_char", "c_size_t", "character",
"complex", "double", "integer", "logical", "real"]);
var isOperatorChar = /[+\-*&=<>\/\:]/;
var litOperator = new RegExp("(\.and\.|\.or\.|\.eq\.|\.lt\.|\.le\.|\.gt\.|\.ge\.|\.ne\.|\.not\.|\.eqv\.|\.neqv\.)", "i");
var litOperator = /^\.(and|or|eq|lt|le|gt|ge|ne|not|eqv|neqv)\./i;

function tokenBase(stream, state) {

Expand Down
1 change: 1 addition & 0 deletions airflow_code_editor/static/mode/javascript/javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ CodeMirror.defineMode("javascript", function(config, parserConfig) {
cx.state.context = new Context(cx.state.context, cx.state.localVars, true)
cx.state.localVars = null
}
pushcontext.lex = pushblockcontext.lex = true
function popcontext() {
cx.state.localVars = cx.state.context.vars
cx.state.context = cx.state.context.prev
Expand Down
2 changes: 1 addition & 1 deletion airflow_code_editor/static/mode/mllike/mllike.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ CodeMirror.defineMode('mllike', function(_config, parserConfig) {
}
}
if (ch === '(') {
if (stream.eat('*')) {
if (stream.match(/^\*(?!\))/)) {
state.commentLevel++;
state.tokenize = tokenComment;
return state.tokenize(stream, state);
Expand Down
Loading

0 comments on commit 613e8d8

Please sign in to comment.