Replies: 1 comment
-
I think I managed to do it ! Here is the lua configuration of lsp-zero if someone needs it: -- NOTE: to make any of this work you need a language server.
-- If you don't know what that is, watch this 5 min video:
-- https://www.youtube.com/watch?v=LaS32vctfOY
-- Reserve a space in the gutter
vim.opt.signcolumn = 'yes'
-- Add cmp_nvim_lsp capabilities settings to lspconfig
-- This should be executed before you configure any language server
local lspconfig_defaults = require('lspconfig').util.default_config
lspconfig_defaults.capabilities = vim.tbl_deep_extend(
'force',
lspconfig_defaults.capabilities,
require('cmp_nvim_lsp').default_capabilities()
)
-- Function to find the closest .venv directory
local function find_closest_venv(start_path)
local path = vim.fn.fnamemodify(start_path, ":p:h")
while path do
local venv_path = path .. '/.venv'
if vim.fn.isdirectory(venv_path) == 1 then
return venv_path
end
local parent_path = vim.fn.fnamemodify(path, ":h")
if parent_path == path then
break
end
path = parent_path
end
return nil
end
-- Configure pyright with lsp-zero
require('lspconfig').pyright.setup{
on_new_config = function(new_config, root_dir)
local venv = find_closest_venv(root_dir)
if venv then
new_config.settings.python.pythonPath = venv .. '/bin/python'
else
new_config.settings.python.pythonPath = vim.fn.exepath('python') -- fallback to system python
end
end,
root_dir = function(fname)
return require('lspconfig').util.root_pattern('.git', 'setup.py', 'setup.cfg', 'pyproject.toml', 'requirements.txt')(fname) or
require('lspconfig').util.path.dirname(fname)
end,
}
-- This is where you enable features that only work
-- if there is a language server active in the file
vim.api.nvim_create_autocmd('LspAttach', {
desc = 'LSP actions',
callback = function(event)
local opts = {buffer = event.buf}
vim.keymap.set('n', 'K', '<cmd>lua vim.lsp.buf.hover()<cr>', opts)
vim.keymap.set('n', 'gd', '<cmd>lua vim.lsp.buf.definition()<cr>', opts)
vim.keymap.set('n', 'gD', '<cmd>lua vim.lsp.buf.declaration()<cr>', opts)
vim.keymap.set('n', 'gi', '<cmd>lua vim.lsp.buf.implementation()<cr>', opts)
vim.keymap.set('n', 'go', '<cmd>lua vim.lsp.buf.type_definition()<cr>', opts)
vim.keymap.set('n', 'gr', '<cmd>lua vim.lsp.buf.references()<cr>', opts)
vim.keymap.set('n', 'gs', '<cmd>lua vim.lsp.buf.signature_help()<cr>', opts)
vim.keymap.set('n', '<F2>', '<cmd>lua vim.lsp.buf.rename()<cr>', opts)
vim.keymap.set({'n', 'x'}, '<F3>', '<cmd>lua vim.lsp.buf.format({async = true})<cr>', opts)
vim.keymap.set('n', '<F4>', '<cmd>lua vim.lsp.buf.code_action()<cr>', opts)
end,
})
local cmp = require('cmp')
cmp.setup({
sources = {
{name = 'nvim_lsp'},
},
snippet = {
expand = function(args)
-- You need Neovim v0.10 to use vim.snippet
vim.snippet.expand(args.body)
end,
},
mapping = cmp.mapping.preset.insert({}),
})
|
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello,
Is there a way in lsp-zero to change the lsp used in a file dynamically?
Let's see a concrete example. I'm doing python dev and I'm working on a project with several packages. Each package has a .venv folder containing the virtual env needed to work on this package.
When I'm editing some file in some package, I want lsp-zero to connect to the closest .venv to show me information specific to this package.
Do you think this is possible in lsp-zero?
Sorry If this is a noob question, I just started using nvim
Thanks.
Beta Was this translation helpful? Give feedback.
All reactions