Files
vern-nvim/lua/vern/actions/python.lua
T
2026-10-22 00:19:19 -04:00

108 lines
3.8 KiB
Lua

local M = {}
-- -- Simple test function that pulls directly from the table and prints
-- function M.test_print()
-- local configs = require("vern.configs")
-- local pconfigs = require("vern.configs.python")
--
-- local cfg = vim.tbl_deep_extend("force", {}, configs, pconfigs)
-- -- Pulling straight from the python_config table
-- local raw_path = cfg.python.templates.project.bin.path
-- local dirs = cfg.python.dirs
-- local context = { project_name = "my_cool_app" }
--
-- local final_path = M.resolve_path(raw_path, dirs, context)
--
-- print("Raw path:", raw_path)
-- print("Resolved path:", final_path)
-- end
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)
local bin
for tbl_name, tbl in pairs(py.templates.project) do
local raw_path = join(tbl.path, tbl.name)
local path = utils.resolve_path(raw_path, py.dirs, ctx)
if tbl_name == "bin" then
bin = join(root, path)
end
local abs_file_path = join(root, path)
local abs_dir_path = vim.fs.dirname(abs_file_path)
vim.fn.mkdir(abs_dir_path, "p")
utils.dump(abs_file_path, templates.fetch(tbl.kind_path, tbl.kind, pname))
end
utils.init_git(root, "python")
local venv_path = root .. "/venv"
vim.fn.system("python3 -m venv " .. venv_path)
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 py = get(opts, "python")
local cfg = get(py, "templates", "file", "mod") or {}
local raw_path = join(cfg.path, cfg.name)
local context = { project_name = utils.proj_name(), name = fname }
local path = utils.resolve_path(raw_path, py.dirs or {}, 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/{project_name}/main.py"
local full_path = join(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