Update python.lua

This commit is contained in:
DarthMooMancer
2025-03-14 09:43:53 -04:00
parent ba33432491
commit 11b4dba228
+52 -40
View File
@@ -7,30 +7,42 @@ M.new_python_module_file = nil
M.config = { M.config = {
project_root = "~/Projects/Python", project_root = "~/Projects/Python",
keybinds = { keybinds = {
["<Esc>"] = "CloseTerminal", ["<Esc>"] = "CloseTerminal",
["<leader>pr"] = "PythonRun", ["<leader>pr"] = "PythonRun",
["<leader>nf"] = "NewPythonFile", ["<leader>nf"] = "NewPythonFile",
}, },
terminal = { terminal = {
right_padding = 0, right_padding = 0,
bottom_padding = 0, bottom_padding = 0,
left_padding = 0, left_padding = 0,
top_padding = 0, top_padding = 0,
border = true, border = true,
number = true, number = true,
relativenumber = true, relativenumber = true,
scroll = true, scroll = true,
} }
} }
function M.setup(opts) function M.setup(opts)
M.config = vim.tbl_deep_extend("force", M.config, opts or {}) M.config = vim.tbl_deep_extend("force", M.config, opts or {})
for key, command in pairs(M.config.keybinds) do for key, command in pairs(M.config.keybinds) do
if command == "CloseTerminal" then M.close_key = key end if command == "CloseTerminal" then M.close_key = key end
if command == "PythonRun" then M.c_run = key end if command == "PythonRun" then M.c_run = key end
if command == "NewPythonFile" then M.new_python_file = key end if command == "NewPythonFile" then M.new_python_file = key end
end
local root = M.get_project_root()
if root then
local venv_path = root .. "/venv/bin/activate"
if vim.fn.filereadable(venv_path) == 1 then
-- Activate the virtual environment
vim.env.VIRTUAL_ENV = venv_path
vim.env.PATH = root .. "/venv/bin:" .. vim.env.PATH
print("Virtual environment activated.")
else
print("No virtual environment found.")
end
end end
vim.fn.system("source ./venv/bin/activate")
vim.api.nvim_create_user_command("NewPythonFile", M.create_new_file, {}) vim.api.nvim_create_user_command("NewPythonFile", M.create_new_file, {})
vim.api.nvim_create_user_command("PythonRun", M.run, {}) vim.api.nvim_create_user_command("PythonRun", M.run, {})
@@ -98,16 +110,16 @@ end
function M.create_project() 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 if not project_name or project_name == "" then
print("Project creation canceled.") print("Project creation canceled.")
return return
end end
local project_root = vim.fn.expand(M.config.project_root) .. "/" .. project_name local project_root = vim.fn.expand(M.config.project_root) .. "/" .. project_name
vim.fn.mkdir(project_root, "p") vim.fn.mkdir(project_root, "p")
vim.fn.mkdir(project_root .. "/tests", "p") vim.fn.mkdir(project_root .. "/tests", "p")
write_file(project_root .. "/main.py", [[ write_file(project_root .. "/main.py", [[
def main(): def main():
print("Hello, World!") print("Hello, World!")
@@ -115,11 +127,11 @@ if __name__ == "__main__":
main() main()
]]) ]])
-- Set up virtual environment -- Set up virtual environment
vim.fn.system("python -m venv " .. project_root .. "/venv") vim.fn.system("python -m venv " .. project_root .. "/venv")
write_file(project_root .. "/requirements.txt", "") write_file(project_root .. "/requirements.txt", "")
write_file(project_root .. "/setup.py", string.format([[ write_file(project_root .. "/setup.py", string.format([[
from setuptools import setup, find_packages from setuptools import setup, find_packages
setup( setup(
@@ -131,27 +143,27 @@ setup(
) )
]], project_name)) ]], project_name))
vim.cmd("edit " .. project_root .. "/main.py") vim.cmd("edit " .. project_root .. "/main.py")
end) end)
end end
function M.create_new_file() function M.create_new_file()
vim.ui.input({ prompt = "Enter file name: " }, function(file_name) vim.ui.input({ prompt = "Enter file name: " }, function(file_name)
if not file_name or file_name == "" then if not file_name or file_name == "" then
return print("File creation canceled.") return print("File creation canceled.")
end end
if not file_name:match("%.py$") then if not file_name:match("%.py$") then
file_name = file_name .. ".py" file_name = file_name .. ".py"
end end
local root_dir = M.get_project_root() local root_dir = M.get_project_root()
if not root_dir then if not root_dir then
return print("Error: Project root not found.") return print("Error: Project root not found.")
end end
local file_path = root_dir .. "/" .. file_name local file_path = root_dir .. "/" .. file_name
write_file(file_path, "") write_file(file_path, "")
end) end)
end end