refactor(utils): Split into fs and moved the inits to the project.
This commit is contained in:
1 parent
15f3648af8
commit
adbd2bc0e9
13 files changed
+144
-131
No files matched your search
@@ -1,4 +1,5 @@
|
||||
local actions = require("vern-nvim.actions")
|
||||
---@module "vern-nvim.core.actions"
|
||||
local actions = require("vern-nvim.core.actions")
|
||||
|
||||
---@type VernCoreConfig
|
||||
return {
|
||||
@@ -1,7 +1,10 @@
|
||||
local M = {}
|
||||
|
||||
local utils = require("vern-nvim.utils")
|
||||
local templates = require("vern-nvim.templates")
|
||||
---@module "vern-nvim.core.fs"
|
||||
local fs = require("vern-nvim.core.fs")
|
||||
|
||||
---@module "vern-nvim.core.template"
|
||||
local templates = require("vern-nvim.core.template")
|
||||
|
||||
---Imitate create_file so you can skip over and use for each filetype
|
||||
|
||||
@@ -43,7 +46,7 @@ M.create_project = function(opts)
|
||||
if type(dirs) == "table" and next(dirs) then
|
||||
for _, dir in pairs(dirs) do
|
||||
local path = vim.fs.normalize(
|
||||
vim.fs.joinpath(root, utils.fmt(dir, ctx))
|
||||
vim.fs.joinpath(root, fs.fmt_path(dir, nil, ctx))
|
||||
)
|
||||
vim.fn.mkdir(path, "p")
|
||||
end
|
||||
@@ -54,8 +57,7 @@ M.create_project = function(opts)
|
||||
if type(project) == "table" and next(project) then
|
||||
for tbl_name, tbl in pairs(project) do
|
||||
local raw_path = vim.fs.joinpath(tbl.path, tbl.name)
|
||||
local path =
|
||||
utils.resolve_path(raw_path, args[ft].dirs, ctx)
|
||||
local path = fs.fmt_path(raw_path, args[ft].dirs, ctx)
|
||||
if tbl_name == "primary" then
|
||||
bin = vim.fs.joinpath(root, path)
|
||||
end
|
||||
@@ -69,7 +71,7 @@ M.create_project = function(opts)
|
||||
then
|
||||
vim.fn.mkdir(abs_dir_path, "p")
|
||||
end
|
||||
utils.dump(
|
||||
fs.write(
|
||||
abs_file_path,
|
||||
templates.fetch(tbl.kind_path, tbl.kind, ctx)
|
||||
)
|
||||
@@ -82,7 +84,7 @@ M.create_project = function(opts)
|
||||
if tbl.enabled ~= false and tbl.kind then
|
||||
local raw_path = vim.fs.joinpath(tbl.path, tbl.name)
|
||||
local path =
|
||||
utils.resolve_path(raw_path, args[ft].dirs, ctx)
|
||||
fs.fmt_path(raw_path, args[ft].dirs, ctx)
|
||||
local abs_file_path =
|
||||
vim.fs.normalize(vim.fs.joinpath(root, path))
|
||||
local abs_dir_path = vim.fs.dirname(abs_file_path)
|
||||
@@ -92,7 +94,7 @@ M.create_project = function(opts)
|
||||
then
|
||||
vim.fn.mkdir(abs_dir_path, "p")
|
||||
end
|
||||
utils.dump(
|
||||
fs.write(
|
||||
abs_file_path,
|
||||
templates.fetch(tbl.kind_path, tbl.kind, ctx)
|
||||
)
|
||||
@@ -103,8 +105,24 @@ M.create_project = function(opts)
|
||||
if type(langs.create_project) == "function" then
|
||||
langs.create_project(root)
|
||||
end
|
||||
utils.init_git(root, ft)
|
||||
utils.init_readme(root)
|
||||
|
||||
--- Init git
|
||||
local gitignore =
|
||||
require("vern-nvim.templates.extras.gitignore").template(ft)
|
||||
local obj = vim.system({ "git", "-C", root, "init" }):wait()
|
||||
if obj.code ~= 0 then
|
||||
vim.notify(
|
||||
"Git init failed " .. obj.stderr,
|
||||
vim.log.levels.ERROR,
|
||||
{ title = "Vern Git" }
|
||||
)
|
||||
end
|
||||
fs.write(vim.fs.joinpath(root, ".gitignore"), gitignore)
|
||||
|
||||
--- Init Readme
|
||||
local readme = require("vern-nvim.templates.extras.readme")
|
||||
fs.write(vim.fs.joinpath(root, "README.md"), readme)
|
||||
|
||||
if bin then vim.cmd.edit(vim.fn.fnameescape(bin)) end
|
||||
vim.notify(
|
||||
"Project: " .. pname .. " has been created!",
|
||||
@@ -138,14 +156,14 @@ M.create_file = function(opts)
|
||||
local function function_write(mod, fname)
|
||||
local raw_path = vim.fs.joinpath(mod.path, mod.name)
|
||||
local context = {
|
||||
project_name = utils.proj_name(),
|
||||
project_name = fs.name(),
|
||||
company_name = args.env.author,
|
||||
file_name = fname,
|
||||
}
|
||||
local path =
|
||||
utils.resolve_path(raw_path, args[filetype].dirs or {}, context)
|
||||
local full_path = vim.fs.joinpath(utils.proj_root(), path)
|
||||
utils.dump(
|
||||
fs.fmt_path(raw_path, args[filetype].dirs or {}, context)
|
||||
local full_path = vim.fs.joinpath(fs.root(), path)
|
||||
fs.write(
|
||||
full_path,
|
||||
templates.fetch(mod.kind_path, mod.kind, context)
|
||||
)
|
||||
@@ -1,7 +1,11 @@
|
||||
local M = {}
|
||||
|
||||
local utils = require("vern-nvim.utils")
|
||||
---@module "vern-nvim.core.terminal"
|
||||
local term = require("vern-nvim.core.terminal")
|
||||
|
||||
---@param opts table
|
||||
---@param build_cmd string
|
||||
---@param run_cmd string
|
||||
M.compile = function(opts, build_cmd, run_cmd)
|
||||
vim.system({ "sh", "-c", build_cmd }, { text = true }, function(obj)
|
||||
if obj.code ~= 0 then
|
||||
@@ -13,16 +17,16 @@ M.compile = function(opts, build_cmd, run_cmd)
|
||||
{ "Error During compilation" },
|
||||
vim.split(error_obj, "\n", { trimempty = true })
|
||||
)
|
||||
local buf, _ = utils.term(
|
||||
local buf, _ = term.open(
|
||||
{ "echo 'compilation failed'" },
|
||||
opts.terminal,
|
||||
true
|
||||
)
|
||||
utils.term_override(buf, error_lines)
|
||||
term.set_lines(buf, error_lines)
|
||||
end)
|
||||
else
|
||||
vim.schedule(
|
||||
function() utils.term({ run_cmd }, opts.terminal, false) end
|
||||
function() term.open({ run_cmd }, opts.terminal, false) end
|
||||
)
|
||||
end
|
||||
end)
|
||||
@@ -0,0 +1,49 @@
|
||||
local M = {}
|
||||
|
||||
---@param raw_path string Combination of tbl.path and tbl.name to override
|
||||
---@param dir_tbl? table<string, string> Strings to replace raw directories in {dirs.<>} with
|
||||
---@param ctx table<string, string> Strings to replace raw names like {project_name} with
|
||||
---@return string path Fully replaced path
|
||||
function M.fmt_path(raw_path, dir_tbl, ctx)
|
||||
--- Replace the {dirs.<key>}
|
||||
raw_path = raw_path:gsub(
|
||||
"{dirs%.([%w_]+)}",
|
||||
function(dir_key) return dir_tbl and dir_tbl[dir_key] or dir_key end
|
||||
)
|
||||
|
||||
--- Replace the {<key>}
|
||||
local path = raw_path:gsub(
|
||||
"{([%w_]+)}",
|
||||
function(key) return ctx and ctx[key] or ("{" .. key .. "}") end
|
||||
)
|
||||
|
||||
return path
|
||||
end
|
||||
|
||||
---@param path string
|
||||
---@param content string
|
||||
function M.write(path, content)
|
||||
local file = assert(io.open(path, "w"), "Error creating file: " .. path)
|
||||
file:write(content)
|
||||
file:close()
|
||||
end
|
||||
|
||||
---@return string
|
||||
function M.name() return vim.fs.basename(M.proj_root()) or "" end
|
||||
|
||||
---@param bufnr? integer Default is 0 (current buffer)
|
||||
---@return string
|
||||
function M.root(bufnr)
|
||||
bufnr = bufnr or 0
|
||||
local root_markers = {
|
||||
".git",
|
||||
"pyproject.toml",
|
||||
"setup.py",
|
||||
"Makefile",
|
||||
"CMakeLists",
|
||||
"Cargo.toml",
|
||||
}
|
||||
return vim.fs.root(bufnr, root_markers) or vim.uv.cwd() or "."
|
||||
end
|
||||
|
||||
return M
|
||||
File renamed without changes.
@@ -1,86 +1,10 @@
|
||||
local M = {}
|
||||
|
||||
---@param path string
|
||||
---@param content string
|
||||
function M.dump(path, content)
|
||||
local file = assert(io.open(path, "w"), "Error creating file: " .. path)
|
||||
file:write(content)
|
||||
file:close()
|
||||
end
|
||||
|
||||
---@param str string String to replace
|
||||
---@param ctx table<string, string> Replacment characters
|
||||
---@return string
|
||||
function M.fmt(str, ctx) return (str:gsub("{([^}]+)}", ctx)) end
|
||||
|
||||
---@param bufnr? integer Default is 0 (current buffer)
|
||||
---@return string
|
||||
function M.proj_root(bufnr)
|
||||
bufnr = bufnr or 0
|
||||
local root_markers = {
|
||||
".git",
|
||||
"pyproject.toml",
|
||||
"setup.py",
|
||||
"Makefile",
|
||||
"CMakeLists",
|
||||
"Cargo.toml",
|
||||
}
|
||||
return vim.fs.root(bufnr, root_markers) or vim.uv.cwd() or "."
|
||||
end
|
||||
|
||||
---@return string
|
||||
function M.proj_name() return vim.fs.basename(M.proj_root()) or "" end
|
||||
|
||||
---@param raw_path string Combination of tbl.path and tbl.name to override
|
||||
---@param dir_tbl table<string, string> Strings to replace raw directories in {dirs.<>} with
|
||||
---@param ctx table<string, string> Strings to replace raw names like {project_name} with
|
||||
---@return string path Fully replaced path
|
||||
function M.resolve_path(raw_path, dir_tbl, ctx)
|
||||
--- Replace the {dirs.<key>}
|
||||
raw_path = raw_path:gsub(
|
||||
"{dirs%.([%w_]+)}",
|
||||
function(dir_key) return dir_tbl and dir_tbl[dir_key] or dir_key end
|
||||
)
|
||||
|
||||
--- Replace the {<key>}
|
||||
local path = raw_path:gsub(
|
||||
"{([%w_]+)}",
|
||||
function(key) return ctx and ctx[key] or ("{" .. key .. "}") end
|
||||
)
|
||||
|
||||
return path
|
||||
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-nvim.templates.extras.gitignore").template(ft)
|
||||
local obj = vim.system({ "git", "-C", path, "init" }):wait()
|
||||
if obj.code ~= 0 then
|
||||
vim.notify(
|
||||
"Git init failed " .. obj.stderr,
|
||||
vim.log.levels.ERROR,
|
||||
{ title = "Vern Git" }
|
||||
)
|
||||
end
|
||||
M.dump(vim.fs.joinpath(path, ".gitignore"), gitignore)
|
||||
end
|
||||
|
||||
---@param path string
|
||||
function M.init_readme(path)
|
||||
local readme = require("vern-nvim.templates.extras.readme")
|
||||
M.dump(vim.fs.joinpath(path, "README.md"), readme)
|
||||
end
|
||||
|
||||
---@param cmd table
|
||||
---@param opts VernTerminalConfig
|
||||
---@param scratch boolean
|
||||
---@return integer, integer?
|
||||
function M.term(cmd, opts, scratch)
|
||||
function M.open(cmd, opts, scratch)
|
||||
opts = opts or {}
|
||||
local buf, win
|
||||
if opts.mode.type == "float" then
|
||||
@@ -161,7 +85,7 @@ end
|
||||
|
||||
---@param buf integer
|
||||
---@param lines table
|
||||
function M.term_override(buf, lines)
|
||||
function M.set_lines(buf, lines)
|
||||
if buf and vim.api.nvim_buf_is_valid(buf) then
|
||||
vim.api.nvim_set_option_value("modifiable", true, { buf = buf })
|
||||
vim.api.nvim_buf_set_lines(buf, 0, -1, false, lines)
|
||||
@@ -1,9 +1,10 @@
|
||||
local M = {}
|
||||
|
||||
local settings = require("vern-nvim.settings")
|
||||
---@module "vern-nvim.config.defaults"
|
||||
local defaults = require("vern-nvim.config.defaults")
|
||||
|
||||
---@type VernCoreConfig
|
||||
M.opts = vim.deepcopy(settings)
|
||||
M.opts = vim.deepcopy(defaults)
|
||||
|
||||
---@class VernUserConfig
|
||||
---@field workspace_root? string
|
||||
@@ -13,7 +14,7 @@ M.opts = vim.deepcopy(settings)
|
||||
---@param user_opts? VernUserConfig
|
||||
function M.setup(user_opts)
|
||||
---@type VernCoreConfig
|
||||
M.opts = vim.tbl_deep_extend("force", {}, settings, user_opts or {})
|
||||
M.opts = vim.tbl_deep_extend("force", {}, defaults, user_opts or {})
|
||||
|
||||
for key, def in pairs(M.opts.keybinds or {}) do
|
||||
if type(def.action) == "function" then
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
local utils = require("vern-nvim.utils")
|
||||
local comp = require("vern-nvim.langs.shared.compile")
|
||||
---@module "vern-nvim.core.fs"
|
||||
local fs = require("vern-nvim.fs")
|
||||
|
||||
---@module "vern-nvim.core.compile"
|
||||
local comp = require("vern-nvim.core.compile")
|
||||
|
||||
local M = {}
|
||||
|
||||
@@ -33,9 +36,9 @@ end
|
||||
M.run = function(opts)
|
||||
opts = opts or {}
|
||||
|
||||
local build_dir = vim.fn.fnameescape((utils.proj_root() or ".") .. "/build")
|
||||
local build_dir = vim.fn.fnameescape((fs.root() or ".") .. "/build")
|
||||
local build_cmd = string.format("cd %s && cmake --build .", build_dir)
|
||||
local run_cmd = string.format("cd %s && ./%s", build_dir, utils.proj_name())
|
||||
local run_cmd = string.format("cd %s && ./%s", build_dir, fs.name())
|
||||
|
||||
comp.compile(opts, build_cmd, run_cmd)
|
||||
end
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
local utils = require("vern-nvim.utils")
|
||||
local comp = require("vern-nvim.langs.shared.compile")
|
||||
---@module "vern-nvim.core.fs"
|
||||
local fs = require("vern-nvim.core.fs")
|
||||
|
||||
---@module "vern-nvim.core.compile"
|
||||
local comp = require("vern-nvim.core.compile")
|
||||
|
||||
local M = {}
|
||||
|
||||
@@ -28,14 +31,14 @@ M.create_file = function(opts, continue)
|
||||
continue(nil)
|
||||
end)
|
||||
end
|
||||
---
|
||||
|
||||
---@param opts? VernCPPConfig Takes a table for the runtime
|
||||
M.run = function(opts)
|
||||
opts = opts or {}
|
||||
|
||||
local build_dir = vim.fn.fnameescape((utils.proj_root() or ".") .. "/build")
|
||||
local build_dir = vim.fn.fnameescape((fs.root() or ".") .. "/build")
|
||||
local build_cmd = string.format("cd %s && cmake --build .", build_dir)
|
||||
local run_cmd = string.format("cd %s && ./%s", build_dir, utils.proj_name())
|
||||
local run_cmd = string.format("cd %s && ./%s", build_dir, fs.name())
|
||||
|
||||
comp.compile(opts, build_cmd, run_cmd)
|
||||
end
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
local utils = require("vern-nvim.utils")
|
||||
local comp = require("vern-nvim.langs.shared.compile")
|
||||
---@module "vern-nvim.core.fs"
|
||||
local fs = require("vern-nvim.core.fs")
|
||||
|
||||
---@module "vern-nvim.core.compile"
|
||||
local comp = require("vern-nvim.core.compile")
|
||||
|
||||
local M = {}
|
||||
|
||||
@@ -13,16 +16,16 @@ M.create_file = nil
|
||||
---@param opts VernJavaConfig Takes a table for the runtime
|
||||
M.run = function(opts)
|
||||
opts = opts or {}
|
||||
local root = utils.proj_root()
|
||||
local root = fs.root()
|
||||
local context = {
|
||||
project_name = utils.proj_name(),
|
||||
project_name = fs.name()(),
|
||||
company_name = opts.env.author,
|
||||
file_name = "",
|
||||
}
|
||||
|
||||
local src_root = vim.fs.joinpath(
|
||||
root,
|
||||
utils.resolve_path(opts.java.dirs.src, opts.java.dirs, context)
|
||||
fs.fmt_path(opts.java.dirs.src, opts.java.dirs, context)
|
||||
)
|
||||
local build_dir = root .. "/bin"
|
||||
local build_cmd = string.format(
|
||||
|
||||
@@ -1,4 +1,8 @@
|
||||
local utils = require("vern-nvim.utils")
|
||||
---@module "vern-nvim.core.fs"
|
||||
local fs = require("vern-nvim.fs")
|
||||
|
||||
---@module "vern-nvim.core.terminal"
|
||||
local term = require("vern-nvim.core.terminal")
|
||||
|
||||
local M = {}
|
||||
|
||||
@@ -13,13 +17,8 @@ M.create_file = nil
|
||||
M.run = function(opts)
|
||||
opts = opts or {}
|
||||
|
||||
utils.term({
|
||||
"lua " .. vim.fs.joinpath(
|
||||
utils.proj_root(),
|
||||
"lua",
|
||||
utils.proj_name(),
|
||||
"/init.lua"
|
||||
),
|
||||
term.open({
|
||||
"lua " .. vim.fs.joinpath(fs.root(), "lua", fs.name(), "/init.lua"),
|
||||
}, opts.terminal or {}, false)
|
||||
end
|
||||
|
||||
|
||||
@@ -1,9 +1,13 @@
|
||||
local utils = require("vern-nvim.utils")
|
||||
---@module "vern-nvim.core.fs"
|
||||
local fs = require("vern-nvim.fs")
|
||||
|
||||
---@module "vern-nvim.core.terminal"
|
||||
local term = require("vern-nvim.core.terminal")
|
||||
|
||||
local M = {}
|
||||
|
||||
M.attach = function()
|
||||
local root = require("vern-nvim.utils").proj_root()
|
||||
local root = fs.root()
|
||||
if not root then return end
|
||||
local venv_dir = nil
|
||||
if vim.uv.fs_stat(root .. "/venv/bin/python") then
|
||||
@@ -35,9 +39,9 @@ M.create_file = nil
|
||||
---@param opts VernPythonConfig Takes a table for the runtime
|
||||
M.run = function(opts)
|
||||
opts = opts or {}
|
||||
local root = utils.proj_root() or "."
|
||||
local root = fs.root() or "."
|
||||
local context = {
|
||||
project_name = utils.proj_name(),
|
||||
project_name = fs.name(),
|
||||
company_name = opts.env.author,
|
||||
file_name = "",
|
||||
}
|
||||
@@ -45,10 +49,10 @@ M.run = function(opts)
|
||||
|
||||
local exe = vim.fs.joinpath(bin.path, bin.name)
|
||||
or "src/{project_name}/main.py"
|
||||
local exe_path = utils.resolve_path(exe, opts.python.dirs, context)
|
||||
local exe_path = fs.fmt_path(exe, opts.python.dirs, context)
|
||||
local full_path = vim.fs.joinpath(vim.fn.expand(root), exe_path)
|
||||
|
||||
utils.term({ "python3 " .. full_path }, opts.terminal or {}, false)
|
||||
term.open({ "python3 " .. full_path }, opts.terminal or {}, false)
|
||||
end
|
||||
|
||||
M.pip_install = function()
|
||||
|
||||
@@ -1,4 +1,8 @@
|
||||
local utils = require("vern-nvim.utils")
|
||||
---@module "vern-nvim.core.fs"
|
||||
local fs = require("vern-nvim.fs")
|
||||
|
||||
---@module "vern-nvim.core.terminal"
|
||||
local term = require("vern-nvim.core.terminal")
|
||||
|
||||
local M = {}
|
||||
|
||||
@@ -24,8 +28,8 @@ M.create_file = nil
|
||||
---@param opts VernRustConfig Takes a table for the runtime
|
||||
M.run = function(opts)
|
||||
opts = opts or {}
|
||||
utils.term(
|
||||
{ "cd " .. utils.proj_root() .. " && cargo run" },
|
||||
term.open(
|
||||
{ "cd " .. fs.root() .. " && cargo run" },
|
||||
opts.terminal or {},
|
||||
false
|
||||
)
|
||||
|
||||
Reference in new issue
Block a user