diff --git a/lua/plugins/dap.lua b/lua/plugins/dap.lua index b11f322..6a3e793 100644 --- a/lua/plugins/dap.lua +++ b/lua/plugins/dap.lua @@ -90,68 +90,13 @@ return { end }) - local function getVenvSuffix() - if vim.loop.os_uname().sysname == 'Linux' then - return 'bin/python' - elseif vim.loop.os_uname().sysname == 'Windows_NT' then - return 'Scripts/python.exe' - end - end - - local function getVenvFromJson(jsonfile) - if not vim.fn.filereadable(jsonfile) then - return nil - end - local f = io.open(jsonfile, 'r') - if not f then - return nil - end - local data = f:read('*a') - f:close() - if data then - local jdata = vim.json.decode(data) - if jdata['venvPath'] ~= nil and jdata['venv'] ~= nil then - return jdata['venvPath'] .. '/' .. jdata['venv'] - end - end - return nil - end - - local function getPythonEnv() - local venv = os.getenv('VIRTUAL_ENV') - if venv ~= nil then - return string.format('%s/%s', venv, getVenvSuffix()) - end - local conda = os.getenv('CONDA_PREFIX') - if conda ~= nil then - return string.format('%s/%s', conda, 'python.exe') - end - - local cwd = vim.fn.getcwd() - - local jsonVenv = getVenvFromJson(cwd .. '/pyrightconfig.json') - if jsonVenv ~= nil then - return jsonVenv .. '/' .. getVenvSuffix() - end - - if vim.fn.executable(cwd .. '/venv/' .. getVenvSuffix()) == 1 then - return cwd .. '/venv/' .. getVenvSuffix() - elseif vim.fn.executable(cwd .. '/.venv/' .. getVenvSuffix()) == 1 then - return cwd .. '/.venv/' .. getVenvSuffix() - else - if vim.loop.os_uname().sysname == 'Linux' then - return '/usr/bin/python' - elseif vim.loop.os_uname().sysname == 'Windows_NT' then - return os.getenv('SCOOP') .. '/apps/python/current/python.exe' - end - end - end + local pythonVenv = require('utils.python_venv') function loadConfigs() require('dap.ext.vscode').load_launchjs() -- Make sure we use the correct python env even for the configs from launch.json for _, config in pairs(dap.configurations.python) do - config.pythonPath = getPythonEnv() + config.pythonPath = pythonVenv.getPythonEnv() config.cwd = vim.fn.getcwd() end require 'telescope'.extensions.dap.configurations {} @@ -161,12 +106,12 @@ return { -- PYTHON dap.adapters.python = { - type = 'executable'; - command = masonpath .. '/packages/debugpy/venv/' .. getVenvSuffix(); - args = { '-m', 'debugpy.adapter' }; + type = 'executable', + command = masonpath .. '/packages/debugpy/venv/' .. pythonVenv.getVenvSuffix(), + args = { '-m', 'debugpy.adapter' }, options = { - detached = true; - }; + detached = true, + }, } local dapui = require('dapui') diff --git a/lua/utils/python_venv.lua b/lua/utils/python_venv.lua new file mode 100644 index 0000000..c8cdca3 --- /dev/null +++ b/lua/utils/python_venv.lua @@ -0,0 +1,59 @@ +local M = {} +function M.getVenvSuffix() + if vim.loop.os_uname().sysname == 'Linux' then + return 'bin/python' + elseif vim.loop.os_uname().sysname == 'Windows_NT' then + return 'Scripts/python.exe' + end +end + +function M.getVenvFromJson(jsonfile) + if not vim.fn.filereadable(jsonfile) then + return nil + end + local f = io.open(jsonfile, 'r') + if not f then + return nil + end + local data = f:read('*a') + f:close() + if data then + local jdata = vim.json.decode(data) + if jdata['venvPath'] ~= nil and jdata['venv'] ~= nil then + return jdata['venvPath'] .. '/' .. jdata['venv'] + end + end + return nil +end + +function M.getPythonEnv() + local venv = os.getenv('VIRTUAL_ENV') + if venv ~= nil then + return string.format('%s/%s', venv, M.getVenvSuffix()) + end + local conda = os.getenv('CONDA_PREFIX') + if conda ~= nil then + return string.format('%s/%s', conda, 'python.exe') + end + + local cwd = vim.fn.getcwd() + + local jsonVenv = M.getVenvFromJson(cwd .. '/pyrightconfig.json') + if jsonVenv ~= nil then + return jsonVenv .. '/' .. M.getVenvSuffix() + end + + if vim.fn.executable(cwd .. '/venv/' .. M.getVenvSuffix()) == 1 then + return cwd .. '/venv/' .. M.getVenvSuffix() + elseif vim.fn.executable(cwd .. '/.venv/' .. M.getVenvSuffix()) == 1 then + return cwd .. '/.venv/' .. M.getVenvSuffix() + else + if vim.loop.os_uname().sysname == 'Linux' then + return '/usr/bin/python' + elseif vim.loop.os_uname().sysname == 'Windows_NT' then + return os.getenv('SCOOP') .. '/apps/python/current/python.exe' + end + end +end + +return M