LazyVim / starter

Starter template for LazyVim
Apache License 2.0
1.01k stars 801 forks source link

Overriding Certain Keybinds (CodeLens) #58

Closed escapingSamsara closed 7 months ago

escapingSamsara commented 7 months ago

I am currently customizing my lazyvim starter setup a little bit and for that i added chatgpt.nvim.

In order to use it the way i want it i was adding which-key descriptions in order to use it and also have a reference.

I, for my needs wanted to use chatgpt by mapping it to c in order to open the menu for the different commands, for which i want to use these mappings:

c = {
  name = "ChatGPT",
    c = { "<cmd>ChatGPT<CR>", "ChatGPT" },
    e = { "<cmd>ChatGPTEditWithInstruction<CR>", "Edit with instruction", mode = { "n", "v" } },
    g = { "<cmd>ChatGPTRun grammar_correction<CR>", "Grammar Correction", mode = { "n", "v" } },
    t = { "<cmd>ChatGPTRun translate<CR>", "Translate", mode = { "n", "v" } },
    k = { "<cmd>ChatGPTRun keywords<CR>", "Keywords", mode = { "n", "v" } },
    d = { "<cmd>ChatGPTRun docstring<CR>", "Docstring", mode = { "n", "v" } },
    a = { "<cmd>ChatGPTRun add_tests<CR>", "Add Tests", mode = { "n", "v" } },
    o = { "<cmd>ChatGPTRun optimize_code<CR>", "Optimize Code", mode = { "n", "v" } },
    s = { "<cmd>ChatGPTRun summarize<CR>", "Summarize", mode = { "n", "v" } },
    f = { "<cmd>ChatGPTRun fix_bugs<CR>", "Fix Bugs", mode = { "n", "v" } },
    x = { "<cmd>ChatGPTRun explain_code<CR>", "Explain Code", mode = { "n", "v" } },
    r = { "<cmd>ChatGPTRun roxygen_edit<CR>", "Roxygen Edit", mode = { "n", "v" } },
    l = { "<cmd>ChatGPTRun code_readability_analysis<CR>", "Code Readability Analysis", mode = { "n", "v" } },
  },

the issue that evolved is that c is already a predefined command (opening the +code keymappings), and oddly, i just cant remap cc to run chatgpt, it always runs codeLens (which is the default action that runs when using this keybind). although i remapped codelens in my keymaps to another command (cl) and also remapped other commands conflicting with the way i want to use chatgpt.

here is my current keymaps.lua file (showing the changed snippets):

---- adding custom which-key ---------
local wk = require("which-key")
wk.register({
  c = {
    name = "+code",
    l = { "vim.lsp.codelens.run", "Run Codelens", mode = { "n", "v" }, has = "codeLens" },
    c = { "<cmd>ChatGPT<CR>", "ChatGPT", mode = { "n", "v" } },
    L = { "vim.lsp.codelens.refresh", "Refresh & Display Codelens", mode = { "n" }, has = "codeLens" },
    i = { "<cmd>LspInfo<cr>", "Lsp Info" },
    C = {
      name = "ChatGPT",
      e = { "<cmd>ChatGPTEditWithInstruction<CR>", "Edit with instruction", mode = { "n", "v" } },
      g = { "<cmd>ChatGPTRun grammar_correction<CR>", "Grammar Correction", mode = { "n", "v" } },
      t = { "<cmd>ChatGPTRun translate<CR>", "Translate", mode = { "n", "v" } },
      k = { "<cmd>ChatGPTRun keywords<CR>", "Keywords", mode = { "n", "v" } },
      d = { "<cmd>ChatGPTRun docstring<CR>", "Docstring", mode = { "n", "v" } },
      a = { "<cmd>ChatGPTRun add_tests<CR>", "Add Tests", mode = { "n", "v" } },
      o = { "<cmd>ChatGPTRun optimize_code<CR>", "Optimize Code", mode = { "n", "v" } },
      s = { "<cmd>ChatGPTRun summarize<CR>", "Summarize", mode = { "n", "v" } },
      f = { "<cmd>ChatGPTRun fix_bugs<CR>", "Fix Bugs", mode = { "n", "v" } },
      x = { "<cmd>ChatGPTRun explain_code<CR>", "Explain Code", mode = { "n", "v" } },
      r = { "<cmd>ChatGPTRun roxygen_edit<CR>", "Roxygen Edit", mode = { "n", "v" } },
      l = { "<cmd>ChatGPTRun code_readability_analysis<CR>", "Code Readability Analysis", mode = { "n", "v" } },
    },
  },
}, { prefix = "<leader>" })
--------------------------------------

---- CodeLens ------------------------
vim.keymap.set({ "n", "v" }, "<leader>cl", "vim.lsp.codelens.run", { desc = "Run Codelens" })
vim.keymap.set({ "n", "v" }, "<leader>cL", "vim.lsp.codelens.refresh", { desc = "Refresh & Display Codelens" })
--------------------------------------

---- ChatGPT  ------------------------
vim.keymap.set("n", "<leader>cc", "<cmd>ChatGPT<CR>", { desc = "ChatGPT" })
vim.keymap.set("v", "<leader>cc", "<cmd>ChatGPT<CR>", { desc = "ChatGPT" })

why does this way not overwrite the codelens keymaps, while on the other hand successfully remapping ci to show LspInfo (default = cl).

Do i need to change the codelens keymaps somewhere else or am i doing something wrong in remapping it?

escapingSamsara commented 7 months ago

EDIT: Seems like i fixed it by going through the docs again and using the described lsp-keymap method (https://www.lazyvim.org/plugins/lsp)

this is the code i added in order to make it work for now:

--/plugins/nvim-lsp.lua

return {
  -- LSP keymaps
  {
    "neovim/nvim-lspconfig",
    init = function()
      local keys = require("lazyvim.plugins.lsp.keymaps").get()
      -- disable a keymap
      keys[#keys + 1] = { "<leader>cc", false } --disable default lsp.codelens.run keymap
      keys[#keys + 1] = { "<leader>cC", false } --disable default lsp.codelens.refresh
      -- add a new keymap
      keys[#keys + 1] = { "<leader>cl", "vim.lsp.codelens.run" }
      keys[#keys + 1] = { "<leader>cL", "vim.lsp.codelens.refresh" }
    end,
  },
}
briandipalma commented 7 months ago

Why have you reopened this issue then?

escapingSamsara commented 7 months ago

Hey there !

I re-opened it because i hope for an answer regarding if this is the envisioned and correct way to do this. A fix is not a fix if it's a dirty one.