ipython-contrib / jupyter_contrib_nbextensions

A collection of various notebook extensions for Jupyter
http://jupyter-contrib-nbextensions.readthedocs.io/en/latest
Other
5.22k stars 806 forks source link

spellchecker: allow a delay before spellchecking #1125

Open mangecoeur opened 6 years ago

mangecoeur commented 6 years ago

Currently, words are spellchecked as they are typed, resulting in partially typed words being highlighted as wrong until you finish typing them - this gets annoying. It would be nice to allow some time to finish writing. This could be as simple as throttling the spellchecker (e.g. to only run every 300ms) or something fancier like waiting until you pause typing (like in Atom editor which allows you to run functions when the text stops changing).

jcb91 commented 6 years ago

Hmm, I must admit I'm surprised this isn't already implemented. Will take a look.

jcb91 commented 6 years ago

Ah, right, it's because it all happens through CodeMirror, which just does the overlaying immediately. I can;t think off the top of my head of a way to delay this, other than removing the entire spellchecker overlay while typing, only to restore is after some delay. Would that suit your requirements @mangecoeur?

mangecoeur commented 6 years ago

That sounds fine, I guess there is no api for delayed/postponed events in codemirror then

On 16 Oct 2017 6:30 pm, "Josh Barnes" notifications@github.com wrote:

Ah, right, it's because it all happens through CodeMirror, which just does the overlaying immediately. I can;t think off the top of my head of a way to delay this, other than removing the entire spellchecker overlay while typing, only to restore is after some delay. Would that suit your requirements @mangecoeur https://github.com/mangecoeur?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/ipython-contrib/jupyter_contrib_nbextensions/issues/1125#issuecomment-336943214, or mute the thread https://github.com/notifications/unsubscribe-auth/AAtYVGop-JNO82xyPl5zjSBNK7go5r2sks5ss4S6gaJpZM4P6jMi .

jcb91 commented 6 years ago

I guess there is no api for delayed/postponed events in codemirror then

Oh, well there certainly is a way to do that, in terms of 'operations', plus the usual events, it's just that the current spellchecker is based on the codemirror overlay add-on, and as such just highlights all the current text stream by returning CSS classes based on content - it doesn't create objects attached to words which you could move around while editing. Every change results in a reparsing of the entire editor source in the same way as syntax highlighting. I guess if you're only spellchecking markdown or raw cells, then perhaps we could just delay all the highlighting updates, in order to delay spellchecking, but I'd have to have a dig around the cm source to be sure whether that'd be possible...

mangecoeur commented 6 years ago

Maybe you could use something like throttle from underscore to wrap whatever code returns the new CSS class, or (depending on how the internals work) keeping a copy of the current state and only update it after a minimum time has passed between calls. I don't think its necessary to remove the entire spell check overlay while typing, only that the spell check function is called slightly less frequently so that most of the time you will finish typing a word before the spell check kicks in.

jcb91 commented 6 years ago

keeping a copy of the current state and only update it after a minimum time has passed between calls

That's sort of what I'm suggesting, but it's not simple. On editing, codemirror queues syntax highlighting from the edit position (line) onwards (I think), but I'm not really sure how that happens, or how to delay it... throttling is easy, but saving state in a state machine you only have limited access to... more tricky. My alternative idea was to simply tell CM that there are no spelling errors, allow it to do its own error-free highlighting, then explicitly r-trigger highlighting again later. I haven't yet figured out how to do that :disappointed:

mangecoeur commented 6 years ago

Hm sounds tricky. Well why not try your approach and see, my only worry is that you might end up with the spellcheck highlights 'flashing' as they get removed and re-added which could be annoying.

jcb91 commented 6 years ago

Yeah, I've had a go, and unfortunately so far I can't see a way to prevent codemirror from doing its syntax highlighting :thinking:. I did manage a version where spellcheck highlighting is removed for anything on or after the current line, then reinstated, but as you suggest, I think it'd be kinda annoying in practice...