diff --git a/lua/Polydev/configs.lua b/lua/Polydev/configs.lua index 79d4902..a440baa 100644 --- a/lua/Polydev/configs.lua +++ b/lua/Polydev/configs.lua @@ -1,6 +1,14 @@ local M = {} M.defaults = { + python = { + project_root = "~/Projects/Python", + keybinds = { + ["pr"] = "PythonRun", + ["pb"] = "PythonPip", + ["pc"] = "PythonCustom" + }, + }, terminal = { right_padding = 0, bottom_padding = 0, @@ -11,16 +19,16 @@ M.defaults = { relativenumber = true, scroll = true, } - -- java = { indent = 4 }, - -- python = { formatter = "black", linting = true }, - -- lua = { diagnostics = true }, - -- c = { compiler = "gcc" }, - -- cpp = { compiler = "g++" }, - -- rust = { edition = "2021" }, - -- html = { use_tailwind = false }, } --- This will be updated by setup() -M.user_config = vim.deepcopy(M.defaults) +M.user_config = {} + +function M.setup(user_opts) + M.user_config = vim.tbl_deep_extend("force", {}, M.defaults, user_opts or {}) +end + +function M.get(lang) + return M.user_config[lang] or {} +end return M diff --git a/lua/Polydev/init.lua b/lua/Polydev/init.lua index 474bb57..8fbf752 100644 --- a/lua/Polydev/init.lua +++ b/lua/Polydev/init.lua @@ -49,6 +49,7 @@ end function M.setup(opts) opts = opts or {} + M.config.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/python.lua b/lua/Polydev/languages/python.lua index 48cb3d3..7f5e666 100644 --- a/lua/Polydev/languages/python.lua +++ b/lua/Polydev/languages/python.lua @@ -1,26 +1,14 @@ -local config = require("Polydev.config") local utils = require("Polydev.utils") +local config = require("Polydev.configs") local M = {} -M.c_run = nil -M.new_pip_module = nil -M.run_custom = nil - -M.config = { - project_root = "~/Projects/Python", - keybinds = { - ["pr"] = "PythonRun", - ["pb"] = "PythonPip", - ["pc"] = "PythonCustom" - }, -} +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 == "PythonRun" then M.c_run = key end - if command == "PythonPip" then M.new_pip_module = key end - if command == "PythonCustom" then M.run_custom = key end + M.opts = vim.tbl_deep_extend("force", {}, config.get("python"), opts or {}) + for key, command in pairs(M.opts.keybinds) do + M.keybinds[command] = key end local root = M.get_project_root() @@ -34,12 +22,13 @@ function M.setup(opts) vim.api.nvim_create_user_command("PythonPip", M.install_dependency, {}) vim.api.nvim_create_user_command("PythonRun", M.run, {}) - vim.api.nvim_create_user_command("PythonCustom", M.custom_run, {}) - - vim.keymap.set("n", M.c_run, ":PythonRun", { silent = true }) - vim.keymap.set("n", M.new_pip_module, ":PythonPip", { silent = true }) - vim.keymap.set("n", M.run_custom, ":PythonCustom", { silent = true }) + if M.keybinds.PythonRun then + vim.keymap.set("n", M.keybinds.PythonRun, ":PythonRun", { silent = true }) + end + if M.keybinds.PythonPip then + vim.keymap.set("n", M.keybinds.PythonPip, ":PythonPip", { silent = true }) + end end function M.get_project_root() @@ -60,7 +49,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({ "/include" }) do vim.fn.mkdir(project_root .. path, "p") end @@ -99,25 +88,23 @@ setup( end function M.create_new_file() - local opts = config.user_config.python - print("Creating Python project with formatter: " .. opts.formatter) - -- vim.ui.input({ prompt = "Enter file name: " }, function(file_name) - -- if not file_name or file_name == "" then - -- return print("File creation canceled.") - -- end - -- - -- if not file_name:match("%.py$") then - -- file_name = file_name .. ".py" - -- end - -- - -- local root_dir = M.get_project_root() - -- if not root_dir then - -- return print("Error: Project root not found.") - -- end - -- - -- local file_path = root_dir .. "/include/" .. file_name - -- utils.write_file(file_path, "") - -- end) + vim.ui.input({ prompt = "Enter file name: " }, function(file_name) + if not file_name or file_name == "" then + return print("File creation canceled.") + end + + if not file_name:match("%.py$") then + file_name = file_name .. ".py" + end + + local root_dir = M.get_project_root() + if not root_dir then + return print("Error: Project root not found.") + end + + local file_path = root_dir .. "/include/" .. file_name + utils.write_file(file_path, "") + end) end function M.install_dependency() @@ -130,38 +117,15 @@ function M.install_dependency() end) end -function M.custom_run() - vim.ui.input({ prompt = "Enter File Name: " }, function(custom_file) - if not custom_file or custom_file == "" then - return print("Run canceled") - end - - local root = M.get_project_root() - -- Detect current shell - local shell = vim.fn.getenv("SHELL") or "" - local venv_cmd - - if shell:match("fish") then - local venv_fish = root .. "/venv/bin/activate.fish" - venv_cmd = vim.fn.filereadable(venv_fish) == 1 and "source " .. venv_fish .. " && " or "" - else - local venv_bash = root .. "/venv/bin/activate" - venv_cmd = vim.fn.filereadable(venv_bash) == 1 and "source " .. venv_bash .. " && " or "" - end - - utils.open_float_terminal(venv_cmd .. "python3 " .. custom_file .. ".py") - end) -end - function M.run() local root = M.get_project_root() if not root then - return print("Error: Project root not found.") + return print("Error: Project root not found.") end local main_file = root .. "/main.py" if vim.fn.filereadable(main_file) == 0 then - return print("Error: main.py not found in project root.") + return print("Error: main.py not found in project root.") end -- Detect current shell @@ -169,11 +133,11 @@ function M.run() local venv_cmd if shell:match("fish") then - local venv_fish = root .. "/venv/bin/activate.fish" - venv_cmd = vim.fn.filereadable(venv_fish) == 1 and "source " .. venv_fish .. " && " or "" + local venv_fish = root .. "/venv/bin/activate.fish" + venv_cmd = vim.fn.filereadable(venv_fish) == 1 and "source " .. venv_fish .. " && " or "" else - local venv_bash = root .. "/venv/bin/activate" - venv_cmd = vim.fn.filereadable(venv_bash) == 1 and "source " .. venv_bash .. " && " or "" + local venv_bash = root .. "/venv/bin/activate" + venv_cmd = vim.fn.filereadable(venv_bash) == 1 and "source " .. venv_bash .. " && " or "" end utils.open_float_terminal(venv_cmd .. "python3 " .. main_file) diff --git a/lua/Polydev/utils.lua b/lua/Polydev/utils.lua index 63e69ff..eae8966 100644 --- a/lua/Polydev/utils.lua +++ b/lua/Polydev/utils.lua @@ -1,5 +1,6 @@ local M = {} local term_cfg = require("Polydev.configs").defaults.terminal +local config = require("Polydev.configs") function M.write_file(path, content) local file = assert(io.open(path, "w"), "Error creating file: " .. path)