Skip to content

Commit

Permalink
...
Browse files Browse the repository at this point in the history
  • Loading branch information
TwIStOy committed Apr 15, 2024
1 parent b61859d commit b39ac2e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
22 changes: 19 additions & 3 deletions lua/dotvim/pkgs/lsp/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ local function create_lsp_autocmds(buffer)
})
end

---@param client? vim.lsp.Client
---@param buffer? number
local function setup_lsp_keymaps(buffer)
local function setup_lsp_keymaps(client, buffer)
---comment normal map
---@param lhs string
---@param rhs any
Expand Down Expand Up @@ -67,6 +68,21 @@ local function setup_lsp_keymaps(buffer)
nmap("[c", methods.prev_diagnostic, "previous-diagnostic")

nmap("]c", methods.next_diagnostic, "next-diagnostic")

local organize_imports = function() end
local code_action_kinds = vim.F.if_nil(
vim.tbl_get(
vim.F.if_nil(client, {}),
"server_capabilities",
"codeActionProvider",
"codeActionKinds"
),
{}
)
if vim.list_contains(code_action_kinds, "source.organizeImports") then
organize_imports = methods.organize_imports
end
nmap("<leader>fi", organize_imports, "organize-imports")
end

M.setup = function()
Expand All @@ -75,12 +91,12 @@ M.setup = function()
if vim.g.vscode then
setup_lsp_keymaps()
else
Core.lsp.on_lsp_attach(function(_, buffer)
Core.lsp.on_lsp_attach(function(client, buffer)
local exists, value =
pcall(vim.api.nvim_buf_get_var, buffer, "_dotvim_lsp_attached")
if not exists or not value then
create_lsp_autocmds(buffer)
setup_lsp_keymaps(buffer)
setup_lsp_keymaps(client, buffer)
vim.api.nvim_buf_set_var(buffer, "_dotvim_lsp_attached", true)
end
end)
Expand Down
7 changes: 7 additions & 0 deletions lua/dotvim/pkgs/lsp/methods.lua
Original file line number Diff line number Diff line change
Expand Up @@ -193,4 +193,11 @@ function Methods.rename(new_name, options)
vim.lsp.buf.rename(new_name, options)
end

function Methods.organize_imports()
vim.lsp.buf.code_action {
context = { only = { "source.organizeImports" } },
apply = true,
}
end

return Methods

0 comments on commit b39ac2e

Please sign in to comment.