From dd004560dbbcfd24e79152e0c7c553224a2ab76b Mon Sep 17 00:00:00 2001 From: Taken Date: Fri, 29 Nov 2024 12:41:11 +0100 Subject: [PATCH] Added venv support for pyright --- lua/taken/plugins/lsp/lspconfig.lua | 33 ++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/lua/taken/plugins/lsp/lspconfig.lua b/lua/taken/plugins/lsp/lspconfig.lua index 3c8fce9..465d875 100644 --- a/lua/taken/plugins/lsp/lspconfig.lua +++ b/lua/taken/plugins/lsp/lspconfig.lua @@ -13,6 +13,8 @@ return { local lspconfig = require("lspconfig") local tserrortranslator = require("ts-error-translator") local cmp_nvim_lsp = require("cmp_nvim_lsp") + local util = require("lspconfig.util") + local path = util.path local on_attach = require("taken.utils.on_attach") local capabilities = cmp_nvim_lsp.default_capabilities() @@ -26,7 +28,6 @@ return { local defaultLsps = { "html", "cssls", - "pyright", "jsonls", "rust_analyzer", "yamlls", @@ -66,6 +67,36 @@ return { }, }) + local function get_python_path(workspace) + -- Use activated virtualenv. + if vim.env.VIRTUAL_ENV then + return path.join(vim.env.VIRTUAL_ENV, "bin", "python") + end + + -- Find and use virtualenv in workspace directory. + for _, pattern in ipairs({ "*", ".*" }) do + local match = vim.fn.glob(path.join(workspace, pattern, "pyvenv.cfg")) + if match ~= "" then + return path.join(path.dirname(match), "bin", "python") + end + end + + return nil + end + + -- python + lspconfig["pyright"].setup({ + capabilities = capabilities, + on_attach = on_attach, + before_init = function(_, config) + local python_path = get_python_path(config.root_dir) + if python_path == nil then + return + end + config.settings.python.pythonPath = get_python_path(config.root_dir) + end, + }) + -- typescript lspconfig["ts_ls"].setup({ capabilities = capabilities,