【问题标题】:new mode for codemirror : Detecting an empty line in a streamcodemirror 的新模式:检测流中的空行
【发布时间】:2014-10-19 09:22:05
【问题描述】:

我正在尝试为 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 行之间的空行。有什么方法可以做到这一点?

【问题讨论】:

    标签: javascript codemirror codemirror-modes


    【解决方案1】:

    您可以在 javascript 正则表达式中使用 [\b] 检测退格:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions

    顺便说一句,/\s\s*/ 可以简化为/\s+/,但是如果您想检测“空行或仅包含空格的行”,您可以使用/\s*/

    另外如果你不关心正则表达式的实际结果数组,你可以使用test()来代替:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/test

    您可以使用^ 表示行首,$ 表示行尾。

    所以代码大致如下:

    CodeMirror.defineMode("rt", function() {
      return {
        startState: function() {
          return {
            state1: true
          };
        },
        token: function(stream, state) {
          if ( /^\s*$/m.test(stream) ) {
            state.state1 = !state.state1;
          }
          stream.skipToEnd();
          return state.state1 ? "status1" : "status2";
        }
      };
    });
    

    m 标志用于指定多行输入字符串应被视为多行。如果使用 m 标志,则 ^ 和 $ 匹配输入字符串中任何行的开头或结尾,而不是整个字符串的开头或结尾。示例:http://www.w3schools.com/jsref/jsref_regexp_m.asp

    【讨论】:

    • 恐怕这会导致线条交替颜色,而不是段落 ^^;
    • 哦,抱歉,我忘记了正则表达式中的 ^$。你可以试试这个吗?
    • 仍然没有成功。 “空行”似乎根本没有通过函数,好像 codemirror 在输入函数之前以某种方式跳过了它们......
    • (我的意思是如果我在函数的开头有console.log(stream),“空行”就不会出现。)
    • 在 Codemirror 的 github 上提交问题如何?它似乎比 Stackoverflow 更活跃:github.com/codemirror/CodeMirror/issues
    【解决方案2】:

    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";
        }
      };
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多