Limiting behavior of electric input

Looking for ideas here.

I’ve got a custom CodeMirror mode that has a lot of auto-indent rules. I think they work great, but occasionally someone decides to do something different, and it’s frustrating when the editor throws away the personal artistic expression in their indentation choices. So, users are requesting that I be more discerning about when to trigger auto-indent.

I’m considering these two rules:

  1. Only trigger “electric” indent when the current line is already it’s “smart” indentation level before the edit that triggers it. That way, if you’ve done something different, I won’t mess with it,OR:
  2. If you’ve made edits to the current line that change the indent, then assume you know what you’re doing and disable electric indents until your cursor leaves that line.

Currently, it looks like implementing either rule would require adding an event handler on every keystroke to track this logic and change config.electricChars appropriately. Anyone see a better way? Is this going to cause problems if the config is changed on every key?

I agree that ‘electric input’, as a model for managing automatic indentation, isn’t great. I haven’t seen any other approach in this space that really works well either, unfortunately. Doing more intelligent analysis of what the user is actually doing sounds like it might help, but is also easy to get wrong, and I don’t currently have time to look into this very deeply (though I guess this question will come up in the design for version 6 as well).

Is there any disadvantage to changing the config on every keypress? Or should I smuggle the information to the mode in some other way. I’m not sure whether CodeMirror recalculates things when the config changes.

Depends on which option you change—some, like lineWrapping, require a bunch of expensive recalculations. Options like electricChars should be very cheap to change.

Thanks! I’ve implemented this by changing electricChars on every keypress. No problems so far. I appreciate the help.