REFACTOR: Cleaning up project and removing more deprecated code

This commit is contained in:
2026-10-16 10:38:06 -04:00
parent 98bd619cfa
commit 06eb74e13f
11 changed files with 142 additions and 332 deletions
+3
View File
@@ -10,6 +10,9 @@
</p>
NOTE: The plugin may not work well with lazy or any other package manager other than vim.pack. I dont understand enough about nvim api and lua to handle these other things right now
## Features
* Create and manage projects directly from Neovim
+12 -16
View File
@@ -1,7 +1,6 @@
local M = {}
local utils = require("vern.utils")
M.default_target_file = "main.py"
function M.create_project(opts) end
function M.create_file(opts) end
@@ -13,32 +12,29 @@ function M.run(opts)
local root = utils.get_project_root() or "."
local project_name = vim.fs.basename(root)
local pattern = vim.tbl_get(
opts,
"templates",
"project",
"executable",
"target"
) or "src/%s/main.py"
local rel_path = pattern:find("%%s")
and string.format(pattern, project_name)
or pattern
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 full_path = root .. "/" .. rel_path
utils.terminal({ "python3 " .. full_path }, opts)
utils.terminal({ "python3 " .. full_path }, opts.terminal or {})
end
---@param cmd_args? table Takes a table for the runtime
function M.pip_install(opts, cmd_args)
if cmd_args and cmd_args.args and cmd_args ~= "" then
utils.terminal({ "pip install " .. cmd_args.args })
local args = cmd_args and cmd_args.args and vim.trim(cmd_args.args) or ""
if args ~= "" then
utils.terminal({ "pip install " .. args }, opts.terminal or {})
return
end
vim.ui.input({ prompt = "Enter pip module: " }, function(pip_module)
if not pip_module or pip_module == "" then
if not pip_module or vim.trim(pip_module) == "" then
vim.notify("Pip Cancelled", vim.log.levels.WARN)
return
end
utils.terminal({ "pip install " .. pip_module }, opts or {})
utils.terminal(
{ "pip install " .. vim.trim(pip_module) },
opts.terminal or {}
)
end)
end
+2 -2
View File
@@ -4,7 +4,7 @@ local config = require("vern.configs.python")
return function(bufnr, user_opts)
local opts = vim.tbl_deep_extend("force", {}, config, user_opts or {})
vim.api.nvim_buf_create_user_command(bufnr, "Pythonrun", function()
if actions.run then
if type(actions.run) == "function" then
actions.run(opts)
end
end, { desc = "Run python project" })
@@ -15,7 +15,7 @@ return function(bufnr, user_opts)
end
end, { nargs = "*", desc = "Install python packages" })
for key, def in pairs(opts.keybinds or {}) do
for key, def in pairs(opts.python.keybinds or {}) do
if type(def.action) == "function" then
vim.keymap.set("n", key, function()
def.action(opts)
-26
View File
@@ -1,26 +0,0 @@
local actions = require("vern.actions")
return {
workspace_root = "~/personal/Projects",
keybinds = {
["<leader>pc"] = {
desc = "Creates a new project",
action = actions.create_project,
},
["<leader>pf"] = {
desc = "Creates a new project file",
action = actions.create_file,
},
},
terminal = {
mode = "split",
win = {
type = "horizontal",
anchor = "bottom",
size = "20",
},
border = {
enabled = true,
type = "rounded",
},
},
}
+38
View File
@@ -0,0 +1,38 @@
local actions = require("vern.actions")
return {
workspace_root = "~/personal/Projects",
keybinds = {
["<leader>pc"] = {
desc = "Creates a new project",
action = actions.create_project,
},
["<leader>pf"] = {
desc = "Creates a new project file",
action = actions.create_file,
},
},
terminal = {
mode = {
type = "split", --- can be split or float
win = {
float = {
padding = {
left = 0,
right = 0,
top = 0,
bottom = 0,
},
border = {
enabled = true,
type = vim.o.winborder,
},
},
split = {
type = "horizontal", --- can be vertical or horizontal
anchor = "bottom", --- can be right or bottom
size = "20",
},
},
},
},
}
+2
View File
@@ -1,6 +1,7 @@
local actions = require("vern.actions.python")
return {
python = {
root = "Python", --- later will concat the project_root to the front
keybinds = {
["<leader>pr"] = {
@@ -40,4 +41,5 @@ return {
},
},
},
},
}
+3 -3
View File
@@ -1,11 +1,11 @@
local M = {}
local global_opts = require("vern.configs.default")
local configs = require("vern.configs")
local actions = require("vern.actions")
M.opts = {}
function M.setup(user_opts)
M.opts = vim.tbl_deep_extend("force", {}, global_opts, user_opts or {})
M.opts = vim.tbl_deep_extend("force", {}, configs, user_opts or {})
--- Bind global keybinds
for key, def in pairs(M.opts.keybinds or {}) do
@@ -43,7 +43,7 @@ function M.setup(user_opts)
local ft = vim.bo[args.buf].filetype
local ok, attach = pcall(require, "vern.attach." .. ft)
if ok and type(attach) == "function" then
attach(args.buf, M.user_opts)
attach(args.buf, M.opts)
end
end,
desc = "Attach languages to buffer",
-189
View File
@@ -1,189 +0,0 @@
local M = {}
---@param path string
---@return table
local function get_entries(path)
local result = {}
local handle = vim.uv.fs_scandir(path)
if not handle then return result end
while true do
local name, type = vim.uv.fs_scandir_next(handle)
if not name then break end
table.insert(result, {
name = name .. (type == "directory" and "/" or ""),
type = type,
full_path = path .. "/" .. name,
})
end
table.sort(result, function(a, b)
if a.type == "directory" and b.type ~= "directory" then return true end
if b.type == "directory" and a.type ~= "directory" then return false end
return a.name < b.name
end)
return result
end
---@param project_root string
function M.manager(project_root)
local templates = require("Polydev.templates")
local width, height = math.floor(vim.o.columns * 0.40), math.floor(vim.o.lines * 0.4)
local row, col = math.floor((vim.o.lines - height) / 3), math.floor((vim.o.columns - width) / 2)
local content_row = row + 2
local content_height = height - 3
local cwd = vim.fn.getcwd()
local current_view = {}
local content_buf = vim.api.nvim_create_buf(false, true)
local content_win = vim.api.nvim_open_win(content_buf, true, {
relative = "editor",
width = width,
height = content_height,
row = content_row,
col = col,
style = "minimal",
border = "rounded",
})
local function close_all_windows()
if vim.api.nvim_win_is_valid(content_win) then
pcall(vim.api.nvim_win_close, content_win, true)
end
end
local function update_screen()
local root = vim.fn.expand(project_root)
if vim.fn.isdirectory(root) == 0 then
vim.fn.mkdir(root, "p")
end
local entries = get_entries(cwd)
if not cwd:find(root, 1, true) then
cwd = root
entries = get_entries(cwd)
end
if cwd ~= root then
table.insert(entries, 1, {
name = "../",
type = "directory",
full_path = vim.fn.fnamemodify(cwd, ":h"),
is_up = true,
})
end
current_view = vim.deepcopy(entries)
local lines = {}
if #current_view == 0 then
table.insert(lines, "No projects found in " .. cwd)
table.insert(lines, "Press d to create a project, or a to add a folder.")
else
for i, item in ipairs(current_view) do
table.insert(lines, string.format("%-2d | %-27s | %-6s", i, item.name, item.type))
end
end
vim.api.nvim_buf_set_lines(content_buf, 0, -1, false, lines)
end
local function get_selected_entry()
local win = content_win
if not win or not vim.api.nvim_win_is_valid(win) then return end
local row = vim.api.nvim_win_get_cursor(win)[1]
return current_view[row], row
end
---@param type string
---@param callback fun(lang: string, name: string)
local function get_inputs(type, callback)
vim.ui.input({ prompt = "Enter language for new " .. type .. ": " }, function(lang)
if not lang or lang == "" then return end
vim.ui.input({ prompt = "Enter " .. type .. " name: " }, function(name)
if not name or name == "" then return end
callback(lang, name)
end)
end)
end
update_screen()
local buf = content_buf
vim.keymap.set("n", "<ESC>", function()
if vim.api.nvim_win_is_valid(content_win) then
pcall(vim.api.nvim_win_close, content_win, true)
end
end, { buffer = buf })
vim.keymap.set("n", "a", function ()
vim.ui.input({ prompt = "New folder name: " }, function(input)
if input then
vim.fn.mkdir(cwd .. "/" .. input, "p")
update_screen()
end
end)
end, { buffer = buf })
vim.keymap.set("n", "R", function ()
local entry = get_selected_entry()
if entry ~= nil and not entry.is_up then
vim.ui.input({ prompt = "Rename: ", default = entry.name }, function(input)
if input then
local new_path = cwd .. "/" .. input
vim.fn.rename(entry.full_path, new_path)
update_screen()
end
end)
end
end, { buffer = buf })
vim.keymap.set("n", "D", function()
local entry = get_selected_entry()
if entry ~= nil and not entry.is_up then
vim.ui.input({ prompt = "Deletion of " .. entry.name .. ": (y/N) Defaults to N: " }, function(input)
if input == "y" or input == "Y" then
vim.fn.delete(entry.full_path, "rf")
update_screen()
end
end)
end
end, { buffer = buf })
vim.keymap.set("n", "%", function()
get_inputs("file", function(lang, name)
local file = templates.files[lang]
if not file or not file.run then
return print("Error: No file creation method for " .. lang)
end
close_all_windows()
file.run(name)
end)
end, { buffer = buf })
vim.keymap.set("n", "x", function()
get_inputs("aux file", function(lang, name)
close_all_windows()
templates.extras.run(lang, name)
end)
end, { buffer = buf })
vim.keymap.set("n", "d", function()
get_inputs("project", function(lang, name)
local project = templates.projects[lang]
if not project or not project.run then
return print("Error: No project creation method for " .. lang)
end
local opts = require("Polydev.configs").get(lang)
local run_root = opts.project_root or project_root
close_all_windows()
project.run(name, run_root)
end)
end, { buffer = buf })
vim.keymap.set("n", "<CR>", function()
local entry = get_selected_entry()
if not entry then return end
if entry.type == "file" then
close_all_windows()
vim.cmd("edit " .. vim.fn.fnameescape(entry.full_path))
elseif entry.type == "directory" then
cwd = entry.full_path
vim.cmd("cd " .. cwd)
update_screen()
else
print("Error")
end
end, { buffer = buf })
end
return M
+42 -55
View File
@@ -19,80 +19,69 @@ end
---@return integer, integer?
function M.terminal(cmd, opts)
opts = opts or {}
if opts.terminal.mode == "floating" then
local buf, win
if opts.mode.type == "float" then
local ui = vim.api.nvim_list_uis()[1]
local width, height, row, col
if opts.terminal ~= nil then
local dim = {
width = math.max(
1,
math.floor(ui.width * 0.9)
- opts.terminal.left
- opts.terminal.right
)
- opts.mode.win.float.padding.left
- opts.mode.win.float.padding.right
),
height = math.max(
1,
math.floor(ui.height * 0.9)
- opts.terminal.top
- opts.terminal.bottom
)
row = math.floor((ui.height - height) / 2) + opts.terminal.top
col = math.floor((ui.width - width) / 2) + opts.terminal.left
end
if opts.terminal == nil then
width = math.max(1, math.floor(ui.width * 0.9))
height = math.max(1, math.floor(ui.height * 0.9))
row = math.floor((ui.height - height) / 2)
col = math.floor((ui.width - width) / 2)
end
local buf = vim.api.nvim_create_buf(false, true)
local win = vim.api.nvim_open_win(buf, true, {
- opts.mode.win.float.padding.top
- opts.mode.win.float.padding.bottom
),
}
local pos = {
row = math.floor(
(
ui.height
- (vim.o.laststatus == 3 and 1 or 0)
- vim.o.cmdheight
- dim.height
) / 2
),
col = math.floor((ui.width - dim.width) / 2),
}
buf = vim.api.nvim_create_buf(false, true)
win = vim.api.nvim_open_win(buf, true, {
relative = "editor",
width = width,
height = height,
row = row,
col = col,
width = dim.width,
height = dim.height,
row = pos.row,
col = pos.col,
style = "minimal",
border = opts.terminal.border.enabled and opts.terminal.border.type,
border = opts.mode.win.float.border.enabled
and opts.mode.win.float.border.type
or nil,
})
vim.api.nvim_set_option_value(
"winhighlight",
"Normal:PolydevNormal,FloatBorder:PolydevBorder",
{ win = win }
)
vim.fn.jobstart(table.concat(cmd, "/"), { term = true })
vim.api.nvim_buf_set_keymap(
buf,
"n",
"<ESC>",
"i<C-\\><C-n>:q<CR>",
{ noremap = true, silent = true }
)
return buf, win
end
if opts.terminal.mode == "split" then
if opts.terminal.win.type == "vertical" then
if opts.terminal.win.anchor == "right" then
if opts.mode.type == "split" then
if opts.mode.win.split.type == "vertical" then
if opts.mode.win.split.anchor == "right" then
vim.cmd("set splitright")
end
vim.cmd(opts.mode.win.split.size .. " vsplit")
end
if opts.terminal.win.type == "horizontal" then
if opts.terminal.win.anchor == "bottom" then
if opts.mode.win.split.type == "horizontal" then
if opts.mode.win.split.anchor == "bottom" then
vim.cmd("set splitbelow")
end
vim.cmd(opts.mode.win.split.size .. " split")
end
if opts.terminal.win.type == "vertical" then
vim.cmd(opts.terminal.win.size .. " vsplit")
end
if opts.terminal.win.type == "horizontal" then
vim.cmd(opts.terminal.win.size .. " split")
end
local buf = vim.api.nvim_create_buf(false, true)
local win = vim.api.nvim_get_current_win()
buf = vim.api.nvim_create_buf(false, true)
win = vim.api.nvim_get_current_win()
vim.api.nvim_win_set_buf(0, buf)
end
vim.fn.jobstart(table.concat(cmd, "/"), { term = true })
vim.api.nvim_buf_set_keymap(
buf,
@@ -103,8 +92,6 @@ function M.terminal(cmd, opts)
)
return buf, win
end
return 0
end
return M
-1
View File
@@ -1 +0,0 @@
require("vern").setup()