Skip to content

Commit 4651de8

Browse files
Carreaumarijnh
authored andcommitted
[python mode] Add MIME for Cython dialect
1 parent 8068b3d commit 4651de8

5 files changed

Lines changed: 63 additions & 4 deletions

File tree

doc/modes.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ <h1><span class="logo-braces">{ }</span> <a href="http://codemirror.net/">CodeMi
3131
<li><a href="../mode/coffeescript/index.html">CoffeeScript</a></li>
3232
<li><a href="../mode/commonlisp/index.html">Common Lisp</a></li>
3333
<li><a href="../mode/css/index.html">CSS</a></li>
34+
<li><a href="../mode/python/index.html">Cython</a></li>
3435
<li><a href="../mode/d/index.html">D</a></li>
3536
<li><a href="../mode/diff/index.html">diff</a></li>
3637
<li><a href="../mode/ecl/index.html">ECL</a></li>

index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ <h2 style="margin-top: 0">Supported modes:</h2>
4040
<li><a href="mode/coffeescript/index.html">CoffeeScript</a></li>
4141
<li><a href="mode/commonlisp/index.html">Common Lisp</a></li>
4242
<li><a href="mode/css/index.html">CSS</a></li>
43+
<li><a href="mode/python/index.html">Cython</a></li>
4344
<li><a href="mode/d/index.html">D</a></li>
4445
<li><a href="mode/diff/index.html">diff</a></li>
4546
<li><a href="mode/ecl/index.html">ECL</a></li>

