Skip to content

Commit 8a41f75

Browse files
committed
[clike mode] Add triple-quoted string support for Scala
1 parent e7761e7 commit 8a41f75

File tree

2 files changed

+45
-5
lines changed

2 files changed

+45
-5
lines changed

mode/clike/clike.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@ CodeMirror.defineMode("clike", function(config, parserConfig) {
298298
},
299299
modeProps: {fold: ["brace", "include"]}
300300
});
301+
301302
def("text/x-java", {
302303
name: "clike",
303304
keywords: words("abstract assert boolean break byte case catch char class const continue default " +
@@ -315,6 +316,7 @@ CodeMirror.defineMode("clike", function(config, parserConfig) {
315316
},
316317
modeProps: {fold: ["brace", "import"]}
317318
});
319+
318320
def("text/x-csharp", {
319321
name: "clike",
320322
keywords: words("abstract as base break case catch checked class const continue" +
@@ -341,6 +343,19 @@ CodeMirror.defineMode("clike", function(config, parserConfig) {
341343
}
342344
}
343345
});
346+
347+
function tokenTripleString(stream, state) {
348+
var escaped = false;
349+
while (!stream.eol()) {
350+
if (!escaped && stream.match('"""')) {
351+
state.tokenize = null;
352+
break;
353+
}
354+
escaped = stream.next() != "\\" && !escaped;
355+
}
356+
return "string";
357+
}
358+
344359
def("text/x-scala", {
345360
name: "clike",
346361
keywords: words(
@@ -366,8 +381,6 @@ CodeMirror.defineMode("clike", function(config, parserConfig) {
366381
"Compiler Double Exception Float Integer Long Math Number Object Package Pair Process " +
367382
"Runtime Runnable SecurityManager Short StackTraceElement StrictMath String " +
368383
"StringBuffer System Thread ThreadGroup ThreadLocal Throwable Triple Void"
369-
370-
371384
),
372385
multiLineStrings: true,
373386
blockKeywords: words("catch class do else finally for forSome if match switch try while"),
@@ -376,9 +389,15 @@ CodeMirror.defineMode("clike", function(config, parserConfig) {
376389
"@": function(stream) {
377390
stream.eatWhile(/[\w\$_]/);
378391
return "meta";
392+
},
393+
'"': function(stream, state) {
394+
if (!stream.match('""')) return false;
395+
state.tokenize = tokenTripleString;
396+
return state.tokenize(stream, state);
379397
}
380398
}
381399
});
400+
382401
def(["x-shader/x-vertex", "x-shader/x-fragment"], {
383402
name: "clike",
384403
keywords: words("float int bool void " +

mode/clike/index.html

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,21 @@ <h2>Java example</h2>
169169
member = value;
170170
}
171171
}
172+
</textarea></div>
173+
174+
<h2>Scala example</h2>
175+
176+
<div><textarea id="scala-code">
177+
object FilterTest extends App {
178+
def filter(xs: List[Int], threshold: Int) = {
179+
def process(ys: List[Int]): List[Int] =
180+
if (ys.isEmpty) ys
181+
else if (ys.head < threshold) ys.head :: process(ys.tail)
182+
else process(ys.tail)
183+
process(xs)
184+
}
185+
println(filter(List(1, 9, 2, 8, 3, 7, 4), 5))
186+
}
172187
</textarea></div>
173188

174189
<script>
@@ -187,6 +202,11 @@ <h2>Java example</h2>
187202
matchBrackets: true,
188203
mode: "text/x-java"
189204
});
205+
var scalaEditor = CodeMirror.fromTextArea(document.getElementById("scala-code"), {
206+
lineNumbers: true,
207+
matchBrackets: true,
208+
mode: "text/x-scala"
209+
});
190210
var mac = CodeMirror.keyMap.default == CodeMirror.keyMap.macDefault;
191211
CodeMirror.keyMap.default[(mac ? "Cmd" : "Ctrl") + "-Space"] = "autocomplete";
192212
</script>
@@ -198,7 +218,8 @@ <h2>Java example</h2>
198218
directives are recognized.</p>
199219

200220
<p><strong>MIME types defined:</strong> <code>text/x-csrc</code>
201-
(C code), <code>text/x-c++src</code> (C++
202-
code), <code>text/x-java</code> (Java
203-
code), <code>text/x-csharp</code> (C#).</p>
221+
(C), <code>text/x-c++src</code> (C++), <code>text/x-java</code>
222+
(Java), <code>text/x-csharp</code> (C#),
223+
<code>text/x-scala</code> (Scala), <code>text/x-vertex</code>
224+
and <code>x-shader/x-fragment</code> (shader programs).</p>
204225
</article>

0 commit comments

Comments
 (0)