Skip to content

Commit

Permalink
refactor-lsp
Browse files Browse the repository at this point in the history
  • Loading branch information
mosheavni committed Apr 18, 2024
1 parent f29c59d commit 234f4c2
Show file tree
Hide file tree
Showing 4 changed files with 262 additions and 245 deletions.
220 changes: 42 additions & 178 deletions .config/nvim/lua/plugins/lsp/init.lua
Original file line number Diff line number Diff line change
@@ -1,81 +1,8 @@
local actions = function()
return {
['Format (<leader>lp)'] = function()
require('user.lsp.formatting').format()
end,
['Code Actions (<leader>la)'] = function()
vim.lsp.buf.code_action()
end,
['Code Lens (<leader>lx)'] = function()
vim.lsp.codelens.run()
end,
['Show Definition (gd)'] = function()
vim.cmd 'Lspsaga peek_definition'
end,
['Show Declaration (gD)'] = function()
vim.lsp.buf.declaration()
end,
['Show Type Definition (gy)'] = function()
vim.lsp.buf.type_definition()
end,
['Show Implementation (gi)'] = function()
vim.lsp.buf.implementation()
end,
['Find References (gr)'] = function()
vim.cmd 'Lspsaga finder'
end,
['Signature Help (<leader>lk)'] = function()
vim.lsp.buf.signature_help()
end,
['Signature Documentation (K)'] = function()
-- vim.lsp.buf.hover()
vim.cmd 'Lspsaga hover_doc'
end,
['Rename symbol (<leader>lrn)'] = function()
vim.cmd 'Lspsaga rename ++project'
end,
['Diagnostics quickfix list (<leader>lq)'] = function()
vim.diagnostic.setqflist()
end,
['Clear Diagnostics'] = function()
vim.diagnostic.reset()
end,
['Delete Log'] = function()
vim.system { 'rm', '-rf', vim.lsp.get_log_path() }
end,
}
end

