stylus icon indicating copy to clipboard operation
stylus copied to clipboard

FR: Increase undo buffer in codemirror editor

Open Gitoffthelawn opened this issue 5 years ago • 8 comments

Can you please increase the size of the undo buffer in the codemirror editor? Thanks.

Gitoffthelawn avatar Oct 26 '20 15:10 Gitoffthelawn

You can run the following code in devtools console:

prefs.set('editor.options', {...prefs.get('editor.options'), undoDepth: 500})

We won't be exposing such low-level options, but we can allow configuring them via JSON like Violentmonkey does:

image

There'll be a gear icon or a button/link in the Options block that opens a dialog similar to linter config.

tophf avatar Oct 26 '20 16:10 tophf

@tophf

  1. Thank you for the devtools command.
  2. For undoDepth, is that the number of steps, bytes, KB, or something else?
  3. Yes, configuring them all via JSON like VM or the linter would be wonderful.

Gitoffthelawn avatar Oct 26 '20 17:10 Gitoffthelawn

You can find out yourself what that means. I think ... indicates thoughtfulness.

tophf avatar Oct 26 '20 17:10 tophf

@tophf

I finally had a few minutes of free time to try the code you provided.

To test it, I tried the following in a devtools console in Firefox:

prefs.set('editor.options', {...prefs.get('editor.options'), undoDepth: 2})

I want to increase the undoDepth, not decrease it, but I first wanted to test the code you provided to ensure it works.

According to the CodeMirror manual, undoDepth accepts any integer.

Yet after doing this, the depth of the undo function within Stylus is still much greater than 2.

Do you know how to get your code working? Also, I would like to change the ctrl+space keyboard shortcut within the CodeMirror editor to something else. How would that be done?

Gitoffthelawn avatar Feb 12 '21 10:02 Gitoffthelawn

That's because CodeMirror resets undoDepth to infinity when we call its method clearHistory(). Probably a bug in CodeMirror. ~I'll add a workaround in our code~. The next version of CodeMirror will be fixed. Meanwhile you'll have to manually run editor.getEditors().forEach(cm => cm.doc.history.undoDepth = 2) each time you open the editor.

change the ctrl+space

Run this once:

prefs.set('editor.options', {...prefs.get('editor.options'), extraKeys: {...CodeMirror.defaults.extraKeys, 'Esc': 'autocomplete'}})

tophf avatar Feb 12 '21 11:02 tophf

@tophf

Thanks so much!

When is Stylus calling the clearHistory() method?

To clarify, the detailed steps I took were:

  1. Open a style in the Stylus code editor
  2. Open devtools console
  3. Run the following code:
prefs.set('editor.options', {...prefs.get('editor.options'), undoDepth: 2})
  1. Perform a bunch of edits in the Stylus code editor
  2. Try to undo 10+ edits (which works, instead of failing as expected)

When in that sequence is Stylus calling the clearHistory() method?

Also, is Stylus supposed to save these CodeMirror settings between Firefox sessions (once CodeMirror is updated... per your reply)?

Regarding both new code tidbits you provided... much thanks. I'm hoping to have a few minutes of free time to try them both this weekend.

Gitoffthelawn avatar Feb 12 '21 12:02 Gitoffthelawn

When in that sequence is Stylus calling the clearHistory()

When the editor is initialized.

is Stylus supposed to save these CodeMirror settings

Yes, except for the undoDepth hack.

tophf avatar Feb 12 '21 13:02 tophf

@tophf I performed a reboot a few hours ago, and the original code you provided to change the undo depth now is working. Obviously, it wasn't the reboot that caused it to work, but more likely Firefox being closed and then re-opened.

It's also stored in the Stylus export file, which is real good.

I'm going to try the keyboard shortcut key hack next.

I like your idea of "a gear icon or a button/link in the Options block that opens a dialog similar to linter config" to edit a JSON representation of CodeMirror settings.

Gitoffthelawn avatar Feb 13 '21 14:02 Gitoffthelawn