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

Can bindings reference other bindings? #13

Closed digitalmaster closed 4 months ago

digitalmaster commented 4 months ago

Hi 👋 - I'm testing out this plugin (currently migrating from ModalEdit). Is there a way to reuse existing bindings in other ones? ModalEdit does this with the modalEdit.typeNormalKeys command which simulates typing an exiting binding.

For example, how would you implement something like "delete inside [char]"? In ModelEdit we define select command that selects the range, then just call typeNormalKeys with vi[char] then deleteRight, then insertMode.

How do we achieve something similar here?

Thanks for the help 🙏

DCsunset commented 4 months ago

I'm not sure if I understand the scenario correctly, but are you trying to reference another key definition in the keymap? If so, you can simply do it using JavaScript in the config file with two stages:

// define direct keys
let keymap = {
  normal: {
    h: "cursorLeft",
    j: "cursorDown",
    k: "cursorUp",
    l: "cursorRight",
    w: [
      "cancelSelection",
      "cursorWordStartRightSelect",
    ]
  }
};
// define recursive keys
keymap.normal.Z = [ keymap.normal.h, keymap.normal.w ];

module.exports = keymap;

Or even with getter functions (note: this can only access keybindings at the same level):

let keymap = {
  normal: {
    h: "cursorLeft",
    j: "cursorDown",
    k: "cursorUp",
    l: "cursorRight",
    w: [
      "cancelSelection",
      "cursorWordStartRightSelect",
    ],
    get Z() { return [ this.h, this.w ]; }
  }
};

module.exports = keymap;
digitalmaster commented 4 months ago

Oh wow! Of course 🙈😅.. it's just JS. So used to doing this in json that I forgot. Thanks will give it a shot. Thank you 😊

Side note: Are there any advanced or fully fleshed out examples of bindings I could reference?

DCsunset commented 4 months ago

I think presets/helix.js is an example you could look at. Not sure if it's advanced enough for you.

digitalmaster commented 4 months ago

Gotcha.. I guess i'm just trying to get a sense for how to do basic movements like da( (which deletes everything around the current parens) or si' (which selects text inside single quotes).

For example, these two references for ModalEdit really helped me figure things out:

anthonyjclark commented 4 months ago

Here is my current mappings: https://gist.github.com/anthonyjclark/1de4882c13ffd3c61863bc657557253e

You might be able to grab some things from there.

digitalmaster commented 4 months ago

Thank you kind sir. 🙌