Repository navigation
Description
Although some people like mirror cursor, it currently has issues. Many of these issues are existing behavior of multi-cursor. Some are hard to change, some can only be worked around in sophisticated ways, and some can only be fixed by code in vscode core.
- Pasting creates unwanted content at HTML closing tag Pasting after selecting html tag pastes twice #87067
- Duplicate line creates two lines Mirror cursor makes extra copy when duplicating row #87161
- Pasting create contents at two places [html] mirror cursor: pasting a full line will copy the full line twice #88072
- Multi cursor doesn't work nicely with mirror cursor [html] [rename on type] does not work properly with multicursor #88127
- When inserting space at
<h1|></h1>, it creates 2 edits on undo stack [html] Mirror cursor devolves into multi-cursor when undoing (ctrl-z) #87732
UI wise, it has issues as well:
- It's confusing which cursor in the main cursor
- Content changes can happen outside viewport, and there's no indication
- The multiple cursors aren't shown in scrollbar/minimap
And in my own experience, it doesn't play nicely with Vim extension.
In short, multi-cursor was the closest we could get to auto rename tag. We had to choose it because it's the only option for making 2 changes with 1 document edit. But it's not exactly the behavior people want (people want changes reflected in the mirrored region, not adding one more cursor).
Additionally, people has expressed interest in having mirror cursor for JSX: microsoft/TypeScript#51832
And we think it would be pretty cool, if this works for loop variables, function params, etc. For example:
for (let i|=0; i| < 10; i|++) {
count += i|
}To fix the issues, and to make this feature available to more languages, Alexandru Dima (@alexdima), Johannes Rieken (@jrieken) and I think it's best to add a new editor concept, synced regions. It would work this way:
- We introduce an API,
OnTypeRenameProvider - When cursor moves in a doc, VS Code asks this provider for a list of ranges
- These ranges are marked as in sync
- Any content change (type, delete, paste) will cause all other synced regions to update in one edit
- We can have a setting to switch between
semi-automatic(press a shortcut to enter this mode) andfull-automatic(like current mirror cursor).
Activity
Errors4l commentedon Jan 13, 2020
This proposed solution seems like a good way to mirror the cursor without the current annoyances (or bugs).
That said, I think it's important not to take it too far when it comes to automatically mirroring other ranges. Changing html tags is relatively safe, but variable names could quickly cause problems that the user might not see until it's too late. We already have F2 for that (though the same could be said for HTML tags, I suppose)
Of course, it all depends on the implementation of the provider. But still:

