a more robust paredit deleteBackward #2689
Draft
+379
−192
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What has changed?
A pattern is established for non-async command handlers that run faster and expose themselves less to the risk of race conditions. This will be helpful for commands that might be issued rapidly, such as backspace.
A pattern is roughed out for slightly-delayed, debounced reformat-as-you-type, which is technically necessitated by the non-async command handlers because the reformatter needs a post-edit document and model, which do not exist until after the command has exited and processChanges has been apprised of the changes. Anyway, decoupling reformatting from the commands helps keep the commands fast enough to enjoy on autorepeat.
paredit.backspace is the guinea pig for the aforementioned patterns.
paredit.backspace is registered with registerTextEditorCommand (so it receives a TextEditorEdit from VS Code), and does not await anything. In the pareditCommands array, it has "handlerNow" instead of "handler", distinguishing the registration technique. (A name suffix helps flag async/sync twins, and the particular suffix 'Now' is shorter and more pointed than 'Sync'.)
paredit.backspace uses a new non-async editNow, vs the async "edit", in EditableDocument. Such is implemented in doc-mirror too. To avoid duplication, and whereas the predominant logic wasn't essentially async, it has moved to the "...Now" function and the original async function delegates to it with async dressings before or after.
paredit.backspace uses a new non-async getConfigNow (instead of async "getConfig"). getConfig already stuffed the haul from LSP in a variable; getConfigNow uses that variable. A new TTL of a few seconds is observed to avoid flagrant staleness.
paredit.backspace's non-async ModelEdits are contrived to result in the appropriate final caret location. For example, the subroutine backspaceOnWhitespace is changed to use delete & then insert, instead of a single changeRange, because insert positions the cursor and changeRange does not. The reason for exploiting the selection naturally consequent from the edit is that - if both an edit and a selection are attempted - the selection apparently isn't well coordinated with the edit and can result in garble when backspace is on autorepeat. (Not every command could be implemented this way, but not every command needs to be non-async.)
Compensating for paredit.backspace not explicitly specifying a selection to refine each edit, StringDocument infers a selection change from the ModelEdits, in mimicry of VS Code's text editor. StringDocument's opinion about the selection is important to unit tests.
Work Shedding ("pressure release valve"): wrapPareditCommandNow, the "...Now" non-async-handler analog of wrapPareditCommand, makes a handler with a document-version safety guard that compares with a version number kept in the model by processChanges. In case of mismatch, the handler discards the command and logs a console warning, "paredit is skipping (command name) because TextDocumentChangeEvent is overdue". Without this guard, it is not uncommon for autorepeat to invoke backspace a second or third time before processChanges is apprised of the first backspace. Consequently the 2nd and 3rd invocations would use an outdated model and produce a garbled document. (It turns out not to be productive to put a similar guard in async handlers' wrapPareditCommand, since autorepeat may invoke a second paredit command while an in-progress async paredit command, which has not yet modified the document, is parked awaiting something.)
paredit.backspace invokes a new scheduleFormatAsType (but doesn't await it!) in one case: upon removal of an empty pair of brackets.
Risks
Fixes #2611
My Calva PR Checklist
I have:
dev
branch. (Or have specific reasons to target some other branch.)published
. (Sorry for the nagging.)[Unreleased]
entry inCHANGELOG.md
, linking the issue(s) that the PR is addressing.Added to or updated docs in this branch, if appropriatenpm run prettier-format
)npm run eslint
before creating your PR, or runnpm run eslint-watch
to eslint as you go).Ping @PEZ, @bpringe, @corasaurus-hex, @Cyrik