Files
vern-nvim/lua/vern/actions/python.lua
T

83 lines
2.9 KiB
Lua

local M = {}
local utils = require("vern.utils")
local templates = require("vern.templates")
local join, get = vim.fs.joinpath, vim.tbl_get
function M.create_project(opts)
vim.ui.input({ prompt = "Enter project name: " }, function(pname)
pname = pname and vim.trim(pname)
if not pname or pname == "" then
return
end
local configs = require("vern.configs")
local pconfigs = require("vern.configs.python")
local cfg = vim.tbl_deep_extend("force", {}, configs, pconfigs, opts or {})
local ctx = { project_name = pname }
local py = cfg.python
local root = join(vim.fn.expand(cfg.workspace_root), py.root, pname)
vim.fn.mkdir(join(root, utils.fmt(py.source_root, ctx)), "p")
local tmpl = get(py, "templates", "project") or {}
local function dump(k, alt, id)
local path = join(root, utils.fmt(get(tmpl, k, "target") or alt, ctx))
utils.dump(path, templates.fetch("python", id, pname))
return path
end
local bin = dump("bin", "src/{project_name}/main.py", "main")
dump("pkg", "src/{project_name}/__init__.py", "__init__")
dump("spec", "pyproject.toml", "pyproject")
utils.init_git(root, "python")
vim.cmd.edit(vim.fn.fnameescape(bin))
end)
end
function M.create_file(opts)
vim.ui.input({ prompt = "Enter file name: " }, function(fname)
fname = fname and vim.trim(fname)
if not fname or fname == "" then
return
end
local root = get(opts, "python", "templates", "file", "module").target or {}
local context = { project_name = utils.proj_name(), name = fname }
local path = join(utils.proj_root(), utils.fmt(root, context))
utils.dump(path, templates.fetch("python", "module", fname))
vim.cmd.edit(vim.fn.expand(path))
end)
end
--- Goal is to add providers for uv and venv so its available to more users
---@param opts? table Takes a table for the runtime
function M.run(opts)
opts = opts or {}
local root = utils.proj_root() or "."
local pname = vim.fs.basename(root)
local exe = opts.python.templates.project.bin.target or "src/%s/main.py"
local full_path = vim.fn.expand(root, utils.fmt(exe, { project_name = pname }))
utils.term({ "python3 " .. full_path }, opts.terminal or {})
end
---@param cmd_args? table Takes a table for the runtime
function M.pip_install(opts, cmd_args)
local args = cmd_args and cmd_args.args and vim.trim(cmd_args.args) or ""
if args ~= "" then
utils.term({ "pip install " .. args }, opts.terminal or {})
return
end
vim.ui.input({ prompt = "Enter pip module: " }, function(pip_module)
if not pip_module or vim.trim(pip_module) == "" then
vim.notify("Pip Cancelled", vim.log.levels.WARN)
return
end
utils.term({ "pip install " .. vim.trim(pip_module) }, opts.terminal or {})
end)
end
return M