codemmirror中的换行符

newline characters in CodeMirror

本文关键字:换行符 codemmirror      更新时间:2023-09-26

我正在为CodeMirror编写一个上下文无关的解析器,它一次解析一个字符,并根据所采取的状态转换输出样式。代码使用换行字符'n来触发状态转换,但是CodeMirror似乎从输入文本中删除了这些字符(console.log (char === ''n')总是返回false)

是否有办法配置CodeMirror给我'n作为输入?文档似乎没有提到这个问题。

我的状态对象的格式如下

{
    state1: {
       active: true,
       edges: {
           ''n': 'state2'
       }
    },
    state2: {
       active: false,
       edges: {
           '#': 'state1'
       }
    }
}

如果需要任何额外的信息或澄清,请告诉我

console.log (char === ''n')总是返回false并不一定意味着CodeMirror去掉换行符-文本将按原样传递,即'n将作为两个字符传递- 'n

尝试在你的模式中使用token方法来检测流中的'n:

var newLine = '''n';
token : function(stream) {
    var next = stream.next();
    var tokenName = null;
    if ('''' === next) {
        var match = stream.match(new RegExp(newLine.charAt(1)));
        match && (tokenName = 'some-style' || null);
    }
    return tokenName;
}

您还可以将该方法推广到任何序列,而不仅仅是'n:

var sequence = 'some-sequence';
token : function(stream) {
    var next = stream.next();
    var tokenName = null;
    var ch = sequence.charAt(0);
    // search for the first letter
    if (next === ch) {
        // try to match the rest of the sequence
        var match = stream.match(new RegExp(sequence.substring(1)));
        match && (tokenName = 'some-style' || null);
    }
    return tokenName;
}

这还没有经过测试,但我认为已经足够了。请让我知道你的情况。