In my custom mode for CodeMirror, I want the electricInput event to fire when the user types a line starting with the word bank
(with optional leading whitespace).
I have electricInput set up like this: electricInput: /\s*bank$/i
The event DOES fire when the user types bank
at the beginning of a line. It does NOT fire when there are spaces before the word bank
. Why?
(the RegEx seems to be fine. I have a grammar rule in that mode with the same RegEx, and it matches the token as expected, regardless of leading white spaces:
CodeMirror.defineSimpleMode("myMode", {
start: [
{regex: /\s*bank$/i, token: 'bank', sol: true, indent: true}
Thanks to Marijn's kind help on the CodeMirror discussions forum, I was able to solve this: a custom indent
function needs to be passed to defineSimpleMode
. Then, we still need to set electricInput
(because otherwise the indent function does not get called when typing bank
). But no event handler for onElectricInput
is required.
CodeMirror.defineSimpleMode("myMode", {
start: [
...
],
meta: {
electricInput: /\s*bank$/i,
indent: function (state, textAfter, line) {
if (textAfter.substring(0, 4).toLowerCase() === 'bank') return 0
return 2;
}
}
});