Cleanup util functions

This commit is contained in:
2023-09-02 21:51:20 +02:00
parent 172f0be68c
commit f8b940401a
3 changed files with 21 additions and 51 deletions
+4 -10
View File
@@ -28,29 +28,23 @@ vim.fn.execute("nnoremap <silent> <leader>dr :lua require'dap'.repl.open()<CR>")
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",
+7 -13
View File
@@ -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(),
}
+10 -28
View File
@@ -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