How to insert a "line break" in my custom mode ?


#1

Hi there,

I’m trying to write a mode for a simple language. I managed most of the colouring with the tokenizer and the states.
However the code will come to codemirror as text, with no indentation/line breaks. I’d like to do those.

For example, I’d like to add a line break when it enters a state. What’s the best way to do that ?
I thought of using the command “newlineAndIndent” but I can’t figure out how the mode may access the codemirror object and I’m not sure it’s the best way to do it.

I’ve also defined an indent function in my mode and was hoping to use it, but I cannot find an example of how to use that.

Many thanks in advance !


#2

Modes only provide information about the document, they don’t modify it. If you want to implement auto-formatting, you’ll have to do it outside of your mode.


#3

thanks for that quick answer.

do you think it’s possible to use tokens and css to break lines without breaking the editor ? It seems such a shame to do the work twice as the tokenizer already identified everything. I’ve tried white-space:pre-line; but it does cause problems.


#4

You don’t have to do the work twice – you can inspect tokens produced by the mode from other code.

And no, doing pseudo-line breaking with CSS is not supported, nor is it going to do what you want (the line breaks wouldn’t be in the actual content, you couldn’t put your cursor directly before them, and so on).


#5

you are correct, I just thought of inspecting the tokens, which would help a lot :slight_smile:

thank you !