Search code examples
javascriptnewlinecodemirror

newline characters in CodeMirror


I'm writing a context-free parser for CodeMirror that parses code one character at a time, and outputs a style based on the state transition taken. The code uses the line-break character \n to trigger a state-transition, but CodeMirror seems to strip these from the input text (console.log (char === '\n') always returns false)

Is there anyway to configure CodeMirror to give me \n's as an input? The documentation didn't seem to mention the issue.

My state object is formatted as below

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

If any additional information or clarification is needed let me know


Solution

  • Having console.log (char === '\n') always returning false does not necessarily mean that CodeMirror strips newline characters - the text is passed as is, i.e. \n will be passed as two characters - \ and n.

    Try and utilize the token method in your mode to detect \n in the stream:

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

    You can also generalize that approach to work with any sequence, not just \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;
    }
    

    This has not been tested, but I suspect would be sufficient to do the trick. Please let me know how is it going for you.