我正在尝试为codemirror开发一个简单的模式。此模式将以蓝色和绿色交替为段落上色。段落之间的分隔是空行或仅包含空格的行。
这是可以使用的代码版本,但存在一个大问题,即未检测到空行:
CodeMirror.defineMode("rt", function() {
return {
startState: function() {return {state1: true};},
token: function(stream, state) {
if (stream.match(/\s\s*/)!=null){ # this fails to detect empty lines
state.state1 = !state.state1;
}
stream.skipToEnd();
if (state.state1) { return "status1"; }
return "status2";
}
};
});
如果我将其应用于以下文本:
line 1
line 2 # the next line is just a backspace and is not detected
line 3
line 4 # the next line is a few spaces followed by a backspace, it is detected
line 5
line 6
它可以从1号线到4号线用一种颜色着色,从5号线到6号线用另一种颜色着色,这是可以预期的。
我试图找到一种更新代码的方法,以便它检测第2行和第3行之间的空行。
Codemirror的文档说:
默认情况下,标记文档时仅跳过空白行。对于具有大量空白行的语言,您可以在模式上定义一个blankLine(state)方法,每当传递空白行时都会调用该方法,以便它可以更新解析器状态。
(http://codemirror.net/doc/manual.html#modeapi)
以下代码有效(添加了blankLine函数):
CodeMirror.defineMode("rt", function() {
return {
startState: function() {return {state1: true};},
blankLine: function (state){ state.state1 = !state.state1; },
token: function(stream, state) {
console.log(stream)
if (stream.match(/^\s*$/)!=null){
state.state1 = !state.state1;
}
stream.skipToEnd();
if (state.state1) { return "status1"; }
return "status2";
}
};
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句