which-key.nvim icon indicating copy to clipboard operation
which-key.nvim copied to clipboard

[Help with implementation]

Open AdamBD opened this issue 2 years ago • 1 comments

Hello,

I am trying to implement a set of custom keymappings to use with buffers that have neovimLSP running on them. My setup script for the LSP calls this file to setup the key mapping for the buffers I open. However these key mappings are not being registered

Could someone help me understand whats wrong in my code?

local M = {}

local whichkey = require "which-key"

local keymap = vim.api.nvim_set_keymap
local buf_keymap = vim.api.nvim_buf_set_keymap

local function keymappings(client, bufnr)
  local opts = { noremap = true, silent = true }

  -- Key mappings
  buf_keymap(bufnr, "n", "K", "<cmd>lua vim.lsp.buf.hover()<CR>", opts)
  keymap("n", "[d", "<cmd>lua vim.diagnostic.goto_prev()<CR>", opts)
  keymap("n", "]d", "<cmd>lua vim.diagnostic.goto_next()<CR>", opts)
  keymap("n", "[e", "<cmd>lua vim.diagnostic.goto_prev({severity = vim.diagnostic.severity.ERROR})<CR>", opts)
  keymap("n", "]e", "<cmd>lua vim.diagnostic.goto_next({severity = vim.diagnostic.severity.ERROR})<CR>", opts)

  -- Whichkey
  local keymap_t = {
    t = {
      name = "Code",
      r = { "<cmd>lua vim.lsp.buf.rename()<CR>", "Rename" },
      a = { "<cmd>lua vim.lsp.buf.code_action()<CR>", "Code Action" },
      d = { "<cmd>lua vim.diagnostic.open_float()<CR>", "Line Diagnostics" },
      i = { "<cmd>LspInfo<CR>", "Lsp Info" },
    },
  }
  if client.resolved_capabilities.document_formatting then
    keymap_t.l.f = { "<cmd>lua vim.lsp.buf.formatting()<CR>", "Format Document" }
  end

  whichkey.register(keymap_t, { buffer = bufnr, prefix = "<leader>" })
end

function M.setup(client, bufnr)
  keymappings(client, bufnr)
end

return M

AdamBD avatar Jan 12 '23 08:01 AdamBD

  1. Are you calling require("<this_script>").setup{client=<client>, bufnr=<buffer>} anywhere? In some BufEnter autocmd perhaps scoping to buffers?
  2. You don't seem to be passing bufnr to the which-key -wrapped vim.*.buf.* commands, not an expert here but I think those are scoped to specific buffers (so the LSP knows the language I assume)

Look here for examples: https://github.com/neovim/nvim-lspconfig#suggested-configuration

puttehi avatar Jan 13 '23 00:01 puttehi

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 7 days.

github-actions[bot] avatar Jul 06 '24 01:07 github-actions[bot]