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 #2689

Draft
wants to merge 4 commits into
base: dev
Choose a base branch
from

Conversation

pbwolf
Copy link

@pbwolf pbwolf commented Jan 1, 2025

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

  • 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 purposely no longer overrides 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).
    • Updated the [Unreleased] entry in CHANGELOG.md, linking the issue(s) that the PR is addressing.
  • 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)
    • N/A 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 Jan 1, 2025

Deploy Preview for calva-docs ready!

Name Link
🔨 Latest commit 1cf9e65
🔍 Latest deploy log https://app.netlify.com/sites/calva-docs/deploys/677addc923227700085045c5
😎 Deploy Preview https://deploy-preview-2689--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.

@bpringe bpringe requested a review from PEZ January 3, 2025 19:17
@pbwolf pbwolf marked this pull request as draft January 5, 2025 20:33
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