Skip to content

Commit 31a4d5b

Browse files
Carreaumarijnh
authored andcommitted
Allow unicode identifiers on python3, and @ operator.
1 parent cc953bf commit 31a4d5b

File tree

2 files changed

+33
-8
lines changed

2 files changed

+33
-8
lines changed

mode/python/index.html

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,25 @@ <h2>Python mode</h2>
6565
a.b
6666
a.b.c
6767

68+
#Unicode identifiers on Python3
69+
# a = x\ddot
70+
a⃗ = ẍ
71+
# a = v\dot
72+
a⃗ = v̇
73+
74+
#F\vec = m \cdot a\vec
75+
F⃗ = m•a⃗
76+
6877
# Operators
6978
+ - * / % & | ^ ~ < >
7079
== != <= >= <> << >> // **
7180
and or not in is
7281

82+
#infix matrix multiplication operator (PEP 465)
83+
A @ B
84+
7385
# Delimiters
74-
() [] {} , : ` = ; @ . # Note that @ and . require the proper context.
86+
() [] {} , : ` = ; @ . # Note that @ and . require the proper context on Python 2.
7587
+= -= *= /= %= &= |= ^=
7688
//= >>= <<= **=
7789

@@ -146,7 +158,7 @@ <h2>Cython mode</h2>
146158
<script>
147159
var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
148160
mode: {name: "python",
149-
version: 2,
161+
version: 3,
150162
singleLineStringErrors: false},
151163
lineNumbers: true,
152164
indentUnit: 4,
@@ -171,12 +183,12 @@ <h2>Configuration Options for Python mode:</h2>
171183
<h2>Advanced Configuration Options:</h2>
172184
<p>Usefull for superset of python syntax like Enthought enaml, IPython magics and questionmark help</p>
173185
<ul>
174-
<li>singleOperators - RegEx - Regular Expression for single operator matching, default : <pre>^[\\+\\-\\*/%&amp;|\\^~&lt;&gt;!]</pre></li>
186+
<li>singleOperators - RegEx - Regular Expression for single operator matching, default : <pre>^[\\+\\-\\*/%&amp;|\\^~&lt;&gt;!]</pre> including <pre>@</pre> on Python 3</li>
175187
<li>singleDelimiters - RegEx - Regular Expression for single delimiter matching, default : <pre>^[\\(\\)\\[\\]\\{\\}@,:`=;\\.]</pre></li>
176188
<li>doubleOperators - RegEx - Regular Expression for double operators matching, default : <pre>^((==)|(!=)|(&lt;=)|(&gt;=)|(&lt;&gt;)|(&lt;&lt;)|(&gt;&gt;)|(//)|(\\*\\*))</pre></li>
177189
<li>doubleDelimiters - RegEx - Regular Expressoin for double delimiters matching, default : <pre>^((\\+=)|(\\-=)|(\\*=)|(%=)|(/=)|(&amp;=)|(\\|=)|(\\^=))</pre></li>
178190
<li>tripleDelimiters - RegEx - Regular Expression for triple delimiters matching, default : <pre>^((//=)|(&gt;&gt;=)|(&lt;&lt;=)|(\\*\\*=))</pre></li>
179-
<li>identifiers - RegEx - Regular Expression for identifier, default : <pre>^[_A-Za-z][_A-Za-z0-9]*</pre></li>
191+
<li>identifiers - RegEx - Regular Expression for identifier, default : <pre>^[_A-Za-z][_A-Za-z0-9]*</pre> on Python 2 and <pre>^[_A-Za-z\u00A1-\uFFFF][_A-Za-z0-9\u00A1-\uFFFF]*</pre> on Python 3.</li>
180192
<li>extra_keywords - list of string - List of extra words ton consider as keywords</li>
181193
<li>extra_builtins - list of string - List of extra words ton consider as builtins</li>
182194
</ul>

mode/python/python.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,20 @@
4848
CodeMirror.defineMode("python", function(conf, parserConf) {
4949
var ERRORCLASS = "error";
5050

51-
var singleOperators = parserConf.singleOperators || new RegExp("^[\\+\\-\\*/%&|\\^~<>!]");
5251
var singleDelimiters = parserConf.singleDelimiters || new RegExp("^[\\(\\)\\[\\]\\{\\}@,:`=;\\.]");
5352
var doubleOperators = parserConf.doubleOperators || new RegExp("^((==)|(!=)|(<=)|(>=)|(<>)|(<<)|(>>)|(//)|(\\*\\*))");
5453
var doubleDelimiters = parserConf.doubleDelimiters || new RegExp("^((\\+=)|(\\-=)|(\\*=)|(%=)|(/=)|(&=)|(\\|=)|(\\^=))");
5554
var tripleDelimiters = parserConf.tripleDelimiters || new RegExp("^((//=)|(>>=)|(<<=)|(\\*\\*=))");
56-
var identifiers = parserConf.identifiers|| new RegExp("^[_A-Za-z][_A-Za-z0-9]*");
55+
56+
if (parserConf.version && parseInt(parserConf.version, 10) == 3){
57+
// since http://legacy.python.org/dev/peps/pep-0465/ @ is also an operator
58+
var singleOperators = parserConf.singleOperators || new RegExp("^[\\+\\-\\*/%&|\\^~<>!@]");
59+
var identifiers = parserConf.identifiers|| new RegExp("^[_A-Za-z\u00A1-\uFFFF][_A-Za-z0-9\u00A1-\uFFFF]*");
60+
} else {
61+
var singleOperators = parserConf.singleOperators || new RegExp("^[\\+\\-\\*/%&|\\^~<>!]");
62+
var identifiers = parserConf.identifiers|| new RegExp("^[_A-Za-z][_A-Za-z0-9]*");
63+
}
64+
5765
var hangingIndent = parserConf.hangingIndent || conf.indentUnit;
5866

5967
var myKeywords = commonKeywords, myBuiltins = commonBuiltins;
@@ -252,8 +260,13 @@
252260
}
253261

254262
// Handle decorators
255-
if (current == "@")
256-
return stream.match(identifiers, false) ? "meta" : ERRORCLASS;
263+
if (current == "@"){
264+
if(parserConf.version && parseInt(parserConf.version, 10) == 3){
265+
return stream.match(identifiers, false) ? "meta" : "operator";
266+
} else {
267+
return stream.match(identifiers, false) ? "meta" : ERRORCLASS;
268+
}
269+
}
257270

258271
if ((style == "variable" || style == "builtin")
259272
&& state.lastStyle == "meta")

0 commit comments

Comments
 (0)