Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keymap Context Alias & Nested Contexts #20481

Open
1 task done
abueide opened this issue Nov 10, 2024 · 0 comments
Open
1 task done

Keymap Context Alias & Nested Contexts #20481

abueide opened this issue Nov 10, 2024 · 0 comments
Labels
admin read Pending admin review enhancement [core label] triage Maintainer needs to classify the issue

Comments

@abueide
Copy link

abueide commented Nov 10, 2024

Check for existing issues

  • Completed

Describe the feature

My keymap has gotten kind of complex and it can be tiring and verbose to specify new contexts where only one thing changes. For example I use vim mode which adds a lot of context management overhead, and often times I want to bind a key to a different task depending on the filetype.

{
    "context": "Editor && (vim_mode == normal || vim_mode == visual) && !VimWaiting && !menu && extension == gleam",
    "bindings": {
      "space r": ["task::Spawn", { "task_name": "gleam-run" }],
      "space t": ["task::Spawn", { "task_name": "gleam-test" }]
    }
  },
  {
    "context": "Editor && (vim_mode == normal || vim_mode == visual) && !VimWaiting && !menu && extension == kt",
    "bindings": {
      "space r": ["task::Spawn", { "task_name": "gradle-run" }],
      "space t": ["task::Spawn", { "task_name": "gradle-test" }]
    }
  },

as you can see there is a lot of cognitive overhead in simply changing one parameter for the context, namely && extension == kt. When I want to place a keymap I generally just want to place it in somewhere that makes sense, and parsing out a long boolean expression looking for small differences between each long expression can end up taking quite a bit of time.

I see two (possibly complementary) solutions.

The first (most backward compatible one) would be to allow context aliases. See this for example:

Old config

{
    "context": "Editor && (vim_mode == normal || vim_mode == visual) && !VimWaiting && !menu",
    "bindings": {
      "space g h d": "editor::ToggleHunkDiff",
      "space g h r": "editor::RevertSelectedHunks",

      "space t i": "editor::ToggleInlayHints",

      "space c z": "workspace::ToggleCenteredLayout",
      "space m p": "markdown::OpenPreview",
      "space m P": "markdown::OpenPreviewToTheSide",

      "space s w": "pane::DeploySearch",
      "space s g": "editor::FindAllReferences",

      "space a i": "assistant::ToggleFocus",
      "g f": "editor::OpenExcerpts",

      "space c a": ["task::Spawn", { "task_name": "colmena-apply-on" }],
      "space c l": ["task::Spawn", { "task_name": "colmena-apply-local" }],

      "space f p": "projects::OpenRecent",
      "space f x": "zed::Extensions",
      "space f k": ["task::Spawn", { "task_name": "open-keymap" }],
      "space f s": ["task::Spawn", { "task_name": "open-settings" }],
      "space f t": ["task::Spawn", { "task_name": "open-tasks" }]
    }
  },
  {
    "context": "Editor && (vim_mode == normal || vim_mode == visual) && !VimWaiting && !menu && extension == gleam",
    "bindings": {
      "space r": ["task::Spawn", { "task_name": "gleam-run" }],
      "space t": ["task::Spawn", { "task_name": "gleam-test" }]
    }
  },
  {
    "context": "Editor && (vim_mode == normal || vim_mode == visual) && !VimWaiting && !menu && extension == kt",
    "bindings": {
      "space r": ["task::Spawn", { "task_name": "gradle-run" }],
      "space t": ["task::Spawn", { "task_name": "gradle-test" }]
    }
  },

Config with context aliases

{
  "context_alias": {
    "n": "vim_mode == normal",
    "v": "vim_mode == visual",
    "i": "vim_mode == insert",
    "kt": "extension == kt",
    "md": "extension == md",
    "gleam": "extension == gleam",
    "ed": "Editor && !VimWaiting && !menu",
    "ed_nv": "$ed && ($n || $v)",
  }
}

[
  {
    "context": "$ed_nv",
    "bindings": {
      "space g h d": "editor::ToggleHunkDiff",
      "space g h r": "editor::RevertSelectedHunks",

      "space t i": "editor::ToggleInlayHints",

      "space c z": "workspace::ToggleCenteredLayout",
      "space m p": "markdown::OpenPreview",
      "space m P": "markdown::OpenPreviewToTheSide",

      "space s w": "pane::DeploySearch",
      "space s g": "editor::FindAllReferences",

      "space a i": "assistant::ToggleFocus",
      "g f": "editor::OpenExcerpts",

      "space c a": ["task::Spawn", { "task_name": "colmena-apply-on" }],
      "space c l": ["task::Spawn", { "task_name": "colmena-apply-local" }],

      "space f p": "projects::OpenRecent",
      "space f x": "zed::Extensions",
      "space f k": ["task::Spawn", { "task_name": "open-keymap" }],
      "space f s": ["task::Spawn", { "task_name": "open-settings" }],
      "space f t": ["task::Spawn", { "task_name": "open-tasks" }]
    }
  },
  {
    "context": "$ed_nv && $gleam",
    "bindings": {
      "space r": ["task::Spawn", { "task_name": "gleam-run" }],
      "space t": ["task::Spawn", { "task_name": "gleam-test" }]
    }
  },
  {
    "context": "$ed_nv && $kt",
    "bindings": {
      "space r": ["task::Spawn", { "task_name": "gradle-run" }],
      "space t": ["task::Spawn", { "task_name": "gradle-test" }]
    }
  },

The new version is a lot easier to parse and find where I need to put a keymap quickly. It also just allows for a lot more customization without affecting people's existing configs, since its just an optional additional field to specify the aliases.

Another solution would be to allow nesting contexts, in a tree structure that allow you to put more generic keybindings at the top of the tree and contexts that add on to the parent context limiting it by just one more expression. That way we could have the afformentioned $ed_nv context, then under that context add another one for $kt and $gleam specific bindings that add on to the parent context.









### Environment

N/A

### If applicable, add mockups / screenshots to help present your vision of the feature


N/A
@abueide abueide added admin read Pending admin review enhancement [core label] triage Maintainer needs to classify the issue labels Nov 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
admin read Pending admin review enhancement [core label] triage Maintainer needs to classify the issue
Projects
None yet
Development

No branches or pull requests

1 participant