DCsunset / vscode-modal-editor

Customizable extension to turn VS Code into a modal editor
GNU Affero General Public License v3.0
36 stars 4 forks source link

Automatically switch to "select" mode when text is selected? #14

Closed digitalmaster closed 4 months ago

digitalmaster commented 4 months ago

Is it possible to automatically enable select mode when text is selected? This is a pretty powerful behavior i'm missing from ModalEdit since it enables me to have a different set of bindings for when text is selected.

Currently the only way seems to be to manually call modalEditor.setSelectMode after select actions. But this will still miss the case when I manually select text by mouse (don't judge 🙈 ).

Did some digging to see if there's vscode event that I could bind anytime text is selected to run the select mode command but haven't been able to find one yet.

Is there a way to do this?

DCsunset commented 4 months ago

I'm not sure if the event can be listened on in the editor, but there is an event called onDidChangeTextEditorSelection in the VSCode API. If you have some time, could you double check whether you can register any function for it?

If it's not doable outside the extension, I think we may consider allowing user to register their own function through this extension.

digitalmaster commented 4 months ago

If you have some time, could you double check whether you can register any function for it?

Yup, looks like you can. In fact, looks like you already have a function registered (tho it's not clear to me exactly what it does 🙈).

I just tested it and it does work - tho seems to trigger when i'm just moving around for some reason 🤔

Edit: Wrapping the mode change with function like this seems to do the trick of only triggering when text is actually selected.

DCsunset commented 4 months ago

Currently, the function implements inclusive range. I was actually asking if you can register a function in VSCode settings without modifying the code.

Probably you can't do that. In that case, I think it's a good idea to support user-defined event handlers.

digitalmaster commented 4 months ago

I love the idea of being able register commands to run on editor events. Something like:

{
  "events": {
    "onTextSelect": "modalEditor.setSelectMode",
    "onSearch": "closeFindWidget"
  }
}
digitalmaster commented 4 months ago

Sorry for spamming here.. but found another potential use case for this 🙈

I'm looking for a way to implement this behavior (tldr: clearing selection when entering normal mode).

If we could bind events the the mode change action I could also achieve the same thing :)

Edit: This does the trick for me in the mean time tho 🙏

DCsunset commented 4 months ago

You can easily achieve it without using events by calling clearSelections after setNormalMode since you can bind a key to a list of commands.

In the mean time, I think it's a better idea to go through README thoroughly before you add a lot of settings, because It can help you understand the extension better and possibly save your some time.

digitalmaster commented 4 months ago

Oh interesting. Can you say more about this. I actually have gone through the entire readme a couple of times 🙈 tho gotto admit that it's not immediately obvious to me how we'd bind the clearSelections command to an event not triggered by a config binding 🤔 Isn't the setNormalMode command bound to a vscode esc key binding? Gonna take another look...

DCsunset commented 4 months ago

You can overwrite the default keybinding for Esc to modalEditor.executeCommand, similar to the code in section https://github.com/DCsunset/vscode-modal-editor?tab=readme-ov-file#command-context

digitalmaster commented 4 months ago

Yup, that did the trick. Thanks for the help. Apologies for the confusion 🙏

For reference,

  {
    "key": "escape",
    "command": "modalEditor.executeCommand",
    "when": "editorTextFocus",
    "args": [
      "modalEditor.setNormalMode",
      "modalEditor.clearSelections",
    ]
  },