Update python.lua
This commit is contained in:
+34
-48
@@ -32,7 +32,6 @@ function M.setup(opts)
|
|||||||
if command == "NewPythonFile" then M.new_python_file = key end
|
if command == "NewPythonFile" then M.new_python_file = key end
|
||||||
if command == "NewPythonModuleFile" then M.new_python_module_file = key end
|
if command == "NewPythonModuleFile" then M.new_python_module_file = key end
|
||||||
end
|
end
|
||||||
vim.fn.system("source ./venv/bin/activate")
|
|
||||||
|
|
||||||
vim.api.nvim_create_user_command("NewPythonModuleFile", M.create_new_module_file, {})
|
vim.api.nvim_create_user_command("NewPythonModuleFile", M.create_new_module_file, {})
|
||||||
vim.api.nvim_create_user_command("NewPythonFile", M.create_new_file, {})
|
vim.api.nvim_create_user_command("NewPythonFile", M.create_new_file, {})
|
||||||
@@ -47,10 +46,10 @@ function M.open_float_terminal(cmd)
|
|||||||
local ui = vim.api.nvim_list_uis()[1]
|
local ui = vim.api.nvim_list_uis()[1]
|
||||||
local term_cfg = M.config.terminal
|
local term_cfg = M.config.terminal
|
||||||
|
|
||||||
local width = math.max(1, math.floor(ui.width * 0.9) - term_cfg.left_padding - term_cfg.right_padding)
|
local width = math.floor(ui.width * 0.9) - term_cfg.left_padding - term_cfg.right_padding
|
||||||
local height = math.max(1, math.floor(ui.height * 0.9) - term_cfg.top_padding - (term_cfg.bottom_padding + 1))
|
local height = math.floor(ui.height * 0.9) - term_cfg.top_padding - term_cfg.bottom_padding
|
||||||
local row = math.max(1, math.floor((ui.height - height) / 2) + term_cfg.top_padding)
|
local row = math.floor((ui.height - height) / 2) + term_cfg.top_padding
|
||||||
local col = math.max(1, math.floor((ui.width - width) / 2) + term_cfg.left_padding)
|
local col = math.floor((ui.width - width) / 2) + term_cfg.left_padding
|
||||||
|
|
||||||
local buf = vim.api.nvim_create_buf(false, true)
|
local buf = vim.api.nvim_create_buf(false, true)
|
||||||
local win = vim.api.nvim_open_win(buf, true, {
|
local win = vim.api.nvim_open_win(buf, true, {
|
||||||
@@ -63,22 +62,19 @@ function M.open_float_terminal(cmd)
|
|||||||
border = term_cfg.border and "rounded" or "none",
|
border = term_cfg.border and "rounded" or "none",
|
||||||
})
|
})
|
||||||
|
|
||||||
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, "cursorline", true)
|
||||||
if(term_cfg.number == true) then
|
if term_cfg.number then
|
||||||
vim.api.nvim_win_set_option(win, "number", true)
|
vim.api.nvim_win_set_option(win, "number", true)
|
||||||
if(term_cfg.relativenumber == true) then
|
if term_cfg.relativenumber then
|
||||||
vim.api.nvim_win_set_option(win, "relativenumber", true)
|
vim.api.nvim_win_set_option(win, "relativenumber", true)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
if(term_cfg.scroll == true) then
|
if term_cfg.scroll then
|
||||||
vim.api.nvim_win_set_option(win, "scrolloff", 5)
|
vim.api.nvim_win_set_option(win, "scrolloff", 5)
|
||||||
end
|
end
|
||||||
|
|
||||||
vim.fn.termopen(cmd)
|
vim.fn.termopen(cmd)
|
||||||
vim.api.nvim_buf_set_keymap(buf, "n", M.close_key, "i<C-\\><C-n>:q<CR>", { noremap = true, silent = true })
|
vim.api.nvim_buf_set_keymap(buf, "n", M.close_key, "i<C-\\><C-n>:q<CR>", { noremap = true, silent = true })
|
||||||
return buf, win
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local function write_file(path, content)
|
local function write_file(path, content)
|
||||||
@@ -97,12 +93,9 @@ function M.create_project()
|
|||||||
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
|
||||||
for _, path in ipairs({ "/" .. project_name, "/tests" }) do
|
vim.fn.mkdir(project_root, "p")
|
||||||
vim.fn.mkdir(project_root .. path, "p")
|
vim.fn.mkdir(project_root .. "/tests", "p")
|
||||||
end
|
|
||||||
|
|
||||||
write_file(project_root .. "/" .. project_name .. "/__init__.py", "")
|
|
||||||
write_file(project_root .. "/" .. project_name .. ".polydev", project_name)
|
|
||||||
write_file(project_root .. "/main.py", [[
|
write_file(project_root .. "/main.py", [[
|
||||||
def main():
|
def main():
|
||||||
print("Hello, World!")
|
print("Hello, World!")
|
||||||
@@ -111,34 +104,21 @@ if __name__ == "__main__":
|
|||||||
main()
|
main()
|
||||||
]])
|
]])
|
||||||
|
|
||||||
vim.fn.system("python -m venv " .. project_root .. "/venv && source " .. project_root .. "/venv/bin/activate")
|
-- Set up virtual environment
|
||||||
|
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(
|
||||||
name="%s", # Name of the project
|
name="%s",
|
||||||
version="0.1.0", # Version of the project
|
version="0.1.0",
|
||||||
packages=find_packages(), # Automatically find packages
|
packages=find_packages(),
|
||||||
install_requires=[ # List of dependencies
|
install_requires=[],
|
||||||
"requests>=2.25.1", # Example dependency
|
python_requires=">=3.6",
|
||||||
"numpy>=1.20.0", # Another example dependency
|
|
||||||
],
|
|
||||||
author="Your Name", # Author's name
|
|
||||||
author_email="your.email@example.com", # Author's email
|
|
||||||
description="A simple Python project", # Short description
|
|
||||||
long_description=open('README.md').read(), # Long description from README
|
|
||||||
long_description_content_type="text/markdown", # Format of long description
|
|
||||||
url="https://github.com/yourname/%s", # URL for the project repository
|
|
||||||
classifiers=[ # Some additional metadata
|
|
||||||
"Programming Language :: Python :: 3",
|
|
||||||
"License :: OSI Approved :: MIT License",
|
|
||||||
"Operating System :: OS Independent",
|
|
||||||
],
|
|
||||||
python_requires=">=3.6", # Minimum Python version required
|
|
||||||
)
|
)
|
||||||
]], project_name, project_name))
|
]], project_name))
|
||||||
|
|
||||||
vim.cmd("edit " .. project_root .. "/main.py")
|
vim.cmd("edit " .. project_root .. "/main.py")
|
||||||
end)
|
end)
|
||||||
@@ -150,7 +130,6 @@ function M.create_new_file()
|
|||||||
return print("File creation canceled.")
|
return print("File creation canceled.")
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Ensure filename has .py extension
|
|
||||||
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
|
||||||
@@ -160,10 +139,7 @@ function M.create_new_file()
|
|||||||
return print("Error: Project root not found.")
|
return print("Error: Project root not found.")
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Extract project name from root directory
|
local file_path = root_dir .. "/" .. file_name
|
||||||
local project_name = vim.fn.fnamemodify(root_dir, ":t")
|
|
||||||
local file_path = root_dir .. "/" .. project_name .. "/" .. file_name
|
|
||||||
|
|
||||||
write_file(file_path, "")
|
write_file(file_path, "")
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
@@ -171,20 +147,30 @@ end
|
|||||||
function M.get_project_root()
|
function M.get_project_root()
|
||||||
local dir = vim.fn.expand("%:p:h")
|
local dir = vim.fn.expand("%:p:h")
|
||||||
while dir ~= "/" do
|
while dir ~= "/" do
|
||||||
if vim.fn.isdirectory(dir .. "/src") == 1 or vim.fn.filereadable(dir .. "/*.polydev") == 1 then
|
if vim.fn.isdirectory(dir .. "/venv") == 1 or vim.fn.filereadable(dir .. "/main.py") == 1 then
|
||||||
return dir
|
return dir
|
||||||
end
|
end
|
||||||
dir = vim.fn.fnamemodify(dir, ":h")
|
dir = vim.fn.fnamemodify(dir, ":h")
|
||||||
end
|
end
|
||||||
|
return nil
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.run()
|
function M.run()
|
||||||
local root = M.get_project_root()
|
local root = M.get_project_root()
|
||||||
if not root then return print("Error: Project root not found.") end
|
if not root then
|
||||||
local build_dir = root .. "/build"
|
return print("Error: Project root not found.")
|
||||||
local files = vim.fn.glob(build_dir .. "/*.polydev", true, true)
|
end
|
||||||
if #files == 0 then return print("Error: No .polydev file found in build directory.") end
|
|
||||||
M.open_float_terminal("cd " .. build_dir .. " && ./" .. files[1]:match("([^/]+)%.polydev$"))
|
local main_file = root .. "/main.py"
|
||||||
|
if vim.fn.filereadable(main_file) == 0 then
|
||||||
|
return print("Error: main.py not found in project root.")
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Check if virtual environment exists
|
||||||
|
local venv_path = root .. "/venv/bin/activate"
|
||||||
|
local venv_cmd = vim.fn.filereadable(venv_path) == 1 and "source " .. venv_path .. " && " or ""
|
||||||
|
|
||||||
|
M.open_float_terminal(venv_cmd .. "python " .. main_file)
|
||||||
end
|
end
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
|||||||
Reference in New Issue
Block a user