PYTHON: Project setup fixed and working, just need to add providers
This commit is contained in:
@@ -7,22 +7,20 @@ M.create_project = function(opts)
|
|||||||
default = "",
|
default = "",
|
||||||
completion = "filetype",
|
completion = "filetype",
|
||||||
}, function(ft)
|
}, function(ft)
|
||||||
if ft ~= nil and vim.trim(ft) ~= "" then
|
ft = ft and vim.trim(ft)
|
||||||
|
if not ft or vim.trim(ft) == "" then
|
||||||
|
return
|
||||||
|
end
|
||||||
local ok, actions = pcall(require, "vern.actions." .. ft)
|
local ok, actions = pcall(require, "vern.actions." .. ft)
|
||||||
local configs = require("vern.configs." .. ft)
|
local configs = require("vern.configs." .. ft)
|
||||||
local args = vim.tbl_deep_extend("force", {}, configs, opts or {})
|
local args = vim.tbl_deep_extend("force", {}, configs, opts or {})
|
||||||
if
|
if ok and actions and type(actions.create_project) == "function" then
|
||||||
ok
|
|
||||||
and actions
|
|
||||||
and type(actions.create_project) == "function"
|
|
||||||
then
|
|
||||||
actions.create_project(args)
|
actions.create_project(args)
|
||||||
else
|
else
|
||||||
vim.notify("Unknown Filetype: " .. ft, vim.log.levels.WARN, {
|
vim.notify("Unknown Filetype: " .. ft, vim.log.levels.WARN, {
|
||||||
title = "Vern Project",
|
title = "Vern Project",
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
end
|
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -33,7 +31,10 @@ M.create_file = function(opts)
|
|||||||
default = "",
|
default = "",
|
||||||
completion = "filetype",
|
completion = "filetype",
|
||||||
}, function(ft)
|
}, function(ft)
|
||||||
if ft ~= nil and vim.trim(ft) ~= "" then
|
ft = ft and vim.trim(ft)
|
||||||
|
if not ft or vim.trim(ft) == "" then
|
||||||
|
return
|
||||||
|
end
|
||||||
local ok, actions = pcall(require, "vern.actions." .. ft)
|
local ok, actions = pcall(require, "vern.actions." .. ft)
|
||||||
local configs = require("vern.configs." .. ft)
|
local configs = require("vern.configs." .. ft)
|
||||||
local args = vim.tbl_deep_extend("force", {}, configs, opts or {})
|
local args = vim.tbl_deep_extend("force", {}, configs, opts or {})
|
||||||
@@ -47,7 +48,6 @@ M.create_file = function(opts)
|
|||||||
title = "Vern File",
|
title = "Vern File",
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
end
|
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
+35
-71
@@ -1,84 +1,53 @@
|
|||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
local utils = require("vern.utils")
|
local utils = require("vern.utils")
|
||||||
local render = require("vern.templates")
|
local templates = require("vern.templates")
|
||||||
local templates = require("vern.templates.python")
|
|
||||||
|
|
||||||
|
local join, get = vim.fs.joinpath, vim.tbl_get
|
||||||
function M.create_project(opts)
|
function M.create_project(opts)
|
||||||
opts = opts or {}
|
vim.ui.input({ prompt = "Enter project name: " }, function(pname)
|
||||||
vim.ui.input({
|
pname = pname and vim.trim(pname)
|
||||||
prompt = "Enter project name: ",
|
if not pname or pname == "" then
|
||||||
default = "",
|
|
||||||
}, function(pname)
|
|
||||||
if not pname or vim.trim(pname) == "" then
|
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
pname = vim.trim(pname)
|
|
||||||
|
|
||||||
local configs = require("vern.configs")
|
local configs = require("vern.configs")
|
||||||
local pconfigs = require("vern.configs.python")
|
local pconfigs = require("vern.configs.python")
|
||||||
|
|
||||||
local cfg =
|
local cfg = vim.tbl_deep_extend("force", {}, configs, pconfigs, opts or {})
|
||||||
vim.tbl_deep_extend("force", {}, configs, pconfigs, opts or {})
|
local ctx = { project_name = pname }
|
||||||
local project_root = vim.fs.joinpath(
|
local py = cfg.python
|
||||||
vim.fn.expand(cfg.workspace_root),
|
|
||||||
cfg.python.root,
|
|
||||||
pname
|
|
||||||
)
|
|
||||||
local ctx = {
|
|
||||||
project_name = pname,
|
|
||||||
}
|
|
||||||
|
|
||||||
local dest = vim.tbl_get(cfg, "python", "templates", "project") or {}
|
local root = join(vim.fn.expand(cfg.workspace_root), py.root, pname)
|
||||||
|
vim.fn.mkdir(join(root, utils.fmt(py.source_root, ctx)), "p")
|
||||||
|
|
||||||
vim.fn.mkdir(
|
local tmpl = get(py, "templates", "project") or {}
|
||||||
vim.fs.joinpath(
|
local function dump(k, alt, id)
|
||||||
project_root,
|
local path = join(root, utils.fmt(get(tmpl, k, "target") or alt, ctx))
|
||||||
utils.fmt(cfg.python.source_root, ctx)
|
utils.dump(path, templates.fetch("python", id, pname))
|
||||||
),
|
return path
|
||||||
"p"
|
|
||||||
)
|
|
||||||
local function dump(dst, alt, tmpl)
|
|
||||||
local full_path = vim.fs.joinpath(
|
|
||||||
project_root,
|
|
||||||
utils.fmt(vim.tbl_get(dest, dst, "target") or alt, ctx)
|
|
||||||
)
|
|
||||||
utils.dump(full_path, tmpl)
|
|
||||||
return full_path
|
|
||||||
end
|
end
|
||||||
local bin =
|
|
||||||
dump("bin", "src/{project_name}/main.py", templates.main(pname))
|
local bin = dump("bin", "src/{project_name}/main.py", "main")
|
||||||
dump("pkg", "src/{project_name}/__init__.py", templates.__init__(pname))
|
dump("pkg", "src/{project_name}/__init__.py", "__init__")
|
||||||
dump("spec", "pyproject.toml", templates.pyproject(pname))
|
dump("spec", "pyproject.toml", "pyproject")
|
||||||
|
utils.init_git(root, "python")
|
||||||
|
|
||||||
vim.cmd.edit(vim.fn.fnameescape(bin))
|
vim.cmd.edit(vim.fn.fnameescape(bin))
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.create_file(opts)
|
function M.create_file(opts)
|
||||||
opts = opts or {}
|
vim.ui.input({ prompt = "Enter file name: " }, function(fname)
|
||||||
vim.ui.input({
|
fname = fname and vim.trim(fname)
|
||||||
prompt = "Enter file name: ",
|
if not fname or fname == "" then
|
||||||
default = "",
|
return
|
||||||
}, function(fname)
|
|
||||||
if fname ~= nil and vim.trim(fname) ~= "" then
|
|
||||||
local loc = vim.tbl_get(
|
|
||||||
opts,
|
|
||||||
"python",
|
|
||||||
"templates",
|
|
||||||
"file",
|
|
||||||
"module"
|
|
||||||
).target or {}
|
|
||||||
local context = {
|
|
||||||
project_name = utils.get_project_name(),
|
|
||||||
name = fname,
|
|
||||||
}
|
|
||||||
local rel_path = utils.fmt(loc, context)
|
|
||||||
local path = vim.fs.joinpath(utils.get_project_root(), rel_path)
|
|
||||||
utils.dump(path, templates.)
|
|
||||||
vim.cmd("edit " .. utils.get_project_root() .. "/" .. rel_path)
|
|
||||||
end
|
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)
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -86,21 +55,19 @@ end
|
|||||||
---@param opts? table Takes a table for the runtime
|
---@param opts? table Takes a table for the runtime
|
||||||
function M.run(opts)
|
function M.run(opts)
|
||||||
opts = opts or {}
|
opts = opts or {}
|
||||||
local root = utils.get_project_root() or "."
|
local root = utils.proj_root() or "."
|
||||||
local project_name = vim.fs.basename(root)
|
local pname = vim.fs.basename(root)
|
||||||
|
|
||||||
local exe = opts.python.templates.project.bin.target or "src/%s/main.py"
|
local exe = opts.python.templates.project.bin.target or "src/%s/main.py"
|
||||||
-- local rel_path = exe:find("%%s") and string.format(exe, project_name) or exe
|
local full_path = vim.fn.expand(root, utils.fmt(exe, { project_name = pname }))
|
||||||
local rel_path = utils.fmt(exe, { project_name = project_name })
|
utils.term({ "python3 " .. full_path }, opts.terminal or {})
|
||||||
local full_path = root .. "/" .. rel_path
|
|
||||||
utils.terminal({ "python3 " .. full_path }, opts.terminal or {})
|
|
||||||
end
|
end
|
||||||
|
|
||||||
---@param cmd_args? table Takes a table for the runtime
|
---@param cmd_args? table Takes a table for the runtime
|
||||||
function M.pip_install(opts, cmd_args)
|
function M.pip_install(opts, cmd_args)
|
||||||
local args = cmd_args and cmd_args.args and vim.trim(cmd_args.args) or ""
|
local args = cmd_args and cmd_args.args and vim.trim(cmd_args.args) or ""
|
||||||
if args ~= "" then
|
if args ~= "" then
|
||||||
utils.terminal({ "pip install " .. args }, opts.terminal or {})
|
utils.term({ "pip install " .. args }, opts.terminal or {})
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
vim.ui.input({ prompt = "Enter pip module: " }, function(pip_module)
|
vim.ui.input({ prompt = "Enter pip module: " }, function(pip_module)
|
||||||
@@ -108,10 +75,7 @@ function M.pip_install(opts, cmd_args)
|
|||||||
vim.notify("Pip Cancelled", vim.log.levels.WARN)
|
vim.notify("Pip Cancelled", vim.log.levels.WARN)
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
utils.terminal(
|
utils.term({ "pip install " .. vim.trim(pip_module) }, opts.terminal or {})
|
||||||
{ "pip install " .. vim.trim(pip_module) },
|
|
||||||
opts.terminal or {}
|
|
||||||
)
|
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -27,8 +27,10 @@ return function(bufnr, user_opts)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local root = require("vern.utils").get_project_root()
|
local root = require("vern.utils").proj_root()
|
||||||
if root then
|
if not root then
|
||||||
|
return
|
||||||
|
end
|
||||||
local venv_dir = nil
|
local venv_dir = nil
|
||||||
if vim.uv.fs_stat(root .. "/venv/bin/python") then
|
if vim.uv.fs_stat(root .. "/venv/bin/python") then
|
||||||
venv_dir = root .. "/venv"
|
venv_dir = root .. "/venv"
|
||||||
@@ -39,5 +41,4 @@ return function(bufnr, user_opts)
|
|||||||
vim.env.VIRTUAL_ENV = venv_dir
|
vim.env.VIRTUAL_ENV = venv_dir
|
||||||
vim.env.PATH = venv_dir .. "/bin:" .. vim.env.PATH
|
vim.env.PATH = venv_dir .. "/bin:" .. vim.env.PATH
|
||||||
end
|
end
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
---@param opts table
|
---@param lang string
|
||||||
M.template = function(opts)
|
M.template = function(lang)
|
||||||
local base = [=[
|
local base = [=[
|
||||||
.DS_Store
|
.DS_Store
|
||||||
|
|
||||||
@@ -13,7 +13,7 @@ M.template = function(opts)
|
|||||||
|
|
||||||
]=]
|
]=]
|
||||||
local lang_block = {
|
local lang_block = {
|
||||||
python = [=[
|
python = vim.trim([=[
|
||||||
__pycache__/
|
__pycache__/
|
||||||
build/
|
build/
|
||||||
dist/
|
dist/
|
||||||
@@ -21,10 +21,10 @@ dist/
|
|||||||
env/
|
env/
|
||||||
venv/
|
venv/
|
||||||
.pytest_cache/
|
.pytest_cache/
|
||||||
]=],
|
]=]),
|
||||||
}
|
}
|
||||||
|
|
||||||
local extra = (opts and opts and lang_block[opts.lang]) or ""
|
local extra = (lang and lang_block[lang]) or ""
|
||||||
return base .. extra
|
return base .. extra
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -1,14 +1,19 @@
|
|||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
function M.imp(ft, template, name)
|
---@param ft string Filetype of template
|
||||||
|
---@param template string Template in language table
|
||||||
|
---@param name string A string to replace temp values in template
|
||||||
|
---@return string
|
||||||
|
function M.fetch(ft, template, name)
|
||||||
ft, template, name = ft or "", template or "", name or ""
|
ft, template, name = ft or "", template or "", name or ""
|
||||||
if ft == "" or template == "" or name == "" then
|
if ft == "" or template == "" or name == "" then
|
||||||
return
|
return ""
|
||||||
end
|
end
|
||||||
local ok, mod = pcall(require, "vern.templates." .. ft)
|
local ok, mod = pcall(require, "vern.templates." .. ft)
|
||||||
if ok and type(mod) ~= "table" then
|
if ok and type(mod) == "table" then
|
||||||
return string.gsub(mod[template], "%%s", name)
|
return (string.gsub(mod[template], "%%s", name))
|
||||||
end
|
end
|
||||||
|
return ""
|
||||||
end
|
end
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
|||||||
@@ -1,4 +1,9 @@
|
|||||||
return {
|
return {
|
||||||
|
--[[ =====================================================================
|
||||||
|
TEMPLATE: MAIN (Entrypoint)
|
||||||
|
File: main.py
|
||||||
|
Target: src/{project_name}/main.py
|
||||||
|
===================================================================== --]]
|
||||||
main = vim.trim([=[
|
main = vim.trim([=[
|
||||||
def main() -> None:
|
def main() -> None:
|
||||||
print("Hello from %s!")
|
print("Hello from %s!")
|
||||||
@@ -6,12 +11,24 @@ def main() -> None:
|
|||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
]=]),
|
]=]) .. "\n",
|
||||||
|
|
||||||
|
--[[ =====================================================================
|
||||||
|
TEMPLATE: INIT (Package Anchor)
|
||||||
|
File: __init__.py
|
||||||
|
Target: src/{project_name}/__init__.py
|
||||||
|
===================================================================== --]]
|
||||||
__init__ = vim.trim([=[
|
__init__ = vim.trim([=[
|
||||||
# %s init
|
# %s init
|
||||||
|
|
||||||
__all__: list[str] = []
|
__all__: list[str] = []
|
||||||
]=]),
|
]=]) .. "\n",
|
||||||
|
|
||||||
|
--[[ =====================================================================
|
||||||
|
TEMPLATE: PYPROJECT (Spec / Build Manifest)
|
||||||
|
File: pyproject.toml
|
||||||
|
Target: pyproject.toml
|
||||||
|
===================================================================== --]]
|
||||||
pyproject = vim.trim([=[
|
pyproject = vim.trim([=[
|
||||||
[build-system]
|
[build-system]
|
||||||
requires = ["hatchling"]
|
requires = ["hatchling"]
|
||||||
@@ -27,11 +44,16 @@ dependencies = []
|
|||||||
|
|
||||||
[project.scripts]
|
[project.scripts]
|
||||||
%s = "%s.main:main"
|
%s = "%s.main:main"
|
||||||
]=]),
|
]=]) .. "\n",
|
||||||
|
|
||||||
|
--[[ =====================================================================
|
||||||
|
TEMPLATE: MODULE (Standalone File)
|
||||||
|
File: {name}.py
|
||||||
|
Target: src/{project_name}/{name}.py
|
||||||
|
===================================================================== --]]
|
||||||
module = vim.trim([=[
|
module = vim.trim([=[
|
||||||
"""
|
"""
|
||||||
Module: %s
|
Module: %s
|
||||||
"""
|
"""
|
||||||
|
]=]) .. "\n",
|
||||||
]=]),
|
|
||||||
}
|
}
|
||||||
|
|||||||
+21
-33
@@ -13,7 +13,7 @@ function M.fmt(str, ctx)
|
|||||||
end
|
end
|
||||||
|
|
||||||
---@param bufnr? integer Default is 0 (current buffer)
|
---@param bufnr? integer Default is 0 (current buffer)
|
||||||
function M.get_project_root(bufnr)
|
function M.proj_root(bufnr)
|
||||||
bufnr = bufnr or 0
|
bufnr = bufnr or 0
|
||||||
local root_markers = {
|
local root_markers = {
|
||||||
".git",
|
".git",
|
||||||
@@ -25,15 +25,28 @@ function M.get_project_root(bufnr)
|
|||||||
}
|
}
|
||||||
return vim.fs.root(bufnr, root_markers) or vim.uv.cwd()
|
return vim.fs.root(bufnr, root_markers) or vim.uv.cwd()
|
||||||
end
|
end
|
||||||
---
|
|
||||||
---@return string
|
---@return string
|
||||||
function M.get_project_name()
|
function M.proj_name()
|
||||||
return vim.fn.fnamemodify(M.get_project_root(), ":t")
|
return vim.fn.fnamemodify(M.proj_root(), ":t")
|
||||||
end
|
end
|
||||||
---
|
|
||||||
|
---@param path string
|
||||||
|
---@param ft string
|
||||||
|
function M.init_git(path, ft)
|
||||||
|
ft = ft and vim.trim(ft)
|
||||||
|
if not ft or ft == "" then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
local gitignore = require("vern.templates.extras.gitignore").template(ft)
|
||||||
|
vim.fn.system({ "git", "-C", path, "init" })
|
||||||
|
M.dump(vim.fs.joinpath(path, ".gitignore"), gitignore)
|
||||||
|
end
|
||||||
|
|
||||||
---@param cmd table
|
---@param cmd table
|
||||||
---@return integer, integer?
|
---@return integer, integer?
|
||||||
function M.terminal(cmd, opts)
|
function M.term(cmd, opts)
|
||||||
opts = opts or {}
|
opts = opts or {}
|
||||||
local buf, win
|
local buf, win
|
||||||
if opts.mode.type == "float" then
|
if opts.mode.type == "float" then
|
||||||
@@ -56,12 +69,7 @@ function M.terminal(cmd, opts)
|
|||||||
|
|
||||||
local pos = {
|
local pos = {
|
||||||
row = math.floor(
|
row = math.floor(
|
||||||
(
|
(ui.height - (vim.o.laststatus == 3 and 1 or 0) - vim.o.cmdheight - dim.height) / 2
|
||||||
ui.height
|
|
||||||
- (vim.o.laststatus == 3 and 1 or 0)
|
|
||||||
- vim.o.cmdheight
|
|
||||||
- dim.height
|
|
||||||
) / 2
|
|
||||||
),
|
),
|
||||||
col = math.floor((ui.width - dim.width) / 2),
|
col = math.floor((ui.width - dim.width) / 2),
|
||||||
}
|
}
|
||||||
@@ -74,9 +82,7 @@ function M.terminal(cmd, opts)
|
|||||||
row = pos.row,
|
row = pos.row,
|
||||||
col = pos.col,
|
col = pos.col,
|
||||||
style = "minimal",
|
style = "minimal",
|
||||||
border = opts.mode.win.float.border.enabled
|
border = opts.mode.win.float.border.enabled and opts.mode.win.float.border.type or nil,
|
||||||
and opts.mode.win.float.border.type
|
|
||||||
or nil,
|
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
if opts.mode.type == "split" then
|
if opts.mode.type == "split" then
|
||||||
@@ -111,12 +117,6 @@ end
|
|||||||
|
|
||||||
return M
|
return M
|
||||||
|
|
||||||
-- M.opts = {}
|
|
||||||
--
|
|
||||||
-- function M.setup(opts)
|
|
||||||
-- M.opts = vim.tbl_deep_extend("force", {}, require("Polydev.configs").get("terminal"), opts or {})
|
|
||||||
-- end
|
|
||||||
--
|
|
||||||
-- function M.exists(path)
|
-- function M.exists(path)
|
||||||
-- return (io.open(path, "r") ~= nil)
|
-- return (io.open(path, "r") ~= nil)
|
||||||
-- end
|
-- end
|
||||||
@@ -124,16 +124,4 @@ return M
|
|||||||
--
|
--
|
||||||
--
|
--
|
||||||
--
|
--
|
||||||
-- ---@param path string
|
|
||||||
-- ---@param gitignore_lines table
|
|
||||||
-- function M.init_git(path, gitignore_lines)
|
|
||||||
-- vim.fn.mkdir(path, "p")
|
|
||||||
-- vim.fn.system({ "git", "-C", path, "init" })
|
|
||||||
--
|
|
||||||
-- if gitignore_lines and #gitignore_lines > 0 then
|
|
||||||
-- local contents = table.concat(gitignore_lines, "\n")
|
|
||||||
-- M.write_file({ path, ".gitignore" }, contents)
|
|
||||||
-- end
|
|
||||||
-- end
|
|
||||||
----
|
|
||||||
-- return M
|
-- return M
|
||||||
|
|||||||
+1
-1
@@ -1,7 +1,7 @@
|
|||||||
# stylua.toml
|
# stylua.toml
|
||||||
|
|
||||||
# How many characters per line before wrapping
|
# How many characters per line before wrapping
|
||||||
column_width = 80
|
column_width = 100
|
||||||
|
|
||||||
# Indentation style: "Tabs" or "Spaces"
|
# Indentation style: "Tabs" or "Spaces"
|
||||||
line_endings = "Unix"
|
line_endings = "Unix"
|
||||||
|
|||||||
Reference in New Issue
Block a user