Skip to content

Commit

Permalink
Upgrade CodeMirror
Browse files Browse the repository at this point in the history
  • Loading branch information
andreax79 committed Jun 30, 2023
1 parent 12145ec commit ab58116
Show file tree
Hide file tree
Showing 13 changed files with 80 additions and 63 deletions.
34 changes: 17 additions & 17 deletions airflow_code_editor/static/addon/hint/sql-hint.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@

function isArray(val) { return Object.prototype.toString.call(val) == "[object Array]" }

function getModeConf(editor, field) {
return editor.getModeAt(editor.getCursor()).config[field] || CodeMirror.resolveMode("text/x-sql")[field]
}

function getKeywords(editor) {
var mode = editor.doc.modeOption;
if (mode === "sql") mode = "text/x-sql";
return CodeMirror.resolveMode(mode).keywords;
return getModeConf(editor, "keywords") || []
}

function getIdentifierQuote(editor) {
var mode = editor.doc.modeOption;
if (mode === "sql") mode = "text/x-sql";
return CodeMirror.resolveMode(mode).identifierQuote || "`";
return getModeConf(editor, "identifierQuote") || "`";
}

function getText(item) {
Expand Down Expand Up @@ -109,9 +109,9 @@
var nameParts = getText(name).split(".");
for (var i = 0; i < nameParts.length; i++)
nameParts[i] = identifierQuote +
// duplicate identifierQuotes
nameParts[i].replace(new RegExp(identifierQuote,"g"), identifierQuote+identifierQuote) +
identifierQuote;
// duplicate identifierQuotes
nameParts[i].replace(new RegExp(identifierQuote,"g"), identifierQuote+identifierQuote) +
identifierQuote;
var escaped = nameParts.join(".");
if (typeof name == "string") return escaped;
name = shallowClone(name);
Expand Down Expand Up @@ -283,21 +283,21 @@
}
return w;
};
addMatches(result, search, defaultTable, function(w) {
addMatches(result, search, defaultTable, function(w) {
return objectOrClass(w, "CodeMirror-hint-table CodeMirror-hint-default-table");
});
addMatches(
});
addMatches(
result,
search,
tables, function(w) {
return objectOrClass(w, "CodeMirror-hint-table");
}
);
if (!disableKeywords)
addMatches(result, search, keywords, function(w) {
);
if (!disableKeywords)
addMatches(result, search, keywords, function(w) {
return objectOrClass(w.toUpperCase(), "CodeMirror-hint-keyword");
});
}
});
}

return {list: result, from: Pos(cur.line, start), to: Pos(cur.line, end)};
});
Expand Down
4 changes: 2 additions & 2 deletions airflow_code_editor/static/addon/merge/merge.js
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,7 @@
lock.setAttribute("tabindex", "0");
var lockWrap = elt("div", [lock], "CodeMirror-merge-scrolllock-wrap");
CodeMirror.on(lock, "click", function() { setScrollLock(dv, !dv.lockScroll); });
CodeMirror.on(lock, "keyup", function(e) { e.key === "Enter" && setScrollLock(dv, !dv.lockScroll); });
CodeMirror.on(lock, "keyup", function(e) { (e.key === "Enter" || e.code === "Space") && setScrollLock(dv, !dv.lockScroll); });
var gapElts = [lockWrap];
if (dv.mv.options.revertButtons !== false) {
dv.copyButtons = elt("div", null, "CodeMirror-merge-copybuttons-" + dv.type);
Expand All @@ -624,7 +624,7 @@
copyChunk(dv, dv.edit, dv.orig, node.chunk);
}
CodeMirror.on(dv.copyButtons, "click", copyButtons);
CodeMirror.on(dv.copyButtons, "keyup", function(e) { e.key === "Enter" && copyButtons(e); });
CodeMirror.on(dv.copyButtons, "keyup", function(e) { (e.key === "Enter" || e.code === "Space") && copyButtons(e); });
gapElts.unshift(dv.copyButtons);
}
if (dv.mv.options.connect != "align") {
Expand Down
10 changes: 6 additions & 4 deletions airflow_code_editor/static/codemirror.js
Original file line number Diff line number Diff line change
Expand Up @@ -8259,8 +8259,8 @@
}

function disableBrowserMagic(field, spellcheck, autocorrect, autocapitalize) {
field.setAttribute("autocorrect", autocorrect ? "" : "off");
field.setAttribute("autocapitalize", autocapitalize ? "" : "off");
field.setAttribute("autocorrect", autocorrect ? "on" : "off");
field.setAttribute("autocapitalize", autocapitalize ? "on" : "off");
field.setAttribute("spellcheck", !!spellcheck);
}

