calva icon indicating copy to clipboard operation
calva copied to clipboard

2732 slurp barf multicursor

Open pbwolf opened this issue 10 months ago • 2 comments

What has changed?

  • Slurp and barf support multi-cursor editing (backward and forward: four commands in all).
  • Internally (paredit.ts), multi-cursor concerns are factored out and separated from per-command strategy:
    • The core algorithm of each command - the calculation of ModelEdits for a single cursor - is extracted to a new function.
      • Following the example of rewrapSexpr, the new functions produce an array of changeRange edits. Previous use of deleteRange or insertString by slurp and barf is changed to changeRange.
        • The 100% changeRange technique worked OK in VS Code but flunked the unit tests, exposing a bug and requiring a correction in the model's emulation of how VS Code moves cursors after edits.
    • A single new shared multi-cursor-collation function to deduplicate and sort all cursors' ModelEdits is extracted from rewrapSexpr.
    • In sum: rewrap, slurp, and barf all use the same multi-cursor coordination heuristics, which may be general enough to help extend further editing commands for multiple cursors.
  • Barf no longer requires an async touch-up of the selection positions: reducing likelihood of a race condition between two barf commands issued in rapid succession. (Slurp already did not require it.)
  • New multi-cursor tests of slurp and barf, forward and backward, are based on the first and simplest single-cursor test of the four respective functions.
  • Single-cursor tests of slurp and barf are not modified.
  • Reformat-as-you-type reformats all edited points, and preserves all cursors so you can proceed to issue further multi-cursor editing commands.
    • It no longer relies on editing commands to provide a formatDepth. Instead, the ranges to reformat are computed from the list of edits.
    • It preserves multiple cursors by translating the reformatted text blob into little changeRange edits that only adjust the whitespace.

Loose ends:

  • Slurp, barf, and rewrap do not check whether the user enabled multi-cursor features (nor is a {multicursor:false} option heeded), because the single-cursor case is not a distinct procedure. This deviation from documentation could be remedied in the documentation... but on the other hand, paredit's other multi-cursor support has been established for two or three years and the burden of supporting a feature flag might no longer be required?

Wobbly parts:

  • The multicursor-capable reformat-as-you-type is rather ambitious, and it affects all commands that edit via the MirrorDocument, even if there is just 1 cursor.

Addresses #2732

My Calva PR Checklist

I have:

  • [x] Read How to Contribute.
  • [x] Directed this pull request at the dev branch. (Or have specific reasons to target some other branch.)
  • [x] Made sure I have changed the PR base branch, so that it is not published. (Sorry for the nagging.)
  • [x] Made sure there is an issue registered with a clear problem statement that this PR addresses, (created the issue if it was not present).
    • [x] Updated the [Unreleased] entry in CHANGELOG.md, linking the issue(s) that the PR is addressing.
  • [x] 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.
  • [x] Added to or updated docs in this branch, if appropriate
  • [x] Tests
    • [x] Tested the particular change
    • [x] Figured if the change might have some side effects and tested those as well.
  • [x] Formatted all JavaScript and TypeScript code that was changed. (use the prettier extension or run npm run prettier-format)
  • [x] 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

pbwolf avatar Feb 17 '25 05:02 pbwolf

Deploy Preview for calva-docs ready!

Name Link
Latest commit eea04fca52824483ae456b7ed0937df3c923726d
Latest deploy log https://app.netlify.com/sites/calva-docs/deploys/67bd146995e02700088cd73e
Deploy Preview https://deploy-preview-2733--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.

netlify[bot] avatar Feb 17 '25 05:02 netlify[bot]

You are awesome, @pbwolf! ❤️

I will have a closer look tomorrow. Must 💤 now.

PEZ avatar Feb 17 '25 23:02 PEZ

I found this issue, don't know if you are aware:

[:a |:b]

barf forward

Expected:

[:a|] :b

Actual:

[:a] |:b

It's as if it attaches the cursor position to the closest form. Barfing forward from here works as expected:

[:a| :b]

PEZ avatar Feb 23 '25 20:02 PEZ

@pez I added 2 unit tests to check that barf forward keeps the cursor within the shortened list, and made it do so.

pbwolf avatar Feb 23 '25 23:02 pbwolf

Hmm. Barf backward, also, can leave the cursor outside the narrowed form, unlike in version 486.

pbwolf avatar Feb 24 '25 02:02 pbwolf

Added a unit test that barf backward keeps the cursor within the shortened list, and made it do so.

pbwolf avatar Feb 25 '25 00:02 pbwolf

I forgot to report back that I tested the barf forward after your fix and that it worked great. Now have also tested barf backward and it behaves beautifully. And I am getting addicted to multi cursor slurp and barf!

PEZ avatar Feb 25 '25 08:02 PEZ

"Undo" treats the formatting and the slurp/barf as two distinct events. Is that tolerable? (It has the benefit, that if something surprises you, you can distinguish a-surprise-during-formatting from a-surprise-during-structural-editing.)

pbwolf avatar Feb 25 '25 09:02 pbwolf

Undo is a bit strange also in current Calva. Now tried with this branch, since you mentioned it. I think it is acceptable, as in I rather have multi cursor slurp and barf with these extra undo's than staying on single cursor. But it is a bit confusing. Especially when the formatting didn't do anything. We could release as is and try make it better later?

PEZ avatar Feb 25 '25 10:02 PEZ

Undo is a bit strange also in current Calva. Now tried with this branch, since you mentioned it. I think it is acceptable, as in I rather have multi cursor slurp and barf with these extra undo's than staying on single cursor. But it is a bit confusing. Especially when the formatting didn't do anything. We could release as is and try make it better later?

I agree

pbwolf avatar Feb 27 '25 00:02 pbwolf

What's your plan for this, @pbwolf? Asking for a friend who longs for it. 😀

PEZ avatar May 09 '25 05:05 PEZ

Since multi-cursor formatting (a simple matter) has been resolved separately, I made a new, greatly-simplified PR #2816 for Slurp and Barf. I will try pressing the Close button on this PR that had combined the two enhancements.

pbwolf avatar May 10 '25 14:05 pbwolf