1
0
mirror of https://github.com/jdhao/nvim-config.git synced 2025-06-08 14:14:33 +02:00

Switch Python LSP for completion and add ruff

pyright servers type checking, completion, and ruff servers linting and
diagnostics (ruff can also format Python files, but it is slightly
different from black)
This commit is contained in:
jdhao 2024-11-05 22:07:09 +01:00
parent 97f72936ac
commit 9769c24633

View File

@ -3,6 +3,7 @@ local api = vim.api
local keymap = vim.keymap local keymap = vim.keymap
local lsp = vim.lsp local lsp = vim.lsp
local diagnostic = vim.diagnostic local diagnostic = vim.diagnostic
local lspconfig = require("lspconfig")
local utils = require("utils") local utils = require("utils")
@ -114,69 +115,97 @@ local custom_attach = function(client, bufnr)
end end
local capabilities = require('cmp_nvim_lsp').default_capabilities() local capabilities = require('cmp_nvim_lsp').default_capabilities()
-- required by nvim-ufo -- required by nvim-ufo
capabilities.textDocument.foldingRange = { capabilities.textDocument.foldingRange = {
dynamicRegistration = false, dynamicRegistration = false,
lineFoldingOnly = true lineFoldingOnly = true
} }
local lspconfig = require("lspconfig") -- For what diagnostic is enabled in which type checking mode, check doc:
-- https://github.com/microsoft/pyright/blob/main/docs/configuration.md#diagnostic-settings-defaults
-- Currently, the pyright also has some issues displaying hover documentation:
-- https://www.reddit.com/r/neovim/comments/1gdv1rc/what_is_causeing_the_lsp_hover_docs_to_looks_like/
if utils.executable("pylsp") then if utils.executable('pyright') then
local venv_path = os.getenv('VIRTUAL_ENV') local new_capability = {
local py_path = nil -- this will remove some of the diagnostics that duplicates those from ruff, idea taken and adapted from
-- decide which python executable to use for mypy -- here: https://github.com/astral-sh/ruff-lsp/issues/384#issuecomment-1989619482
if venv_path ~= nil then textDocument = {
py_path = venv_path .. "/bin/python3" publishDiagnostics = {
else tagSupport = {
py_path = vim.g.python3_host_prog valueSet = { 2 }
end }
}
}
}
local merged_capability = vim.tbl_deep_extend("force", capabilities, new_capability)
lspconfig.pylsp.setup { lspconfig.pyright.setup {
cmd = { "delance-langserver", "--stdio" },
on_attach = custom_attach, on_attach = custom_attach,
-- capabilities = merged_capability,
capabilities = capabilities,
settings = { settings = {
pylsp = { pyright = {
plugins = { -- disable import sorting and use Ruff for this
-- formatter options disableOrganizeImports = true,
black = { enabled = true }, disableTaggedHints = false,
autopep8 = { enabled = false }, },
yapf = { enabled = false }, python = {
-- linter options analysis = {
pylint = { enabled = true, executable = "pylint" }, autoSearchPaths = true,
ruff = { enabled = false }, diagnosticMode = "workspace",
pyflakes = { enabled = false }, typeCheckingMode = "standard",
pycodestyle = { enabled = false }, useLibraryCodeForTypes = true,
-- type checker -- we can this setting below to redefine some diagnostics
pylsp_mypy = { diagnosticSeverityOverrides = {
enabled = true, deprecateTypingAliases = false,
overrides = { "--python-executable", py_path, true }, },
report_progress = true, -- inlay hint settings are provided by pylance?
live_mode = false inlayHints = {
callArgumentNames = "partial",
functionReturnTypes = true,
pytestParameters = true,
variableTypes = true,
}, },
-- auto-completion options
jedi_completion = { fuzzy = true },
-- import sorting
isort = { enabled = true },
}, },
}, },
}, },
flags = {
debounce_text_changes = 200,
},
capabilities = capabilities,
} }
else else
vim.notify("pylsp not found!", vim.log.levels.WARN, { title = "Nvim-config" }) vim.notify("pyright not found!", vim.log.levels.WARN, { title = 'Nvim-config' })
end end
-- if utils.executable('pyright') then if utils.executable("ruff") then
-- lspconfig.pyright.setup{ require('lspconfig').ruff.setup({
-- on_attach = custom_attach, on_attach = custom_attach,
-- capabilities = capabilities capabilities = capabilities,
-- } init_options = {
-- else -- the settings can be found here: https://docs.astral.sh/ruff/editors/settings/
-- vim.notify("pyright not found!", vim.log.levels.WARN, {title = 'Nvim-config'}) settings = {
-- end organizeImports = true,
}
}
})
end
-- Disable ruff hover feature in favor of Pyright
vim.api.nvim_create_autocmd("LspAttach", {
group = vim.api.nvim_create_augroup('lsp_attach_disable_ruff_hover', { clear = true }),
callback = function(args)
local client = vim.lsp.get_client_by_id(args.data.client_id)
-- vim.print(client.name, client.server_capabilities)
if client == nil then
return
end
if client.name == 'ruff' then
client.server_capabilities.hoverProvider = false
end
end,
desc = 'LSP: Disable hover capability from Ruff',
})
if utils.executable("ltex-ls") then if utils.executable("ltex-ls") then
lspconfig.ltex.setup { lspconfig.ltex.setup {