mirror of
https://github.com/documize/community.git
synced 2025-07-28 17:49:41 +02:00
CodeMirror upgrade to 5.38.0
This commit is contained in:
parent
36be6243ad
commit
cfe30dcde5
52 changed files with 905 additions and 413 deletions
9
gui/public/codemirror/mode/python/index.html
vendored
9
gui/public/codemirror/mode/python/index.html
vendored
|
@ -126,6 +126,15 @@ class ExampleClass(ParentClass):
|
|||
def __init__(self, mixin = 'Hello'):
|
||||
self.mixin = mixin
|
||||
|
||||
# Python 3.6 f-strings (https://www.python.org/dev/peps/pep-0498/)
|
||||
f'My name is {name}, my age next year is {age+1}, my anniversary is {anniversary:%A, %B %d, %Y}.'
|
||||
f'He said his name is {name!r}.'
|
||||
f"""He said his name is {name!r}."""
|
||||
f'{"quoted string"}'
|
||||
f'{{ {4*10} }}'
|
||||
f'This is an error }'
|
||||
f'This is ok }}'
|
||||
fr'x={4*10}\n'
|
||||
</textarea></div>
|
||||
|
||||
|
||||
|
|
113
gui/public/codemirror/mode/python/python.js
vendored
113
gui/public/codemirror/mode/python/python.js
vendored
|
@ -41,7 +41,7 @@
|
|||
CodeMirror.defineMode("python", function(conf, parserConf) {
|
||||
var ERRORCLASS = "error";
|
||||
|
||||
var delimiters = parserConf.delimiters || parserConf.singleDelimiters || /^[\(\)\[\]\{\}@,:`=;\.]/;
|
||||
var delimiters = parserConf.delimiters || parserConf.singleDelimiters || /^[\(\)\[\]\{\}@,:`=;\.\\]/;
|
||||
// (Backwards-compatiblity with old, cumbersome config system)
|
||||
var operators = [parserConf.singleOperators, parserConf.doubleOperators, parserConf.doubleDelimiters, parserConf.tripleDelimiters,
|
||||
parserConf.operators || /^([-+*/%\/&|^]=?|[<>=]+|\/\/=?|\*\*=?|!=|[~!@])/]
|
||||
|
@ -62,7 +62,7 @@
|
|||
var identifiers = parserConf.identifiers|| /^[_A-Za-z\u00A1-\uFFFF][_A-Za-z0-9\u00A1-\uFFFF]*/;
|
||||
myKeywords = myKeywords.concat(["nonlocal", "False", "True", "None", "async", "await"]);
|
||||
myBuiltins = myBuiltins.concat(["ascii", "bytes", "exec", "print"]);
|
||||
var stringPrefixes = new RegExp("^(([rbuf]|(br))?('{3}|\"{3}|['\"]))", "i");
|
||||
var stringPrefixes = new RegExp("^(([rbuf]|(br)|(fr))?('{3}|\"{3}|['\"]))", "i");
|
||||
} else {
|
||||
var identifiers = parserConf.identifiers|| /^[_A-Za-z][_A-Za-z0-9]*/;
|
||||
myKeywords = myKeywords.concat(["exec", "print"]);
|
||||
|
@ -76,9 +76,10 @@
|
|||
|
||||
// tokenizers
|
||||
function tokenBase(stream, state) {
|
||||
if (stream.sol()) state.indent = stream.indentation()
|
||||
var sol = stream.sol() && state.lastToken != "\\"
|
||||
if (sol) state.indent = stream.indentation()
|
||||
// Handle scope changes
|
||||
if (stream.sol() && top(state).type == "py") {
|
||||
if (sol && top(state).type == "py") {
|
||||
var scopeOffset = top(state).offset;
|
||||
if (stream.eatSpace()) {
|
||||
var lineOffset = stream.indentation();
|
||||
|
@ -100,13 +101,8 @@
|
|||
function tokenBaseInner(stream, state) {
|
||||
if (stream.eatSpace()) return null;
|
||||
|
||||
var ch = stream.peek();
|
||||
|
||||
// Handle Comments
|
||||
if (ch == "#") {
|
||||
stream.skipToEnd();
|
||||
return "comment";
|
||||
}
|
||||
if (stream.match(/^#.*/)) return "comment";
|
||||
|
||||
// Handle Number Literals
|
||||
if (stream.match(/^[0-9\.]/, false)) {
|
||||
|
@ -146,8 +142,14 @@
|
|||
|
||||
// Handle Strings
|
||||
if (stream.match(stringPrefixes)) {
|
||||
state.tokenize = tokenStringFactory(stream.current());
|
||||
return state.tokenize(stream, state);
|
||||
var isFmtString = stream.current().toLowerCase().indexOf('f') !== -1;
|
||||
if (!isFmtString) {
|
||||
state.tokenize = tokenStringFactory(stream.current());
|
||||
return state.tokenize(stream, state);
|
||||
} else {
|
||||
state.tokenize = formatStringFactory(stream.current(), state.tokenize);
|
||||
return state.tokenize(stream, state);
|
||||
}
|
||||
}
|
||||
|
||||
for (var i = 0; i < operators.length; i++)
|
||||
|
@ -178,6 +180,77 @@
|
|||
return ERRORCLASS;
|
||||
}
|
||||
|
||||
function formatStringFactory(delimiter, tokenOuter) {
|
||||
while ("rubf".indexOf(delimiter.charAt(0).toLowerCase()) >= 0)
|
||||
delimiter = delimiter.substr(1);
|
||||
|
||||
var singleline = delimiter.length == 1;
|
||||
var OUTCLASS = "string";
|
||||
|
||||
function tokenFString(stream, state) {
|
||||
// inside f-str Expression
|
||||
if (stream.match(delimiter)) {
|
||||
// expression ends pre-maturally, but very common in editing
|
||||
// Could show error to remind users to close brace here
|
||||
state.tokenize = tokenString
|
||||
return OUTCLASS;
|
||||
} else if (stream.match('{')) {
|
||||
// starting brace, if not eaten below
|
||||
return "punctuation";
|
||||
} else if (stream.match('}')) {
|
||||
// return to regular inside string state
|
||||
state.tokenize = tokenString
|
||||
return "punctuation";
|
||||
} else {
|
||||
// use tokenBaseInner to parse the expression
|
||||
return tokenBaseInner(stream, state);
|
||||
}
|
||||
}
|
||||
|
||||
function tokenString(stream, state) {
|
||||
while (!stream.eol()) {
|
||||
stream.eatWhile(/[^'"\{\}\\]/);
|
||||
if (stream.eat("\\")) {
|
||||
stream.next();
|
||||
if (singleline && stream.eol())
|
||||
return OUTCLASS;
|
||||
} else if (stream.match(delimiter)) {
|
||||
state.tokenize = tokenOuter;
|
||||
return OUTCLASS;
|
||||
} else if (stream.match('{{')) {
|
||||
// ignore {{ in f-str
|
||||
return OUTCLASS;
|
||||
} else if (stream.match('{', false)) {
|
||||
// switch to nested mode
|
||||
state.tokenize = tokenFString
|
||||
if (stream.current()) {
|
||||
return OUTCLASS;
|
||||
} else {
|
||||
// need to return something, so eat the starting {
|
||||
stream.next();
|
||||
return "punctuation";
|
||||
}
|
||||
} else if (stream.match('}}')) {
|
||||
return OUTCLASS;
|
||||
} else if (stream.match('}')) {
|
||||
// single } in f-string is an error
|
||||
return ERRORCLASS;
|
||||
} else {
|
||||
stream.eat(/['"]/);
|
||||
}
|
||||
}
|
||||
if (singleline) {
|
||||
if (parserConf.singleLineStringErrors)
|
||||
return ERRORCLASS;
|
||||
else
|
||||
state.tokenize = tokenOuter;
|
||||
}
|
||||
return OUTCLASS;
|
||||
}
|
||||
tokenString.isString = true;
|
||||
return tokenString;
|
||||
}
|
||||
|
||||
function tokenStringFactory(delimiter) {
|
||||
while ("rubf".indexOf(delimiter.charAt(0).toLowerCase()) >= 0)
|
||||
delimiter = delimiter.substr(1);
|
||||
|
@ -258,14 +331,16 @@
|
|||
if (current == ":" && !state.lambda && top(state).type == "py")
|
||||
pushPyScope(state);
|
||||
|
||||
var delimiter_index = current.length == 1 ? "[({".indexOf(current) : -1;
|
||||
if (delimiter_index != -1)
|
||||
pushBracketScope(stream, state, "])}".slice(delimiter_index, delimiter_index+1));
|
||||
if (current.length == 1 && !/string|comment/.test(style)) {
|
||||
var delimiter_index = "[({".indexOf(current);
|
||||
if (delimiter_index != -1)
|
||||
pushBracketScope(stream, state, "])}".slice(delimiter_index, delimiter_index+1));
|
||||
|
||||
delimiter_index = "])}".indexOf(current);
|
||||
if (delimiter_index != -1) {
|
||||
if (top(state).type == current) state.indent = state.scopes.pop().offset - hangingIndent
|
||||
else return ERRORCLASS;
|
||||
delimiter_index = "])}".indexOf(current);
|
||||
if (delimiter_index != -1) {
|
||||
if (top(state).type == current) state.indent = state.scopes.pop().offset - hangingIndent
|
||||
else return ERRORCLASS;
|
||||
}
|
||||
}
|
||||
if (state.dedent > 0 && stream.eol() && top(state).type == "py") {
|
||||
if (state.scopes.length > 1) state.scopes.pop();
|
||||
|
|
5
gui/public/codemirror/mode/python/test.js
vendored
5
gui/public/codemirror/mode/python/test.js
vendored
|
@ -30,6 +30,9 @@
|
|||
MT("before_equal_sign_" + c, "[variable a] [operator " + c + "=] [variable b]");
|
||||
}
|
||||
|
||||
MT("fValidStringPrefix", "[string f'this is a {formatted} string']");
|
||||
MT("fValidStringPrefix", "[string f'this is a]{[variable formatted]}[string string']");
|
||||
MT("fValidExpressioninFString", "[string f'expression ]{[number 100][operator *][number 5]}[string string']");
|
||||
MT("fInvalidFString", "[error f'this is wrong}]");
|
||||
MT("fNestedFString", "[string f'expression ]{[number 100] [operator +] [string f'inner]{[number 5]}[string ']}[string string']");
|
||||
MT("uValidStringPrefix", "[string u'this is an unicode string']");
|
||||
})();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue