Skip to content
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
wants to merge 15 commits into from

Conversation

pbwolf
Copy link

@pbwolf pbwolf commented Dec 29, 2024

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

  • format.ts - the original async paths tangential to format-as-you-type were touched. Is any feature disrupted?
  • paredit-test.ts - the backspace tests, when checking the cursor location after an edit, are in fact checking the doc.model's new simulation of VS Code's TextEditor behavior, since paredit.backspace can no longer override post-edit selection.

Fixes #2611

My Calva PR Checklist

I have:

  • Read How to Contribute.
  • Directed this pull request at the dev branch. (Or have specific reasons to target some other branch.)
  • Made sure I have changed the PR base branch, so that it is not published. (Sorry for the nagging.)
  • Made sure there is an issue registered with a clear problem statement that this PR addresses, (created the issue if it was not present).
    • No Updated the [Unreleased] entry in CHANGELOG.md, linking the issue(s) that the PR is addressing.
  • I think not Figured if anything about the fix warrants tests on Mac/Linux/Windows/Remote/Whatever, and either tested it there if so, or mentioned it in the PR.
  • N/A Added to or updated docs in this branch, if appropriate
  • Tests
    • Tested the particular change (backspace)
    • Don't know Figured if the change might have some side effects and tested those as well.
  • Formatted all JavaScript and TypeScript code that was changed. (use the prettier extension or run npm run prettier-format)
  • Confirmed that there are no linter warnings or errors (use the eslint extension, run npm run eslint before creating your PR, or run npm run eslint-watch to eslint as you go).

Ping @PEZ, @bpringe, @corasaurus-hex, @Cyrik

Copy link

netlify bot commented Dec 29, 2024

Deploy Preview for calva-docs ready!

Name Link
🔨 Latest commit 0b309fb
🔍 Latest deploy log https://app.netlify.com/sites/calva-docs/deploys/6774a7d94c1c9800084d13c0
😎 Deploy Preview https://deploy-preview-2685--calva-docs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@pbwolf
Copy link
Author

pbwolf commented Jan 1, 2025

Superseding

@pbwolf pbwolf closed this Jan 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant