diff --git a/lua/vern/actions/init.lua b/lua/vern/actions/init.lua index 1fb5c2e..8bc0739 100644 --- a/lua/vern/actions/init.lua +++ b/lua/vern/actions/init.lua @@ -1,16 +1,54 @@ local M = {} -M.create_project = function(opts, args) +M.create_project = function(opts) opts = opts or {} - local test = nil - if opts and opts.terminal then - test = opts.terminal.mode - end - if args and args.args and args ~= "" then - print(string.format("Opts: %s, Args: %s", test, args.args)) - return - end + vim.ui.input({ + prompt = "Project language (e.g. python): ", + default = "", + completion = "filetype", + }, function(ft) + if ft ~= nil and vim.trim(ft) ~= "" then + local ok, actions = pcall(require, "vern.actions." .. ft) + local configs = require("vern.configs." .. ft) + local args = vim.tbl_deep_extend("force", {}, configs, opts or {}) + if + ok + and actions + and type(actions.create_project) == "function" + then + actions.create_project(args) + else + vim.notify("Unknown Filetype: " .. ft, vim.log.levels.WARN, { + title = "Vern Project", + }) + end + end + end) +end + +M.create_file = function(opts) + opts = opts or {} + vim.ui.input({ + prompt = "File language (e.g. python): ", + default = "", + completion = "filetype", + }, function(ft) + if ft ~= nil and vim.trim(ft) ~= "" then + local ok, actions = pcall(require, "vern.actions." .. ft) + local configs = require("vern.configs." .. ft) + local args = vim.tbl_deep_extend("force", {}, configs, opts or {}) + if ok and actions and type(actions.create_file) == "function" then + actions.create_file(args) + else + vim.notify("Unknown Filetype: " .. ft, vim.log.levels.WARN, { + title = "Vern File", + }) + vim.notify("Error: " .. actions, vim.log.levels.ERROR, { + title = "Vern File", + }) + end + end + end) end -M.create_file = function(opts, args) end return M diff --git a/lua/vern/actions/python.lua b/lua/vern/actions/python.lua index 9ec09a4..a25282f 100644 --- a/lua/vern/actions/python.lua +++ b/lua/vern/actions/python.lua @@ -1,9 +1,145 @@ local M = {} local utils = require("vern.utils") +local templates = require("vern.templates.python") -function M.create_project(opts) end -function M.create_file(opts) end +function M.create_project(opts) + opts = opts or {} + vim.ui.input({ + prompt = "Enter project name: ", + default = "", + }, function(pname) + if not pname or vim.trim(pname) == "" then + return + end + + pname = vim.trim(pname) + + local configs = require("vern.configs") + local pconfigs = require("vern.configs.python") + + local cfg = + vim.tbl_deep_extend("force", {}, configs, pconfigs, opts or {}) + local project_root = vim.fs.joinpath( + vim.fn.expand(cfg.workspace_root), + cfg.python.root, + pname + ) + local context = { + project_name = pname, + } + + local bin = vim.tbl_get( + cfg, + "python", + "templates", + "project", + "bin", + "target" + ) or "" + -- bin = bin["bin"].target or "src/{project_name}/main.py" + + local bin_path = utils.interpolate(bin, context) + vim.notify(bin_path, vim.log.levels.INFO) + vim.fn.mkdir( + vim.fs.dirname(vim.fs.joinpath(project_root, bin_path)), + "p" + ) + utils.write_file( + vim.fs.joinpath(project_root, bin_path), + templates.main(pname) + ) + vim.notify( + string.format("Created project %s at %s", pname, project_root), + vim.log.levels.INFO + ) + -- vim.cmd("edit " .. utils.get_project_root() .. "/" .. rel_path) + -- vim.fn.mkdir(full_project_root .. "/include", "p") + -- utils.write_file( + -- { full_project_root, "main.py" }, + -- [[ + -- def main(): + -- print("Hello, World!") + -- + -- if __name__ == "__main__": + -- main() + -- ]] + -- ) + -- utils.write_file({ full_project_root, "include/__init__.py" }, "") + -- utils.write_file({ full_project_root, "requirements.txt" }, "") + -- utils.write_file( + -- { full_project_root, "setup.py" }, + -- string.format( + -- [[ + -- from setuptools import setup, find_packages + -- + -- setup( + -- name="%s", + -- version="0.1.0", + -- packages=find_packages(), + -- install_requires=[], + -- python_requires=">=3.6", + -- ) + -- ]], + -- project_name + -- ) + -- ) + -- local gitignore = { + -- "venv/", + -- } + -- utils.init_git(full_project_root, gitignore) + -- vim.cmd("edit " .. full_project_root .. "/main.py") + -- local venv_path = full_project_root .. "/venv" + -- vim.fn.system("python3 -m venv " .. venv_path) + -- vim.g.python3_host_prog = venv_path .. "/bin/python" + -- vim.fn.setenv("VIRTUAL_ENV", venv_path) + -- vim.fn.setenv("PATH", venv_path .. "/bin:" .. vim.fn.getenv("PATH")) + -- print("Virtual environment activated for " .. project_name) + -- end + -- + -- local context = { + -- project_name = utils.get_project_name(), + -- name = pname, + -- } + -- -- local exe = opts.python.templates.project["executable"].target + -- local init = opts.python.templates.project["init"].target + -- local manifest = opts.python.templates.project["manifest"].target + -- + -- local exe = ( + -- opts.python.templates.project["executable"].target:gsub( + -- "{(%w+)}", + -- context + -- ) + -- ) + -- + -- -- local loc = opts.python.templates.file["module"].target + -- -- local rel_path = (loc:gsub("{(%w+)}", context)) + -- local path = vim.fs.joinpath(utils.get_project_root(), exe) + -- utils.write_file(path, templates.main(pname)) + + -- vim.cmd("edit " .. utils.get_project_root() .. "/" .. exe) + end) +end + +function M.create_file(opts) + opts = opts or {} + vim.ui.input({ + prompt = "Enter file name: ", + default = "", + }, function(fname) + if fname ~= nil and vim.trim(fname) ~= "" then + local loc = opts.python.templates.file["module"].target + local context = { + project_name = utils.get_project_name(), + name = fname, + } + local rel_path = (loc:gsub("{(%w+)}", context)) + local path = vim.fs.joinpath(utils.get_project_root(), rel_path) + utils.write_file(path, templates.module(fname)) + vim.cmd("edit " .. utils.get_project_root() .. "/" .. rel_path) + end + 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 @@ -14,7 +150,8 @@ function M.run(opts) local exe = opts.python.templates.project.executable.target or "src/%s/main.py" - local rel_path = exe:find("%%s") and string.format(exe, project_name) or exe + -- local rel_path = exe:find("%%s") and string.format(exe, project_name) or exe + local rel_path = string.gsub(exe, "%%s", project_name) local full_path = root .. "/" .. rel_path utils.terminal({ "python3 " .. full_path }, opts.terminal or {}) end diff --git a/lua/vern/configs/python.lua b/lua/vern/configs/python.lua index 56de2b1..d2d031b 100644 --- a/lua/vern/configs/python.lua +++ b/lua/vern/configs/python.lua @@ -1,37 +1,31 @@ -local actions = require("vern.actions.python") - return { python = { root = "Python", --- later will concat the project_root to the front keybinds = { ["pr"] = { desc = "Executes python code", - action = actions.run, + action = require("vern.actions.python").run, }, ["pb"] = { desc = "Asks for what python package to install", - action = actions.pip_install, + action = require("vern.actions.python").pip_install, }, }, templates = { project = { - ["executable"] = { - template = { "vern.templates.python", "main" }, - target = "src/%s/main.py", + ["bin"] = { + target = "src/{project_name}/main.py", }, ["init"] = { - template = { "vern.templates.python", "__init__" }, - target = "src/%s/__init__.py", + target = "src/{project_name}/__init__.py", }, ["manifest"] = { - template = { "vern.templates.python", "pyproject" }, target = "pyproject.toml", }, }, file = { ["module"] = { - template = { "vern.templates.python", "module" }, - target = "src/%s.py", + target = "src/{project_name}/{name}.py", }, }, extras = { diff --git a/lua/vern/init.lua b/lua/vern/init.lua index 2a7b104..6f02fa4 100644 --- a/lua/vern/init.lua +++ b/lua/vern/init.lua @@ -1,5 +1,11 @@ local M = {} +function M.test() + vim.ui.input({ prompt = "Test: ", default = "" }, function(input) + print(input) + end) +end + local configs = require("vern.configs") local actions = require("vern.actions") M.opts = {} @@ -20,15 +26,15 @@ function M.setup(user_opts) end --- Load project cmdline commands - vim.api.nvim_create_user_command("VernCreateProject", function(args) + vim.api.nvim_create_user_command("VernCreateProject", function() if actions.create_project then - actions.create_project(M.opts, args) + actions.create_project(M.opts) end end, { nargs = "*", desc = "Create a new project" }) - vim.api.nvim_create_user_command("VernCreateFile", function(args) + vim.api.nvim_create_user_command("VernCreateFile", function() if type(actions.create_file) == "function" then - actions.create_file(M.opts, args) + actions.create_file(M.opts) end end, { nargs = "*", desc = "Creates a new file for project" }) diff --git a/lua/vern/templates/python.lua b/lua/vern/templates/python.lua index 8d8dd67..4a37fc8 100644 --- a/lua/vern/templates/python.lua +++ b/lua/vern/templates/python.lua @@ -3,12 +3,13 @@ local M = {} M.main = function(opts) local template = [=[ def main() -> None: - printf("Hello from %s!") + print("Hello from %s!") + if __name__ == "__main__": main() ]=] - return string.format(template, opts.project_name) + return string.format(template, opts) end M.__init__ = function(opts) @@ -47,7 +48,7 @@ M.module = function(opts) %s module """ ]=] - return string.format(template, opts.project_name) + return string.format(template, opts) end return M diff --git a/lua/vern/utils.lua b/lua/vern/utils.lua index c429371..467733a 100644 --- a/lua/vern/utils.lua +++ b/lua/vern/utils.lua @@ -1,7 +1,18 @@ local M = {} +---@param path string +---@param content string +function M.write_file(path, content) + local file = assert(io.open(path, "w"), "Error creating file: " .. path) + file:write(content) + file:close() +end + +function M.interpolate(str, ctx) + return (str:gsub("{([^}]+)}", ctx)) +end + ---@param bufnr? integer Default is 0 (current buffer) ----@return string|nil function M.get_project_root(bufnr) bufnr = bufnr or 0 local root_markers = { @@ -15,6 +26,11 @@ function M.get_project_root(bufnr) return vim.fs.root(bufnr, root_markers) or vim.uv.cwd() end --- +---@return string +function M.get_project_name() + return vim.fn.fnamemodify(M.get_project_root(), ":t") +end +--- ---@param cmd table ---@return integer, integer? function M.terminal(cmd, opts) @@ -106,20 +122,7 @@ return M -- end -- -- --- ---@return string --- function M.get_project_name() --- return vim.fn.fnamemodify(M.get_project_root(), ":t") --- end -- --- ---@param path_segments table --- ---@param content string --- function M.write_file(path_segments, content) --- local path = table.concat(path_segments, "/") --- local file = assert(io.open(path, "w"), "Error creating file: " .. path) --- file:write(content) --- file:close() --- vim.cmd("edit " .. path) --- end -- -- ---@param path string -- ---@param gitignore_lines table