From ad9d3d19b16dae67a2c6b733f9dbed786b012b3b Mon Sep 17 00:00:00 2001 From: Folke Lemaitre Date: Sun, 3 Nov 2024 23:03:19 +0100 Subject: [PATCH] feat(snacks): use terminal --- lua/lazyvim/config/keymaps.lua | 14 ++--- lua/lazyvim/plugins/extras/ui/edgy.lua | 25 +++++--- .../plugins/extras/ui/mini-indentscope.lua | 3 +- lua/lazyvim/plugins/extras/util/gitui.lua | 4 +- lua/lazyvim/plugins/init.lua | 29 +++++++++- lua/lazyvim/plugins/ui.lua | 3 +- lua/lazyvim/util/lazygit.lua | 15 +++-- lua/lazyvim/util/news.lua | 16 +++--- lua/lazyvim/util/terminal.lua | 57 ++----------------- 9 files changed, 77 insertions(+), 89 deletions(-) diff --git a/lua/lazyvim/config/keymaps.lua b/lua/lazyvim/config/keymaps.lua index cc226964..2970e4aa 100644 --- a/lua/lazyvim/config/keymaps.lua +++ b/lua/lazyvim/config/keymaps.lua @@ -158,18 +158,12 @@ map("n", "uI", "InspectTree", { desc = "Inspect Tree" }) map("n", "L", function() LazyVim.news.changelog() end, { desc = "LazyVim Changelog" }) -- floating terminal -local lazyterm = function() LazyVim.terminal(nil, { cwd = LazyVim.root() }) end -map("n", "ft", lazyterm, { desc = "Terminal (Root Dir)" }) -map("n", "fT", function() LazyVim.terminal() end, { desc = "Terminal (cwd)" }) -map("n", "", lazyterm, { desc = "Terminal (Root Dir)" }) -map("n", "", lazyterm, { desc = "which_key_ignore" }) +map("n", "fT", function() Snacks.terminal() end, { desc = "Terminal (cwd)" }) +map("n", "ft", function() Snacks.terminal(nil, { cwd = LazyVim.root() }) end, { desc = "Terminal (Root Dir)" }) +map("n", "", function() Snacks.terminal(nil, { cwd = LazyVim.root() }) end, { desc = "Terminal (Root Dir)" }) +map("n", "", function() Snacks.terminal(nil, { cwd = LazyVim.root() }) end, { desc = "which_key_ignore" }) -- Terminal Mappings -map("t", "", "", { desc = "Enter Normal Mode" }) -map("t", "", "wincmd h", { desc = "Go to Left Window" }) -map("t", "", "wincmd j", { desc = "Go to Lower Window" }) -map("t", "", "wincmd k", { desc = "Go to Upper Window" }) -map("t", "", "wincmd l", { desc = "Go to Right Window" }) map("t", "", "close", { desc = "Hide Terminal" }) map("t", "", "close", { desc = "which_key_ignore" }) diff --git a/lua/lazyvim/plugins/extras/ui/edgy.lua b/lua/lazyvim/plugins/extras/ui/edgy.lua index f63e31ad..faecb3b4 100644 --- a/lua/lazyvim/plugins/extras/ui/edgy.lua +++ b/lua/lazyvim/plugins/extras/ui/edgy.lua @@ -31,14 +31,6 @@ return { return vim.api.nvim_win_get_config(win).relative == "" end, }, - { - ft = "lazyterm", - title = "LazyTerm", - size = { height = 0.4 }, - filter = function(buf) - return not vim.b[buf].lazyterm_cmd - end, - }, "Trouble", { ft = "qf", title = "QuickFix" }, { @@ -103,6 +95,7 @@ return { end end + -- trouble for _, pos in ipairs({ "top", "bottom", "left", "right" }) do opts[pos] = opts[pos] or {} table.insert(opts[pos], { @@ -116,6 +109,22 @@ return { end, }) end + + -- snacks float + for _, pos in ipairs({ "top", "bottom", "left", "right" }) do + opts[pos] = opts[pos] or {} + table.insert(opts[pos], { + ft = "snacks_terminal", + size = { height = 0.4 }, + title = "%{b:snacks_terminal.id}: %{b:term_title}", + filter = function(_buf, win) + return vim.w[win].snacks_float + and vim.w[win].snacks_float.position == pos + and vim.w[win].snacks_float.relative == "editor" + and not vim.w[win].trouble_preview + end, + }) + end return opts end, }, diff --git a/lua/lazyvim/plugins/extras/ui/mini-indentscope.lua b/lua/lazyvim/plugins/extras/ui/mini-indentscope.lua index 1b8beeb6..5920079d 100644 --- a/lua/lazyvim/plugins/extras/ui/mini-indentscope.lua +++ b/lua/lazyvim/plugins/extras/ui/mini-indentscope.lua @@ -19,7 +19,8 @@ return { "fzf", "help", "lazy", - "lazyterm", + "snacks_terminal", + "snacks_float", "mason", "neo-tree", "notify", diff --git a/lua/lazyvim/plugins/extras/util/gitui.lua b/lua/lazyvim/plugins/extras/util/gitui.lua index b5033235..e0dd6049 100644 --- a/lua/lazyvim/plugins/extras/util/gitui.lua +++ b/lua/lazyvim/plugins/extras/util/gitui.lua @@ -8,14 +8,14 @@ return { { "gG", function() - LazyVim.terminal.open({ "gitui" }, { esc_esc = false, ctrl_hjkl = false }) + Snacks.terminal({ "gitui" }) end, desc = "GitUi (cwd)", }, { "gg", function() - LazyVim.terminal.open({ "gitui" }, { cwd = LazyVim.root.get(), esc_esc = false, ctrl_hjkl = false }) + Snacks.terminal({ "gitui" }, { cwd = LazyVim.root.get() }) end, desc = "GitUi (Root Dir)", }, diff --git a/lua/lazyvim/plugins/init.lua b/lua/lazyvim/plugins/init.lua index 0a849c1b..1e672afa 100644 --- a/lua/lazyvim/plugins/init.lua +++ b/lua/lazyvim/plugins/init.lua @@ -10,8 +10,35 @@ end require("lazyvim.config").init() +-- Terminal Mappings +local function term_nav(dir) + ---@param self snacks.terminal + return function(self) + return self:is_floating() and "" or vim.schedule(function() + vim.cmd.wincmd(dir) + end) + end +end + return { { "folke/lazy.nvim", version = "*" }, { "LazyVim/LazyVim", priority = 10000, lazy = false, opts = {}, cond = true, version = "*" }, - { "folke/snacks.nvim", lazy = false, opts = {} }, + { + "folke/snacks.nvim", + priority = 1000, + lazy = false, + ---@type snacks.Config + opts = { + terminal = { + float = { + keys = { + nav_h = { "", term_nav("h"), desc = "Go to Left Window", expr = true, mode = "t" }, + nav_j = { "", term_nav("j"), desc = "Go to Lower Window", expr = true, mode = "t" }, + nav_k = { "", term_nav("k"), desc = "Go to Upper Window", expr = true, mode = "t" }, + nav_l = { "", term_nav("l"), desc = "Go to Right Window", expr = true, mode = "t" }, + }, + }, + }, + }, + }, } diff --git a/lua/lazyvim/plugins/ui.lua b/lua/lazyvim/plugins/ui.lua index 14807497..b349665c 100644 --- a/lua/lazyvim/plugins/ui.lua +++ b/lua/lazyvim/plugins/ui.lua @@ -254,7 +254,8 @@ return { "mason", "notify", "toggleterm", - "lazyterm", + "snacks_float", + "snacks_terminal", }, }, } diff --git a/lua/lazyvim/util/lazygit.lua b/lua/lazyvim/util/lazygit.lua index ff96c85c..ca04dc77 100644 --- a/lua/lazyvim/util/lazygit.lua +++ b/lua/lazyvim/util/lazygit.lua @@ -148,19 +148,22 @@ end function M.blame_line(opts) opts = vim.tbl_deep_extend("force", { count = 3, - filetype = "git", - size = { - width = 0.6, - height = 0.6, + interactive = false, + float = { + win = { + width = 0.6, + height = 0.6, + border = "rounded", + }, + bo = { filetype = "git" }, }, - border = "rounded", }, opts or {}) local cursor = vim.api.nvim_win_get_cursor(0) local line = cursor[1] local file = vim.api.nvim_buf_get_name(0) local root = LazyVim.root.detectors.pattern(0, { ".git" })[1] or "." local cmd = { "git", "-C", root, "log", "-n", opts.count, "-u", "-L", line .. ",+1:" .. file } - return require("lazy.util").float_cmd(cmd, opts) + return Snacks.terminal(cmd, opts) end -- stylua: ignore diff --git a/lua/lazyvim/util/news.lua b/lua/lazyvim/util/news.lua index 5fb4a08c..2cf7460e 100644 --- a/lua/lazyvim/util/news.lua +++ b/lua/lazyvim/util/news.lua @@ -72,15 +72,17 @@ function M.open(file, opts) end end - local float = require("lazy.util").float({ + local float = Snacks.float({ file = file, - size = { width = 0.6, height = 0.6 }, + win = { width = 0.6, height = 0.6 }, + wo = { + spell = false, + wrap = false, + signcolumn = "yes", + statuscolumn = " ", + conceallevel = 3, + }, }) - vim.opt_local.spell = false - vim.opt_local.wrap = false - vim.opt_local.signcolumn = "yes" - vim.opt_local.statuscolumn = " " - vim.opt_local.conceallevel = 3 if vim.diagnostic.enable then pcall(vim.diagnostic.enable, false, { bufnr = float.buf }) else diff --git a/lua/lazyvim/util/terminal.lua b/lua/lazyvim/util/terminal.lua index 87f8553b..09fe3b52 100644 --- a/lua/lazyvim/util/terminal.lua +++ b/lua/lazyvim/util/terminal.lua @@ -1,14 +1,11 @@ ---@class lazyvim.util.terminal ----@overload fun(cmd: string|string[], opts: LazyTermOpts): LazyFloat +---@overload fun(cmd: string|string[], opts: snacks.terminal.Config): snacks.terminal local M = setmetatable({}, { __call = function(m, ...) return m.open(...) end, }) ----@type table -local terminals = {} - ---@param shell? string function M.setup(shell) vim.o.shell = shell or vim.o.shell @@ -40,58 +37,12 @@ function M.setup(shell) end end ----@class LazyTermOpts: LazyCmdOptions ----@field interactive? boolean ----@field esc_esc? boolean ----@field ctrl_hjkl? boolean - -- Opens a floating terminal (interactive by default) +---@deprecated use Snacks.terminal instead ---@param cmd? string[]|string ----@param opts? LazyTermOpts +---@param opts? snacks.terminal.Config function M.open(cmd, opts) - opts = vim.tbl_deep_extend("force", { - ft = "lazyterm", - size = { width = 0.9, height = 0.9 }, - backdrop = LazyVim.has("edgy.nvim") and not cmd and 100 or nil, - }, opts or {}, { persistent = true }) --[[@as LazyTermOpts]] - - local termkey = vim.inspect({ cmd = cmd or "shell", cwd = opts.cwd, env = opts.env, count = vim.v.count1 }) - - if terminals[termkey] and terminals[termkey]:buf_valid() then - terminals[termkey]:toggle() - else - terminals[termkey] = require("lazy.util").float_term(cmd, opts) - local buf = terminals[termkey].buf - vim.b[buf].lazyterm_cmd = cmd - if opts.esc_esc == false then - vim.keymap.set("t", "", "", { buffer = buf, nowait = true }) - end - if opts.ctrl_hjkl == false then - vim.keymap.set("t", "", "", { buffer = buf, nowait = true }) - vim.keymap.set("t", "", "", { buffer = buf, nowait = true }) - vim.keymap.set("t", "", "", { buffer = buf, nowait = true }) - vim.keymap.set("t", "", "", { buffer = buf, nowait = true }) - end - - vim.keymap.set("n", "gf", function() - local f = vim.fn.findfile(vim.fn.expand("")) - if f ~= "" then - vim.cmd("close") - vim.cmd("e " .. f) - end - end, { buffer = buf }) - - vim.api.nvim_create_autocmd("BufEnter", { - buffer = buf, - callback = function() - vim.cmd.startinsert() - end, - }) - - vim.cmd("noh") - end - - return terminals[termkey] + return Snacks.terminal(cmd, opts) end return M