max397574 / better-escape.nvim

Map keys without delay when typing
GNU General Public License v3.0
629 stars 19 forks source link

How to replicate previous example from config? #63

Closed RayJameson closed 3 months ago

RayJameson commented 3 months ago

Hi! I have a problem with the plugin before the rewrite, I had this config:

---@type LazySpec
return {
  "max397574/better-escape.nvim",
  cond = not vim.g.vscode,
  opts = function(_, opts)
    opts.mapping = { "JK", "JJ", "jk", "jj" }
    opts.keys = function()
      if vim.bo.filetype == "OverseerForm" then
        return vim.api.nvim_win_get_cursor(0)[2] > 1 and "<esc>lx" or "<esc>x"
      end
      return vim.api.nvim_win_get_cursor(0)[2] > 1 and "<esc>l" or "<esc>"
    end
  end,
}

how to replicate this after the rewrite?

now I have this, but it doesn't escape to normal mode

---@type LazySpec
return {
  "max397574/better-escape.nvim",
  cond = not vim.g.vscode,
  opts = function(_, opts)
    local esc_fn = function()
      if vim.bo.filetype == "OverseerForm" then
        return vim.api.nvim_win_get_cursor(0)[2] > 1 and "<esc>lx" or "<esc>x"
      end
      return vim.api.nvim_win_get_cursor(0)[2] > 1 and "<esc>l" or "<esc>"
    end
    opts.mappings = {
      i = {
        j = {
          -- These can all also be functions
          k = esc_fn,
          j = esc_fn,
        },
      },
      c = {
        j = {
          k = esc_fn,
          j = esc_fn,
        },
      },
      t = {
        j = {
          k = esc_fn,
          j = esc_fn,
        },
      },
      v = {
        j = {
          k = esc_fn,
        },
      },
      s = {
        j = {
          k = esc_fn,
        },
      },
    }
  end,
}

asciicast

max397574 commented 3 months ago

I know what the issue is should be able to povide a solution for it this evening

as a workaround you could use vim.api.nvim_feedkeys(t(key), "in", false) instead of returning the keys t is defined like this https://github.com/max397574/better-escape.nvim/blob/3973b07287293a3c8e2a117f48419af7ad99e60e/lua/better_escape.lua#L3-L9

RayJameson commented 3 months ago

thanks for the fix!