From 55582869341ac5a2c061419aedd14794e030d5a3 Mon Sep 17 00:00:00 2001 From: linrongbin16 Date: Fri, 27 Oct 2023 17:00:45 +0800 Subject: [PATCH] feat(stickybuf.nvim): stick to non-editable buffer (#334) * perf(stickybuf.nvim): stick to buf * perf(stickybuf.nvim): migrate 'keymap.exec' to sticky * feat(stickybuf.nvim): stick to buf * feat(stickybuf.nvim): migrate to sticky --- lua/builtin/utils/keymap.lua | 55 ---- lua/configs/Pocco81/auto-save-nvim/keys.lua | 6 +- lua/configs/akinsho/bufferline-nvim/keys.lua | 32 +-- lua/configs/folke/trouble-nvim/keys.lua | 6 +- lua/configs/gbprod/yanky-nvim/keys.lua | 19 +- lua/configs/linrongbin16/fzfx-nvim/keys.lua | 70 ++--- lua/configs/moll/vim-bbye/keys.lua | 10 +- .../nvim-telescope/telescope-nvim/keys.lua | 272 ++++++------------ lua/configs/stevearc/oil-nvim/keys.lua | 13 +- .../stevearc/stickybuf-nvim/config.lua | 1 + lua/plugins/init.lua | 5 + 11 files changed, 163 insertions(+), 326 deletions(-) create mode 100644 lua/configs/stevearc/stickybuf-nvim/config.lua diff --git a/lua/builtin/utils/keymap.lua b/lua/builtin/utils/keymap.lua index d3d8dd77..1d507c58 100644 --- a/lua/builtin/utils/keymap.lua +++ b/lua/builtin/utils/keymap.lua @@ -1,59 +1,5 @@ -- ---- Key Map ---- ---- @type string[] -local NON_EDITABLE_FIELTYPES = { - ["neo-tree"] = true, - ["NvimTree"] = true, - ["undotree"] = true, - ["vista"] = true, - ["diff"] = true, - ["CHADTree"] = true, -} - ---- @return boolean -local function invalid_window() - local buf_filetype = vim.bo.filetype - return NON_EDITABLE_FIELTYPES[buf_filetype] ~= nil - and NON_EDITABLE_FIELTYPES[buf_filetype] == true -end - ---- @param cmd string|function ---- @return string|function -local function exec(cmd) - --- @param o string|function - local function exec_impl(o) - if type(o) == "string" then - -- vim command - vim.cmd(o) - else - -- lua function - o() - end - end - - --- @return nil - local function wrap() - local n = vim.fn.winnr("$") - local i = 0 - while i < n do - i = i + 1 - if invalid_window() then - -- current window is invalid - -- go to next window - vim.cmd("wincmd w") - else - exec_impl(cmd) - return - end - end - -- finally cannot find a valid window - -- execute command anyway - exec_impl(cmd) - end - - return wrap -end - --- @param rhs string|function|nil --- @param opts table --- @return table @@ -99,7 +45,6 @@ local function set_lazy_key(mode, lhs, rhs, opts) end local M = { - exec = exec, set_key = set_key, set_lazy_key = set_lazy_key, } diff --git a/lua/configs/Pocco81/auto-save-nvim/keys.lua b/lua/configs/Pocco81/auto-save-nvim/keys.lua index edbed5c5..7d361c0f 100644 --- a/lua/configs/Pocco81/auto-save-nvim/keys.lua +++ b/lua/configs/Pocco81/auto-save-nvim/keys.lua @@ -1,10 +1,10 @@ -local keymap = require("builtin.utils.keymap") +local set_lazy_key = require("builtin.utils.keymap").set_lazy_key local M = { - keymap.set_lazy_key( + set_lazy_key( "n", "as", - keymap.exec("ASToggle"), + "ASToggle", { desc = "Toggle auto-save" } ), } diff --git a/lua/configs/akinsho/bufferline-nvim/keys.lua b/lua/configs/akinsho/bufferline-nvim/keys.lua index 09ce73bb..91e77fa8 100644 --- a/lua/configs/akinsho/bufferline-nvim/keys.lua +++ b/lua/configs/akinsho/bufferline-nvim/keys.lua @@ -1,39 +1,39 @@ -local keymap = require("builtin.utils.keymap") +local set_lazy_key = require("builtin.utils.keymap").set_lazy_key local M = { -- go to the last buffer - keymap.set_lazy_key( + set_lazy_key( "n", "0", - keymap.exec("lua require('bufferline').go_to_buffer(-1, true)"), + "lua require('bufferline').go_to_buffer(-1, true)", { desc = "Go to the last buffer" } ), -- go to next/previous buffer - keymap.set_lazy_key( + set_lazy_key( "n", "]b", - keymap.exec("BufferLineCycleNext"), + "BufferLineCycleNext", { desc = "Go to next buffer" } ), - keymap.set_lazy_key( + set_lazy_key( "n", "[b", - keymap.exec("BufferLineCyclePrev"), + "BufferLineCyclePrev", { desc = "Go to previous buffer" } ), -- move/re-order buffer to next/previous position - keymap.set_lazy_key( + set_lazy_key( "n", ".", - keymap.exec("BufferLineMoveNext"), + "BufferLineMoveNext", { desc = "Move buffer to next" } ), - keymap.set_lazy_key( + set_lazy_key( "n", ",", - keymap.exec("BufferLineMovePrev"), + "BufferLineMovePrev", { desc = "Move buffer to previous" } ), } @@ -42,14 +42,12 @@ local M = { for i = 1, 9 do table.insert( M, - keymap.set_lazy_key( + set_lazy_key( "n", string.format("%d", i), - keymap.exec( - string.format( - "lua require('bufferline').go_to_buffer(%d, true)", - i - ) + string.format( + "lua require('bufferline').go_to_buffer(%d, true)", + i ), { desc = string.format("Go to buffer-%d", i) } ) diff --git a/lua/configs/folke/trouble-nvim/keys.lua b/lua/configs/folke/trouble-nvim/keys.lua index 86481696..0b7c9d8d 100644 --- a/lua/configs/folke/trouble-nvim/keys.lua +++ b/lua/configs/folke/trouble-nvim/keys.lua @@ -1,10 +1,10 @@ -local keymap = require("builtin.utils.keymap") +local set_lazy_key = require("builtin.utils.keymap").set_lazy_key local M = { - keymap.set_lazy_key( + set_lazy_key( "n", "tb", - keymap.exec("TroubleToggle"), + "TroubleToggle", { desc = "Toggle trouble (diagnostics)" } ), } diff --git a/lua/configs/gbprod/yanky-nvim/keys.lua b/lua/configs/gbprod/yanky-nvim/keys.lua index 47bb5739..26403597 100644 --- a/lua/configs/gbprod/yanky-nvim/keys.lua +++ b/lua/configs/gbprod/yanky-nvim/keys.lua @@ -1,45 +1,38 @@ -local keymap = require("builtin.utils.keymap") +local set_lazy_key = require("builtin.utils.keymap").set_lazy_key local M = { -- put - keymap.set_lazy_key( + set_lazy_key( { "n", "x" }, "p", "(YankyPutAfter)", { desc = "Put after cursor (yanky)" } ), - keymap.set_lazy_key( + set_lazy_key( { "n", "x" }, "P", "(YankyPutBefore)", { desc = "Put before cursor (yanky)" } ), - keymap.set_lazy_key( + set_lazy_key( { "n", "x" }, "gp", "(YankyGPutAfter)", { desc = "Put after cursor and leave the cursor after (yanky)" } ), - keymap.set_lazy_key( + set_lazy_key( { "n", "x" }, "gP", "(YankyGPutBefore)", { desc = "Put before cursor and leave the cursor after (yanky)" } ), -- yank - keymap.set_lazy_key( + set_lazy_key( { "n", "x" }, "y", "(YankyYank)", { desc = "Yank (yanky)" } ), - -- history - keymap.set_lazy_key( - "n", - "yh", - keymap.exec("YankyRingHistory"), - { desc = "Yank ring history" } - ), } return M diff --git a/lua/configs/linrongbin16/fzfx-nvim/keys.lua b/lua/configs/linrongbin16/fzfx-nvim/keys.lua index 6f3e9057..1fde5d20 100644 --- a/lua/configs/linrongbin16/fzfx-nvim/keys.lua +++ b/lua/configs/linrongbin16/fzfx-nvim/keys.lua @@ -1,115 +1,115 @@ -local keymap = require("builtin.utils.keymap") +local set_lazy_key = require("builtin.utils.keymap").set_lazy_key local M = { -- find files - keymap.set_lazy_key( + set_lazy_key( "n", "f", - keymap.exec("FzfxFiles"), + "FzfxFiles", { desc = "Find files" } ), - keymap.set_lazy_key( + set_lazy_key( "x", "f", - keymap.exec("FzfxFilesV"), + "FzfxFilesV", { desc = "Find files" } ), -- find files by cursor word - keymap.set_lazy_key( + set_lazy_key( "n", "wf", - keymap.exec("FzfxFilesW"), + "FzfxFilesW", { desc = "Find files by cursor word" } ), - keymap.set_lazy_key( + set_lazy_key( "n", "pf", - keymap.exec("FzfxFilesP"), + "FzfxFilesP", { desc = "Find files by yank text" } ), -- search buffer - keymap.set_lazy_key( + set_lazy_key( "n", "bf", - keymap.exec("FzfxBuffers"), + "FzfxBuffers", { desc = "Search buffers" } ), - keymap.set_lazy_key( + set_lazy_key( "x", "bf", - keymap.exec("FzfxBuffersV"), + "FzfxBuffersV", { desc = "Search buffers" } ), -- live grep - keymap.set_lazy_key( + set_lazy_key( "n", "l", - keymap.exec("FzfxLiveGrep"), + "FzfxLiveGrep", { desc = "Live grep" } ), - keymap.set_lazy_key( + set_lazy_key( "x", "l", - keymap.exec("FzfxLiveGrepV"), + "FzfxLiveGrepV", { desc = "Live grep" } ), -- search word - keymap.set_lazy_key( + set_lazy_key( "n", "wl", - keymap.exec("FzfxLiveGrepW"), + "FzfxLiveGrepW", { desc = "Live grep by cursor word" } ), - keymap.set_lazy_key( + set_lazy_key( "n", "pl", - keymap.exec("FzfxLiveGrepP"), + "FzfxLiveGrepP", { desc = "Live grep by yank text" } ), -- git - keymap.set_lazy_key( + set_lazy_key( "n", "gf", - keymap.exec("FzfxGFiles"), + "FzfxGFiles", { desc = "Search git files" } ), - keymap.set_lazy_key( + set_lazy_key( "n", "br", - keymap.exec("FzfxGBranches"), + "FzfxGBranches", { desc = "Search git branches" } ), - keymap.set_lazy_key( + set_lazy_key( "n", "gc", - keymap.exec("FzfxGCommits"), + "FzfxGCommits", { desc = "Search git commits" } ), -- vim commands - keymap.set_lazy_key( + set_lazy_key( "n", "cm", - keymap.exec("FzfxCommands"), + "FzfxCommands", { desc = "Search vim commands" } ), -- diagnostics - keymap.set_lazy_key( + set_lazy_key( "n", "dg", - keymap.exec("FzfxLspDiagnostics"), + "FzfxLspDiagnostics", { desc = "Search lsp diagnostics" } ), - keymap.set_lazy_key( + set_lazy_key( "x", "dg", - keymap.exec("FzfxLspDiagnosticsV"), + "FzfxLspDiagnosticsV", { desc = "Search lsp diagnostics" } ), -- file explorer - keymap.set_lazy_key( + set_lazy_key( "n", "xp", - keymap.exec("FzfxFileExplorer"), + "FzfxFileExplorer", { desc = "File Explorer" } ), } diff --git a/lua/configs/moll/vim-bbye/keys.lua b/lua/configs/moll/vim-bbye/keys.lua index 8e046716..27d1d79f 100644 --- a/lua/configs/moll/vim-bbye/keys.lua +++ b/lua/configs/moll/vim-bbye/keys.lua @@ -1,16 +1,16 @@ -local keymap = require("builtin.utils.keymap") +local set_lazy_key = require("builtin.utils.keymap").set_lazy_key local M = { - keymap.set_lazy_key( + set_lazy_key( "n", "bd", - keymap.exec("Bdelete"), + "Bdelete", { desc = "Close buffer" } ), - keymap.set_lazy_key( + set_lazy_key( "n", "bD", - keymap.exec("Bdelete!"), + "Bdelete!", { desc = "Close buffer forcibly!" } ), } diff --git a/lua/configs/nvim-telescope/telescope-nvim/keys.lua b/lua/configs/nvim-telescope/telescope-nvim/keys.lua index aa921cff..84a944da 100644 --- a/lua/configs/nvim-telescope/telescope-nvim/keys.lua +++ b/lua/configs/nvim-telescope/telescope-nvim/keys.lua @@ -1,199 +1,99 @@ -local keymap = require("builtin.utils.keymap") +local set_lazy_key = require("builtin.utils.keymap").set_lazy_key local FIND = vim.fn.executable("fdfind") > 0 and "fdfind" or "fd" local M = { -- search files - keymap.set_lazy_key( - "n", - "f", - keymap.exec(function() - require("telescope.builtin").find_files({ - find_command = { - FIND, - "-cnever", -- --color=never - "-tf", -- --type file - "-tl", -- --type symlink - "-L", -- --follow - "-i", -- --ignore-case - }, - }) - end), - { desc = "Search files" } - ), - keymap.set_lazy_key( - "n", - "uf", - keymap.exec(function() - require("telescope.builtin").find_files({ - find_command = { - FIND, - "-cnever", -- --color=never - "-tf", -- --type file - "-tl", -- --type symlink - "-L", -- --follow - "-i", -- --ignore-case - "-u", -- --unrestricted (--hidden --no-ignore) - }, - }) - end), - { desc = "Unrestricted search files" } - ), + set_lazy_key("n", "f", function() + require("telescope.builtin").find_files({ + find_command = { + FIND, + "-cnever", -- --color=never + "-tf", -- --type file + "-tl", -- --type symlink + "-L", -- --follow + "-i", -- --ignore-case + }, + }) + end, { desc = "Search files" }), + set_lazy_key("n", "uf", function() + require("telescope.builtin").find_files({ + find_command = { + FIND, + "-cnever", -- --color=never + "-tf", -- --type file + "-tl", -- --type symlink + "-L", -- --follow + "-i", -- --ignore-case + "-u", -- --unrestricted (--hidden --no-ignore) + }, + }) + end, { desc = "Unrestricted search files" }), -- search buffers - keymap.set_lazy_key( - "n", - "b", - keymap.exec(function() - require("telescope.builtin").buffers() - end), - { desc = "Search buffers" } - ), + set_lazy_key("n", "b", function() + require("telescope.builtin").buffers() + end, { desc = "Search buffers" }), -- live grep - keymap.set_lazy_key( - "n", - "l", - keymap.exec(function() - require("telescope").extensions.live_grep_args.live_grep_args() - end), - { desc = "Live grep" } - ), - keymap.set_lazy_key( - "n", - "ul", - keymap.exec(function() - require("telescope").extensions.live_grep_args.live_grep_args({ - additional_args = { "-uu" }, -- --unrestricted (--hidden --no-ignore) - }) - end), - { desc = "Unrestricted live grep" } - ), + set_lazy_key("n", "l", function() + require("telescope").extensions.live_grep_args.live_grep_args() + end, { desc = "Live grep" }), + set_lazy_key("n", "ul", function() + require("telescope").extensions.live_grep_args.live_grep_args({ + additional_args = { "-uu" }, -- --unrestricted (--hidden --no-ignore) + }) + end, { desc = "Unrestricted live grep" }), -- search word - keymap.set_lazy_key( - "n", - "w", - keymap.exec(function() - require("telescope.builtin").grep_string() - end), - { desc = "Search word under cursor" } - ), - keymap.set_lazy_key( - "n", - "uw", - keymap.exec(function() - require("telescope.builtin").grep_string({ - additional_args = { "-uu" }, -- --unrestricted (--hidden --no-ignore) - }) - end), - { desc = "Unrestricted search word under cursor" } - ), + set_lazy_key("n", "w", function() + require("telescope.builtin").grep_string() + end, { desc = "Search word under cursor" }), + set_lazy_key("n", "uw", function() + require("telescope.builtin").grep_string({ + additional_args = { "-uu" }, -- --unrestricted (--hidden --no-ignore) + }) + end, { desc = "Unrestricted search word under cursor" }), -- search git - keymap.set_lazy_key( - "n", - "gf", - keymap.exec(function() - require("telescope.builtin").git_files() - end), - { desc = "Search git files" } - ), - keymap.set_lazy_key( - "n", - "gc", - keymap.exec(function() - require("telescope.builtin").git_commits() - end), - { desc = "Search git commits" } - ), - keymap.set_lazy_key( - "n", - "gb", - keymap.exec(function() - require("telescope.builtin").git_branches() - end), - { desc = "Search git branches" } - ), - keymap.set_lazy_key( - "n", - "gs", - keymap.exec(function() - require("telescope.builtin").git_status() - end), - { desc = "Search git status" } - ), + set_lazy_key("n", "gf", function() + require("telescope.builtin").git_files() + end, { desc = "Search git files" }), + set_lazy_key("n", "gc", function() + require("telescope.builtin").git_commits() + end, { desc = "Search git commits" }), + set_lazy_key("n", "gb", function() + require("telescope.builtin").git_branches() + end, { desc = "Search git branches" }), + set_lazy_key("n", "gs", function() + require("telescope.builtin").git_status() + end, { desc = "Search git status" }), -- search diagnostics - keymap.set_lazy_key( - "n", - "dw", - keymap.exec(function() - require("telescope.builtin").diagnostics() - end), - { desc = "Search workspace diagnostics" } - ), - keymap.set_lazy_key( - "n", - "dd", - keymap.exec(function() - require("telescope.builtin").diagnostics({ - bufnr = 0, - }) - end), - { desc = "Search document diagnostics" } - ), + set_lazy_key("n", "dw", function() + require("telescope.builtin").diagnostics() + end, { desc = "Search workspace diagnostics" }), + set_lazy_key("n", "dd", function() + require("telescope.builtin").diagnostics({ + bufnr = 0, + }) + end, { desc = "Search document diagnostics" }), -- search vim - keymap.set_lazy_key( - "n", - "cm", - keymap.exec(function() - require("telescope.builtin").commands() - end), - { desc = "Search vim commands" } - ), - keymap.set_lazy_key( - "n", - "tg", - keymap.exec(function() - require("telescope.builtin").tags() - end), - { desc = "Search vim tags" } - ), - keymap.set_lazy_key( - "n", - "ch", - keymap.exec(function() - require("telescope.builtin").command_history() - end), - { desc = "Search vim command history" } - ), - keymap.set_lazy_key( - "n", - "sh", - keymap.exec(function() - require("telescope.builtin").search_history() - end), - { desc = "Search vim search history" } - ), - keymap.set_lazy_key( - "n", - "mk", - keymap.exec(function() - require("telescope.builtin").marks() - end), - { desc = "Search vim marks" } - ), - keymap.set_lazy_key( - "n", - "km", - keymap.exec(function() - require("telescope.builtin").keymaps() - end), - { desc = "Search vim key mappings" } - ), - keymap.set_lazy_key( - "n", - "ud", - keymap.exec(function() - require("telescope").extensions.undo.undo() - end), - { desc = "Search vim undo tree" } - ), + set_lazy_key("n", "cm", function() + require("telescope.builtin").commands() + end, { desc = "Search vim commands" }), + set_lazy_key("n", "tg", function() + require("telescope.builtin").tags() + end, { desc = "Search vim tags" }), + set_lazy_key("n", "ch", function() + require("telescope.builtin").command_history() + end, { desc = "Search vim command history" }), + set_lazy_key("n", "sh", function() + require("telescope.builtin").search_history() + end, { desc = "Search vim search history" }), + set_lazy_key("n", "mk", function() + require("telescope.builtin").marks() + end, { desc = "Search vim marks" }), + set_lazy_key("n", "km", function() + require("telescope.builtin").keymaps() + end, { desc = "Search vim key mappings" }), + set_lazy_key("n", "ud", function() + require("telescope").extensions.undo.undo() + end, { desc = "Search vim undo tree" }), } return M diff --git a/lua/configs/stevearc/oil-nvim/keys.lua b/lua/configs/stevearc/oil-nvim/keys.lua index 4ac9c7d1..e824e638 100644 --- a/lua/configs/stevearc/oil-nvim/keys.lua +++ b/lua/configs/stevearc/oil-nvim/keys.lua @@ -1,14 +1,9 @@ -local keymap = require("builtin.utils.keymap") +local set_lazy_key = require("builtin.utils.keymap").set_lazy_key local M = { - keymap.set_lazy_key( - "n", - "ol", - keymap.exec(function() - require("oil").open() - end), - { desc = "Open oil file manager" } - ), + set_lazy_key("n", "ol", function() + require("oil").open() + end, { desc = "Open oil file manager" }), } return M diff --git a/lua/configs/stevearc/stickybuf-nvim/config.lua b/lua/configs/stevearc/stickybuf-nvim/config.lua new file mode 100644 index 00000000..94b3368a --- /dev/null +++ b/lua/configs/stevearc/stickybuf-nvim/config.lua @@ -0,0 +1 @@ +require("stickybuf").setup() diff --git a/lua/plugins/init.lua b/lua/plugins/init.lua index 9cf40f8a..6c222625 100644 --- a/lua/plugins/init.lua +++ b/lua/plugins/init.lua @@ -159,6 +159,11 @@ local M = { dependencies = { "junegunn/fzf" }, config = lua_config("stevearc/dressing.nvim"), }, + { + "stevearc/stickybuf.nvim", + event = { UIEnter }, + config = lua_config("stevearc/stickybuf.nvim"), + }, -- ---- SEARCH ----