local M = {
'neovim/nvim-lspconfig',
event = { 'BufReadPre', 'BufNewFile' },
opts = {
inlay_hints = { enabled = true },
capabilities = {
textDocument = {
completion = {
completionItem = {
snippetSupport = true,
},
},
-- codeAction = {
-- dynamicRegistration = true,
-- codeActionLiteralSupport = {
-- codeActionKind = {
-- valueSet = (function()
-- local res = vim.tbl_values(vim.lsp.protocol.CodeActionKind)
-- table.sort(res)
-- return res
-- end)(),
-- },
-- },
-- },
foldingRange = {
dynamicRegistration = false,
lineFoldingOnly = true,
},
},
},
setup = {
tsserver = function(_, opts)
require('typescript').setup {
Expand All @@ -84,19 +11,6 @@ local M = {
return true
end,

yamlls = function(_, opts)
local yaml_cfg = require('yaml-companion').setup {
schemas = opts.settings.yaml.schemas or {},
builtin_matchers = {
-- Detects Kubernetes files based on content
kubernetes = { enabled = true },
},
lspconfig = opts,
}
require('lspconfig')['yamlls'].setup(yaml_cfg)
return true
end,

helm_ls = function()
local configs = require 'lspconfig.configs'
local util = require 'lspconfig.util'
Expand All @@ -118,100 +32,50 @@ local M = {
},
}

M.init = function()
local start_ls = function()
_G.tmp_write { should_delete = false, new = false }
-- load lsp
require 'lspconfig'
end
vim.keymap.set('n', '<leader>ls', start_ls)
require('user.menu').add_actions('LSP', {
['Start LSP (<leader>ls)'] = function()
start_ls()
end,
})
end

M.config = function(_, opts)
require('user.menu').add_actions('LSP', actions())
require('user.lsp.handlers').setup()

require('lspconfig.ui.windows').default_options.border = require('user.utils').borders.single_rounded

-- Set formatting of lsp log
require('vim.lsp.log').set_format_func(vim.inspect)

local servers = require 'user.lsp.servers'
------------------
-- Capabilities --
------------------
local capabilities = vim.tbl_deep_extend(
'force',
{},
vim.lsp.protocol.make_client_capabilities(),
has_cmp and cmp_nvim_lsp.default_capabilities() or {},
opts.capabilities or {}
)

-----------------
-- Diagnostics --
-----------------
-- show icons in the sidebar
local signs = {
[vim.diagnostic.severity.ERROR] = '',
[vim.diagnostic.severity.WARN] = '',
[vim.diagnostic.severity.HINT] = '',
[vim.diagnostic.severity.INFO] = '',
}
vim.diagnostic.config {
signs = { text = signs },
update_in_insert = false,
virtual_text = {
severity = { min = vim.diagnostic.severity.WARN },
},
float = { border = require('user.utils').float_border },
}

local function setup(server)
local server_opts = vim.tbl_deep_extend('force', {
capabilities = vim.deepcopy(capabilities),
}, servers[server] or {})

if opts.setup[server] then
if opts.setup[server](server, server_opts) then
return
end
elseif opts.setup['*'] then
if opts.setup['*'](server, server_opts) then
return
end
end
require('lspconfig')[server].setup(server_opts)
end
M.init = require('user.lsp.config').init

M.config = function()
require('user.lsp.config').setup()
-- local function setup(server)
-- local server_opts = vim.tbl_deep_extend('force', {
-- capabilities = vim.deepcopy(capabilities),
-- }, servers[server] or {})
--
-- if opts.setup[server] then
-- if opts.setup[server](server, server_opts) then
-- return
-- end
-- elseif opts.setup['*'] then
-- if opts.setup['*'](server, server_opts) then
-- return
-- end
-- end
-- require('lspconfig')[server].setup(server_opts)
-- end

-- get all the servers that are available thourgh mason-lspconfig
local have_mason, mlsp = pcall(require, 'mason-lspconfig')
local all_mslp_servers = {}
if have_mason then
all_mslp_servers = vim.tbl_keys(require('mason-lspconfig.mappings.server').lspconfig_to_package)
end

local ensure_installed = {} ---@type string[]
for server, server_opts in pairs(servers) do
if server_opts then
server_opts = server_opts == true and {} or server_opts
-- run manual setup if mason=false or if this is a server that cannot be installed with mason-lspconfig
if server_opts.mason == false or not vim.tbl_contains(all_mslp_servers, server) then
setup(server)
else
ensure_installed[#ensure_installed + 1] = server
end
end
end

if have_mason then
mlsp.setup { ensure_installed = ensure_installed, handlers = { setup } }
end
-- local have_mason, mlsp = pcall(require, 'mason-lspconfig')
-- local all_mslp_servers = {}
-- if have_mason then
-- all_mslp_servers = vim.tbl_keys(require('mason-lspconfig.mappings.server').lspconfig_to_package)
-- end
--
-- local ensure_installed = {} ---@type string[]
-- for server, server_opts in pairs(servers) do
-- if server_opts then
-- server_opts = server_opts == true and {} or server_opts
-- -- run manual setup if mason=false or if this is a server that cannot be installed with mason-lspconfig
-- if server_opts.mason == false or not vim.tbl_contains(all_mslp_servers, server) then
-- setup(server)
-- else
-- ensure_installed[#ensure_installed + 1] = server
-- end
-- end
-- end
--
-- if have_mason then
-- mlsp.setup { ensure_installed = ensure_installed, handlers = { setup } }
-- end
end

M.dependencies = {
Expand Down
54 changes: 54 additions & 0 deletions .config/nvim/lua/user/lsp/actions.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
local M = {}
M.actions = function()
return {
['Format (<leader>lp)'] = function()
require('user.lsp.formatting').format()
end,
['Code Actions (<leader>la)'] = function()
vim.lsp.buf.code_action()
end,
['Code Lens (<leader>lx)'] = function()
vim.lsp.codelens.run()
end,
['Show Definition (gd)'] = function()
vim.cmd 'Lspsaga peek_definition'
end,
['Show Declaration (gD)'] = function()
vim.lsp.buf.declaration()
end,
['Show Type Definition (gy)'] = function()
vim.lsp.buf.type_definition()
end,
['Show Implementation (gi)'] = function()
vim.lsp.buf.implementation()
end,
['Find References (gr)'] = function()
vim.cmd 'Lspsaga finder'
end,
['Signature Help (<leader>lk)'] = function()
vim.lsp.buf.signature_help()
end,
['Signature Documentation (K)'] = function()
-- vim.lsp.buf.hover()
vim.cmd 'Lspsaga hover_doc'
end,
['Rename symbol (<leader>lrn)'] = function()
vim.cmd 'Lspsaga rename ++project'
end,
['Diagnostics quickfix list (<leader>lq)'] = function()
vim.diagnostic.setqflist()
end,
['Clear Diagnostics'] = function()
vim.diagnostic.reset()
end,
['Delete Log'] = function()
vim.system { 'rm', '-rf', vim.lsp.get_log_path() }
end,
}
end

M.setup = function()
require('user.menu').add_actions('LSP', M.actions())
end

return M
Loading

0 comments on commit 234f4c2

Please sign in to comment.