REFACTOR: I spent all day on it but I am heavily refactoring the API and

organization because it was terrible before and hard to maintain. Now it
should be much easier and there is still of a design decisions to figure
out
This commit is contained in:
2026-10-15 02:07:14 -04:00
parent 2b30e36fad
commit 4bcac5d09b
27 changed files with 875 additions and 480 deletions
+42
View File
@@ -0,0 +1,42 @@
local M = {}
local utils = require("vern.utils")
M.default_target_file = "main.py"
--- Goal is to add providers for uv and venv so its available to more users
---@param opts? table Takes a table for the runtime
function M.run(opts)
opts = opts or {}
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 full_path = root .. "/" .. rel_path
utils.terminal({ "python3 " .. full_path }, opts)
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 })
return
end
vim.ui.input({ prompt = "Enter pip module: " }, function(pip_module)
if not pip_module or pip_module == "" then
vim.notify("Pip Cancelled", vim.log.levels.WARN)
return
end
utils.terminal({ "pip install " .. pip_module }, opts or {})
end)
end
return M
+25
View File
@@ -0,0 +1,25 @@
local M = {}
function M.buf(bufnr, user_opts)
bufnr = (bufnr == nil or bufnr == 0) and vim.api.nvim_get_current_buf()
or bufnr
local ft = vim.bo[bufnr].filetype
if not ft or ft == "" then
return
end
user_opts = user_opts or {}
local default = require("vern.configs.default")
local lang_opts = (user_opts or {})[ft] or {}
local final_opts =
vim.tbl_deep_extend("force", {}, default, user_opts, lang_opts)
final_opts[ft] = nil
local ok, lang_attach = pcall(require, "vern.attach." .. ft) --- like a try / catch block in c++
if ok and type(lang_attach) == "function" then
lang_attach(bufnr, final_opts)
end
end
return M
+43
View File
@@ -0,0 +1,43 @@
local actions = require("vern.actions.python")
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
actions.run(opts)
end
end, { desc = "Run python project" })
vim.api.nvim_buf_create_user_command(bufnr, "Pythonpip", function(cmd_args)
if actions.pip_install then
actions.pip_install(opts, cmd_args)
end
end, { nargs = "*", desc = "Install python packages" })
for key, def in pairs(opts.keybinds or {}) do
if type(def.action) == "function" then
vim.keymap.set("n", key, function()
def.action(opts)
end, {
buffer = bufnr,
silent = true,
desc = def.desc,
})
end
end
local root = require("vern.utils").get_project_root()
if root then
local venv_dir = nil
if vim.uv.fs_stat(root .. "/venv/bin/python") then
venv_dir = root .. "/venv"
elseif vim.uv.fs_stat(root .. "/.venv/bin/python") then
venv_dir = root .. "/.venv"
end
if venv_dir then
vim.env.VIRTUAL_ENV = venv_dir
vim.env.PATH = venv_dir .. "/bin:" .. vim.env.PATH
end
end
end
+15
View File
@@ -0,0 +1,15 @@
return {
workspace_root = "~/personal/Projects",
terminal = {
mode = "split",
win = {
type = "horizontal",
anchor = "bottom",
size = "20",
},
border = {
enabled = true,
type = "rounded",
},
},
}
+43
View File
@@ -0,0 +1,43 @@
local actions = require("vern.actions.python")
return {
root = "Python", --- later will concat the project_root to the front
keybinds = {
["<leader>pr"] = {
desc = "Executes python code",
action = actions.run,
},
["<leader>pb"] = {
desc = "Asks for what python package to install",
action = actions.pip_install,
},
},
templates = {
project = {
["executable"] = {
template = "vern.templates.python.main",
target = "src/%s/main.py",
},
["init"] = {
template = "vern.templates.python.__init__",
target = "src/%s/__init__.py",
},
["manifest"] = {
template = "vern.templates.python.pyproject",
target = "pyproject.toml",
},
},
file = {
["module"] = {
template = "vern.templates.python.module",
target = "src/%s.py",
},
},
extras = {
["gitignore"] = {
template = "vern.templates.extras.gitignore",
target = ".gitignore",
},
},
},
}
+157
View File
@@ -0,0 +1,157 @@
local ok, health = pcall(require, "vim.health")
if not ok then
health = nil
end
local new_api = health ~= nil and type(health.start) == "function"
local function report_start(name)
if new_api then health.start(name) else vim.health.report_start(name) end
end
local function report_ok(msg)
if new_api then health.ok(msg) else vim.health.report_ok(msg) end
end
local function report_info(msg)
if new_api then health.info(msg) else vim.health.report_info(msg) end
end
local function report_warn(msg, advice)
if new_api then
health.warn(msg, advice)
else
vim.health.report_warn(msg, advice)
end
end
local function report_error(msg, advice)
if new_api then
health.error(msg, advice)
else
vim.health.report_error(msg, advice)
end
end
local M = {}
local function fmt_langs(langs)
if type(langs) == "table" then
return "[" .. table.concat(langs, ", ") .. "]"
end
return "[" .. tostring(langs) .. "]"
end
local function check_nvim_version()
if vim.fn.has("nvim-0.10") == 1 then
report_ok(string.format("Neovim %s detected.", tostring(vim.version())))
else
report_error("Polydev requires Neovim 0.10 or newer.", "You are running " .. tostring(vim.version()))
end
end
local function check_setup()
local ok_configs, configs = pcall(require, "Polydev.configs")
if not ok_configs then
report_error("Could not load Polydev.configs", tostring(configs))
return nil
end
if next(configs.user_config or {}) == nil then
report_warn("Polydev.setup() has not run.",
"Add require('Polydev').setup() to your Neovim config, or make sure plugin/Polydev.lua is sourced by your plugin manager.")
return nil
end
report_ok("Polydev.setup() ran and the config is loaded.")
return configs
end
local function check_project_root(configs)
if not configs then return end
local root = configs.get("globals").project_root
local expanded = vim.fn.expand(root)
if vim.fn.isdirectory(expanded) == 1 then
if vim.fn.filewritable(expanded) == 2 then
report_ok(string.format("project_root exists and is writable: %s", expanded))
else
report_warn(string.format("project_root exists but is not writable: %s", expanded))
end
else
report_warn(string.format("project_root does not exist yet: %s", expanded),
"It is created automatically the first time the manager is opened.")
end
end
local function check_ui_input()
local ok_default, default = pcall(require, "vim.ui")
if ok_default and default.input and vim.ui.input ~= default.input then
report_warn("vim.ui.input has been overridden, likely by a UI plugin such as dressing.nvim or nui.nvim.",
"An asynchronous override can break project/file creation from the manager. Polydev now uses callbacks, but report here if it still misbehaves.")
else
report_ok("vim.ui.input is Neovim's default (synchronous) implementation.")
end
end
local function check_tools()
local tools = {
{ name = "git", langs = "all" },
{ name = "node", langs = "javascript" },
{ name = "python3", langs = "python" },
{ name = "pip3", langs = "python" },
{ name = "javac", langs = "java" },
{ name = "java", langs = "java" },
{ name = "cmake", langs = { "c", "cpp" } },
{ name = "cc", langs = { "c", "cpp" } },
{ name = "clang", langs = { "c", "cpp" } },
{ name = "clang-format", langs = { "c", "cpp" } },
{ name = "clang-tidy", langs = { "c", "cpp" } },
{ name = "cargo", langs = "rust" },
{ name = "lua", langs = "lua" },
{ name = "luajit", langs = "lua" },
}
for _, t in ipairs(tools) do
if vim.fn.executable(t.name) == 1 then
report_ok(string.format("%s is available for %s.", t.name, fmt_langs(t.langs)))
else
report_warn(string.format("%s was not found.", t.name),
"Project templates for " .. fmt_langs(t.langs) .. " may fail without it.")
end
end
if vim.fn.executable("python3") == 1 then
vim.fn.system("python3 -c 'import venv' 2>/dev/null")
if vim.v.shell_error == 0 then
report_ok("python3 venv module is available.")
else
report_warn("python3 venv module is missing.",
"Python project creation sets up a virtualenv and needs python3 -m venv.")
end
end
end
local function check_walkup()
local scripts = vim.api.nvim_get_runtime_file("bash_scripts/walkup.sh", false)
if #scripts == 0 then
report_error("bash_scripts/walkup.sh was not found on the runtimepath.",
"Reinstall the plugin so bash_scripts/ sits next to lua/ and plugin/.")
return
end
local script = scripts[1]
if vim.fn.executable(script) == 1 then
report_ok(string.format("bash_scripts/walkup.sh found and executable: %s", script))
else
report_warn(string.format("bash_scripts/walkup.sh is not executable: %s", script),
"Run: chmod +x " .. script)
end
end
function M.check()
report_start("Polydev")
check_nvim_version()
local configs = check_setup()
check_project_root(configs)
check_ui_input()
check_tools()
check_walkup()
end
return M
+20
View File
@@ -0,0 +1,20 @@
local M = {}
M.user_opts = {}
function M.setup(opts)
M.user_opts = opts or {}
local group =
vim.api.nvim_create_augroup("VernAutocommands", { clear = true })
vim.api.nvim_create_autocmd("FileType", {
group = group,
pattern = "*",
callback = function(args)
require("vern.attach").buf(args.buf, M.user_opts)
end,
desc = "Attach languages to buffer",
})
end
return M
+389
View File
@@ -0,0 +1,389 @@
local utils = require("Polydev.utils")
local configs = require("Polydev.configs")
local M = {}
local loaded_languages = {}
---@param lang string
---@param opts? table
---@return boolean
function M.load_language_module(lang, opts)
if loaded_languages[lang] then
return true
end
local ok, mod = pcall(require, "Polydev.languages")
if not ok or type(mod.languages) ~= "table" then
return false
end
local lang_mod = mod.languages[lang]
if type(lang_mod) ~= "table" or type(lang_mod.setup) ~= "function" then
return false
end
lang_mod:setup(opts and opts[lang] or {})
loaded_languages[lang] = lang_mod
return true
end
M.languages = {
javascript = {
setup = function(self, user_opts)
local opts = vim.tbl_deep_extend(
"force",
{},
configs.get("javascript"),
user_opts or {}
)
vim.api.nvim_create_user_command("JavascriptRun", function()
self:run()
end, {})
for key, _ in pairs(opts.keybinds) do
vim.keymap.set(
"n",
key,
":JavascriptRun<CR>",
{ silent = true }
)
end
end,
run = function()
local root = utils.get_project_root()
local cmd = string.format("node " .. root .. "/main.js")
local output = vim.fn.system(cmd)
if not vim.v.shell_error == 0 then
local term_buf = utils.terminal({ cmd })
vim.api.nvim_set_option_value(
"modifiable",
true,
{ buf = term_buf }
)
vim.api.nvim_buf_set_lines(
term_buf,
0,
-1,
false,
vim.list_extend(
{ "Error during compilation:" },
vim.split(output, "\n", { trimempty = true })
)
)
vim.api.nvim_set_option_value(
"modifiable",
false,
{ buf = term_buf }
)
else
utils.terminal({ "node " .. root .. "/main.js" })
end
end,
},
java = {
setup = function(self, user_opts)
local opts = vim.tbl_deep_extend(
"force",
{},
configs.get("java"),
user_opts or {}
)
vim.api.nvim_create_user_command("JavaRun", function()
self:run()
end, {})
for key, _ in pairs(opts.keybinds) do
vim.keymap.set("n", key, ":JavaRun<CR>", { silent = true })
end
end,
run = function()
local root = utils.get_project_root()
local cmd = string.format(
"javac -d %s %s",
root .. "/build",
root .. "/src/*.java"
)
local output = vim.fn.system(cmd)
if not vim.v.shell_error == 0 then
local term_buf = utils.terminal({ cmd })
vim.api.nvim_set_option_value(
"modifiable",
true,
{ buf = term_buf }
)
vim.api.nvim_buf_set_lines(
term_buf,
0,
-1,
false,
vim.list_extend(
{ "Error during compilation:" },
vim.split(output, "\n", { trimempty = true })
)
)
vim.api.nvim_set_option_value(
"modifiable",
false,
{ buf = term_buf }
)
else
utils.terminal({ "java -cp " .. root, "build" .. " Main" })
end
end,
},
c = {
setup = function(self, user_opts)
local opts = vim.tbl_deep_extend(
"force",
{},
configs.get("c"),
user_opts or {}
)
vim.api.nvim_create_user_command("CRun", function()
self:run()
end, {})
for key, _ in pairs(opts.keybinds) do
vim.keymap.set("n", key, ":CRun<CR>", { silent = true })
end
end,
run = function()
local root = utils.get_project_root()
local build_dir = root .. "/build"
local cmd = "cd '" .. build_dir .. "' && cmake --build ."
local output = vim.fn.system(cmd)
local success = vim.v.shell_error == 0
if not success then
local term_buf = utils.terminal({ cmd })
vim.api.nvim_set_option_value(
"modifiable",
true,
{ buf = term_buf }
)
vim.api.nvim_buf_set_lines(
term_buf,
0,
-1,
false,
vim.list_extend(
{ "Error during compilation:" },
vim.split(output, "\n", { trimempty = true })
)
)
vim.api.nvim_set_option_value(
"modifiable",
false,
{ buf = term_buf }
)
end
if success then
local run_cmd = "cd '"
.. build_dir
.. "' && ./"
.. utils.get_project_name()
utils.terminal({ run_cmd })
end
end,
},
cpp = {
setup = function(self, user_opts)
local opts = vim.tbl_deep_extend(
"force",
{},
configs.get("cpp"),
user_opts or {}
)
vim.api.nvim_create_user_command("CppRun", function()
self:run()
end, {})
for key, _ in pairs(opts.keybinds) do
vim.keymap.set("n", key, ":CppRun<CR>", { silent = true })
end
end,
run = function()
local root = utils.get_project_root()
local build_dir = root .. "/build"
local cmd = "cd '" .. build_dir .. "' && cmake --build ."
local output = vim.fn.system(cmd)
local success = vim.v.shell_error == 0
if not success then
local term_buf = utils.terminal({ cmd })
vim.api.nvim_set_option_value(
"modifiable",
true,
{ buf = term_buf }
)
vim.api.nvim_buf_set_lines(
term_buf,
0,
-1,
false,
vim.list_extend(
{ "Error during compilation:" },
vim.split(output, "\n", { trimempty = true })
)
)
vim.api.nvim_set_option_value(
"modifiable",
false,
{ buf = term_buf }
)
end
if success then
local run_cmd = "cd '"
.. build_dir
.. "' && ./"
.. utils.get_project_name()
utils.terminal({ run_cmd })
end
end,
},
html = {
setup = function(user_opts)
vim.tbl_deep_extend(
"force",
{},
configs.get("html"),
user_opts or {}
)
end,
},
rust = {
setup = function(self, user_opts)
local opts = vim.tbl_deep_extend(
"force",
{},
configs.get("rust"),
user_opts or {}
)
vim.api.nvim_create_user_command("RustRun", function()
self:run()
end, {})
for key, _ in pairs(opts.keybinds) do
vim.keymap.set("n", key, ":RustRun<CR>", { silent = true })
end
end,
run = function()
local root = utils.get_project_root()
if not root then
return print("Error: Project root not found.")
end
local cmd = { "cargo build" }
local output = vim.fn.system(table.concat(cmd))
if not vim.v.shell_error == 0 then
local term_buf = utils.terminal(cmd)
vim.api.nvim_set_option_value(
"modifiable",
true,
{ buf = term_buf }
)
vim.api.nvim_buf_set_lines(
term_buf,
0,
-1,
false,
vim.list_extend(
{ "Error during compilation:" },
vim.split(output, "\n", { trimempty = true })
)
)
vim.api.nvim_set_option_value(
"modifiable",
false,
{ buf = term_buf }
)
else
utils.terminal({ "cargo run" })
end
end,
},
python = {
setup = function(self, user_opts)
local opts = vim.tbl_deep_extend(
"force",
{},
configs.get("python"),
user_opts or {}
)
vim.api.nvim_create_user_command("PythonPip", function()
self:install_dependency()
end, {})
vim.api.nvim_create_user_command("PythonRun", function()
self:run()
end, {})
for key, command in pairs(opts.keybinds) do
if command == "PythonRun" then
vim.keymap.set(
"n",
key,
":PythonRun<CR>",
{ silent = true }
)
end
if command == "PythonPip" then
vim.keymap.set(
"n",
key,
":PythonPip<CR>",
{ silent = true }
)
end
end
local root = utils.get_project_root()
if root then
local venv_path = root .. "/venv/bin/activate"
if vim.fn.filereadable(venv_path) == 1 then
vim.env.VIRTUAL_ENV = venv_path
vim.env.PATH = root .. "/venv/bin:" .. vim.env.PATH
end
end
end,
install_dependency = function()
vim.ui.input({ prompt = "Enter pip module: " }, function(pip_module)
if not pip_module or pip_module == "" then
return print("Pip canceled")
end
utils.terminal({ "pip install " .. pip_module })
end)
end,
run = function()
utils.terminal({ "python3 " .. utils.get_project_root(), "main.py" })
end,
},
lua = {
setup = function(self, user_opts)
local opts = vim.tbl_deep_extend(
"force",
{},
configs.get("lua"),
user_opts or {}
)
vim.api.nvim_create_user_command("LuaRun", function()
self:run()
end, {})
for key, _ in pairs(opts.keybinds) do
vim.keymap.set("n", key, ":LuaRun<CR>", { silent = true })
end
end,
run = function()
local init_lua_path = utils.get_project_root()
.. "/lua/"
.. utils.get_project_name()
.. "/init.lua"
if vim.fn.filereadable(init_lua_path) == 1 then
utils.terminal({ "lua " .. init_lua_path })
end
end,
},
}
return M
+26
View File
@@ -0,0 +1,26 @@
local M = {}
local presets = {
right = {
left = 100,
right = 0,
top = 0,
bottom = 0,
},
corner = {
left = 100,
right = 0,
top = 25,
bottom = 0,
}
}
---@param preset string
---@return table|nil
function M.getPresets(preset)
if presets[preset] then
return presets[preset]
end
end
return M
+3
View File
@@ -0,0 +1,3 @@
local M = {}
return M
+23
View File
@@ -0,0 +1,23 @@
Configs and setups currently for each file type
Configs:
project_root = the location on the file system for projects
keybinds = Custom keybinds for each file type
Templates:
projects:
run = creates project
files:
run = creates file
extras:
run = creates auxiliary files for specific languages like c
Languages:
file type:
setup = loads on filetype and loads functions
Run = builds if needed then executes the main file
Use a polydevrc.json or something named like that but in json for user specific
settings per project like changing where the executable for a project is like
how I have been doing manually in CMAKE for c projects to test
+371
View File
@@ -0,0 +1,371 @@
local utils = require("Polydev.utils")
local M = {}
local function extras(guard, file_type, file_name)
if not file_name then return print("Header file creation canceled") end
local guard_macro = file_name:upper():gsub("[^A-Z0-9]", "_") .. guard
local content = string.format([[
#ifndef %s
#define %s
// Add code here
#endif
]], guard_macro, guard_macro)
utils.write_file({ utils.get_project_root(), "include", file_name .. file_type }, content)
end
M.projects = {
javascript = {
run = function(project_name, project_root)
local full_project_root = vim.fn.expand(project_root) .. "/" .. project_name
vim.fn.mkdir(full_project_root, "p")
utils.write_file({ full_project_root, "main.js" }, [[
console.log("Hello World!");
]])
local gitignore = { "" }
utils.init_git(full_project_root, gitignore)
vim.cmd("edit " .. full_project_root .. "/main.js")
end
},
lua = {
run = function(project_name, project_root)
local full_project_root = vim.fn.expand(project_root) .. "/" .. project_name
vim.fn.mkdir(full_project_root .. "/lua/" .. project_name, "p")
utils.write_file({ full_project_root, "lua", project_name, "init.lua" }, [[
local M = {}
return M
]])
local gitignore = { "" }
utils.init_git(full_project_root, gitignore)
vim.cmd("edit " .. full_project_root .. "/lua/" .. project_name .. "/init.lua")
end
},
java = {
run = function(project_name, project_root)
local full_project_root = vim.fn.expand(project_root) .. "/" .. project_name
for _, path in ipairs({ "/src", "/build" }) do vim.fn.mkdir(full_project_root .. path, "p") end
local content = [[
public class Main {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
]]
local gitignore = {
"build/"
}
utils.init_git(full_project_root, gitignore)
utils.write_file({ full_project_root, "src", "Main.java" }, content)
end
},
python = {
run = function(project_name, project_root)
local full_project_root = vim.fn.expand(project_root) .. "/" .. project_name
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
},
c = {
run = function(project_name, project_root)
local full_project_root = vim.fn.expand(project_root) .. "/" .. project_name
for _, path in ipairs({ "/src", "/build", "/include" }) do
vim.fn.mkdir(
full_project_root .. path, "p")
end
utils.write_file({ full_project_root, ".clang-format" }, [[
---
BasedOnStyle: Google
Standard: Latest
UseTab: Always
TabWidth: 8
IndentWidth: 8
BreakBeforeBraces: Allman
SpaceBeforeCpp11BracedList: true
SeparateDefinitionBlocks: Always
Cpp11BracedListStyle: false
BreakAfterReturnType: TopLevel
AlignTrailingComments:
Kind: Always
ReflowComments: Always
AllowShortFunctionsOnASingleLine: All
AllowShortIfStatementsOnASingleLine: AllIfsAndElse
AllowShortLoopsOnASingleLine: true
IndentAccessModifiers: false # Indent access specifiers by one level
AccessModifierOffset: -4 # Optional: adjust offset relative to the class indentation level
]])
utils.write_file({ full_project_root, "src/main.c" }, [[
#include <stdio.h>
int main() {
printf("%s", "Hello World");
return 0;
}
]])
utils.write_file({ full_project_root, ".clang-tidy" }, string.format([[
---
Checks: >
-*,
bugprone-*,
concurrency-*,
misc-*,
modernize-*,
performance-*,
readability-*,
portability-*,
-readability-braces-around-statements,
-readability-implicit-bool-conversion
WarningsAsErrors: ""
CheckOptions:
- key: readability-identifier-naming.VariableCase
value: lower_case
- key: readability-identifier-naming.FunctionCase
value: lower_case
---
]]))
utils.write_file({ full_project_root, "CMakeLists.txt" }, string.format([[
cmake_minimum_required(VERSION 3.16)
set(CMAKE_C_STANDARD 23)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
include_directories(include)
project(%s LANGUAGES C)
set(SOURCES
src/main.c
)
add_executable(%s ${SOURCES})
]], project_name, project_name, project_name))
local gitignore = { "build/", ".cache/" }
utils.init_git(full_project_root, gitignore)
vim.fn.system(string.format(
"cd %s/build && cmake -S .. -B . && cmake --build . && cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON ..",
full_project_root))
vim.cmd("edit " .. full_project_root .. "/src/main.c")
end
},
cpp = {
run = function(project_name, project_root)
local full_project_root = vim.fn.expand(project_root) .. "/" .. project_name
for _, path in ipairs({ "/src", "/build", "/include" }) do
vim.fn.mkdir(full_project_root .. path, "p")
end
utils.write_file({ full_project_root, ".clang-format" }, [[
---
BasedOnStyle: Google
Standard: Latest
UseTab: Always
TabWidth: 8
IndentWidth: 8
BreakBeforeBraces: Allman
SpaceBeforeCpp11BracedList: true
SeparateDefinitionBlocks: Always
Cpp11BracedListStyle: false
BreakAfterReturnType: TopLevel
AlignTrailingComments:
Kind: Always
ReflowComments: Always
AllowShortFunctionsOnASingleLine: All
AllowShortIfStatementsOnASingleLine: AllIfsAndElse
AllowShortLoopsOnASingleLine: true
IndentAccessModifiers: false # Indent access specifiers by one level
AccessModifierOffset: -4 # Optional: adjust offset relative to the class indentation level
]])
utils.write_file({ full_project_root, "src/main.cpp" }, [[
#include <iostream>
int main() {
std::cout << "Hello World\n";
return 0;
}
]])
utils.write_file({ full_project_root, ".clang-tidy" }, string.format([[
---
Checks: >
-*,
bugprone-*,
concurrency-*,
misc-*,
modernize-*,
performance-*,
readability-*,
portability-*,
-readability-braces-around-statements,
-readability-implicit-bool-conversion
WarningsAsErrors: ""
CheckOptions:
- key: readability-identifier-naming.VariableCase
value: lower_case
- key: readability-identifier-naming.FunctionCase
value: lower_case
---
]]))
utils.write_file({ full_project_root, "CMakeLists.txt" }, string.format([[
cmake_minimum_required(VERSION 3.16)
set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
project(%s LANGUAGES CXX)
set(SOURCES
src/main.cpp
)
add_executable(%s ${SOURCES})
]], project_name, project_name))
local gitignore = { "build/", ".cache/" }
utils.init_git(full_project_root, gitignore)
vim.fn.system(string.format(
"cd %s/build && cmake -S .. -B . && cmake --build . && cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON ..",
full_project_root))
vim.cmd("edit " .. full_project_root .. "/src/main.cpp")
end
},
rust = {
run = function(project_name, project_root)
local full_project_root = vim.fn.expand(project_root) .. "/" .. project_name
vim.fn.system("cargo new " .. full_project_root)
vim.cmd("edit " .. full_project_root .. "/src/main.rs")
end
},
html = {
run = function(project_name, project_root)
local full_project_root = vim.fn.expand(project_root) .. "/" .. project_name
if vim.fn.isdirectory(full_project_root) == 0 then
vim.fn.mkdir(full_project_root, "p") -- Create directory with parent directories
end
local main_html_content = [[
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<h1>My First Heading</h1>
<p>My first paragraph.</p>
</body>
</html>
]]
utils.write_file({ full_project_root, "index.html" }, main_html_content)
end
},
}
M.files = {
lua = {
run = function(file_name)
utils.write_file(
{ utils.get_project_root(), "lua", utils.get_project_name(), file_name .. ".lua" }, "")
end
},
java = {
run = function(file_name)
utils.write_file({ utils.get_project_root(), "src", file_name .. ".java" }, string.format([[
public class %s {
// New File
}
]], file_name))
end
},
python = {
run = function(file_name)
utils.write_file({ utils.get_project_root(), "include", file_name .. ".py" }, "")
end
},
c = {
run = function(file_name)
utils.write_file({ utils.get_project_root(), "src", file_name .. ".c" }, "")
end
},
cpp = {
run = function(file_name)
utils.write_file({ utils.get_project_root(), "src", file_name .. ".cpp" }, "")
end
},
rust = {
run = function(file_name)
utils.write_file({ utils.get_project_root(), "src", file_name .. ".rs" }, "")
end
},
html = {
run = function(file_name)
utils.write_file({ ".", file_name .. ".html" }, string.format([[
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<h1>My First Heading</h1>
<p>My first paragraph.</p>
</body>
</html>
]]))
end
}
}
M.extras = {
run = function(lang, file_name)
if lang == "c" then
extras("_H", ".h", file_name)
print("Successful creation of " .. file_name .. ".h")
elseif lang == "cpp" then
extras("_HPP", ".hpp", file_name)
print("Successful creation of " .. file_name .. ".hpp")
else
print("Extras language is not found: enter cpp or c")
end
end
}
return M
+31
View File
@@ -0,0 +1,31 @@
local M = {}
---@param opts table
M.template = function(opts)
local base = [=[
.DS_Store
.vscode/
.idea/
.env
.env.*
]=]
local lang_block = {
python = [=[
__pycache__/
build/
dist/
.venv/
env/
venv/
.pytest_cache/
]=],
}
local extra = (opts and opts and lang_block[opts.lang]) or ""
return base .. extra
end
return M
+13
View File
@@ -0,0 +1,13 @@
local M = {}
---@param opts table
M.template = function(opts)
local template = [=[
# %s
__all__: list[str] = []
]=]
return string.format(template, opts.project_name)
end
return M
+15
View File
@@ -0,0 +1,15 @@
local M = {}
---@param opts table
M.template = function(opts)
local template = [=[
def main() -> None:
printf("Hello from %s!")
if __name__ == "__main__":
main()
]=]
return string.format(template, opts.project_name)
end
return M
+13
View File
@@ -0,0 +1,13 @@
local M = {}
---@param opts table
M.template = function(opts)
local template = [=[
"""
%s module
"""
]=]
return string.format(template, opts.project_name)
end
return M
+25
View File
@@ -0,0 +1,25 @@
local M = {}
---@param opts table
M.template = function(opts)
local n = opts.project_name
local template = [=[
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"
[project]
name = "%s"
version = "0.1.0"
description = "Project %s"
readme = "README.md"
requires-python = ">=3.10"
dependencies = []
[project.scripts]
%s = "%s.main:main"
]=]
return string.format(template, n, n, n, n)
end
return M
+189
View File
@@ -0,0 +1,189 @@
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
+149
View File
@@ -0,0 +1,149 @@
local M = {}
---@param bufnr? integer Default is 0 (current buffer)
---@return string|nil
function M.get_project_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()
end
---
---@param cmd table
---@return integer, integer?
function M.terminal(cmd, opts)
opts = opts or {}
if opts.terminal.mode == "floating" then
local ui = vim.api.nvim_list_uis()[1]
local width, height, row, col
if opts.terminal ~= nil then
width = math.max(
1,
math.floor(ui.width * 0.9)
- opts.terminal.left
- opts.terminal.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, {
relative = "editor",
width = width,
height = height,
row = row,
col = col,
style = "minimal",
border = opts.terminal.border.enabled and opts.terminal.border.type,
})
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
vim.cmd("set splitright")
end
end
if opts.terminal.win.type == "horizontal" then
if opts.terminal.win.anchor == "bottom" then
vim.cmd("set splitbelow")
end
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()
vim.api.nvim_win_set_buf(0, buf)
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
return 0
end
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)
-- return (io.open(path, "r") ~= nil)
-- 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
-- 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