local util = require("lspconfig.util")
|
|
|
|
-- Function to get python path (you can customize this)
|
|
local function get_python_path(workspace)
|
|
-- Check for virtual environment
|
|
if vim.env.VIRTUAL_ENV then
|
|
return vim.env.VIRTUAL_ENV .. "/bin/python"
|
|
end
|
|
|
|
-- Check for poetry
|
|
if util.root_pattern("pyproject.toml")(workspace) then
|
|
local handle = io.popen("cd " .. workspace .. " && poetry env info --path 2>/dev/null")
|
|
if handle then
|
|
local poetry_venv = handle:read("*a"):gsub("\n", "")
|
|
handle:close()
|
|
if poetry_venv ~= "" then
|
|
return poetry_venv .. "/bin/python"
|
|
end
|
|
end
|
|
end
|
|
|
|
-- Check for pipenv
|
|
if util.root_pattern("Pipfile")(workspace) then
|
|
local handle = io.popen("cd " .. workspace .. " && pipenv --venv 2>/dev/null")
|
|
if handle then
|
|
local pipenv_venv = handle:read("*a"):gsub("\n", "")
|
|
handle:close()
|
|
if pipenv_venv ~= "" then
|
|
return pipenv_venv .. "/bin/python"
|
|
end
|
|
end
|
|
end
|
|
|
|
-- Default to system python
|
|
return "python3"
|
|
end
|
|
|
|
return {
|
|
before_init = function(_, config)
|
|
config.settings.python.pythonPath = get_python_path(config.root_dir)
|
|
end,
|
|
settings = {
|
|
python = {
|
|
analysis = {
|
|
autoSearchPaths = true,
|
|
useLibraryCodeForTypes = true,
|
|
diagnosticMode = "workspace",
|
|
},
|
|
},
|
|
},
|
|
}
|