diff --git a/NEWS.md b/NEWS.md index 20b6156b..b2bb693b 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,19 @@ # What's new? +## 13.x + +- Big new release with a lot of changes and improvements! + +- The biggest change is the move of a bunch of core features to + [snacks.nvim](https://github.com/folke/snacks.nvim) and fully + integrating it into **LazyVim**. + +- I highly suggest having a look at the **snacks.nvim** documentation + to see all the new features and improvements. Most important changes: + - `Snacks.notifier` for notifications instead of `nvim-notify` + - `Snacks.terminal` is similar to `lazyterm`, but has more features + and creates bottom splits by default (similar to the `edgy` integrating) + ## 12.x - **Markdown Extra**: [headlines.nvim](https://github.com/lukas-reineke/headlines.nvim) has been removed in favor of [markdown.nvim](https://github.com/MeanderingProgrammer/markdown.nvim) diff --git a/lua/lazyvim/config/autocmds.lua b/lua/lazyvim/config/autocmds.lua index e301bc1e..0a6d92e3 100644 --- a/lua/lazyvim/config/autocmds.lua +++ b/lua/lazyvim/config/autocmds.lua @@ -55,20 +55,22 @@ vim.api.nvim_create_autocmd("FileType", { group = augroup("close_with_q"), pattern = { "PlenaryTestPopup", + "checkhealth", + "dbout", + "gitsigns-blame", "grug-far", "help", "lspinfo", + "neotest-output", + "neotest-output-panel", + "neotest-summary", "notify", "qf", + "snacks_win", + "snacks_notif", "spectre_panel", "startuptime", "tsplayground", - "neotest-output", - "checkhealth", - "neotest-summary", - "neotest-output-panel", - "dbout", - "gitsigns-blame", }, callback = function(event) vim.bo[event.buf].buflisted = false @@ -124,29 +126,3 @@ vim.api.nvim_create_autocmd({ "BufWritePre" }, { vim.fn.mkdir(vim.fn.fnamemodify(file, ":p:h"), "p") end, }) - -vim.filetype.add({ - pattern = { - [".*"] = { - function(path, buf) - return vim.bo[buf] - and vim.bo[buf].filetype ~= "bigfile" - and path - and vim.fn.getfsize(path) > vim.g.bigfile_size - and "bigfile" - or nil - end, - }, - }, -}) - -vim.api.nvim_create_autocmd({ "FileType" }, { - group = augroup("bigfile"), - pattern = "bigfile", - callback = function(ev) - vim.b.minianimate_disable = true - vim.schedule(function() - vim.bo[ev.buf].syntax = vim.filetype.match({ buf = ev.buf }) or "" - end) - end, -}) diff --git a/lua/lazyvim/config/keymaps.lua b/lua/lazyvim/config/keymaps.lua index cc226964..422cadd2 100644 --- a/lua/lazyvim/config/keymaps.lua +++ b/lua/lazyvim/config/keymaps.lua @@ -37,7 +37,9 @@ map("n", "[b", "bprevious", { desc = "Prev Buffer" }) map("n", "]b", "bnext", { desc = "Next Buffer" }) map("n", "bb", "e #", { desc = "Switch to Other Buffer" }) map("n", "`", "e #", { desc = "Switch to Other Buffer" }) -map("n", "bd", LazyVim.ui.bufremove, { desc = "Delete Buffer" }) +map("n", "bd", function() + Snacks.bufdelete() +end, { desc = "Delete Buffer" }) map("n", "bD", ":bd", { desc = "Delete Buffer and Window" }) -- Clear search with @@ -115,37 +117,28 @@ map("n", "[w", diagnostic_goto(false, "WARN"), { desc = "Prev Warning" }) -- stylua: ignore start -- toggle options -LazyVim.toggle.map("uf", LazyVim.toggle.format()) -LazyVim.toggle.map("uF", LazyVim.toggle.format(true)) -LazyVim.toggle.map("us", LazyVim.toggle("spell", { name = "Spelling" })) -LazyVim.toggle.map("uw", LazyVim.toggle("wrap", { name = "Wrap" })) -LazyVim.toggle.map("uL", LazyVim.toggle("relativenumber", { name = "Relative Number" })) -LazyVim.toggle.map("ud", LazyVim.toggle.diagnostics) -LazyVim.toggle.map("ul", LazyVim.toggle.number) -LazyVim.toggle.map( "uc", LazyVim.toggle("conceallevel", { values = { 0, vim.o.conceallevel > 0 and vim.o.conceallevel or 2 } })) -LazyVim.toggle.map("uT", LazyVim.toggle.treesitter) -LazyVim.toggle.map("ub", LazyVim.toggle("background", { values = { "light", "dark" }, name = "Background" })) +LazyVim.format.snacks_toggle():map("uf") +LazyVim.format.snacks_toggle(true):map("uF") +Snacks.toggle.option("spell", { name = "Spelling"}):map("us") +Snacks.toggle.option("wrap", {name = "Wrap"}):map("uw") +Snacks.toggle.option("relativenumber", { name = "Relative Number"}):map("uL") +Snacks.toggle.diagnostics():map("ud") +Snacks.toggle.line_number():map("ul") +Snacks.toggle.option("conceallevel", {off = 0, on = vim.o.conceallevel > 0 and vim.o.conceallevel or 2}):map("uc") +Snacks.toggle.treesitter():map("uT") +Snacks.toggle.option("background", { off = "light", on = "dark" , name = "Dark Background"}):map("ub") if vim.lsp.inlay_hint then - LazyVim.toggle.map("uh", LazyVim.toggle.inlay_hints) + Snacks.toggle.inlay_hints():map("uh") end -- lazygit -map("n", "gg", function() LazyVim.lazygit( { cwd = LazyVim.root.git() }) end, { desc = "Lazygit (Root Dir)" }) -map("n", "gG", function() LazyVim.lazygit() end, { desc = "Lazygit (cwd)" }) -map("n", "gb", LazyVim.lazygit.blame_line, { desc = "Git Blame Line" }) -map("n", "gB", LazyVim.lazygit.browse, { desc = "Git Browse" }) - -map("n", "gf", function() - local git_path = vim.api.nvim_buf_get_name(0) - LazyVim.lazygit({args = { "-f", vim.trim(git_path) }, cwd = LazyVim.root.git()}) -end, { desc = "Lazygit Current File History" }) - -map("n", "gl", function() - LazyVim.lazygit({ args = { "log" }, cwd = LazyVim.root.git() }) -end, { desc = "Lazygit Log" }) -map("n", "gL", function() - LazyVim.lazygit({ args = { "log" } }) -end, { desc = "Lazygit Log (cwd)" }) +map("n", "gg", function() Snacks.lazygit( { cwd = LazyVim.root.git() }) end, { desc = "Lazygit (Root Dir)" }) +map("n", "gG", function() Snacks.lazygit() end, { desc = "Lazygit (cwd)" }) +map("n", "gb", function() Snacks.git.blame_line() end, { desc = "Git Blame Line" }) +map("n", "gB", function() Snacks.gitbrowse() end, { desc = "Git Browse" }) +map("n", "gf", function() Snacks.lazygit.log_file() end, { desc = "Lazygit Current File History" }) +map("n", "gl", function() Snacks.lazygit.log({ cwd = LazyVim.root.git() }) end, { desc = "Lazygit Log" }) +map("n", "gL", function() Snacks.lazygit.log() end, { desc = "Lazygit Log (cwd)" }) -- quit map("n", "qq", "qa", { desc = "Quit All" }) @@ -158,18 +151,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" }) @@ -178,7 +165,7 @@ map("n", "w", "", { desc = "Windows", remap = true }) map("n", "-", "s", { desc = "Split Window Below", remap = true }) map("n", "|", "v", { desc = "Split Window Right", remap = true }) map("n", "wd", "c", { desc = "Delete Window", remap = true }) -LazyVim.toggle.map("wm", LazyVim.toggle.maximize) +LazyVim.ui.maximize():map("wm") -- tabs map("n", "l", "tablast", { desc = "Last Tab" }) diff --git a/lua/lazyvim/config/options.lua b/lua/lazyvim/config/options.lua index 3ad70c16..796bbf16 100644 --- a/lua/lazyvim/config/options.lua +++ b/lua/lazyvim/config/options.lua @@ -107,7 +107,6 @@ opt.spelloptions:append("noplainbuffer") opt.splitbelow = true -- Put new windows below current opt.splitkeep = "screen" opt.splitright = true -- Put new windows right of current -opt.statuscolumn = [[%!v:lua.require'lazyvim.util'.ui.statuscolumn()]] opt.tabstop = 2 -- Number of spaces tabs count for opt.termguicolors = true -- True color support opt.timeoutlen = vim.g.vscode and 1000 or 300 -- Lower than default (1000) to quickly trigger which-key diff --git a/lua/lazyvim/plugins/coding.lua b/lua/lazyvim/plugins/coding.lua index 18ce7407..34a747ea 100644 --- a/lua/lazyvim/plugins/coding.lua +++ b/lua/lazyvim/plugins/coding.lua @@ -201,6 +201,7 @@ return { library = { { path = "luvit-meta/library", words = { "vim%.uv" } }, { path = "LazyVim", words = { "LazyVim" } }, + { path = "snacks.nvim", words = { "Snacks" } }, { path = "lazy.nvim", words = { "LazyVim" } }, }, }, diff --git a/lua/lazyvim/plugins/editor.lua b/lua/lazyvim/plugins/editor.lua index a57d9aeb..bac949fc 100644 --- a/lua/lazyvim/plugins/editor.lua +++ b/lua/lazyvim/plugins/editor.lua @@ -105,7 +105,7 @@ return { }, config = function(_, opts) local function on_move(data) - LazyVim.lsp.on_rename(data.source, data.destination) + Snacks.rename.on_rename_file(data.source, data.destination) end local events = require("neo-tree.events") diff --git a/lua/lazyvim/plugins/extras/editor/mini-files.lua b/lua/lazyvim/plugins/extras/editor/mini-files.lua index 461e6dca..70a42940 100644 --- a/lua/lazyvim/plugins/extras/editor/mini-files.lua +++ b/lua/lazyvim/plugins/extras/editor/mini-files.lua @@ -104,7 +104,7 @@ return { vim.api.nvim_create_autocmd("User", { pattern = "MiniFilesActionRename", callback = function(event) - LazyVim.lsp.on_rename(event.data.from, event.data.to) + Snacks.rename.on_rename_file(event.data.from, event.data.to) end, }) end, diff --git a/lua/lazyvim/plugins/extras/lang/markdown.lua b/lua/lazyvim/plugins/extras/lang/markdown.lua index 4829098b..e7977cb5 100644 --- a/lua/lazyvim/plugins/extras/lang/markdown.lua +++ b/lua/lazyvim/plugins/extras/lang/markdown.lua @@ -108,7 +108,7 @@ return { ft = { "markdown", "norg", "rmd", "org" }, config = function(_, opts) require("render-markdown").setup(opts) - LazyVim.toggle.map("um", { + Snacks.toggle({ name = "Render Markdown", get = function() return require("render-markdown.state").enabled @@ -121,7 +121,7 @@ return { m.disable() end end, - }) + }):map("um") end, }, } diff --git a/lua/lazyvim/plugins/extras/ui/edgy.lua b/lua/lazyvim/plugins/extras/ui/edgy.lua index f63e31ad..dbe76d1b 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 terminal + 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_win + and vim.w[win].snacks_win.position == pos + and vim.w[win].snacks_win.relative == "editor" + and not vim.w[win].trouble_preview + end, + }) + end return opts end, }, diff --git a/lua/lazyvim/plugins/extras/ui/mini-animate.lua b/lua/lazyvim/plugins/extras/ui/mini-animate.lua index 1136230a..925a9f80 100644 --- a/lua/lazyvim/plugins/extras/ui/mini-animate.lua +++ b/lua/lazyvim/plugins/extras/ui/mini-animate.lua @@ -21,7 +21,7 @@ return { end, }) - LazyVim.toggle.map("ua", { + Snacks.toggle({ name = "Mini Animate", get = function() return not vim.g.minianimate_disable @@ -29,7 +29,7 @@ return { set = function(state) vim.g.minianimate_disable = not state end, - }) + }):map("ua") local animate = require("mini.animate") return { diff --git a/lua/lazyvim/plugins/extras/ui/mini-indentscope.lua b/lua/lazyvim/plugins/extras/ui/mini-indentscope.lua index 1b8beeb6..3c49d83b 100644 --- a/lua/lazyvim/plugins/extras/ui/mini-indentscope.lua +++ b/lua/lazyvim/plugins/extras/ui/mini-indentscope.lua @@ -14,17 +14,19 @@ return { init = function() vim.api.nvim_create_autocmd("FileType", { pattern = { + "Trouble", "alpha", "dashboard", "fzf", "help", "lazy", - "lazyterm", "mason", "neo-tree", "notify", + "snacks_notif", + "snacks_terminal", + "snacks_win", "toggleterm", - "Trouble", "trouble", }, callback = function() diff --git a/lua/lazyvim/plugins/extras/ui/treesitter-context.lua b/lua/lazyvim/plugins/extras/ui/treesitter-context.lua index 019977ed..ba9565a1 100644 --- a/lua/lazyvim/plugins/extras/ui/treesitter-context.lua +++ b/lua/lazyvim/plugins/extras/ui/treesitter-context.lua @@ -4,8 +4,7 @@ return { event = "VeryLazy", opts = function() local tsc = require("treesitter-context") - - LazyVim.toggle.map("ut", { + Snacks.toggle({ name = "Treesitter Context", get = tsc.enabled, set = function(state) @@ -15,8 +14,7 @@ return { tsc.disable() end end, - }) - + }):map("ut") return { mode = "cursor", max_lines = 3 } end, } 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 4a43f115..ad646b8e 100644 --- a/lua/lazyvim/plugins/init.lua +++ b/lua/lazyvim/plugins/init.lua @@ -10,7 +10,48 @@ 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", + priority = 1000, + lazy = false, + opts = function() + ---@type snacks.Config + return { + toggle = { map = LazyVim.safe_keymap_set }, + notifier = { enabled = not LazyVim.has("noice.nvim") }, + terminal = { + win = { + 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" }, + }, + }, + }, + } + end, + keys = { + { + "un", + function() + Snacks.notifier.hide() + end, + desc = "Dismiss All Notifications", + }, + }, + }, } diff --git a/lua/lazyvim/plugins/lsp/init.lua b/lua/lazyvim/plugins/lsp/init.lua index 3e8bc8b7..eb37acc3 100644 --- a/lua/lazyvim/plugins/lsp/init.lua +++ b/lua/lazyvim/plugins/lsp/init.lua @@ -129,8 +129,6 @@ return { LazyVim.lsp.setup() LazyVim.lsp.on_dynamic_capability(require("lazyvim.plugins.lsp.keymaps").on_attach) - LazyVim.lsp.words.setup(opts.document_highlight) - -- diagnostics signs if vim.fn.has("nvim-0.10.0") == 0 then if type(opts.diagnostics.signs) ~= "boolean" then diff --git a/lua/lazyvim/plugins/lsp/keymaps.lua b/lua/lazyvim/plugins/lsp/keymaps.lua index 2a35dc97..e9b7c49d 100644 --- a/lua/lazyvim/plugins/lsp/keymaps.lua +++ b/lua/lazyvim/plugins/lsp/keymaps.lua @@ -25,17 +25,17 @@ function M.get() { "ca", vim.lsp.buf.code_action, desc = "Code Action", mode = { "n", "v" }, has = "codeAction" }, { "cc", vim.lsp.codelens.run, desc = "Run Codelens", mode = { "n", "v" }, has = "codeLens" }, { "cC", vim.lsp.codelens.refresh, desc = "Refresh & Display Codelens", mode = { "n" }, has = "codeLens" }, - { "cR", LazyVim.lsp.rename_file, desc = "Rename File", mode ={"n"}, has = { "workspace/didRenameFiles", "workspace/willRenameFiles" } }, + { "cR", function() Snacks.rename.rename_file() end, desc = "Rename File", mode ={"n"}, has = { "workspace/didRenameFiles", "workspace/willRenameFiles" } }, { "cr", vim.lsp.buf.rename, desc = "Rename", has = "rename" }, { "cA", LazyVim.lsp.action.source, desc = "Source Action", has = "codeAction" }, - { "]]", function() LazyVim.lsp.words.jump(vim.v.count1) end, has = "documentHighlight", - desc = "Next Reference", cond = function() return LazyVim.lsp.words.enabled end }, - { "[[", function() LazyVim.lsp.words.jump(-vim.v.count1) end, has = "documentHighlight", - desc = "Prev Reference", cond = function() return LazyVim.lsp.words.enabled end }, - { "", function() LazyVim.lsp.words.jump(vim.v.count1, true) end, has = "documentHighlight", - desc = "Next Reference", cond = function() return LazyVim.lsp.words.enabled end }, - { "", function() LazyVim.lsp.words.jump(-vim.v.count1, true) end, has = "documentHighlight", - desc = "Prev Reference", cond = function() return LazyVim.lsp.words.enabled end }, + { "]]", function() Snacks.words.jump(vim.v.count1) end, has = "documentHighlight", + desc = "Next Reference", cond = function() return Snacks.words.is_enabled() end }, + { "[[", function() Snacks.words.jump(-vim.v.count1) end, has = "documentHighlight", + desc = "Prev Reference", cond = function() return Snacks.words.is_enabled() end }, + { "", function() Snacks.words.jump(vim.v.count1, true) end, has = "documentHighlight", + desc = "Next Reference", cond = function() return Snacks.words.is_enabled() end }, + { "", function() Snacks.words.jump(-vim.v.count1, true) end, has = "documentHighlight", + desc = "Prev Reference", cond = function() return Snacks.words.is_enabled() end }, } return M._keys diff --git a/lua/lazyvim/plugins/ui.lua b/lua/lazyvim/plugins/ui.lua index 14807497..136c372b 100644 --- a/lua/lazyvim/plugins/ui.lua +++ b/lua/lazyvim/plugins/ui.lua @@ -1,39 +1,4 @@ return { - -- Better `vim.notify()` - { - "rcarriga/nvim-notify", - keys = { - { - "un", - function() - require("notify").dismiss({ silent = true, pending = true }) - end, - desc = "Dismiss All Notifications", - }, - }, - opts = { - stages = "static", - timeout = 3000, - max_height = function() - return math.floor(vim.o.lines * 0.75) - end, - max_width = function() - return math.floor(vim.o.columns * 0.75) - end, - on_open = function(win) - vim.api.nvim_win_set_config(win, { zindex = 100 }) - end, - }, - init = function() - -- when noice is not enabled, install notify on VeryLazy - if not LazyVim.has("noice.nvim") then - LazyVim.on_very_lazy(function() - vim.notify = require("notify") - end) - end - end, - }, - -- This is what powers LazyVim's fancy-looking -- tabs, which include filetype icons and close buttons. { @@ -55,9 +20,9 @@ return { opts = { options = { -- stylua: ignore - close_command = function(n) LazyVim.ui.bufremove(n) end, + close_command = function(n) Snacks.bufdelete(n) end, -- stylua: ignore - right_mouse_command = function(n) LazyVim.ui.bufremove(n) end, + right_mouse_command = function(n) Snacks.bufdelete(n) end, diagnostics = "nvim_lsp", always_show_bufferline = false, diagnostics_indicator = function(_, _, diag) @@ -226,7 +191,7 @@ return { "lukas-reineke/indent-blankline.nvim", event = "LazyFile", opts = function() - LazyVim.toggle.map("ug", { + Snacks.toggle({ name = "Indention Guides", get = function() return require("ibl.config").get_config(0).enabled @@ -234,7 +199,7 @@ return { set = function(state) require("ibl").setup_buffer(0, { enabled = state }) end, - }) + }):map("ug") return { indent = { @@ -244,17 +209,19 @@ return { scope = { show_start = false, show_end = false }, exclude = { filetypes = { - "help", + "Trouble", "alpha", "dashboard", - "neo-tree", - "Trouble", - "trouble", + "help", "lazy", "mason", + "neo-tree", "notify", + "snacks_notif", + "snacks_terminal", + "snacks_win", "toggleterm", - "lazyterm", + "trouble", }, }, } diff --git a/lua/lazyvim/util/deprecated.lua b/lua/lazyvim/util/deprecated.lua new file mode 100644 index 00000000..43d73fcf --- /dev/null +++ b/lua/lazyvim/util/deprecated.lua @@ -0,0 +1,59 @@ +local M = {} + +M.moved = { + lsp = { + rename_file = { "Snacks.rename.rename_file" }, + on_rename = { "Snacks.rename.on_rename_file" }, + words = { "Snacks.words" }, + }, + terminal = { + open = { "Snacks.terminal" }, + __call = { "Snacks.terminal" }, + }, + ui = { + statuscolumn = { "Snacks.statuscolumn" }, + bufremove = { "Snacks.bufdelete" }, + }, +} + +---@param name string +---@param mod table +function M.decorate(name, mod) + if not M.moved[name] then + return mod + end + setmetatable(mod, { + __call = function(_, ...) + local to = M.moved[name].__call[1] + LazyVim.deprecate("LazyVim." .. name, to) + local ret = vim.tbl_get(_G, unpack(vim.split(to, ".", { plain = true }))) + return ret(...) + end, + __index = function(_, k) + if M.moved[name][k] then + local to = M.moved[name][k][1] + LazyVim.deprecate("LazyVim." .. name .. "." .. k, to) + local ret = vim.tbl_get(_G, unpack(vim.split(to, ".", { plain = true }))) + return ret + end + return nil + end, + }) +end + +function M.lazygit() + LazyVim.deprecate("LazyVim.lazygit", "Snacks.lazygit") + return Snacks.lazygit +end + +function M.toggle() + LazyVim.deprecate("LazyVim.toggle", "Snacks.toggle") + return { + map = function() end, + wrap = function() + return {} + end, + } +end + +return M diff --git a/lua/lazyvim/util/format.lua b/lua/lazyvim/util/format.lua index de6ad052..7b0ccfd2 100644 --- a/lua/lazyvim/util/format.lua +++ b/lua/lazyvim/util/format.lua @@ -177,4 +177,20 @@ function M.setup() end, { desc = "Show info about the formatters for the current buffer" }) end +---@param buf? boolean +function M.snacks_toggle(buf) + return Snacks.toggle({ + name = "Auto Format (" .. (buf and "Buffer" or "Global") .. ")", + get = function() + if not buf then + return vim.g.autoformat == nil or vim.g.autoformat + end + return LazyVim.format.enabled() + end, + set = function(state) + LazyVim.format.enable(state, buf) + end, + }) +end + return M diff --git a/lua/lazyvim/util/init.lua b/lua/lazyvim/util/init.lua index 106cbdc0..8d94f582 100644 --- a/lua/lazyvim/util/init.lua +++ b/lua/lazyvim/util/init.lua @@ -6,8 +6,6 @@ local LazyUtil = require("lazy.core.util") ---@field lsp lazyvim.util.lsp ---@field root lazyvim.util.root ---@field terminal lazyvim.util.terminal ----@field lazygit lazyvim.util.lazygit ----@field toggle lazyvim.util.toggle ---@field format lazyvim.util.format ---@field plugin lazyvim.util.plugin ---@field extras lazyvim.util.extras @@ -20,36 +18,17 @@ local LazyUtil = require("lazy.core.util") ---@field cmp lazyvim.util.cmp local M = {} ----@type table -local deprecated = { - get_clients = "lsp", - on_attach = "lsp", - on_rename = "lsp", - root_patterns = { "root", "patterns" }, - get_root = { "root", "get" }, - float_term = { "terminal", "open" }, - toggle_diagnostics = { "toggle", "diagnostics" }, - toggle_number = { "toggle", "number" }, - fg = "ui", - telescope = "pick", -} - setmetatable(M, { __index = function(t, k) if LazyUtil[k] then return LazyUtil[k] end - local dep = deprecated[k] - if dep then - local mod = type(dep) == "table" and dep[1] or dep - local key = type(dep) == "table" and dep[2] or k - M.deprecate([[LazyVim.]] .. k, [[LazyVim.]] .. mod .. "." .. key) - ---@diagnostic disable-next-line: no-unknown - t[mod] = require("lazyvim.util." .. mod) -- load here to prevent loops - return t[mod][key] + if k == "lazygit" or k == "toggle" then -- HACK: special case for lazygit + return M.deprecated[k]() end ---@diagnostic disable-next-line: no-unknown t[k] = require("lazyvim.util." .. k) + M.deprecated.decorate(k, t[k]) return t[k] end, }) diff --git a/lua/lazyvim/util/lazygit.lua b/lua/lazyvim/util/lazygit.lua deleted file mode 100644 index ff96c85c..00000000 --- a/lua/lazyvim/util/lazygit.lua +++ /dev/null @@ -1,231 +0,0 @@ ----@class lazyvim.util.lazygit ----@field config_dir? string ----@overload fun(cmd: string|string[], opts: LazyTermOpts): LazyFloat -local M = setmetatable({}, { - __call = function(m, ...) - return m.open(...) - end, -}) - ----@alias LazyGitColor {fg?:string, bg?:string, bold?:boolean} - ----@class LazyGitTheme: table ----@field activeBorderColor LazyGitColor ----@field cherryPickedCommitBgColor LazyGitColor ----@field cherryPickedCommitFgColor LazyGitColor ----@field defaultFgColor LazyGitColor ----@field inactiveBorderColor LazyGitColor ----@field optionsTextColor LazyGitColor ----@field searchingActiveBorderColor LazyGitColor ----@field selectedLineBgColor LazyGitColor ----@field unstagedChangesColor LazyGitColor -M.theme = { - [241] = { fg = "Special" }, - activeBorderColor = { fg = "MatchParen", bold = true }, - cherryPickedCommitBgColor = { fg = "Identifier" }, - cherryPickedCommitFgColor = { fg = "Function" }, - defaultFgColor = { fg = "Normal" }, - inactiveBorderColor = { fg = "FloatBorder" }, - optionsTextColor = { fg = "Function" }, - searchingActiveBorderColor = { fg = "MatchParen", bold = true }, - selectedLineBgColor = { bg = "Visual" }, -- set to `default` to have no background colour - unstagedChangesColor = { fg = "DiagnosticError" }, -} - -M.theme_path = LazyVim.norm(vim.fn.stdpath("cache") .. "/lazygit-theme.yml") - --- re-create config file on startup -M.dirty = true - --- re-create theme file on ColorScheme change -vim.api.nvim_create_autocmd("ColorScheme", { - callback = function() - M.dirty = true - end, -}) - --- Opens lazygit ----@param opts? LazyTermOpts | {args?: string[]} -function M.open(opts) - if vim.g.lazygit_theme ~= nil then - LazyVim.deprecate("vim.g.lazygit_theme", "vim.g.lazygit_config") - end - - opts = vim.tbl_deep_extend("force", {}, { - esc_esc = false, - ctrl_hjkl = false, - }, opts or {}) - - local cmd = { "lazygit" } - vim.list_extend(cmd, opts.args or {}) - - if vim.g.lazygit_config then - if M.dirty then - M.update_config() - end - - if not M.config_dir then - local Process = require("lazy.manage.process") - local ok, lines = pcall(Process.exec, { "lazygit", "-cd" }) - if ok then - M.config_dir = lines[1] - vim.env.LG_CONFIG_FILE = LazyVim.norm(M.config_dir .. "/config.yml" .. "," .. M.theme_path) - local custom_config = LazyVim.norm(M.config_dir .. "/custom.yml") - if vim.uv.fs_stat(custom_config) and vim.uv.fs_stat(custom_config).type == "file" then - vim.env.LG_CONFIG_FILE = vim.env.LG_CONFIG_FILE .. "," .. custom_config - end - else - ---@diagnostic disable-next-line: cast-type-mismatch - ---@cast lines string - LazyVim.error( - { "Failed to get **lazygit** config directory.", "Will not apply **lazygit** config.", "", "# Error:", lines }, - { title = "lazygit" } - ) - end - end - end - - return LazyVim.terminal(cmd, opts) -end - -function M.set_ansi_color(idx, color) - io.write(("\27]4;%d;%s\7"):format(idx, color)) -end - ----@param v LazyGitColor ----@return string[] -function M.get_color(v) - ---@type string[] - local color = {} - if v.fg then - color[1] = LazyVim.ui.color(v.fg) - elseif v.bg then - color[1] = LazyVim.ui.color(v.bg, true) - end - if v.bold then - table.insert(color, "bold") - end - return color -end - -function M.update_config() - ---@type table - local theme = {} - - for k, v in pairs(M.theme) do - if type(k) == "number" then - local color = M.get_color(v) - -- LazyGit uses color 241 a lot, so also set it to a nice color - -- pcall, since some terminals don't like this - pcall(M.set_ansi_color, k, color[1]) - else - theme[k] = M.get_color(v) - end - end - - local config = [[ -os: - editPreset: "nvim-remote" -gui: - nerdFontsVersion: 3 - theme: -]] - - ---@type string[] - local lines = {} - for k, v in pairs(theme) do - lines[#lines + 1] = (" %s:"):format(k) - for _, c in ipairs(v) do - lines[#lines + 1] = (" - %q"):format(c) - end - end - config = config .. table.concat(lines, "\n") - require("lazy.util").write_file(M.theme_path, config) - M.dirty = false -end - ----@param opts? {count?: number}|LazyCmdOptions -function M.blame_line(opts) - opts = vim.tbl_deep_extend("force", { - count = 3, - filetype = "git", - size = { - width = 0.6, - height = 0.6, - }, - 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) -end - --- stylua: ignore -M.remote_patterns = { - { "^(https?://.*)%.git$" , "%1" }, - { "^git@(.+):(.+)%.git$" , "https://%1/%2" }, - { "^git@(.+):(.+)$" , "https://%1/%2" }, - { "^git@(.+)/(.+)$" , "https://%1/%2" }, - { "^ssh://git@(.*)$" , "https://%1" }, - { "ssh%.dev%.azure%.com/v3/(.*)/(.*)$", "dev.azure.com/%1/_git/%2" }, - { "^https://%w*@(.*)" , "https://%1" }, - { "^git@(.*)" , "https://%1" }, - { ":%d+" , "" }, - { "%.git$" , "" }, -} - ----@param remote string -function M.get_url(remote) - local ret = remote - for _, pattern in ipairs(M.remote_patterns) do - ret = ret:gsub(pattern[1], pattern[2]) - end - return ret:find("https://") == 1 and ret or ("https://%s"):format(ret) -end - -function M.browse() - local lines = require("lazy.manage.process").exec({ "git", "remote", "-v" }) - local remotes = {} ---@type {name:string, url:string}[] - - for _, line in ipairs(lines) do - local name, remote = line:match("(%S+)%s+(%S+)%s+%(fetch%)") - if name and remote then - local url = M.get_url(remote) - if url then - table.insert(remotes, { - name = name, - url = url, - }) - end - end - end - - local function open(remote) - if remote then - LazyVim.info(("Opening [%s](%s)"):format(remote.name, remote.url)) - if vim.fn.has("nvim-0.10") == 0 then - require("lazy.util").open(remote.url, { system = true }) - return - end - vim.ui.open(remote.url) - end - end - - if #remotes == 0 then - return LazyVim.error("No git remotes found") - elseif #remotes == 1 then - return open(remotes[1]) - end - - vim.ui.select(remotes, { - prompt = "Select remote to browse", - format_item = function(item) - return item.name .. (" "):rep(8 - #item.name) .. " 🔗 " .. item.url - end, - }, open) -end - -return M diff --git a/lua/lazyvim/util/lsp.lua b/lua/lazyvim/util/lsp.lua index 7efd8622..e868dcf0 100644 --- a/lua/lazyvim/util/lsp.lua +++ b/lua/lazyvim/util/lsp.lua @@ -118,63 +118,6 @@ function M.on_supports_method(method, fn) }) end -function M.rename_file() - local buf = vim.api.nvim_get_current_buf() - local old = assert(LazyVim.root.realpath(vim.api.nvim_buf_get_name(buf))) - local root = assert(LazyVim.root.realpath(LazyVim.root.get({ normalize = true }))) - assert(old:find(root, 1, true) == 1, "File not in project root") - - local extra = old:sub(#root + 2) - - vim.ui.input({ - prompt = "New File Name: ", - default = extra, - completion = "file", - }, function(new) - if not new or new == "" or new == extra then - return - end - new = LazyVim.norm(root .. "/" .. new) - vim.fn.mkdir(vim.fs.dirname(new), "p") - M.on_rename(old, new, function() - vim.fn.rename(old, new) - vim.cmd.edit(new) - vim.api.nvim_buf_delete(buf, { force = true }) - vim.fn.delete(old) - end) - end) -end - ----@param from string ----@param to string ----@param rename? fun() -function M.on_rename(from, to, rename) - local changes = { files = { { - oldUri = vim.uri_from_fname(from), - newUri = vim.uri_from_fname(to), - } } } - - local clients = M.get_clients() - for _, client in ipairs(clients) do - if client.supports_method("workspace/willRenameFiles") then - local resp = client.request_sync("workspace/willRenameFiles", changes, 1000, 0) - if resp and resp.result ~= nil then - vim.lsp.util.apply_workspace_edit(resp.result, client.offset_encoding) - end - end - end - - if rename then - rename() - end - - for _, client in ipairs(clients) do - if client.supports_method("workspace/didRenameFiles") then - client.notify("workspace/didRenameFiles", changes) - end - end -end - ---@return _.lspconfig.options function M.get_config(server) local configs = require("lspconfig.configs") @@ -260,82 +203,6 @@ function M.format(opts) end end ----@alias LspWord {from:{[1]:number, [2]:number}, to:{[1]:number, [2]:number}} 1-0 indexed -M.words = {} -M.words.enabled = false -M.words.ns = vim.api.nvim_create_namespace("vim_lsp_references") - ----@param opts? {enabled?: boolean} -function M.words.setup(opts) - opts = opts or {} - if not opts.enabled then - return - end - M.words.enabled = true - local handler = vim.lsp.handlers["textDocument/documentHighlight"] - vim.lsp.handlers["textDocument/documentHighlight"] = function(err, result, ctx, config) - if not vim.api.nvim_buf_is_loaded(ctx.bufnr) then - return - end - vim.lsp.buf.clear_references() - return handler(err, result, ctx, config) - end - - M.on_supports_method("textDocument/documentHighlight", function(_, buf) - vim.api.nvim_create_autocmd({ "CursorHold", "CursorHoldI", "CursorMoved", "CursorMovedI" }, { - group = vim.api.nvim_create_augroup("lsp_word_" .. buf, { clear = true }), - buffer = buf, - callback = function(ev) - if not require("lazyvim.plugins.lsp.keymaps").has(buf, "documentHighlight") then - return false - end - - if not ({ M.words.get() })[2] then - if ev.event:find("CursorMoved") then - vim.lsp.buf.clear_references() - elseif not LazyVim.cmp.visible() then - vim.lsp.buf.document_highlight() - end - end - end, - }) - end) -end - ----@return LspWord[] words, number? current -function M.words.get() - local cursor = vim.api.nvim_win_get_cursor(0) - local current, ret = nil, {} ---@type number?, LspWord[] - for _, extmark in ipairs(vim.api.nvim_buf_get_extmarks(0, M.words.ns, 0, -1, { details = true })) do - local w = { - from = { extmark[2] + 1, extmark[3] }, - to = { extmark[4].end_row + 1, extmark[4].end_col }, - } - ret[#ret + 1] = w - if cursor[1] >= w.from[1] and cursor[1] <= w.to[1] and cursor[2] >= w.from[2] and cursor[2] <= w.to[2] then - current = #ret - end - end - return ret, current -end - ----@param count number ----@param cycle? boolean -function M.words.jump(count, cycle) - local words, idx = M.words.get() - if not idx then - return - end - idx = idx + count - if cycle then - idx = (idx - 1) % #words + 1 - end - local target = words[idx] - if target then - vim.api.nvim_win_set_cursor(0, target.from) - end -end - M.action = setmetatable({}, { __index = function(_, action) return function() diff --git a/lua/lazyvim/util/mini.lua b/lua/lazyvim/util/mini.lua index e7320a73..28d38f5b 100644 --- a/lua/lazyvim/util/mini.lua +++ b/lua/lazyvim/util/mini.lua @@ -121,7 +121,7 @@ end ---@param opts {skip_next: string, skip_ts: string[], skip_unbalanced: boolean, markdown: boolean} function M.pairs(opts) - LazyVim.toggle.map("up", { + Snacks.toggle({ name = "Mini Pairs", get = function() return not vim.g.minipairs_disable @@ -129,7 +129,8 @@ function M.pairs(opts) set = function(state) vim.g.minipairs_disable = not state end, - }) + }):map("up") + local pairs = require("mini.pairs") pairs.setup(opts) local open = pairs.open diff --git a/lua/lazyvim/util/news.lua b/lua/lazyvim/util/news.lua index 5fb4a08c..f473f3a3 100644 --- a/lua/lazyvim/util/news.lua +++ b/lua/lazyvim/util/news.lua @@ -72,15 +72,23 @@ function M.open(file, opts) end end - local float = require("lazy.util").float({ - file = file, - size = { width = 0.6, height = 0.6 }, + Snacks.config.style("news", { + 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 + + local float = Snacks.win({ + file = file, + style = "news", + }) + if vim.diagnostic.enable then pcall(vim.diagnostic.enable, false, { bufnr = float.buf }) else diff --git a/lua/lazyvim/util/plugin.lua b/lua/lazyvim/util/plugin.lua index 92c75540..f4a933e9 100644 --- a/lua/lazyvim/util/plugin.lua +++ b/lua/lazyvim/util/plugin.lua @@ -71,31 +71,6 @@ function M.extra_idx(name) end function M.lazy_file() - -- This autocmd will only trigger when a file was loaded from the cmdline. - -- It will render the file as quickly as possible. - vim.api.nvim_create_autocmd("BufReadPost", { - once = true, - callback = function(event) - -- Skip if we already entered vim - if vim.v.vim_did_enter == 1 then - return - end - - -- Try to guess the filetype (may change later on during Neovim startup) - local ft = vim.filetype.match({ buf = event.buf }) - if ft then - -- Add treesitter highlights and fallback to syntax - local lang = vim.treesitter.language.get_lang(ft) - if not (lang and pcall(vim.treesitter.start, event.buf, lang)) then - vim.bo[event.buf].syntax = ft - end - - -- Trigger early redraw - vim.cmd([[redraw]]) - end - end, - }) - -- Add support for the LazyFile event local Event = require("lazy.core.handler.event") diff --git a/lua/lazyvim/util/terminal.lua b/lua/lazyvim/util/terminal.lua index 87f8553b..b9345c1d 100644 --- a/lua/lazyvim/util/terminal.lua +++ b/lua/lazyvim/util/terminal.lua @@ -1,13 +1,5 @@ ---@class lazyvim.util.terminal ----@overload fun(cmd: string|string[], opts: LazyTermOpts): LazyFloat -local M = setmetatable({}, { - __call = function(m, ...) - return m.open(...) - end, -}) - ----@type table -local terminals = {} +local M = {} ---@param shell? string function M.setup(shell) @@ -40,58 +32,4 @@ 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) ----@param cmd? string[]|string ----@param opts? LazyTermOpts -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] -end - return M diff --git a/lua/lazyvim/util/toggle.lua b/lua/lazyvim/util/toggle.lua deleted file mode 100644 index d693dd9d..00000000 --- a/lua/lazyvim/util/toggle.lua +++ /dev/null @@ -1,210 +0,0 @@ ----@class lazyvim.util.toggle -local M = {} - ----@class lazyvim.Toggle ----@field name string ----@field icon? string ----@field color_enabled? string ----@field color_disabled? string ----@field get fun():boolean ----@field set fun(state:boolean) - ----@class lazyvim.Toggle.wrap: lazyvim.Toggle ----@operator call:boolean - ----@param toggle lazyvim.Toggle -function M.wrap(toggle) - return setmetatable(toggle, { - __call = function() - toggle.set(not toggle.get()) - local state = toggle.get() - if state then - LazyVim.info("Enabled " .. toggle.name, { title = toggle.name }) - else - LazyVim.warn("Disabled " .. toggle.name, { title = toggle.name }) - end - return state - end, - }) --[[@as lazyvim.Toggle.wrap]] -end - ----@param lhs string ----@param toggle lazyvim.Toggle -function M.map(lhs, toggle) - local t = M.wrap(toggle) - LazyVim.safe_keymap_set("n", lhs, function() - t() - end, { desc = "Toggle " .. toggle.name }) - M.wk(lhs, toggle) -end - -function M.wk(lhs, toggle) - if not LazyVim.has("which-key.nvim") then - return - end - local function safe_get() - local ok, enabled = pcall(toggle.get) - if not ok then - LazyVim.error({ "Failed to get toggle state for **" .. toggle.name .. "**:\n", enabled }, { once = true }) - end - return enabled - end - require("which-key").add({ - { - lhs, - icon = function() - return safe_get() and { icon = toggle.icon or " ", color = toggle.color_enabled or "green" } - or { icon = toggle.icon or " ", color = toggle.color_disabled or "yellow" } - end, - desc = function() - return (safe_get() and "Disable " or "Enable ") .. toggle.name - end, - }, - }) -end - -M.treesitter = M.wrap({ - name = "Treesitter Highlight", - get = function() - return vim.b.ts_highlight - end, - set = function(state) - if state then - vim.treesitter.start() - else - vim.treesitter.stop() - end - end, -}) - ----@param buf? boolean -function M.format(buf) - return M.wrap({ - name = "Auto Format (" .. (buf and "Buffer" or "Global") .. ")", - get = function() - if not buf then - return vim.g.autoformat == nil or vim.g.autoformat - end - return LazyVim.format.enabled() - end, - set = function(state) - LazyVim.format.enable(state, buf) - end, - }) -end - ----@param opts? {values?: {[1]:any, [2]:any}, name?: string} -function M.option(option, opts) - opts = opts or {} - local name = opts.name or option - local on = opts.values and opts.values[2] or true - local off = opts.values and opts.values[1] or false - return M.wrap({ - name = name, - get = function() - return vim.opt_local[option]:get() == on - end, - set = function(state) - vim.opt_local[option] = state and on or off - end, - }) -end - -local nu = { number = true, relativenumber = true } -M.number = M.wrap({ - name = "Line Numbers", - get = function() - return vim.opt_local.number:get() or vim.opt_local.relativenumber:get() - end, - set = function(state) - if state then - vim.opt_local.number = nu.number - vim.opt_local.relativenumber = nu.relativenumber - else - nu = { number = vim.opt_local.number:get(), relativenumber = vim.opt_local.relativenumber:get() } - vim.opt_local.number = false - vim.opt_local.relativenumber = false - end - end, -}) - -M.diagnostics = M.wrap({ - name = "Diagnostics", - get = function() - local enabled = false - if vim.diagnostic.is_enabled then - enabled = vim.diagnostic.is_enabled() - elseif vim.diagnostic.is_disabled then - enabled = not vim.diagnostic.is_disabled() - end - return enabled - end, - set = function(state) - if vim.fn.has("nvim-0.10") == 0 then - if state then - pcall(vim.diagnostic.enable) - else - pcall(vim.diagnostic.disable) - end - else - vim.diagnostic.enable(state) - end - end, -}) - -M.inlay_hints = M.wrap({ - name = "Inlay Hints", - get = function() - return vim.lsp.inlay_hint.is_enabled({ bufnr = 0 }) - end, - set = function(state) - vim.lsp.inlay_hint.enable(state, { bufnr = 0 }) - end, -}) - ----@type {k:string, v:any}[] -M._maximized = nil -M.maximize = M.wrap({ - name = "Maximize", - get = function() - return M._maximized ~= nil - end, - set = function(state) - if state then - M._maximized = {} - local function set(k, v) - table.insert(M._maximized, 1, { k = k, v = vim.o[k] }) - vim.o[k] = v - end - set("winwidth", 999) - set("winheight", 999) - set("winminwidth", 10) - set("winminheight", 4) - vim.cmd("wincmd =") - -- `QuitPre` seems to be executed even if we quit a normal window, so we don't want that - -- `VimLeavePre` might be another consideration? Not sure about differences between the 2 - vim.api.nvim_create_autocmd("ExitPre", { - once = true, - group = vim.api.nvim_create_augroup("lazyvim_restore_max_exit_pre", { clear = true }), - desc = "Restore width/height when close Neovim while maximized", - callback = function() - M.maximize.set(false) - end, - }) - else - for _, opt in ipairs(M._maximized) do - vim.o[opt.k] = opt.v - end - M._maximized = nil - vim.cmd("wincmd =") - end - end, -}) - -setmetatable(M, { - __call = function(m, ...) - return m.option(...) - end, -}) - -return M diff --git a/lua/lazyvim/util/ui.lua b/lua/lazyvim/util/ui.lua index 7c6780ea..14d0850f 100644 --- a/lua/lazyvim/util/ui.lua +++ b/lua/lazyvim/util/ui.lua @@ -1,77 +1,6 @@ ---@class lazyvim.util.ui local M = {} ----@alias Sign {name:string, text:string, texthl:string, priority:number} - --- Returns a list of regular and extmark signs sorted by priority (low to high) ----@return Sign[] ----@param buf number ----@param lnum number -function M.get_signs(buf, lnum) - -- Get regular signs - ---@type Sign[] - local signs = {} - - if vim.fn.has("nvim-0.10") == 0 then - -- Only needed for Neovim <0.10 - -- Newer versions include legacy signs in nvim_buf_get_extmarks - for _, sign in ipairs(vim.fn.sign_getplaced(buf, { group = "*", lnum = lnum })[1].signs) do - local ret = vim.fn.sign_getdefined(sign.name)[1] --[[@as Sign]] - if ret then - ret.priority = sign.priority - signs[#signs + 1] = ret - end - end - end - - -- Get extmark signs - local extmarks = vim.api.nvim_buf_get_extmarks( - buf, - -1, - { lnum - 1, 0 }, - { lnum - 1, -1 }, - { details = true, type = "sign" } - ) - for _, extmark in pairs(extmarks) do - signs[#signs + 1] = { - name = extmark[4].sign_hl_group or extmark[4].sign_name or "", - text = extmark[4].sign_text, - texthl = extmark[4].sign_hl_group, - priority = extmark[4].priority, - } - end - - -- Sort by priority - table.sort(signs, function(a, b) - return (a.priority or 0) < (b.priority or 0) - end) - - return signs -end - ----@return Sign? ----@param buf number ----@param lnum number -function M.get_mark(buf, lnum) - local marks = vim.fn.getmarklist(buf) - vim.list_extend(marks, vim.fn.getmarklist()) - for _, mark in ipairs(marks) do - if mark.pos[1] == buf and mark.pos[2] == lnum and mark.mark:match("[a-zA-Z]") then - return { text = mark.mark:sub(2), texthl = "DiagnosticHint" } - end - end -end - ----@param sign? Sign ----@param len? number -function M.icon(sign, len) - sign = sign or {} - len = len or 2 - local text = vim.fn.strcharpart(sign.text or "", 0, len) ---@type string - text = text .. string.rep(" ", len - vim.fn.strchars(text)) - return sign.texthl and ("%#" .. sign.texthl .. "#" .. text .. "%*") or text -end - function M.foldtext() local ok = pcall(vim.treesitter.get_parser, vim.api.nvim_get_current_buf()) local ret = ok and vim.treesitter.foldtext and vim.treesitter.foldtext() @@ -91,74 +20,6 @@ function M.foldtext() return ret end -function M.statuscolumn() - local win = vim.g.statusline_winid - local buf = vim.api.nvim_win_get_buf(win) - local is_file = vim.bo[buf].buftype == "" - local show_signs = vim.wo[win].signcolumn ~= "no" - - local components = { "", "", "" } -- left, middle, right - - local show_open_folds = vim.g.lazyvim_statuscolumn and vim.g.lazyvim_statuscolumn.folds_open - local use_githl = vim.g.lazyvim_statuscolumn and vim.g.lazyvim_statuscolumn.folds_githl - - if show_signs then - local signs = M.get_signs(buf, vim.v.lnum) - - ---@type Sign?,Sign?,Sign? - local left, right, fold, githl - for _, s in ipairs(signs) do - if s.name and (s.name:find("GitSign") or s.name:find("MiniDiffSign")) then - right = s - if use_githl then - githl = s["texthl"] - end - else - left = s - end - end - - vim.api.nvim_win_call(win, function() - if vim.fn.foldclosed(vim.v.lnum) >= 0 then - fold = { text = vim.opt.fillchars:get().foldclose or "", texthl = githl or "Folded" } - elseif - show_open_folds - and not LazyVim.ui.skip_foldexpr[buf] - and tostring(vim.treesitter.foldexpr(vim.v.lnum)):sub(1, 1) == ">" - then -- fold start - fold = { text = vim.opt.fillchars:get().foldopen or "", texthl = githl } - end - end) - -- Left: mark or non-git sign - components[1] = M.icon(M.get_mark(buf, vim.v.lnum) or left) - -- Right: fold icon or git sign (only if file) - components[3] = is_file and M.icon(fold or right) or "" - end - - -- Numbers in Neovim are weird - -- They show when either number or relativenumber is true - local is_num = vim.wo[win].number - local is_relnum = vim.wo[win].relativenumber - if (is_num or is_relnum) and vim.v.virtnum == 0 then - if vim.fn.has("nvim-0.11") == 1 then - components[2] = "%l" -- 0.11 handles both the current and other lines with %l - else - if vim.v.relnum == 0 then - components[2] = is_num and "%l" or "%r" -- the current line - else - components[2] = is_relnum and "%r" or "%l" -- other lines - end - end - components[2] = "%=" .. components[2] .. " " -- right align - end - - if vim.v.virtnum ~= 0 then - components[2] = "%= " - end - - return table.concat(components, "") -end - ---@return {fg?:string}? function M.fg(name) local color = M.color(name) @@ -224,47 +85,45 @@ function M.foldexpr() return "0" end ----@param buf number? -function M.bufremove(buf) - buf = buf or 0 - buf = buf == 0 and vim.api.nvim_get_current_buf() or buf - - if vim.bo.modified then - local choice = vim.fn.confirm(("Save changes to %q?"):format(vim.fn.bufname()), "&Yes\n&No\n&Cancel") - if choice == 0 or choice == 3 then -- 0 for / and 3 for Cancel - return - end - if choice == 1 then -- Yes - vim.cmd.write() - end - end - - for _, win in ipairs(vim.fn.win_findbuf(buf)) do - vim.api.nvim_win_call(win, function() - if not vim.api.nvim_win_is_valid(win) or vim.api.nvim_win_get_buf(win) ~= buf then - return +function M.maximize() + ---@type {k:string, v:any}[]? + local maximized = nil + return Snacks.toggle({ + name = "Maximize", + get = function() + return maximized ~= nil + end, + set = function(state) + if state then + maximized = {} + local function set(k, v) + table.insert(maximized, 1, { k = k, v = vim.o[k] }) + vim.o[k] = v + end + set("winwidth", 999) + set("winheight", 999) + set("winminwidth", 10) + set("winminheight", 4) + vim.cmd("wincmd =") + -- `QuitPre` seems to be executed even if we quit a normal window, so we don't want that + -- `VimLeavePre` might be another consideration? Not sure about differences between the 2 + vim.api.nvim_create_autocmd("ExitPre", { + once = true, + group = vim.api.nvim_create_augroup("lazyvim_restore_max_exit_pre", { clear = true }), + desc = "Restore width/height when close Neovim while maximized", + callback = function() + M.maximize.set(false) + end, + }) + else + for _, opt in ipairs(maximized) do + vim.o[opt.k] = opt.v + end + maximized = nil + vim.cmd("wincmd =") end - -- Try using alternate buffer - local alt = vim.fn.bufnr("#") - if alt ~= buf and vim.fn.buflisted(alt) == 1 then - vim.api.nvim_win_set_buf(win, alt) - return - end - - -- Try using previous buffer - local has_previous = pcall(vim.cmd, "bprevious") - if has_previous and buf ~= vim.api.nvim_win_get_buf(win) then - return - end - - -- Create new listed buffer - local new_buf = vim.api.nvim_create_buf(true, false) - vim.api.nvim_win_set_buf(win, new_buf) - end) - end - if vim.api.nvim_buf_is_valid(buf) then - pcall(vim.cmd, "bdelete! " .. buf) - end + end, + }) end return M diff --git a/tests/util/lazygit_spec.lua b/tests/util/lazygit_spec.lua deleted file mode 100644 index 518e886a..00000000 --- a/tests/util/lazygit_spec.lua +++ /dev/null @@ -1,42 +0,0 @@ ----@module "luassert" - -_G.LazyVim = require("lazyvim.util") - --- stylua: ignore -local git_remotes_cases = { - ["https://github.com/LazyVim/LazyVim.git"] = "https://github.com/LazyVim/LazyVim", - ["https://github.com/LazyVim/LazyVim"] = "https://github.com/LazyVim/LazyVim", - ["git@github.com:LazyVim/LazyVim"] = "https://github.com/LazyVim/LazyVim", - ["git@ssh.dev.azure.com:v3/neovim-org/owner/repo"] = "https://dev.azure.com/neovim-org/owner/_git/repo", - ["https://folkelemaitre@bitbucket.org/samiulazim/neovim.git"] = "https://bitbucket.org/samiulazim/neovim", - ["git@bitbucket.org:samiulazim/neovim.git"] = "https://bitbucket.org/samiulazim/neovim", - ["git@gitlab.com:inkscape/inkscape.git"] = "https://gitlab.com/inkscape/inkscape", - ["https://gitlab.com/inkscape/inkscape.git"] = "https://gitlab.com/inkscape/inkscape", - ["git@github.com:torvalds/linux.git"] = "https://github.com/torvalds/linux", - ["https://github.com/torvalds/linux.git"] = "https://github.com/torvalds/linux", - ["git@bitbucket.org:team/repo.git"] = "https://bitbucket.org/team/repo", - ["https://bitbucket.org/team/repo.git"] = "https://bitbucket.org/team/repo", - ["git@gitlab.com:example-group/example-project.git"] = "https://gitlab.com/example-group/example-project", - ["https://gitlab.com/example-group/example-project.git"] = "https://gitlab.com/example-group/example-project", - ["git@ssh.dev.azure.com:v3/org/project/repo"] = "https://dev.azure.com/org/project/_git/repo", - ["https://username@dev.azure.com/org/project/_git/repo"] = "https://dev.azure.com/org/project/_git/repo", - ["ssh://git@ghe.example.com:2222/org/repo.git"] = "https://ghe.example.com/org/repo", - ["https://ghe.example.com/org/repo.git"] = "https://ghe.example.com/org/repo", - ["git-codecommit.us-east-1.amazonaws.com/v1/repos/MyDemoRepo"] = "https://git-codecommit.us-east-1.amazonaws.com/v1/repos/MyDemoRepo", - ["https://git-codecommit.us-east-1.amazonaws.com/v1/repos/MyDemoRepo"] = "https://git-codecommit.us-east-1.amazonaws.com/v1/repos/MyDemoRepo", - ["ssh://git@source.developers.google.com:2022/p/project/r/repo"] = "https://source.developers.google.com/p/project/r/repo", - ["https://source.developers.google.com/p/project/r/repo"] = "https://source.developers.google.com/p/project/r/repo", - ["git@git.sr.ht:~user/repo"] = "https://git.sr.ht/~user/repo", - ["https://git.sr.ht/~user/repo"] = "https://git.sr.ht/~user/repo", - ["git@git.sr.ht:~user/another-repo"] = "https://git.sr.ht/~user/another-repo", - ["https://git.sr.ht/~user/another-repo"] = "https://git.sr.ht/~user/another-repo", -} - -describe("util.lazygit", function() - for remote, expected in pairs(git_remotes_cases) do - it("should parse git remote " .. remote, function() - local url = LazyVim.lazygit.get_url(remote) - assert.are.equal(expected, url) - end) - end -end)