emartineau / SelectionWrapper

Wraps text depending on the key pressed, like in Visual Studio Code
MIT License
35 stars 9 forks source link

ability to keep the selection? #10

Open AngryCarrot789 opened 3 years ago

AngryCarrot789 commented 3 years ago

so if you select some code and wrap it in ( )s, it wont deselect the code, allowing you to move the caret to the start of the brackets (like what you can do in intellij for example).

great plugin btw :)

emartineau commented 2 years ago

Sorry for getting back so late! I've given it multiple attempts myself awhile back and again just recently. With the way the extension currently works, I'm not sure if it's possible to do this (as much as I also want the same). I'll leave this issue open in case someone manages to figure this one out/ a future change makes this doable.

bestknighter commented 2 years ago

I might give this one a shot soon. Hints to where I should start looking would be very much appreciated.

emartineau commented 2 years ago

These might be a good starting point: https://docs.microsoft.com/en-us/dotnet/api/microsoft.visualstudio.text.editor.iwpftextview?view=visualstudiosdk-2022

The extension logic currently is roughly as follows:

  1. Listen for text buffer changes
  2. Capturing the selected text before the change occurs
  3. The change occurs, there's initially just the typed character in the buffer as is the default behavior in VS.
  4. The char before the text caret/cursor is checked
  5. If it's one of the "left" chars (e.g. (,[,{), the "wrapping" occurs. Wrapping is actually just taking the "left" char, getting the matching "right" char (e.g. ),],}), appending the right char to the previously captured selected text, and pasting the result right where the caret is.

There is an API (IWpfTextView->GetMultiSelectionBroker->AddSelection) to select text, but I couldn't get it to work. My guess is that the default selection clearing that occurs is happening after the event handler defined in this extension. It's been half a year since I last tried so I might be misremembering.

There's also https://docs.microsoft.com/en-us/dotnet/api/microsoft.visualstudio.text.editor.keyprocessor?view=visualstudiosdk-2022 which would probably be a better way to go about the triggering the wrap, but I couldn't figure this out either.

If you decide to give it a try, good luck! PRs are welcome

bestknighter commented 2 years ago

Awesome! I'm a bit busy right now but I'll keep this one saved. I def want to try doing it soon.