From e6c3353b512775064d7950d333a604158ee12ede Mon Sep 17 00:00:00 2001 From: DarthMooMancer <107283522+DarthMooMancer@users.noreply.github.com> Date: Sat, 17 May 2025 18:33:55 -0400 Subject: [PATCH] feat(utils): added ability to have floating or split windows and anchors of where each are fix(ui): also changed deprecated features to use newer features by neovim --- docs/setup.md | 25 ++++++++-- lua/Polydev/configs.lua | 5 +- lua/Polydev/presets.lua | 4 +- lua/Polydev/ui.lua | 4 +- lua/Polydev/utils.lua | 106 ++++++++++++++++++++++++---------------- 5 files changed, 93 insertions(+), 51 deletions(-) diff --git a/docs/setup.md b/docs/setup.md index 426fdd3..d551c90 100644 --- a/docs/setup.md +++ b/docs/setup.md @@ -114,14 +114,31 @@ cpp = { ["pr"] = "CppRun", } } - ``` ## **Terminal Configuration** ```lua --- In Neovim 0.11+, numbers don't work so they will be reimplemented later terminal = { - preset = nil, -- also try : ("right", "corner") - border = true + -- floating or split + mode = "split", + win = { + -- type: "vertical" or "horizonal" -- Only for split windows + -- anchor: "bottom" or "right" or "center" or "corner" + -- "bottom" and "right" are only split anchors + -- "center" and "corner" are only floating anchors + type = "vertical", anchor = "right" + }, + border = { + enabled = true, + -- type: border decoration, follows `winborder` + -- "bold": Bold line box. + -- "double": Double-line box. + -- "none": No border. + -- "rounded": Like "single", but with rounded corners ("╭" etc.). + -- "shadow": Drop shadow effect, by blending with the background. + -- "single": Single-line box. + -- "solid": Adds padding by a single whitespace cell. + type = "rounded" + } } ``` diff --git a/lua/Polydev/configs.lua b/lua/Polydev/configs.lua index e60fc78..e10eb33 100644 --- a/lua/Polydev/configs.lua +++ b/lua/Polydev/configs.lua @@ -53,7 +53,10 @@ function M.setup(user_opts) }, }, terminal = { - preset = nil, + mode = "split", -- floating or split + win = { + type = "vertical", anchor = "right" -- type: "vertical" or "horizonal" -- anchor: "below" or "right" or "center" or "corner" + }, border = { enabled = true, type = "rounded", diff --git a/lua/Polydev/presets.lua b/lua/Polydev/presets.lua index 855eb95..5c1658f 100644 --- a/lua/Polydev/presets.lua +++ b/lua/Polydev/presets.lua @@ -29,7 +29,9 @@ local presets = { ---@param preset string ---@return table|nil function M.getPresets(preset) - return presets[preset] + if presets[preset] then + return presets[preset] + end end return M diff --git a/lua/Polydev/ui.lua b/lua/Polydev/ui.lua index e1bf0a9..df54006 100644 --- a/lua/Polydev/ui.lua +++ b/lua/Polydev/ui.lua @@ -4,10 +4,10 @@ local M = {} ---@return table local function get_entries(path) local result = {} - local handle = vim.loop.fs_scandir(path) + local handle = vim.uv.fs_scandir(path) if not handle then return result end while true do - local name, type = vim.loop.fs_scandir_next(handle) + local name, type = vim.uv.fs_scandir_next(handle) if not name then break end -- Check if this is a project file local is_project = false diff --git a/lua/Polydev/utils.lua b/lua/Polydev/utils.lua index acb34ca..415abb5 100644 --- a/lua/Polydev/utils.lua +++ b/lua/Polydev/utils.lua @@ -5,13 +5,6 @@ function M.setup(opts) M.opts = vim.tbl_deep_extend("force", {}, require("Polydev.configs").get("terminal"), opts or {}) end ----@param path string ----@return boolean|nil -function M.is_dir(path) - local stat = vim.loop.fs_stat(path) - return stat and stat.type == "directory" -end - ---@return string function M.get_project_root() local plugin_dir = vim.fn.fnamemodify(debug.getinfo(1, "S").source:sub(2), ":h:h:h") @@ -50,46 +43,73 @@ function M.init_git(path, gitignore_lines) end ---@param cmd table ----@return integer, integer +---@return integer, integer? function M.terminal(cmd) - local opts = require("Polydev.presets").getPresets(M.opts.preset) - local ui = vim.api.nvim_list_uis()[1] - local width - local height - local row - local col + if M.opts.mode == "floating" then + local opts = require("Polydev.presets").getPresets(M.opts.win.anchor) - if opts ~= nil then - width = math.max(1, math.floor(ui.width * 0.9) - opts.left_padding - opts.right_padding) - height = math.max(1, math.floor(ui.height * 0.9) - opts.top_padding - opts.bottom_padding) - row = math.floor((ui.height - height) / 2) + opts.top_padding - col = math.floor((ui.width - width) / 2) + opts.left_padding + local ui = vim.api.nvim_list_uis()[1] + local width + local height + local row + local col + + if opts ~= nil then + width = math.max(1, math.floor(ui.width * 0.9) - opts.left_padding - opts.right_padding) + height = math.max(1, math.floor(ui.height * 0.9) - opts.top_padding - opts.bottom_padding) + row = math.floor((ui.height - height) / 2) + opts.top_padding + col = math.floor((ui.width - width) / 2) + opts.left_padding + end + if opts == nil then + width = math.max(1, math.floor(ui.width * 0.9)) + height = math.max(1, math.floor(ui.height * 0.9)) + row = math.floor((ui.height - height) / 2) + col = math.floor((ui.width - width) / 2) + end + local buf = vim.api.nvim_create_buf(false, true) + local win = vim.api.nvim_open_win(buf, true, { + relative = "editor", + width = width, + height = height, + row = row, + col = col, + style = "minimal", + border = M.opts.border.enabled and M.opts.border.type, + }) + vim.api.nvim_set_option_value("winblend", vim.o.pumblend, { win = win }) + vim.api.nvim_set_option_value("winhighlight", "Normal:PolydevNormal,FloatBorder:PolydevBorder", { win = win }) + vim.api.nvim_set_option_value("cursorline", true, { win = win }) + vim.api.nvim_set_option_value("scrolloff", 5, { win = win }) + vim.fn.jobstart(table.concat(cmd, "/"), { term = true }) + vim.api.nvim_buf_set_keymap(buf, "n", "", "i:q", { noremap = true, silent = true }) + return buf, win end - if opts == nil then - width = math.max(1, math.floor(ui.width * 0.9)) - height = math.max(1, math.floor(ui.height * 0.9)) - row = math.floor((ui.height - height) / 2) - col = math.floor((ui.width - width) / 2) + if M.opts.mode == "split" then + if M.opts.win.type == "vertical" then + if M.opts.win.anchor == "right" then + vim.cmd("set splitright") + end + end + if M.opts.win.type == "horizontal" then + if M.opts.anchor == "bottom" then + vim.cmd("set splitbelow") + end + end + + if M.opts.win.type == "vertical" then + vim.cmd("vsplit") + end + if M.opts.win.type == "horizontal" then + vim.cmd("split") + end + local buf = vim.api.nvim_create_buf(false, true) + vim.api.nvim_win_set_buf(0, buf) + + vim.fn.jobstart(table.concat(cmd, "/"), { term = true }) + vim.api.nvim_buf_set_keymap(buf, "n", "", "i:q", { noremap = true, silent = true }) + return buf end - - local buf = vim.api.nvim_create_buf(false, true) - local win = vim.api.nvim_open_win(buf, true, { - relative = "editor", - width = width, - height = height, - row = row, - col = col, - style = "minimal", - border = M.opts.border.enabled and M.opts.border.type, - }) - vim.api.nvim_set_option_value("winblend", vim.o.pumblend, { win = win }) - vim.api.nvim_set_option_value("winhighlight", "Normal:Pmenu,FloatBorder:Pmenu", { win = win }) - vim.api.nvim_set_option_value("cursorline", true, { win = win }) - vim.api.nvim_set_option_value("scrolloff", 5, { win = win }) - - vim.fn.jobstart(table.concat(cmd, "/"), { term = true }) - vim.api.nvim_buf_set_keymap(buf, "n", "", "i:q", { noremap = true, silent = true }) - return buf, win + return 0 end return M