mode/meta.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ CodeMirror.modeInfo = [
1616
{name: 'ECL', mime: 'text/x-ecl', mode: 'ecl'},
1717
{name: 'Erlang', mime: 'text/x-erlang', mode: 'erlang'},
1818
{name: 'Gas', mime: 'text/x-gas', mode: 'gas'},
19-
{name: 'GitHub Flavored Markdown', mode: 'gfm'},
19+
{name: 'GitHub Flavored Markdown', mime: 'text/x-gfm', mode: 'gfm'},
2020
{name: 'GO', mime: 'text/x-go', mode: 'go'},
2121
{name: 'Groovy', mime: 'text/x-groovy', mode: 'groovy'},
2222
{name: 'Haskell', mime: 'text/x-haskell', mode: 'haskell'},
@@ -47,6 +47,7 @@ CodeMirror.modeInfo = [
4747
{name: 'Plain Text', mime: 'text/plain', mode: 'null'},
4848
{name: 'Properties files', mime: 'text/x-properties', mode: 'clike'},
4949
{name: 'Python', mime: 'text/x-python', mode: 'python'},
50+
{name: 'Cython', mime: 'text/x-cython', mode: 'python'},
5051
{name: 'R', mime: 'text/x-rsrc', mode: 'r'},
5152
{name: 'reStructuredText', mime: 'text/x-rst', mode: 'rst'},
5253
{name: 'Ruby', mime: 'text/x-ruby', mode: 'ruby'},

mode/python/index.html

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
</head>
1313
<body>
1414
<h1>CodeMirror: Python mode</h1>
15-
15+
<h2>Python mode</h2>
1616
<div><textarea id="code" name="code">
1717
# Literals
1818
1234
@@ -102,6 +102,34 @@ <h1>CodeMirror: Python mode</h1>
102102
self.mixin = mixin
103103

104104
</textarea></div>
105+
106+
107+
<h2>Cython mode</h2>
108+
109+
<div><textarea id="code-cython" name="code-cython">
110+
111+
import numpy as np
112+
cimport cython
113+
from libc.math cimport sqrt
114+
115+
@cython.boundscheck(False)
116+
@cython.wraparound(False)
117+
def pairwise_cython(double[:, ::1] X):
118+
cdef int M = X.shape[0]
119+
cdef int N = X.shape[1]
120+
cdef double tmp, d
121+
cdef double[:, ::1] D = np.empty((M, M), dtype=np.float64)
122+
for i in range(M):
123+
for j in range(M):
124+
d = 0.0
125+
for k in range(N):
126+
tmp = X[i, k] - X[j, k]
127+
d += tmp * tmp
128+
D[i, j] = sqrt(d)
129+
return np.asarray(D)
130+
131+
</textarea></div>
132+
105133
<script>
106134
var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
107135
mode: {name: "python",
@@ -111,9 +139,19 @@ <h1>CodeMirror: Python mode</h1>
111139
indentUnit: 4,
112140
tabMode: "shift",
113141
matchBrackets: true
142+
});
143+
144+
CodeMirror.fromTextArea(document.getElementById("code-cython"), {
145+
mode: {name: "text/x-cython",
146+
version: 2,
147+
singleLineStringErrors: false},
148+
lineNumbers: true,
149+
indentUnit: 4,
150+
tabMode: "shift",
151+
matchBrackets: true
114152
});
115153
</script>
116-
<h2>Configuration Options:</h2>
154+
<h2>Configuration Options for Python mode:</h2>
117155
<ul>
118156
<li>version - 2/3 - The version of Python to recognize. Default is 2.</li>
119157
<li>singleLineStringErrors - true/false - If you have a single-line string that is not terminated at the end of the line, this will show subsequent lines as errors if true, otherwise it will consider the newline as the end of the string. Default is false.</li>
@@ -127,9 +165,11 @@ <h2>Advanced Configuration Options:</h2>
127165
<li>doubleDelimiters - RegEx - Regular Expressoin for double delimiters matching, default : <pre>^((\\+=)|(\\-=)|(\\*=)|(%=)|(/=)|(&amp;=)|(\\|=)|(\\^=))</pre></li>
128166
<li>tripleDelimiters - RegEx - Regular Expression for triple delimiters matching, default : <pre>^((//=)|(&gt;&gt;=)|(&lt;&lt;=)|(\\*\\*=))</pre></li>
129167
<li>identifiers - RegEx - Regular Expression for identifier, default : <pre>^[_A-Za-z][_A-Za-z0-9]*</pre></li>
168+
<li>extra_keywords - list of string - List of extra words ton consider as keywords</li>
169+
<li>extra_builtins - list of string - List of extra words ton consider as builtins</li>
130170
</ul>
131171

132172

133-
<p><strong>MIME types defined:</strong> <code>text/x-python</code>.</p>
173+
<p><strong>MIME types defined:</strong> <code>text/x-python</code> and <code>text/x-cython</code>.</p>
134174
</body>
135175
</html>

mode/python/python.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ CodeMirror.defineMode("python", function(conf, parserConf) {
3636
var py3 = {'builtins': ['ascii', 'bytes', 'exec', 'print'],
3737
'keywords': ['nonlocal', 'False', 'True', 'None']};
3838

39+
if(parserConf.extra_keywords != undefined){
40+
commonkeywords = commonkeywords.concat(parserConf.extra_keywords);
41+
}
42+
if(parserConf.extra_builtins != undefined){
43+
commonBuiltins = commonBuiltins.concat(parserConf.extra_builtins);
44+
}
3945
if (!!parserConf.version && parseInt(parserConf.version, 10) === 3) {
4046
commonkeywords = commonkeywords.concat(py3.keywords);
4147
commonBuiltins = commonBuiltins.concat(py3.builtins);
@@ -340,3 +346,13 @@ CodeMirror.defineMode("python", function(conf, parserConf) {
340346
});
341347

342348
CodeMirror.defineMIME("text/x-python", "python");
349+
350+
var words = function(str){return str.split(' ');};
351+
352+
353+
CodeMirror.defineMIME("text/x-cython", {
354+
name: "python",
355+
extra_keywords: words("by cdef cimport cpdef ctypedef enum except"+
356+
"extern gil include nogil property public"+
357+
"readonly struct union DEF IF ELIF ELSE")
358+
});

0 commit comments

Comments
 (0)