Skip to content

Commit

Permalink
feat(stickybuf.nvim): stick to non-editable buffer (#334)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
linrongbin16 authored Oct 27, 2023
1 parent f9ace6d commit 5558286
Show file tree
Hide file tree
Showing 11 changed files with 163 additions and 326 deletions.
55 changes: 0 additions & 55 deletions lua/builtin/utils/keymap.lua
Original file line number Diff line number Diff line change
@@ -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<any, any>
--- @return table<any, any>
Expand Down Expand Up @@ -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,
}
Expand Down
6 changes: 3 additions & 3 deletions lua/configs/Pocco81/auto-save-nvim/keys.lua
Original file line number Diff line number Diff line change
@@ -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",
"<leader>as",
keymap.exec("ASToggle"),
"<cmd>ASToggle<cr>",
{ desc = "Toggle auto-save" }
),
}
Expand Down
32 changes: 15 additions & 17 deletions lua/configs/akinsho/bufferline-nvim/keys.lua
Original file line number Diff line number Diff line change
@@ -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",
"<leader>0",
keymap.exec("lua require('bufferline').go_to_buffer(-1, true)"),
"<cmd>lua require('bufferline').go_to_buffer(-1, true)<cr>",
{ desc = "Go to the last buffer" }
),

-- go to next/previous buffer
keymap.set_lazy_key(
set_lazy_key(
"n",
"]b",
keymap.exec("BufferLineCycleNext"),
"<cmd>BufferLineCycleNext<cr>",
{ desc = "Go to next buffer" }
),
keymap.set_lazy_key(
set_lazy_key(
"n",
"[b",
keymap.exec("BufferLineCyclePrev"),
"<cmd>BufferLineCyclePrev<cr>",
{ desc = "Go to previous buffer" }
),

-- move/re-order buffer to next/previous position
keymap.set_lazy_key(
set_lazy_key(
"n",
"<leader>.",
keymap.exec("BufferLineMoveNext"),
"<cmd>BufferLineMoveNext<cr>",
{ desc = "Move buffer to next" }
),
keymap.set_lazy_key(
set_lazy_key(
"n",
"<leader>,",
keymap.exec("BufferLineMovePrev"),
"<cmd>BufferLineMovePrev<cr>",
{ desc = "Move buffer to previous" }
),
}
Expand All @@ -42,14 +42,12 @@ local M = {
for i = 1, 9 do
table.insert(
M,
keymap.set_lazy_key(
set_lazy_key(
"n",
string.format("<leader>%d", i),
keymap.exec(
string.format(
"lua require('bufferline').go_to_buffer(%d, true)",
i
)
string.format(
"<cmd>lua require('bufferline').go_to_buffer(%d, true)<cr>",
i
),
{ desc = string.format("Go to buffer-%d", i) }
)
Expand Down
6 changes: 3 additions & 3 deletions lua/configs/folke/trouble-nvim/keys.lua
Original file line number Diff line number Diff line change
@@ -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",
"<leader>tb",
keymap.exec("TroubleToggle"),
"<cmd>TroubleToggle<cr>",
{ desc = "Toggle trouble (diagnostics)" }
),
}
Expand Down
19 changes: 6 additions & 13 deletions lua/configs/gbprod/yanky-nvim/keys.lua
Original file line number Diff line number Diff line change
@@ -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",
"<Plug>(YankyPutAfter)",
{ desc = "Put after cursor (yanky)" }
),
keymap.set_lazy_key(
set_lazy_key(
{ "n", "x" },
"P",
"<Plug>(YankyPutBefore)",
{ desc = "Put before cursor (yanky)" }
),
keymap.set_lazy_key(
set_lazy_key(
{ "n", "x" },
"gp",
"<Plug>(YankyGPutAfter)",
{ desc = "Put after cursor and leave the cursor after (yanky)" }
),
keymap.set_lazy_key(
set_lazy_key(
{ "n", "x" },
"gP",
"<Plug>(YankyGPutBefore)",
{ desc = "Put before cursor and leave the cursor after (yanky)" }
),
-- yank
keymap.set_lazy_key(
set_lazy_key(
{ "n", "x" },
"y",
"<Plug>(YankyYank)",
{ desc = "Yank (yanky)" }
),
-- history
keymap.set_lazy_key(
"n",
"<leader>yh",
keymap.exec("YankyRingHistory"),
{ desc = "Yank ring history" }
),
}

return M
70 changes: 35 additions & 35 deletions lua/configs/linrongbin16/fzfx-nvim/keys.lua
Original file line number Diff line number Diff line change
@@ -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",
"<space>f",
keymap.exec("FzfxFiles"),
"<cmd>FzfxFiles<cr>",
{ desc = "Find files" }
),
keymap.set_lazy_key(
set_lazy_key(
"x",
"<space>f",
keymap.exec("FzfxFilesV"),
"<cmd>FzfxFilesV<cr>",
{ desc = "Find files" }
),
-- find files by cursor word
keymap.set_lazy_key(
set_lazy_key(
"n",
"<space>wf",
keymap.exec("FzfxFilesW"),
"<cmd>FzfxFilesW<cr>",
{ desc = "Find files by cursor word" }
),
keymap.set_lazy_key(
set_lazy_key(
"n",
"<space>pf",
keymap.exec("FzfxFilesP"),
"<cmd>FzfxFilesP<cr>",
{ desc = "Find files by yank text" }
),
-- search buffer
keymap.set_lazy_key(
set_lazy_key(
"n",
"<space>bf",
keymap.exec("FzfxBuffers"),
"<cmd>FzfxBuffers<cr>",
{ desc = "Search buffers" }
),
keymap.set_lazy_key(
set_lazy_key(
"x",
"<space>bf",
keymap.exec("FzfxBuffersV"),
"<cmd>FzfxBuffersV<cr>",
{ desc = "Search buffers" }
),
-- live grep
keymap.set_lazy_key(
set_lazy_key(
"n",
"<space>l",
keymap.exec("FzfxLiveGrep"),
"<cmd>FzfxLiveGrep<cr>",
{ desc = "Live grep" }
),
keymap.set_lazy_key(
set_lazy_key(
"x",
"<space>l",
keymap.exec("FzfxLiveGrepV"),
"<cmd>FzfxLiveGrepV<cr>",
{ desc = "Live grep" }
),
-- search word
keymap.set_lazy_key(
set_lazy_key(
"n",
"<space>wl",
keymap.exec("FzfxLiveGrepW"),
"<cmd>FzfxLiveGrepW<cr>",
{ desc = "Live grep by cursor word" }
),
keymap.set_lazy_key(
set_lazy_key(
"n",
"<space>pl",
keymap.exec("FzfxLiveGrepP"),
"<cmd>FzfxLiveGrepP<cr>",
{ desc = "Live grep by yank text" }
),
-- git
keymap.set_lazy_key(
set_lazy_key(
"n",
"<space>gf",
keymap.exec("FzfxGFiles"),
"<cmd>FzfxGFiles<cr>",
{ desc = "Search git files" }
),
keymap.set_lazy_key(
set_lazy_key(
"n",
"<space>br",
keymap.exec("FzfxGBranches"),
"<cmd>FzfxGBranches<cr>",
{ desc = "Search git branches" }
),
keymap.set_lazy_key(
set_lazy_key(
"n",
"<space>gc",
keymap.exec("FzfxGCommits"),
"<cmd>FzfxGCommits<cr>",
{ desc = "Search git commits" }
),
-- vim commands
keymap.set_lazy_key(
set_lazy_key(
"n",
"<space>cm",
keymap.exec("FzfxCommands"),
"<cmd>FzfxCommands<cr>",
{ desc = "Search vim commands" }
),
-- diagnostics
keymap.set_lazy_key(
set_lazy_key(
"n",
"<space>dg",
keymap.exec("FzfxLspDiagnostics"),
"<cmd>FzfxLspDiagnostics<cr>",
{ desc = "Search lsp diagnostics" }
),
keymap.set_lazy_key(
set_lazy_key(
"x",
"<space>dg",
keymap.exec("FzfxLspDiagnosticsV"),
"<cmd>FzfxLspDiagnosticsV<cr>",
{ desc = "Search lsp diagnostics" }
),
-- file explorer
keymap.set_lazy_key(
set_lazy_key(
"n",
"<space>xp",
keymap.exec("FzfxFileExplorer"),
"<cmd>FzfxFileExplorer<cr>",
{ desc = "File Explorer" }
),
}
Expand Down
10 changes: 5 additions & 5 deletions lua/configs/moll/vim-bbye/keys.lua
Original file line number Diff line number Diff line change
@@ -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",
"<leader>bd",
keymap.exec("Bdelete"),
"<cmd>Bdelete<cr>",
{ desc = "Close buffer" }
),
keymap.set_lazy_key(
set_lazy_key(
"n",
"<leader>bD",
keymap.exec("Bdelete!"),
"<cmd>Bdelete!<cr>",
{ desc = "Close buffer forcibly!" }
),
}
Expand Down
Loading

0 comments on commit 5558286

Please sign in to comment.