issue #247 Setting python virtual environment

pull/249/head
ray-x 1 year ago
parent 9ecc909548
commit b55f6101fb

@ -142,6 +142,10 @@ M.defaults = function()
pyright = {
on_attach = on_attach,
-- on_init = require('navigator.lspclient.python').on_init,
on_init = function(client)
require('navigator.lspclient.python').on_init(client)
end,
cmd = { 'pyright-langserver', '--stdio' },
filetypes = { 'python' },
flags = { allow_incremental_sync = true, debounce_text_changes = 500 },

@ -0,0 +1,38 @@
local util = require('lspconfig/util')
local path = util.path
local M = {}
local shell = vim.o.shell
local exepath = vim.fn.exepath
M.pyenv_path = function(workspace)
-- Use activated virtualenv.
if vim.env.VIRTUAL_ENV then
return path.join(vim.env.VIRTUAL_ENV, 'bin', 'python'), 'virtual env'
end
-- Find and use virtualenv in workspace directory.
for _, pattern in ipairs({ '*', '.*' }) do
local match = vim.fn.glob(path.join(workspace, pattern, 'pyvenv.cfg'))
local sep = require('navigator.util').path_sep()
local py = 'bin' .. sep .. 'python'
if match ~= '' then
print('found', match)
print(vim.fn.glob(path.join(workspace, pattern)))
match = string.gsub(match, 'pyvenv.cfg', py)
return match, string.format('venv base folder: %s', match)
end
match = vim.fn.glob(path.join(workspace, pattern, 'poetry.lock'))
if match ~= '' then
local venv_base_folder = vim.fn.trim(vim.fn.system('poetry env info -p'))
return path.join(venv_base_folder, 'bin', 'python'), string.format('venv base folder: %s', venv_base_folder)
end
end
-- Fallback to system Python.
return exepath('python3') or exepath('python') or 'python', 'fallback to system python path'
end
M.on_init = function(client)
local python_path, msg = M.pyenv_path(client.config.root_dir)
vim.notify(string.format('%s \ncurrent python path: %s', msg, python_path))
client.config.settings.python.pythonPath = python_path
end
return M
Loading…
Cancel
Save