From 8aa730a66d57eeff9a0aa3befb8ca8d70d5ad4d7 Mon Sep 17 00:00:00 2001 From: DarthMooMancer <107283522+DarthMooMancer@users.noreply.github.com> Date: Wed, 16 Apr 2025 02:06:14 -0400 Subject: [PATCH] First release. Polydev is finally in stable condition to be more of a release than a base commit --- README.md | 4 +-- lua/Polydev/configs.lua | 63 +++++++++++++++++++++++++++++++++- lua/Polydev/init.lua | 1 + lua/Polydev/languages/c.lua | 41 +++++++++------------- lua/Polydev/languages/cpp.lua | 53 +++++++++++++--------------- lua/Polydev/languages/html.lua | 14 +++----- lua/Polydev/languages/java.lua | 35 ++++++++----------- lua/Polydev/languages/lua.lua | 11 +++--- lua/Polydev/languages/rust.lua | 29 +++++++--------- lua/Polydev/utils.lua | 24 ++----------- 10 files changed, 142 insertions(+), 133 deletions(-) diff --git a/README.md b/README.md index 85c4ac1..9a86a2c 100644 --- a/README.md +++ b/README.md @@ -13,13 +13,13 @@ The ability to - * Build those files * Run those files * Builtin terminal for running code +* And More! ### Currently Supported (And working) Languages * Java * Python -* C +* C/C++ * Lua -* C++ ## Questions and Contributing diff --git a/lua/Polydev/configs.lua b/lua/Polydev/configs.lua index 532b985..386cdf5 100644 --- a/lua/Polydev/configs.lua +++ b/lua/Polydev/configs.lua @@ -1,6 +1,50 @@ local M = {} +---@class Keybinds +---@field [string] string +--- +---@class Language +---@field project_root string +---@field keybinds? Keybinds -- ? makes fields optional +---@field build_attributes? string +--- +---@class Terminal +---@field right_padding integer +---@field left_padding integer +---@field bottom_padding integer +---@field top_padding integer +---@field border boolean +---@field number boolean +---@field relativenumber boolean +--- +---@class Defaults +---@field html Language +---@field rust Language +---@field java Language +---@field python Language +---@field lua Language +---@field c Language +---@field cpp Language +---@field terminal Terminal +---@type Defaults M.defaults = { + html = { + project_root = "~/Projects/Html", + }, + rust = { + project_root = "~/Projects/Rust", + keybinds = { + ["pb"] = "RustBuild", + ["pr"] = "RustRun", + }, + }, + java = { + project_root = "~/Projects/Java", + keybinds = { + ["pb"] = "JavaBuild", + ["pr"] = "JavaRun", + }, + }, python = { project_root = "~/Projects/Python", keybinds = { @@ -14,6 +58,24 @@ M.defaults = { ["pr"] = "LuaRun", }, }, + c = { + project_root = "~/Projects/C", + keybinds = { + ["pb"] = "CBuild", + ["pr"] = "CRun", + ["nh"] = "NewCHeaderFile", + }, + build_attributes = "" + }, + cpp = { + project_root = "~/Projects/Cpp", + keybinds = { + ["pb"] = "CppBuild", + ["pr"] = "CppRun", + ["nh"] = "NewCppHeaderFile", + }, + build_attributes = "", + }, terminal = { right_padding = 0, bottom_padding = 0, @@ -22,7 +84,6 @@ M.defaults = { border = true, number = true, relativenumber = true, - scroll = true, } } diff --git a/lua/Polydev/init.lua b/lua/Polydev/init.lua index 1c7ad16..37e75af 100644 --- a/lua/Polydev/init.lua +++ b/lua/Polydev/init.lua @@ -51,6 +51,7 @@ end function M.setup(opts) opts = opts or {} M.config.setup(opts) + M.utils.setup(opts) for lang, user_opts in pairs(opts) do local default = M.config.defaults[lang] or {} M.config.user_config[lang] = vim.tbl_deep_extend("force", default, user_opts) diff --git a/lua/Polydev/languages/c.lua b/lua/Polydev/languages/c.lua index 2b0d839..194bb02 100644 --- a/lua/Polydev/languages/c.lua +++ b/lua/Polydev/languages/c.lua @@ -1,38 +1,30 @@ -local M = {} local utils = require("Polydev.utils") +local M = {} -M.c_build = nil -M.c_run = nil -M.new_c_header_file = nil - -M.config = { - project_root = "~/Projects/C", - keybinds = { - ["pb"] = "CBuild", - ["pr"] = "CRun", - ["nh"] = "NewCHeaderFile", - }, - build_attributes = "" -} +M.keybinds = {} +M.opts = nil function M.setup(opts) - M.config = vim.tbl_deep_extend("force", M.config, opts or {}) - for key, command in pairs(M.config.keybinds) do - if command == "CBuild" then M.c_build = key end - if command == "CRun" then M.c_run = key end - if command == "NewCHeaderFile" then M.new_c_header_file = key end + M.opts = vim.tbl_deep_extend("force", {}, require("Polydev.configs").get("c"), opts or {}) + for key, command in pairs(M.opts.keybinds) do + M.keybinds[command] = key end vim.api.nvim_create_user_command("NewCHeaderFile", M.create_new_header_file, {}) vim.api.nvim_create_user_command("CBuild", M.build, {}) vim.api.nvim_create_user_command("CRun", M.run, {}) - vim.keymap.set("n", M.c_build, ":CBuild", { silent = true }) - vim.keymap.set("n", M.c_run, ":CRun", { silent = true }) - vim.keymap.set("n", M.new_c_header_file, ":NewCHeaderFile", { silent = true }) + if M.keybinds.CBuild then + vim.keymap.set("n", M.keybinds.CBuild, ":CBuild", { silent = true }) + end + if M.keybinds.CRun then + vim.keymap.set("n", M.keybinds.CRun, ":CRun", { silent = true }) + end + if M.keybinds.NewCHeaderFile then + vim.keymap.set("n", M.keybinds.NewCHeaderFile, ":NewCHeaderFile", { silent = true }) + end end - function M.create_project() vim.ui.input({ prompt = "Enter project name: " }, function(project_name) if not project_name or project_name == "" then @@ -40,7 +32,7 @@ function M.create_project() return end - local project_root = vim.fn.expand(M.config.project_root) .. "/" .. project_name + local project_root = vim.fn.expand(M.opts.project_root) .. "/" .. project_name for _, path in ipairs({ "/src", "/build", "/include" }) do vim.fn.mkdir(project_root .. path, "p") end @@ -108,6 +100,7 @@ set(CMAKE_C_STANDARD_REQUIRED ON) ]])) vim.cmd("edit " .. project_root .. "/src/main.c") + vim.fn.system(string.format("cd %s/build/ && cmake .. && cmake --build .", project_root)) end) end diff --git a/lua/Polydev/languages/cpp.lua b/lua/Polydev/languages/cpp.lua index 7621374..383f282 100644 --- a/lua/Polydev/languages/cpp.lua +++ b/lua/Polydev/languages/cpp.lua @@ -1,35 +1,28 @@ -local M = {} local utils = require("Polydev.utils") +local M = {} -M.c_build = nil -M.c_run = nil -M.new_c_header_file = nil - -M.config = { - project_root = "~/Projects/C", - keybinds = { - ["pb"] = "CBuild", - ["pr"] = "CRun", - ["nh"] = "NewCHeaderFile", - }, - build_attributes = "" -} +M.keybinds = {} +M.opts = nil function M.setup(opts) - M.config = vim.tbl_deep_extend("force", M.config, opts or {}) - for key, command in pairs(M.config.keybinds) do - if command == "CBuild" then M.c_build = key end - if command == "CRun" then M.c_run = key end - if command == "NewCHeaderFile" then M.new_c_header_file = key end + M.opts = vim.tbl_deep_extend("force", {}, require("Polydev.configs").get("cpp"), opts or {}) + for key, command in pairs(M.opts.keybinds) do + M.keybinds[command] = key end - vim.api.nvim_create_user_command("NewCHeaderFile", M.create_new_header_file, {}) - vim.api.nvim_create_user_command("CBuild", M.build, {}) - vim.api.nvim_create_user_command("CRun", M.run, {}) + vim.api.nvim_create_user_command("NewCppHeaderFile", M.create_new_header_file, {}) + vim.api.nvim_create_user_command("CppBuild", M.build, {}) + vim.api.nvim_create_user_command("CppRun", M.run, {}) - vim.keymap.set("n", M.c_build, ":CBuild", { silent = true }) - vim.keymap.set("n", M.c_run, ":CRun", { silent = true }) - vim.keymap.set("n", M.new_c_header_file, ":NewCHeaderFile", { silent = true }) + if M.keybinds.CppBuild then + vim.keymap.set("n", M.keybinds.CppBuild, ":CppBuild", { silent = true }) + end + if M.keybinds.CppRun then + vim.keymap.set("n", M.keybinds.CppRun, ":CppRun", { silent = true }) + end + if M.keybinds.NewCppHeaderFile then + vim.keymap.set("n", M.keybinds.NewCppHeaderFile, ":NewCppHeaderFile", { silent = true }) + end end function M.create_project() @@ -39,7 +32,7 @@ function M.create_project() return end - local project_root = vim.fn.expand(M.config.project_root) .. "/" .. project_name + local project_root = vim.fn.expand(M.opts.project_root) .. "/" .. project_name for _, path in ipairs({ "/src", "/build", "/include" }) do vim.fn.mkdir(project_root .. path, "p") end @@ -63,7 +56,7 @@ int main() { cmake_minimum_required(VERSION 3.10) project(%s) include_directories(include) -set(SOURCES src/main.c) +set(SOURCES src/main.cpp) add_executable(%s ${SOURCES}) set(CMAKE_EXPORT_COMPILE_COMMANDS ON) set(CMAKE_C_STANDARD 23) @@ -84,7 +77,7 @@ set(CMAKE_C_STANDARD_REQUIRED ON) "generator": "Ninja", "binaryDir": "${sourceDir}/build", "cacheVariables": { - "CMAKE_TOOLCHAIN_FILE": "$env{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake" + "CMAKE_TOOLCHAIN_FILE": "$env{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake" } } ] @@ -99,13 +92,15 @@ set(CMAKE_C_STANDARD_REQUIRED ON) "name": "default", "inherits": "vcpkg", "environment": { - "VCPKG_ROOT": "/Users//vcpkg" + "VCPKG_ROOT": "/Users//vcpkg" } } ] } ]])) + vim.fn.system(string.format("cd %s/build/ && cmake .. && cmake --build .", project_root)) + vim.cmd("edit " .. project_root .. "/src/main.cpp") end) end diff --git a/lua/Polydev/languages/html.lua b/lua/Polydev/languages/html.lua index acae369..859298c 100644 --- a/lua/Polydev/languages/html.lua +++ b/lua/Polydev/languages/html.lua @@ -1,21 +1,16 @@ -local M = {} local utils = require("Polydev.utils") +local M = {} -M.html_build = nil -M.html_run = nil - -M.config = { - project_root = "~/Projects/Html", -} +M.opts = nil function M.setup(opts) - M.config = vim.tbl_deep_extend("force", M.config, opts or {}) + M.opts = vim.tbl_deep_extend("force", {}, require("Polydev.configs").get("html"), opts or {}) end function M.create_project() vim.ui.input({ prompt = "Enter project name: " }, function(project_name) if not project_name or project_name == "" then return print("Project creation canceled.") end - local project_root = vim.fn.expand(M.config.project_root) .. "/" .. project_name + local project_root = vim.fn.expand(M.opts.project_root) .. "/" .. project_name local main_html_content = [[ @@ -31,7 +26,6 @@ function M.create_project() ]] - utils.write_file(project_root .. "index.html", main_html_content) end) end diff --git a/lua/Polydev/languages/java.lua b/lua/Polydev/languages/java.lua index f53cd2a..ededd78 100644 --- a/lua/Polydev/languages/java.lua +++ b/lua/Polydev/languages/java.lua @@ -1,36 +1,30 @@ -local M = {} local utils = require("Polydev.utils") +local M = {} -M.java_build = nil -M.java_run = nil - -M.config = { - project_root = "~/Projects/Java", - keybinds = { - ["pb"] = "JavaBuild", - ["pr"] = "JavaRun", - }, -} +M.keybinds = {} +M.opts = nil function M.setup(opts) - M.config = vim.tbl_deep_extend("force", M.config, opts or {}) - - for key, command in pairs(M.config.keybinds) do - if command == "JavaBuild" then M.java_build = key end - if command == "JavaRun" then M.java_run = key end + M.opts = vim.tbl_deep_extend("force", {}, require("Polydev.configs").get("java"), opts or {}) + for key, command in pairs(M.opts.keybinds) do + M.keybinds[command] = key end - vim.keymap.set("n", M.java_build, ":JavaBuild", { silent = true }) - vim.keymap.set("n", M.java_run, ":JavaRun", { silent = true }) - vim.api.nvim_create_user_command("JavaBuild", M.build, {}) vim.api.nvim_create_user_command("JavaRun", M.run, {}) + + if M.keybinds.JavaRun then + vim.keymap.set("n", M.keybinds.JavaRun, ":JavaRun", { silent = true }) + end + if M.keybinds.JavaBuild then + vim.keymap.set("n", M.keybinds.JavaBuild, ":JavaBuild", { silent = true }) + end end function M.create_project() vim.ui.input({ prompt = "Enter project name: " }, function(project_name) if not project_name or project_name == "" then return print("Project creation canceled.") end - local project_root = vim.fn.expand(M.config.project_root) .. "/" .. project_name + local project_root = vim.fn.expand(M.opts.project_root) .. "/" .. project_name for _, path in ipairs({ "/src", "/build" }) do vim.fn.mkdir(project_root .. path, "p") end local main_java_content = [[ @@ -40,7 +34,6 @@ public class Main { } } ]] - utils.write_file(project_root .. "/src/Main.java", main_java_content) end) end diff --git a/lua/Polydev/languages/lua.lua b/lua/Polydev/languages/lua.lua index b6c33f5..f80386d 100644 --- a/lua/Polydev/languages/lua.lua +++ b/lua/Polydev/languages/lua.lua @@ -1,12 +1,11 @@ local utils = require("Polydev.utils") -local config = require("Polydev.configs") local M = {} M.keybinds = {} M.opts = nil function M.setup(opts) - M.opts = vim.tbl_deep_extend("force", {}, config.get("lua"), opts or {}) + M.opts = vim.tbl_deep_extend("force", {}, require("Polydev.configs").get("lua"), opts or {}) for key, command in pairs(M.opts.keybinds) do M.keybinds[command] = key end @@ -29,14 +28,13 @@ function M.get_project_root() end function M.get_project_name() - local root = M.get_project_root() - if not root then return nil end - local polydev_file = vim.fn.glob(root .. "/*.polydev") + local polydev_file = vim.fn.glob((M.get_project_root() or "") .. "/*.polydev") return polydev_file:match("([^/]+)%.polydev$") end function M.create_project() - vim.ui.input({ prompt = "Enter project name: " }, function(project_name) + vim.ui.input({ prompt = "Enter project name: " }, + function(project_name) if not project_name or project_name == "" then return print("Project creation canceled.") end local project_root = vim.fn.expand(M.opts.project_root) .. "/" .. project_name for _, path in ipairs({ "/lua/" .. project_name }) do vim.fn.mkdir(project_root .. path, "p") end @@ -75,4 +73,3 @@ function M.run() end return M - diff --git a/lua/Polydev/languages/rust.lua b/lua/Polydev/languages/rust.lua index 06badf8..8cd59bb 100644 --- a/lua/Polydev/languages/rust.lua +++ b/lua/Polydev/languages/rust.lua @@ -1,29 +1,24 @@ -local M = {} local utils = require("Polydev.utils") +local M = {} -M.rust_build = nil -M.rust_run = nil - -M.config = { - project_root = "~/Projects/Rust", - keybinds = { - ["pb"] = "RustBuild", - ["pr"] = "RustRun", - }, -} +M.keybinds = {} +M.opts = nil function M.setup(opts) - M.config = vim.tbl_deep_extend("force", M.config, opts or {}) - for key, command in pairs(M.config.keybinds) do - if command == "RustBuild" then M.rust_build = key end - if command == "RustRun" then M.rust_run = key end + M.opts = vim.tbl_deep_extend("force", {}, require("Polydev.configs").get("rust"), opts or {}) + for key, command in pairs(M.opts.keybinds) do + M.keybinds[command] = key end vim.api.nvim_create_user_command("RustBuild", M.build, {}) vim.api.nvim_create_user_command("RustRun", M.run, {}) - vim.keymap.set("n", M.rust_build, ":RustBuild", { silent = true }) - vim.keymap.set("n", M.rust_run, ":RustRun", { silent = true }) + if M.keybinds.RustRun then + vim.keymap.set("n", M.keybinds.RustRun, ":RustRun", { silent = true }) + end + if M.keybinds.RustBuild then + vim.keymap.set("n", M.keybinds.RustBuild, ":RustBuild", { silent = true }) + end end function M.create_project() diff --git a/lua/Polydev/utils.lua b/lua/Polydev/utils.lua index bb0f44c..7206a48 100644 --- a/lua/Polydev/utils.lua +++ b/lua/Polydev/utils.lua @@ -1,14 +1,9 @@ -local config = require("Polydev.configs") local M = {} M.opts = {} function M.setup(opts) - -- Get the terminal options from the config (either user-config or defaults) - local terminal_opts = config.get("terminal") - - -- If no user options are passed in, merge them with the defaults from config - M.opts = vim.tbl_deep_extend("force", {}, terminal_opts, opts or {}) + M.opts = vim.tbl_deep_extend("force", {}, require("Polydev.configs").get("terminal"), opts or {}) end function M.write_file(path, content) @@ -20,19 +15,6 @@ function M.write_file(path, content) end function M.open_float_terminal(cmd) - if not M.opts.number then - print("Number is false") - end - - if M.opts.number then - print("Number is true") - end - -- Ensure M.opts is correctly set before accessing - if not M.opts.left_padding then - print("Error: Terminal padding options are missing.") - return - end - local ui = vim.api.nvim_list_uis()[1] local width = math.max(1, math.floor(ui.width * 0.9) - M.opts.left_padding or 0 - M.opts.right_padding or 0) @@ -54,15 +36,13 @@ function M.open_float_terminal(cmd) vim.api.nvim_win_set_option(win, "winblend", vim.o.pumblend) vim.api.nvim_win_set_option(win, "winhighlight", "Normal:Pmenu,FloatBorder:Pmenu") vim.api.nvim_win_set_option(win, "cursorline", true) + vim.api.nvim_win_set_option(win, "scrolloff", 5) if(M.opts.number == true) then vim.api.nvim_win_set_option(win, "number", true) if(M.opts.relativenumber == true) then vim.api.nvim_win_set_option(win, "relativenumber", true) end end - if(M.opts.scroll == true) then - vim.api.nvim_win_set_option(win, "scrolloff", 5) - end vim.fn.termopen(cmd) vim.api.nvim_buf_set_keymap(buf, "n", "", "i:q", { noremap = true, silent = true })