-
-
Notifications
You must be signed in to change notification settings - Fork 222
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
a more robust paredit deleteBackward #2685
Closed
Closed
+365
−181
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
…registerTextEditorCommand
…registerTextEditorCommand
…on to avoid setting the selection immediately after the edit, which occasionally led to mixups
✅ Deploy Preview for calva-docs ready!
To edit notification comments on pull requests, go to your Netlify site configuration. |
Superseding |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
This is for review and comment to fix 2611, the backspace garble.
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. paredit.backspace is the guinea pig. The non-async pattern need not "take over the world" but will be helpful for commands that might be issued rapidly. deleteForward could be another candidate.
A pattern is roughed out for slightly-delayed, debounced reformat-as-you-type, which is technically necessitated by the non-async command handlers. 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 suffix helps keep twins identifiable, and the particular suffix 'Now' is shorter and more pointed than 'Sync'.)
paredit.backspace uses a new editNow, vs the async "edit", in EditableDocument. Such is implemented in doc-mirror too. To avoid duplication, as a rule the predominant logic 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 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 ModelEdits are contrived to result in the appropriate final caret location. For example, the subroutine backspaceOnWhitespace is changed to use delete followed by insert, instead of a single changeRange, because insert positions the cursor and replace does not. The reason for the convention is that - if both an edit and a selection were 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.
wrapPareditCommandNow, the "...Now" synchronous-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. This is "work shedding". 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... another race condition! 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