diff --git a/lua/config/nvim-dap.lua b/lua/config/nvim-dap.lua index 30ef6e4..f958454 100644 --- a/lua/config/nvim-dap.lua +++ b/lua/config/nvim-dap.lua @@ -28,29 +28,23 @@ vim.fn.execute("nnoremap dr :lua require'dap'.repl.open()") local utils = require("utils") -M = {} +local M = {} local env_ok = false local dap = nil local function check_env() - local debugpy = utils.exec("python -m debugpy --version") - assert(debugpy.rc == 0, "Python module debugpy is required") + utils.assert_available("python3") + utils.assert_python3_module("debugpy") env_ok = true end -local function load_dap() - local ok, p = pcall(require, "dap") - assert(ok, "nvim-dap is required") - return p -end - local function start(config) if not env_ok then check_env() end if not dap then - dap = load_dap() + dap = require("dap") dap.adapters.python = { type = "executable", command = "python", diff --git a/lua/pytest.lua b/lua/pytest.lua index 41157eb..7cb4746 100644 --- a/lua/pytest.lua +++ b/lua/pytest.lua @@ -16,35 +16,29 @@ local utils = require("utils") -M = {} +local M = {} local env_ok = false local dap = nil local function check_env() - local debugpy = utils.exec("python -m debugpy --version") - assert(debugpy.rc == 0, "Python module debugpy is required") - local pytest = utils.exec("python -m pytest --version") - assert(pytest.rc == 0, "Python module pytest is required") + utils.exec("Asdf") + utils.assert_available("python3") + utils.assert_python3_module("debugpy") + utils.assert_python3_module("pytest") env_ok = true end -local function load_dap() - local ok, dap = pcall(require, "dap") - assert(ok, "nvim-dap is required") - return dap -end - function M.run(args) assert(type(args) == "table", "Args not specified or of wrong type") if not env_ok then check_env() end if not dap then - dap = load_dap() + dap = require("dap") dap.adapters.python = { type = "executable", - command = "python", + command = "python3", args = { "-m", "debugpy.adapter", }, cwd = vim.fn.getcwd(), } diff --git a/lua/utils.lua b/lua/utils.lua index d492dfe..835f681 100644 --- a/lua/utils.lua +++ b/lua/utils.lua @@ -14,37 +14,10 @@ limitations under the License. ]] -M = {} +local M = {} M.os_name = vim.loop.os_uname().sysname -function M.has_value(tab, val) - for _, value in ipairs(tab) do if value == val then return true end end - - return false -end - ----@param cmd table: Array of executable + args -function M.exec(cmd) - local out = vim.fn.system(cmd) - local rc = vim.v.shell_error - - return { out = out, rc = rc, } -end - -function M.get_color(group, attr) - return vim.fn.synIDattr(vim.fn.synIDtrans(vim.fn.hlID(group)), attr) -end - -function M.get_hl(name) - local ok, hl = pcall(vim.api.nvim_get_hl_by_name, name, true) - if not ok then return end - for _, key in pairs({ "foreground", "background", "special", }) do - if hl[key] then hl[key] = string.format("#%06x", hl[key]) end - end - return hl -end - --- Check that an executable is available --- @param exe string: Array to look for --- @return boolean @@ -83,4 +56,13 @@ function M.assert_any_available(exes) end end +--- Asserts that a python module is installed +---@param mod string: The python module to check +function M.assert_python3_module(mod) + local resp = vim.system({ "python3", "-m", "pip", "show", mod, }):wait() + if not resp.code == 0 then + error("Python3 module " .. mod .. " not installed:\n" .. resp.stdout .. "\n" .. resp.stderr) + end +end + return M