octref commentedon Jan 13, 2020
Agreed. We'll make this an opt-in.
ramyhhh commentedon Feb 10, 2020
I would add tow more problems:
I suggest to tweak mirror cursor by only activating it when the whole tag is selected (which is easily done with a double click), it's true that this way is a bit limiting in terms of per character editing, but in most cases re-typing the whole tag again is not so bad.
octref commentedon Feb 11, 2020
Just an idea: Having a key to enter the mode always makes it less discoverable. Maybe one implementation could be:
For example:
<div|></div>, the twodivbeing the synced regions<di|></di|>and your changes are syncedoctref commentedon Feb 18, 2020
I have tried out an editor contribution implementation with
ICodeEditor#executeEditsandICodeEditor#onDidChangeModelContent. Here's how it current works:iexecuteEdits(source, edits, endCursorState)to make an edit and set cursor ending positionisets cursor state, so myendCursorStateis lostI also cannot interleave
typewith my edits precisely, so when user types fast, this breaks.The good things are this implementation would fix most of the complaints:
What I would need is an editor core concept/API, here's my sketches with Peng Lyu (@rebornix) so far:
OnTypeRenameProviderfor synced regions. Keep all synced regions as markersAlexandru Dima (@alexdima) Would love to hear your input here.
bmix commentedon Mar 10, 2020
Nice, this definitely needs built in editor support!
As somebody working a lot with XML files, I'd really appreciate a solid mirroring of element tags and relatives. My use cases are:
<elem attr="value" />), the closing tag, that was before, needs to be erased as well.While some of these currently can be done with various add-ons, they tend to break and the solutions are not solid.
May I advice against needing to select the whole tag before this action? That would make editing very anti-fluid, especially in context of intellisense completions or correcting typos.
Some other places, where this, or similar issues have arisen:
Of course, designing this API would require some philosophical evaluation about editor support vs. LSP support. My choice would be to keep this as simple as possible and leave the heavy lifting to an LSP, with a fallback for editor-only scenarios.
Thank you for your consideration.
12 remaining items
gjsjohnmurray commentedon Mar 19, 2020
bmx (@bmx) I have been having good results with ScreenToGif
octref commentedon Mar 19, 2020
Andrii Dieiev (@IllusionMH)
Any content that matches
/^s/.It doesn't match
/^sso changes will be duplicated.No since we decided to make the feature language agnostic to editor, so other programming languages can implement this support.
People will complain about pasting
class="foo bar"doesn't work.Andreas M. (@bmix)
Please open new issues with exact reproducible steps and expected behaviors.
IllusionMH commentedon Mar 19, 2020
That's why my idea was to provide
enforceStopPatternflag alongside of each range (or maybe inverted flag depending on defaults).Return type of
provideOnTypeRenameRangeswill beProviderResult<Range[] | Array<{range: Range; enforceStopPattern?: boolean}>>;HTML/JSX will have
/\s/as stop pattern TSX will have/[\s<]/(because of generic components).In this 3 cases LS will provide this flag when ranges should have different behavior between ranges - open/closing tags: everything is pasted in first range (open tag), and only part before stop pasted into second range (closing tag).
Other languages (or cases) - won't provide this flag - all places will have same treatment.
That's rough idea, however I'd like see what drawbacks it has.
octref commentedon Mar 19, 2020
I'm not sure how that would help with replacing
<|h1|></h1>withh2 class="foo".Let's assume the
stopPatternis only enforced for the opening tag but not closing tag, what error cases does that prevent?IllusionMH commentedon Mar 19, 2020
Looks like I wasn't clear what I mean under "stopPattern is enforced" and that meaning of
stopPatternprobably changes. So idea is likeIn case of
<|h1|></h1>withh2 class="foo"in my theory it should be:Return type
when paste of
pastedValue = 'h2 class="foo"',stopPattern = /\s/applied to:replaceString = pasteValuethereforeh1replaced withh2 class="foo"as isreplaceString = pasteValue.split(stopPattern)[0]thereforeh1replaced with justh2(yes, splitting is not optimal, but simples expample of how to take part before stop)
If it's enforced for open tag, but not close tag result will be other way around - closing tag would have class, and opening tag will be `h2.
Not sure yet sure what behavior is better if pasted value starts with
stopPattern.Currently pasting
class="foo"(starts with space) when<|h1|></h1>will result in< class="foo"></h1>.To reproduce this behavior it will be no-op for cases when
enforceStopPattern: true, but paste as is to other places withenforceStopPattern: falsebergamin commentedon Mar 19, 2020
Andreas M. (@bmix) In case you are looking for a mobile one as well, I've been using GIF Maker-Editor. It doesn't keep a logo or a button on top all the time and let you edit so you can cut off the start and end of the video/gif for not showing the parts you commanded it to start/end recording
I just wish it could share with Giphy app to instantly upload it there
IllusionMH commentedon Mar 30, 2020
Pine (@octref) any feedback about behavior proposed in #88424 (comment) ?
octref commentedon Apr 2, 2020
Andrii Dieiev (@IllusionMH) I think
stopPatternmeans full stop, not really "stop until here". Pasting to duplicate full content or not duplicating at all would be easy to understand. Pasting to only replace some content but not others is confusing, especially because the pattern is not visible to users.Without this feature, is that an edit you would do?
If user's intention is to add
class="foo"toh1, I think 99% of the cases is they either paste the attribute with or without leading space right after h1. As I said, the goal is not to guarantee all your edits result in valid HTML, the goal is to provide editing helper that's easy to understand and streamline your common edits.octref commentedon Apr 2, 2020
API proposal is tracked in #94316. Will need to do that first before applying that to JSX / XML / etc.
IllusionMH commentedon Apr 2, 2020
can be changed to any meaningful name e.g.
safeInsertPattern(which should match part that can be safely inserted wheresafeInsertPatternis enforced) etc.Main point - differentiate between ranges that should accept whole clipboard content vs. ranges that accept only "safe" part
No I will not paste
[space]attr="value"while I haveh1selected, however I will definitely pasteh2 class="featured"which unfortunately invalidates closing tag (and I'm not alone) and it might not be noticeable while closing tag is not visible in editor.