nvim: configure LSPs

This commit is contained in:
tea 2024-09-03 19:50:40 +02:00
parent 3541a94115
commit a7ed98d874
No known key found for this signature in database
4 changed files with 192 additions and 6 deletions

View File

@ -19,7 +19,7 @@ function M.map(mode, shortcut, command, description, buffer)
vim.keymap.set(mode, shortcut, command, opts) vim.keymap.set(mode, shortcut, command, opts)
end end
-- Files that could indicate the project's root directory -- files that could indicate the project's root directory
M.project_root_markers = { M.project_root_markers = {
'.git', '.hg', '.svn', '.bzr', '_darcs', '.git', '.hg', '.svn', '.bzr', '_darcs',
'.projectile', '.luarc.json', '.editorconfig', 'Cargo.toml' '.projectile', '.luarc.json', '.editorconfig', 'Cargo.toml'
@ -96,7 +96,7 @@ function M.current_function()
end end
-- Insert or update a modeline at the bottom of the buffer -- insert or update a modeline at the bottom of the buffer
function M.insert_modeline() function M.insert_modeline()
local comment_string = vim.o.commentstring local comment_string = vim.o.commentstring
if comment_string == '' then if comment_string == '' then

183
.config/nvim/lua/my/lsp.lua Normal file
View File

@ -0,0 +1,183 @@
local map = require('my.functions').map
local on_attach = function(client, bufnr)
-- TODO: key mappings
if client.server_capabilities.documentHighlightProvider then
vim.api.nvim_create_augroup('config_lsp', { clear = false })
vim.api.nvim_clear_autocmds({
buffer = bufnr,
group = 'config_lsp',
})
vim.api.nvim_create_autocmd({ 'CursorHold', 'CursorHoldI' }, {
group = 'config_lsp',
buffer = bufnr,
callback = vim.lsp.buf.document_highlight,
})
vim.api.nvim_create_autocmd({ 'CursorMoved', 'CursorMovedI' }, {
group = 'config_lsp',
buffer = bufnr,
callback = vim.lsp.buf.clear_references,
})
end
-- don't autoformat code like text
vim.bo.formatoptions = vim.o.formatoptions:gsub('t', '')
-- virtual text hint for parameters
require ('lsp_signature').on_attach({
bind = true,
handler_opts = {
border = 'rounded'
},
floating_window = false
}, bufnr)
require('lsp-status').on_attach(client)
end
-- add completion capabilities
local capabilities = require('cmp_nvim_lsp').default_capabilities()
-- add progress capabilities
local lsp_status = require('lsp-status')
lsp_status.register_progress()
capabilities = vim.tbl_extend('keep', capabilities, lsp_status.capabilities)
-- opens lsp.util.open_floating_preview() -> nvim_open_win()
vim.lsp.handlers['textDocument/hover'] = vim.lsp.with(vim.lsp.handlers.hover, {
border = 'rounded',
width = 60,
focusable = false,
-- next 2 lines do nothing, overwritten by open_floating_preview()
relative = 'win',
anchor = 'NE',
})
-- setup servers
local lspconfig = require('lspconfig')
-- set up language servers that don't need any special configuration
local simple_ls = {
['vscode-html-language-server'] = 'html',
['vscode-css-language-server'] = 'cssls',
['vscode-json-language-server'] = 'jsonls',
['gopls'] = 'gopls',
['cmake-language-server'] = 'cmake',
['yaml-language-server'] = 'yamlls',
['docker-langserver'] = 'dockerls',
}
for exe, ls in pairs(simple_ls) do
if vim.fn.executable(exe) == 1 then
lspconfig[ls].setup({
on_attach = on_attach,
capabilities = capabilities
})
end
end
if vim.fn.executable('clangd') > 0 then
require("clangd_extensions").setup({
server = {
cmd = {
'clangd',
'--compile-commands-dir=build',
'--clang-tidy', -- needs >=clangd-9
'--ranking-model=decision_forest' -- needs >=clangd-12
},
init_options = { clangdFileStatus = true },
on_attach = on_attach,
capabilities = capabilities,
handlers = lsp_status.extensions.clangd.setup(),
},
extensions = {
inlay_hints = {
show_parameter_hints = false
}
}
})
end
-- TODO: lua-language-server
if vim.fn.executable('pylsp') == 1 then
lspconfig.pylsp.setup({
settings = {
pylsp = {
}
},
on_attach = on_attach,
capabilities = capabilities
})
end
if vim.fn.executable('lemminx') == 1 then
lspconfig.lemminx.setup({
filetypes = {
'xml', 'xsd', 'xsl', 'xslt', 'svg', 'gentoo-metadata'
},
settings = {
xml = {
server = { -- path for cached XML Schemas
workDir = vim.fn.stdpath('cache') .. '/lemminx'
}
}
},
on_attach = on_attach,
capabilities = capabilities
})
end
if vim.fn.glob('/usr/lib64/perl5/vendor_perl/5.*/Perl/LanguageServer.pm') ~= ''
then
lspconfig.perlls.setup({
on_attach = on_attach,
capabilities = capabilities
})
end
if vim.fn.executable('qml-lsp') == 1 then
lspconfig.qml_lsp.setup({
filetypes = { 'qml', 'qmljs' },
on_attach = on_attach,
capabilities = capabilities
})
end
if vim.fn.executable('bash-language-server') > 0 then
lspconfig.bashls.setup({
filetypes = { 'sh', 'bash', 'ebuild', 'gentoo-init-d.sh' },
on_attach = on_attach,
capabilities = capabilities
})
end
if vim.fn.executable('typescript-language-server') > 0 then
lspconfig.tsserver.setup({
cmd = {
'typescript-language-server', '--stdio',
'--tsserver-path', 'tsserver'
},
on_attach = on_attach,
capabilities = capabilities
})
end
if vim.fn.executable('rust-analyzer') == 1 then
require("rust-tools").setup({
server = {
on_attach = on_attach,
capabilities = capabilities,
settings = {
["rust-analyzer"] = {
-- checkOnSave = {
-- command = "clippy",
-- },
-- diagnostics = {
-- enable = true,
-- },
}
}
},
})
end

View File

@ -1,4 +1,3 @@
-- TODO: configure
return { return {
{ 'https://github.com/neovim/nvim-lspconfig', { 'https://github.com/neovim/nvim-lspconfig',
version = '*', version = '*',
@ -8,8 +7,12 @@ return {
version = '*', version = '*',
}, },
{ 'https://github.com/ray-x/lsp_signature.nvim' }, { 'https://github.com/ray-x/lsp_signature.nvim' },
{ 'https://github.com/p00f/clangd_extensions.nvim' }, { 'https://github.com/p00f/clangd_extensions.nvim',
cond = vim.fn.executable('clangd') == 1,
},
{ 'https://github.com/simrat39/rust-tools.nvim', { 'https://github.com/simrat39/rust-tools.nvim',
dependencies = 'nvim-lspconfig', dependencies = 'nvim-lspconfig',
} cond = vim.fn.executable('rust-analyzer') == 1,
},
{ 'https://github.com/nvim-lua/lsp-status.nvim' },
} }

View File

@ -101,7 +101,7 @@ return {
{ 'https://github.com/nvim-lualine/lualine.nvim', { 'https://github.com/nvim-lualine/lualine.nvim',
dependencies = { dependencies = {
'nvim-web-devicons', 'nvim-web-devicons',
'https://github.com/nvim-lua/lsp-status.nvim', 'lsp-status.nvim',
}, },
init = function() init = function()
vim.o.showmode = false vim.o.showmode = false