mirror of
https://github.com/LazyVim/LazyVim.git
synced 2026-07-21 20:11:06 +00:00
feat(lsp): auto add organizeImport keymaps for LSPs that support it
This commit is contained in:
parent
242f0983de
commit
e54689ebdc
5 changed files with 45 additions and 38 deletions
|
|
@ -38,23 +38,8 @@ return {
|
|||
logLevel = "error",
|
||||
},
|
||||
},
|
||||
keys = {
|
||||
{
|
||||
"<leader>co",
|
||||
LazyVim.lsp.action["source.organizeImports"],
|
||||
desc = "Organize Imports",
|
||||
},
|
||||
},
|
||||
},
|
||||
ruff_lsp = {
|
||||
keys = {
|
||||
{
|
||||
"<leader>co",
|
||||
LazyVim.lsp.action["source.organizeImports"],
|
||||
desc = "Organize Imports",
|
||||
},
|
||||
},
|
||||
},
|
||||
ruff_lsp = {},
|
||||
},
|
||||
setup = {
|
||||
[ruff] = function()
|
||||
|
|
|
|||
|
|
@ -23,15 +23,7 @@ return {
|
|||
"neovim/nvim-lspconfig",
|
||||
opts = {
|
||||
servers = {
|
||||
svelte = {
|
||||
keys = {
|
||||
{
|
||||
"<leader>co",
|
||||
LazyVim.lsp.action["source.organizeImports"],
|
||||
desc = "Organize Imports",
|
||||
},
|
||||
},
|
||||
},
|
||||
svelte = {},
|
||||
},
|
||||
},
|
||||
},
|
||||
|
|
|
|||
|
|
@ -56,11 +56,6 @@ return {
|
|||
},
|
||||
},
|
||||
keys = {
|
||||
{
|
||||
"<leader>co",
|
||||
LazyVim.lsp.action["source.organizeImports"],
|
||||
desc = "Organize Imports",
|
||||
},
|
||||
{
|
||||
"<leader>cu",
|
||||
LazyVim.lsp.action["source.removeUnused.ts"],
|
||||
|
|
@ -136,11 +131,6 @@ return {
|
|||
end,
|
||||
desc = "File References",
|
||||
},
|
||||
{
|
||||
"<leader>co",
|
||||
LazyVim.lsp.action["source.organizeImports"],
|
||||
desc = "Organize Imports",
|
||||
},
|
||||
{
|
||||
"<leader>cM",
|
||||
LazyVim.lsp.action["source.addMissingImports.ts"],
|
||||
|
|
|
|||
|
|
@ -99,6 +99,12 @@ return {
|
|||
desc = "Next Reference", enabled = function() return Snacks.words.is_enabled() end },
|
||||
{ "<a-p>", function() Snacks.words.jump(-vim.v.count1, true) end, has = "documentHighlight",
|
||||
desc = "Prev Reference", enabled = function() return Snacks.words.is_enabled() end },
|
||||
{
|
||||
"<leader>co",
|
||||
LazyVim.lsp.action["source.organizeImports"],
|
||||
desc = "Organize Imports",
|
||||
code_action = "source.organizeImports",
|
||||
},
|
||||
},
|
||||
},
|
||||
stylua = { enabled = false },
|
||||
|
|
|
|||
|
|
@ -3,8 +3,8 @@ local M = {}
|
|||
---@type LazyKeysLspSpec[]|nil
|
||||
M._keys = {}
|
||||
|
||||
---@alias LazyKeysLspSpec LazyKeysSpec|{has?:string|string[], enabled?:fun():boolean}
|
||||
---@alias LazyKeysLsp LazyKeys|{has?:string|string[], enabled?:fun():boolean}
|
||||
---@alias LazyKeysLspSpec LazyKeysSpec|{has?:string|string[], enabled?:(fun():boolean), code_action?:string}
|
||||
---@alias LazyKeysLsp LazyKeys|{has?:string|string[], code_action?:string, enabled?:fun():boolean}
|
||||
|
||||
---@deprecated
|
||||
---@return LazyKeysLspSpec[]
|
||||
|
|
@ -44,7 +44,41 @@ function M.set(filter, spec)
|
|||
for _, keys in pairs(Keys.resolve(spec)) do
|
||||
---@cast keys LazyKeysLsp
|
||||
local filters = {} ---@type vim.lsp.get_clients.Filter[]
|
||||
if keys.has then
|
||||
if keys.code_action then
|
||||
filter = vim.tbl_extend("force", vim.deepcopy(filter), { method = "textDocument/codeAction" })
|
||||
filters[#filters + 1] = filter
|
||||
|
||||
---@param kinds? string[]
|
||||
local function has(kinds)
|
||||
for _, k in ipairs(kinds or {}) do
|
||||
if type(k) == "string" and vim.startswith(k, keys.code_action) then
|
||||
return true
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local enabled = keys.enabled
|
||||
keys.enabled = function()
|
||||
if type(enabled) == "function" and not enabled() then
|
||||
return false
|
||||
end
|
||||
local clients = vim.lsp.get_clients(filter)
|
||||
for _, client in ipairs(clients) do
|
||||
-- check server cababilities first
|
||||
if has(vim.tbl_get(client, "server_capabilities", "codeActionProvider", "codeActionKinds")) then
|
||||
return true
|
||||
end
|
||||
-- check dynamic capabilities
|
||||
local regs = client.dynamic_capabilities:get("codeActionProvider")
|
||||
for _, reg in ipairs(regs or {}) do
|
||||
if has(vim.tbl_get(reg, "registerOptions", "codeActionKinds")) then
|
||||
return true
|
||||
end
|
||||
end
|
||||
end
|
||||
return false
|
||||
end
|
||||
elseif keys.has then
|
||||
local methods = type(keys.has) == "string" and { keys.has } or keys.has --[[@as string[] ]]
|
||||
for _, method in ipairs(methods) do
|
||||
method = method:find("/") and method or ("textDocument/" .. method)
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue