REFACTOR: Annotations for autocompletion, reworked how projects are made
and handled, wrote the c file replacing the copied python code.
This commit is contained in:
+69
-28
@@ -3,7 +3,9 @@ local M = {}
|
||||
local utils = require("vern.utils")
|
||||
local templates = require("vern.templates")
|
||||
|
||||
local join, get = vim.fs.joinpath, vim.tbl_get
|
||||
local join = vim.fs.joinpath
|
||||
|
||||
---@param opts VernCConfig
|
||||
function M.create_project(opts)
|
||||
vim.ui.input({ prompt = "Enter project name: " }, function(pname)
|
||||
pname = pname and vim.trim(pname)
|
||||
@@ -11,46 +13,85 @@ function M.create_project(opts)
|
||||
return
|
||||
end
|
||||
|
||||
local configs = require("vern.configs")
|
||||
local pconfigs = require("vern.configs.python")
|
||||
local configs = require("vern.configs.c")
|
||||
|
||||
local cfg = vim.tbl_deep_extend("force", {}, configs, pconfigs, opts or {})
|
||||
---@type VernCConfig
|
||||
local cfg = vim.tbl_deep_extend("force", {}, configs, configs, 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
|
||||
local root = join(vim.fn.expand(cfg.workspace_root), cfg.c.root, pname)
|
||||
vim.fn.mkdir(root, "p")
|
||||
local bin
|
||||
for tbl_name, tbl in pairs(cfg.c.templates.project) do
|
||||
local raw_path = join(tbl.path, tbl.name)
|
||||
local path = utils.resolve_path(raw_path, cfg.c.dirs, ctx)
|
||||
if tbl_name == "primary" then
|
||||
bin = join(root, path)
|
||||
end
|
||||
local abs_file_path = vim.fs.normalize(join(root, path))
|
||||
print("File Path: " .. abs_file_path)
|
||||
local abs_dir_path = vim.fs.dirname(abs_file_path)
|
||||
if abs_dir_path ~= root and not vim.startswith(root, abs_dir_path) then
|
||||
vim.fn.mkdir(abs_dir_path, "p")
|
||||
end
|
||||
utils.dump(abs_file_path, templates.fetch(tbl.kind_path, tbl.kind, pname))
|
||||
end
|
||||
for _, tbl in pairs(cfg.c.templates.extras) do
|
||||
if tbl.enabled then
|
||||
local raw_path = join(tbl.path, tbl.name)
|
||||
local path = utils.resolve_path(raw_path, cfg.c.dirs, ctx)
|
||||
local abs_file_path = vim.fs.normalize(join(root, path))
|
||||
local abs_dir_path = vim.fs.dirname(abs_file_path)
|
||||
if abs_dir_path ~= root and not vim.startswith(root, abs_dir_path) then
|
||||
vim.fn.mkdir(abs_dir_path, "p")
|
||||
end
|
||||
utils.dump(abs_file_path, templates.fetch(tbl.kind_path, tbl.kind, pname))
|
||||
end
|
||||
end
|
||||
|
||||
local bin = dump("bin", py.source_root .. "/main.py", "main")
|
||||
dump("pkg", py.source_root .. "/__init__.py", "__init__")
|
||||
dump("cfg", "pyproject.toml", "pyproject")
|
||||
utils.init_git(root, "python")
|
||||
|
||||
local venv_path = root .. "/venv"
|
||||
vim.fn.system("python3 -m venv " .. venv_path)
|
||||
utils.init_git(root, "c")
|
||||
|
||||
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
|
||||
vim.ui.input({ prompt = "Enter type (c/h): " }, function(ft)
|
||||
ft = ft and vim.trim(ft)
|
||||
if not ft or ft == "" then
|
||||
return
|
||||
end
|
||||
local root = get(opts, "python", "templates", "file", "mod").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))
|
||||
local configs = require("vern.configs.c")
|
||||
---@type VernCConfig
|
||||
local cfg = vim.tbl_deep_extend("force", {}, configs, opts or {})
|
||||
vim.ui.input({ prompt = "Enter file name: " }, function(fname)
|
||||
local root = join(vim.fn.expand(cfg.workspace_root), cfg.c.root, utils.proj_name())
|
||||
local input_txt = fname
|
||||
ft = ft and vim.trim(ft)
|
||||
if not ft or ft == "" then
|
||||
return
|
||||
end
|
||||
local mod
|
||||
if ft == "c" then
|
||||
mod = cfg.c.templates.file.source or {}
|
||||
end
|
||||
if ft == "h" then
|
||||
mod = cfg.c.templates.file.header or {}
|
||||
input_txt = fname:upper()
|
||||
end
|
||||
local raw_path = join(mod.path, mod.name)
|
||||
local context = { project_name = utils.proj_name(), name = fname }
|
||||
local path = utils.resolve_path(raw_path, cfg.c.dirs or {}, context)
|
||||
local abs_file_path = vim.fs.normalize(join(root, path))
|
||||
if ft == "h" then
|
||||
local abs_dir_path = vim.fs.dirname(abs_file_path)
|
||||
if abs_dir_path ~= root and not vim.startswith(root, abs_dir_path) then
|
||||
vim.fn.mkdir(abs_dir_path, "p")
|
||||
end
|
||||
end
|
||||
utils.dump(abs_file_path, templates.fetch(mod.kind_path, mod.kind, input_txt))
|
||||
vim.cmd.edit(vim.fn.expand(abs_file_path))
|
||||
end)
|
||||
end)
|
||||
end
|
||||
|
||||
|
||||
+56
-47
@@ -1,26 +1,11 @@
|
||||
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
|
||||
local join = vim.fs.joinpath
|
||||
|
||||
---@param opts VernPythonConfig
|
||||
function M.create_project(opts)
|
||||
vim.ui.input({ prompt = "Enter project name: " }, function(pname)
|
||||
pname = pname and vim.trim(pname)
|
||||
@@ -28,79 +13,103 @@ function M.create_project(opts)
|
||||
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 configs = require("vern.configs.python")
|
||||
---@type VernPythonConfig
|
||||
local cfg = vim.tbl_deep_extend("force", {}, configs, opts or {})
|
||||
local ctx = { project_name = pname }
|
||||
local py = cfg.python
|
||||
|
||||
local root = join(vim.fn.expand(cfg.workspace_root), py.root, pname)
|
||||
local root = join(vim.fn.expand(cfg.workspace_root), cfg.python.root, pname)
|
||||
vim.fn.mkdir(root, "p")
|
||||
local bin
|
||||
for tbl_name, tbl in pairs(py.templates.project) do
|
||||
for tbl_name, tbl in pairs(cfg.python.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
|
||||
local path = utils.resolve_path(raw_path, cfg.python.dirs, ctx)
|
||||
if tbl_name == "primary" 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")
|
||||
if abs_dir_path ~= root and not vim.startswith(root, abs_dir_path) then
|
||||
vim.fn.mkdir(abs_dir_path, "p")
|
||||
end
|
||||
utils.dump(abs_file_path, templates.fetch(tbl.kind_path, tbl.kind, pname))
|
||||
end
|
||||
for _, tbl in pairs(cfg.python.templates.extras) do
|
||||
if tbl.enabled then
|
||||
local raw_path = join(tbl.path, tbl.name)
|
||||
local path = utils.resolve_path(raw_path, cfg.python.dirs, ctx)
|
||||
local abs_file_path = join(root, path)
|
||||
local abs_dir_path = vim.fs.dirname(abs_file_path)
|
||||
if abs_dir_path ~= root and not vim.startswith(root, abs_dir_path) then
|
||||
vim.fn.mkdir(abs_dir_path, "p")
|
||||
end
|
||||
utils.dump(abs_file_path, templates.fetch(tbl.kind_path, tbl.kind, pname))
|
||||
end
|
||||
end
|
||||
|
||||
utils.init_git(root, "python")
|
||||
|
||||
local venv_path = root .. "/venv"
|
||||
vim.fn.system("python3 -m venv " .. venv_path)
|
||||
|
||||
vim.notify("Project: " .. pname .. " has been created!")
|
||||
vim.cmd.edit(vim.fn.fnameescape(bin))
|
||||
end)
|
||||
end
|
||||
|
||||
---@param opts VernPythonConfig
|
||||
function M.create_file(opts)
|
||||
opts = opts or {}
|
||||
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 configs = require("vern.configs.python")
|
||||
---@type VernPythonConfig
|
||||
local cfg = vim.tbl_deep_extend("force", {}, configs, opts or {})
|
||||
local mod = cfg.python.templates.file.mod or {}
|
||||
|
||||
local raw_path = join(cfg.path, cfg.name)
|
||||
local raw_path = join(mod.path, mod.name)
|
||||
local context = { project_name = utils.proj_name(), name = fname }
|
||||
local path = utils.resolve_path(raw_path, py.dirs or {}, context)
|
||||
local path = utils.resolve_path(raw_path, cfg.python.dirs or {}, context)
|
||||
|
||||
utils.dump(path, templates.fetch("python", "module", fname))
|
||||
utils.dump(path, templates.fetch(mod.kind_path, mod.kind, fname))
|
||||
vim.notify("File: " .. fname .. " has been created!")
|
||||
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
|
||||
---@param opts VernPythonConfig 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 bin = opts.python.templates.project.primary
|
||||
|
||||
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 }))
|
||||
local exe = join(bin.path, bin.name) or "src/{project_name}/main.py"
|
||||
local exe_path = utils.resolve_path(exe, opts.python.dirs, { project_name = pname })
|
||||
local full_path = join(vim.fn.expand(root), exe_path)
|
||||
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
|
||||
function M.pip_install()
|
||||
vim.ui.input({ prompt = "Enter pip module: " }, function(mod)
|
||||
mod = mod and vim.trim(mod)
|
||||
if not mod or mod == "" then
|
||||
vim.notify("Pip Cancelled", vim.log.levels.WARN)
|
||||
return
|
||||
end
|
||||
utils.term({ "pip install " .. vim.trim(pip_module) }, opts.terminal or {})
|
||||
vim.notify("Installing " .. mod, vim.log.levels.INFO)
|
||||
vim.system({ "pip", "install", mod }, { text = true }, function(obj)
|
||||
vim.schedule(function()
|
||||
if obj.code ~= 0 then
|
||||
vim.notify("Pip failed: " .. obj.stderr, vim.log.levels.WARN)
|
||||
else
|
||||
vim.notify("Pip installed " .. mod, vim.log.levels.INFO)
|
||||
end
|
||||
end)
|
||||
end)
|
||||
end)
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user