Got python.lua to finally import opts propery
This commit is contained in:
+17
-9
@@ -1,6 +1,14 @@
|
|||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
M.defaults = {
|
M.defaults = {
|
||||||
|
python = {
|
||||||
|
project_root = "~/Projects/Python",
|
||||||
|
keybinds = {
|
||||||
|
["<leader>pr"] = "PythonRun",
|
||||||
|
["<leader>pb"] = "PythonPip",
|
||||||
|
["<leader>pc"] = "PythonCustom"
|
||||||
|
},
|
||||||
|
},
|
||||||
terminal = {
|
terminal = {
|
||||||
right_padding = 0,
|
right_padding = 0,
|
||||||
bottom_padding = 0,
|
bottom_padding = 0,
|
||||||
@@ -11,16 +19,16 @@ M.defaults = {
|
|||||||
relativenumber = true,
|
relativenumber = true,
|
||||||
scroll = 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 = {}
|
||||||
M.user_config = vim.deepcopy(M.defaults)
|
|
||||||
|
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
|
return M
|
||||||
|
|||||||
@@ -49,6 +49,7 @@ end
|
|||||||
|
|
||||||
function M.setup(opts)
|
function M.setup(opts)
|
||||||
opts = opts or {}
|
opts = opts or {}
|
||||||
|
M.config.setup(opts)
|
||||||
for lang, user_opts in pairs(opts) do
|
for lang, user_opts in pairs(opts) do
|
||||||
local default = M.config.defaults[lang] or {}
|
local default = M.config.defaults[lang] or {}
|
||||||
M.config.user_config[lang] = vim.tbl_deep_extend("force", default, user_opts)
|
M.config.user_config[lang] = vim.tbl_deep_extend("force", default, user_opts)
|
||||||
|
|||||||
@@ -1,26 +1,14 @@
|
|||||||
local config = require("Polydev.config")
|
|
||||||
local utils = require("Polydev.utils")
|
local utils = require("Polydev.utils")
|
||||||
|
local config = require("Polydev.configs")
|
||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
M.c_run = nil
|
M.keybinds = {}
|
||||||
M.new_pip_module = nil
|
M.opts = nil
|
||||||
M.run_custom = nil
|
|
||||||
|
|
||||||
M.config = {
|
|
||||||
project_root = "~/Projects/Python",
|
|
||||||
keybinds = {
|
|
||||||
["<leader>pr"] = "PythonRun",
|
|
||||||
["<leader>pb"] = "PythonPip",
|
|
||||||
["<leader>pc"] = "PythonCustom"
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
function M.setup(opts)
|
function M.setup(opts)
|
||||||
M.config = vim.tbl_deep_extend("force", M.config, opts or {})
|
M.opts = vim.tbl_deep_extend("force", {}, config.get("python"), opts or {})
|
||||||
for key, command in pairs(M.config.keybinds) do
|
for key, command in pairs(M.opts.keybinds) do
|
||||||
if command == "PythonRun" then M.c_run = key end
|
M.keybinds[command] = key
|
||||||
if command == "PythonPip" then M.new_pip_module = key end
|
|
||||||
if command == "PythonCustom" then M.run_custom = key end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local root = M.get_project_root()
|
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("PythonPip", M.install_dependency, {})
|
||||||
vim.api.nvim_create_user_command("PythonRun", M.run, {})
|
vim.api.nvim_create_user_command("PythonRun", M.run, {})
|
||||||
vim.api.nvim_create_user_command("PythonCustom", M.custom_run, {})
|
|
||||||
|
|
||||||
|
if M.keybinds.PythonRun then
|
||||||
vim.keymap.set("n", M.c_run, ":PythonRun<CR>", { silent = true })
|
vim.keymap.set("n", M.keybinds.PythonRun, ":PythonRun<CR>", { silent = true })
|
||||||
vim.keymap.set("n", M.new_pip_module, ":PythonPip<CR>", { silent = true })
|
end
|
||||||
vim.keymap.set("n", M.run_custom, ":PythonCustom<CR>", { silent = true })
|
if M.keybinds.PythonPip then
|
||||||
|
vim.keymap.set("n", M.keybinds.PythonPip, ":PythonPip<CR>", { silent = true })
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.get_project_root()
|
function M.get_project_root()
|
||||||
@@ -60,7 +49,7 @@ function M.create_project()
|
|||||||
return
|
return
|
||||||
end
|
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
|
for _, path in ipairs({ "/include" }) do
|
||||||
vim.fn.mkdir(project_root .. path, "p")
|
vim.fn.mkdir(project_root .. path, "p")
|
||||||
end
|
end
|
||||||
@@ -99,25 +88,23 @@ setup(
|
|||||||
end
|
end
|
||||||
|
|
||||||
function M.create_new_file()
|
function M.create_new_file()
|
||||||
local opts = config.user_config.python
|
vim.ui.input({ prompt = "Enter file name: " }, function(file_name)
|
||||||
print("Creating Python project with formatter: " .. opts.formatter)
|
if not file_name or file_name == "" then
|
||||||
-- vim.ui.input({ prompt = "Enter file name: " }, function(file_name)
|
return print("File creation canceled.")
|
||||||
-- if not file_name or file_name == "" then
|
end
|
||||||
-- return print("File creation canceled.")
|
|
||||||
-- end
|
if not file_name:match("%.py$") then
|
||||||
--
|
file_name = file_name .. ".py"
|
||||||
-- if not file_name:match("%.py$") then
|
end
|
||||||
-- file_name = file_name .. ".py"
|
|
||||||
-- end
|
local root_dir = M.get_project_root()
|
||||||
--
|
if not root_dir then
|
||||||
-- local root_dir = M.get_project_root()
|
return print("Error: Project root not found.")
|
||||||
-- if not root_dir then
|
end
|
||||||
-- return print("Error: Project root not found.")
|
|
||||||
-- end
|
local file_path = root_dir .. "/include/" .. file_name
|
||||||
--
|
utils.write_file(file_path, "")
|
||||||
-- local file_path = root_dir .. "/include/" .. file_name
|
end)
|
||||||
-- utils.write_file(file_path, "")
|
|
||||||
-- end)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.install_dependency()
|
function M.install_dependency()
|
||||||
@@ -130,29 +117,6 @@ function M.install_dependency()
|
|||||||
end)
|
end)
|
||||||
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()
|
function M.run()
|
||||||
local root = M.get_project_root()
|
local root = M.get_project_root()
|
||||||
if not root then
|
if not root then
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
local M = {}
|
local M = {}
|
||||||
local term_cfg = require("Polydev.configs").defaults.terminal
|
local term_cfg = require("Polydev.configs").defaults.terminal
|
||||||
|
local config = require("Polydev.configs")
|
||||||
|
|
||||||
function M.write_file(path, content)
|
function M.write_file(path, content)
|
||||||
local file = assert(io.open(path, "w"), "Error creating file: " .. path)
|
local file = assert(io.open(path, "w"), "Error creating file: " .. path)
|
||||||
|
|||||||
Reference in New Issue
Block a user