Expand All @@ -8275,7 +8275,6 @@
else { te.setAttribute("wrap", "off"); }
// If border: 0; -- iOS fails to open keyboard (issue #1287)
if (ios) { te.style.border = "1px solid black"; }
disableBrowserMagic(te);
return div
}

Expand Down Expand Up @@ -8897,6 +8896,7 @@
}
// Old-fashioned briefly-focus-a-textarea hack
var kludge = hiddenTextarea(), te = kludge.firstChild;
disableBrowserMagic(te);
cm.display.lineSpace.insertBefore(kludge, cm.display.lineSpace.firstChild);
te.value = lastCopied.text.join("\n");
var hadFocus = activeElt(div.ownerDocument);
Expand Down Expand Up @@ -9461,6 +9461,8 @@
// The semihidden textarea that is focused when the editor is
// focused, and receives input.
this.textarea = this.wrapper.firstChild;
var opts = this.cm.options;
disableBrowserMagic(this.textarea, opts.spellcheck, opts.autocorrect, opts.autocapitalize);
};

TextareaInput.prototype.screenReaderLabelChanged = function (label) {
Expand Down Expand Up @@ -9865,7 +9867,7 @@

addLegacyProps(CodeMirror);

CodeMirror.version = "5.65.9";
CodeMirror.version = "5.65.13";

return CodeMirror;

Expand Down
2 changes: 1 addition & 1 deletion airflow_code_editor/static/css/theme/bespin.css
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*/

.cm-s-bespin.CodeMirror {background: #28211c; color: #9d9b97;}
.cm-s-bespin div.CodeMirror-selected {background: #36312e !important;}
.cm-s-bespin div.CodeMirror-selected {background: #59554f !important;}
.cm-s-bespin .CodeMirror-gutters {background: #28211c; border-right: 0px;}
.cm-s-bespin .CodeMirror-linenumber {color: #666666;}
.cm-s-bespin .CodeMirror-cursor {border-left: 1px solid #797977 !important;}
Expand Down
7 changes: 4 additions & 3 deletions airflow_code_editor/static/mode/clike/clike.js
Original file line number Diff line number Diff line change
Expand Up @@ -512,8 +512,8 @@ CodeMirror.defineMode("clike", function(config, parserConfig) {
name: "clike",
keywords: words("abstract as async await base break case catch checked class const continue" +
" default delegate do else enum event explicit extern finally fixed for" +
" foreach goto if implicit in interface internal is lock namespace new" +
" operator out override params private protected public readonly ref return sealed" +
" foreach goto if implicit in init interface internal is lock namespace new" +
" operator out override params private protected public readonly record ref required return sealed" +
" sizeof stackalloc static struct switch this throw try typeof unchecked" +
" unsafe using virtual void volatile while add alias ascending descending dynamic from get" +
" global group into join let orderby partial remove select set value var yield"),
Expand All @@ -522,7 +522,7 @@ CodeMirror.defineMode("clike", function(config, parserConfig) {
" UInt64 bool byte char decimal double short int long object" +
" sbyte float string ushort uint ulong"),
blockKeywords: words("catch class do else finally for foreach if struct switch try while"),
defKeywords: words("class interface namespace struct var"),
defKeywords: words("class interface namespace record struct var"),
typeFirstDefinitions: true,
atoms: words("true false null"),
hooks: {
Expand Down Expand Up @@ -613,6 +613,7 @@ CodeMirror.defineMode("clike", function(config, parserConfig) {
return state.tokenize(stream, state);
},
"'": function(stream) {
if (stream.match(/^(\\[^'\s]+|[^\\'])'/)) return "string-2"
stream.eatWhile(/[\w\$_\xa1-\uffff]/);
return "atom";
},
Expand Down
8 changes: 4 additions & 4 deletions airflow_code_editor/static/mode/dart/dart.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
"use strict";

var keywords = ("this super static final const abstract class extends external factory " +
"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").split(" ");
"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(" ");
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
2 changes: 1 addition & 1 deletion airflow_code_editor/static/mode/javascript/javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -779,7 +779,7 @@ CodeMirror.defineMode("javascript", function(config, parserConfig) {
if (type == "async" ||
(type == "variable" &&
(value == "static" || value == "get" || value == "set" || (isTS && isModifier(value))) &&
cx.stream.match(/^\s+[\w$\xa1-\uffff]/, false))) {
cx.stream.match(/^\s+#?[\w$\xa1-\uffff]/, false))) {
cx.marked = "keyword";
return cont(classBody);
}
Expand Down
3 changes: 0 additions & 3 deletions airflow_code_editor/static/mode/pegjs/pegjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ CodeMirror.defineMode("pegjs", function (config) {
};
},
token: function (stream, state) {
if (stream)

//check for state changes
if (!state.inString && !state.inComment && ((stream.peek() == '"') || (stream.peek() == "'"))) {
state.stringType = stream.peek();
Expand All @@ -43,7 +41,6 @@ CodeMirror.defineMode("pegjs", function (config) {
state.inComment = true;
}

//return state
if (state.inString) {
while (state.inString && !stream.eol()) {
if (stream.peek() === state.stringType) {
Expand Down
6 changes: 3 additions & 3 deletions airflow_code_editor/static/mode/python/python.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"def", "del", "elif", "else", "except", "finally",
"for", "from", "global", "if", "import",
"lambda", "pass", "raise", "return",
"try", "while", "with", "yield", "in"];
"try", "while", "with", "yield", "in", "False", "True"];
var commonBuiltins = ["abs", "all", "any", "bin", "bool", "bytearray", "callable", "chr",
"classmethod", "compile", "complex", "delattr", "dict", "dir", "divmod",
"enumerate", "eval", "filter", "float", "format", "frozenset",
Expand Down Expand Up @@ -60,15 +60,15 @@
if (py3) {
// since http://legacy.python.org/dev/peps/pep-0465/ @ is also an operator
var identifiers = parserConf.identifiers|| /^[_A-Za-z\u00A1-\uFFFF][_A-Za-z0-9\u00A1-\uFFFF]*/;
myKeywords = myKeywords.concat(["nonlocal", "False", "True", "None", "async", "await"]);
myKeywords = myKeywords.concat(["nonlocal", "None", "aiter", "anext", "async", "await", "breakpoint", "match", "case"]);
myBuiltins = myBuiltins.concat(["ascii", "bytes", "exec", "print"]);
var stringPrefixes = new RegExp("^(([rbuf]|(br)|(rb)|(fr)|(rf))?('{3}|\"{3}|['\"]))", "i");
} else {
var identifiers = parserConf.identifiers|| /^[_A-Za-z][_A-Za-z0-9]*/;
myKeywords = myKeywords.concat(["exec", "print"]);
myBuiltins = myBuiltins.concat(["apply", "basestring", "buffer", "cmp", "coerce", "execfile",
"file", "intern", "long", "raw_input", "reduce", "reload",
"unichr", "unicode", "xrange", "False", "True", "None"]);
"unichr", "unicode", "xrange", "None"]);
var stringPrefixes = new RegExp("^(([rubf]|(ur)|(br))?('{3}|\"{3}|['\"]))", "i");
}
var keywords = wordRegexp(myKeywords);
Expand Down
15 changes: 15 additions & 0 deletions airflow_code_editor/static/mode/python/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,19 @@
" [keyword pass]",
" [keyword else]:",
" [variable baz]()")

MT("dedentCase",
"[keyword match] [variable x]:",
" [keyword case] [variable y]:",
" [variable foo]()")
MT("dedentCasePass",
"[keyword match] [variable x]:",
" [keyword case] [variable y]:",
" [keyword pass]")

MT("dedentCaseInFunction",
"[keyword def] [def foo]():",
" [keyword match] [variable x]:",
" [keyword case] [variable y]:",
" [variable foo]()")
})();
23 changes: 12 additions & 11 deletions airflow_code_editor/static/mode/sparql/sparql.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ CodeMirror.defineMode("sparql", function(config) {
"true", "false", "with",
"data", "copy", "to", "move", "add", "create", "drop", "clear", "load", "into"]);
var operatorChars = /[*+\-<>=&|\^\/!\?]/;
var PN_CHARS = "[A-Za-z_\\-0-9]";
var PREFIX_START = new RegExp("[A-Za-z]");
var PREFIX_REMAINDER = new RegExp("((" + PN_CHARS + "|\\.)*(" + PN_CHARS + "))?:");

function tokenBase(stream, state) {
var ch = stream.next();
Expand Down Expand Up @@ -71,20 +74,18 @@ CodeMirror.defineMode("sparql", function(config) {
stream.eatWhile(/[a-z\d\-]/i);
return "meta";
}
else {
stream.eatWhile(/[_\w\d]/);
if (stream.eat(":")) {
else if (PREFIX_START.test(ch) && stream.match(PREFIX_REMAINDER)) {
eatPnLocal(stream);
return "atom";
}
var word = stream.current();
if (ops.test(word))
return "builtin";
else if (keywords.test(word))
return "keyword";
else
return "variable";
}
stream.eatWhile(/[_\w\d]/);
var word = stream.current();
if (ops.test(word))
return "builtin";
else if (keywords.test(word))
return "keyword";
else
return "variable";
}

function eatPnLocal(stream) {
Expand Down
Loading

0 comments on commit ab58116

Please sign in to comment.