From 0b4b5c653c01189ff19e35c5b890605d976647dd Mon Sep 17 00:00:00 2001 From: aloslider <53711835+aloslider@users.noreply.github.com> Date: Fri, 20 Nov 2026 01:48:40 +0300 Subject: [PATCH] [nvim] Add cheatsheet.nvim and a its picker for fzf-lua --- dot_config/nvim/benq/lazy-lock.json | 1 + .../benq/lua/plugins/features/fzf-lua.lua | 314 +++++++++++------- 2 files changed, 195 insertions(+), 120 deletions(-) diff --git a/dot_config/nvim/benq/lazy-lock.json b/dot_config/nvim/benq/lazy-lock.json index d2159e3..1c2d15c 100644 --- a/dot_config/nvim/benq/lazy-lock.json +++ b/dot_config/nvim/benq/lazy-lock.json @@ -2,6 +2,7 @@ "FixCursorHold.nvim": { "branch": "master", "commit": "1900f89dc17c603eec29960f57c00bd9ae696495" }, "blink.cmp": { "branch": "main", "commit": "78336bc89ee5365633bcf754d93df01678b5c08f" }, "bufferline.nvim": { "branch": "main", "commit": "655133c3b4c3e5e05ec549b9f8cc2894ac6f51b3" }, + "cheatsheet.nvim": { "branch": "master", "commit": "6a5c69d42013331a3eda70b340378f2f912b9a90" }, "conform.nvim": { "branch": "master", "commit": "016802de402556da54c36bd7359b441266b01cdd" }, "diffview.nvim": { "branch": "main", "commit": "4516612fe98ff56ae0415a259ff6361a89419b0a" }, "easy-dotnet.nvim": { "branch": "main", "commit": "e698b942a0a91cd5b457b828d16a66b7cb94d732" }, diff --git a/dot_config/nvim/benq/lua/plugins/features/fzf-lua.lua b/dot_config/nvim/benq/lua/plugins/features/fzf-lua.lua index 25766a6..393f21e 100644 --- a/dot_config/nvim/benq/lua/plugins/features/fzf-lua.lua +++ b/dot_config/nvim/benq/lua/plugins/features/fzf-lua.lua @@ -10,125 +10,199 @@ local function search_in_root() return vim.fn.getcwd() end +local function parse_cheatsheet(path) + local cheats = {} + if vim.fn.filereadable(path) ~= 1 then return cheats end + for _, line in ipairs(vim.fn.readfile(path)) do + line = vim.trim(line) + if line ~= "" and not vim.startswith(line, "#") then + local desc, code = line:match("^(.-)%s*|%s*(.+)$") + if desc and code then + table.insert(cheats, { desc, code }) + end + end + end + return cheats +end + +local function command_palette() + local fzf = require("fzf-lua") + local plugin = require("lazy.core.config").plugins["cheatsheet.nvim"] + local sources = { + vim.fn.stdpath("config") .. "/cheatsheet.txt", + } + + if plugin then + table.insert(sources, plugin.dir .. "/cheatsheets/cheatsheet-default.txt") + end + + local lines, lookup = {}, {} + for _, path in ipairs(sources) do + for _, cheat in ipairs(parse_cheatsheet(path)) do + local line = string.format("%-50s %s", cheat[1], cheat[2]) + if not lookup[line] then + table.insert(lines, line) + lookup[line] = cheat[2] + end + end + end + + local function cmdline_part(code) + return code:match("^:([^%[{]*)") + end + + fzf.fzf_exec(lines, { + prompt = "Cheats> ", + winopts = { fullscreen = false, height = 0.5, width = 0.7 }, + actions = { + ["default"] = function(sel) + local code = sel and lookup[sel[1]] + if not code then return end + local cmd = cmdline_part(code) + if cmd then + vim.api.nvim_feedkeys(":" .. cmd, "n", false) + else + vim.fn.setreg("+", code) + vim.notify("Copied: " .. code) + end + end, + -- Alt-Enter: execute immediately (only for plain commands) + ["alt-enter"] = function(sel) + local code = sel and lookup[sel[1]] + local cmd = code and cmdline_part(code) + if cmd then vim.cmd(cmd) end + end, + }, + }) +end + return { - "ibhagwan/fzf-lua", - dependencies = { "nvim-tree/nvim-web-devicons" }, - opts = { - winopts = { - fullscreen = true, - preview = { - layout = "vertical", - vertical = "up:70%", - } - }, - keymap = { - builtin = { - true, - [""] = "preview-down", - [""] = "preview-up", - [""] = "preview-page-down", - [""] = "preview-page-up", - }, - fzf = { - ["ctrl-q"] = "select-all+accept" - } - } - }, - keys = { - { - "ff", - function() require("fzf-lua").files() end, - desc="Find files in project directory" - }, - { - "fF", - function() require("fzf-lua").files({ cwd = search_in_root() }) end, - desc="Find files in current buffer's directory" - }, - { - "fg", - function() require("fzf-lua").live_grep() end, - desc="Find by grepping in project directory" - }, - { - "fG", - function() require("fzf-lua").live_grep({ cwd = search_in_root() }) end, - desc="Find by grepping in current buffer's directory" - }, - { - "fc", - function() require("fzf-lua").files({cwd=vim.fn.stdpath("config")}) end, - desc="Find in nvim config" - }, - { - "fh", - function() - require("fzf-lua").helptags() - end, - desc = "Find in help", - }, - { - "fk", - function() - require("fzf-lua").keymaps() - end, - desc = "Find keymaps", - }, - { - "fB", - function() - require("fzf-lua").builtin() - end, - desc = "Find built-in FZF", - }, - { - "fw", - function() - require("fzf-lua").grep_cword() - end, - desc = "Find current word", - }, - { - "fW", - function() - require("fzf-lua").grep_cWORD() - end, - desc = "Find current WORD", - }, - { - "fd", - function() - require("fzf-lua").diagnostics_document() - end, - desc = "Find diagnostics", - }, - { - "fr", - function() - require("fzf-lua").resume() - end, - desc = "Find resume", - }, - { - "fo", - function() - require("fzf-lua").oldfiles() - end, - desc = "Find old files", - }, - { - "", - function() - require("fzf-lua").buffers() - end, - desc = "Find existing buffers", - }, - { - "/", - function() - require("fzf-lua").lgrep_curbuf() - end, - desc = "Live grep the current buffer", - }, - } + "ibhagwan/fzf-lua", + dependencies = { + { "nvim-tree/nvim-web-devicons" }, + { "doctorfree/cheatsheet.nvim", lazy = true } + }, + opts = { + winopts = { + fullscreen = true, + preview = { + layout = "vertical", + vertical = "up:70%", + } + }, + keymap = { + builtin = { + true, + [""] = "preview-down", + [""] = "preview-up", + [""] = "preview-page-down", + [""] = "preview-page-up", + }, + fzf = { + ["ctrl-q"] = "select-all+accept" + } + } + }, + keys = { + { + "ff", + function() require("fzf-lua").files() end, + desc = "Find files in project directory" + }, + { + "fF", + function() require("fzf-lua").files({ cwd = search_in_root() }) end, + desc = "Find files in current buffer's directory" + }, + { + "fg", + function() require("fzf-lua").live_grep() end, + desc = "Find by grepping in project directory" + }, + { + "fG", + function() require("fzf-lua").live_grep({ cwd = search_in_root() }) end, + desc = "Find by grepping in current buffer's directory" + }, + { + "fc", + function() require("fzf-lua").files({ cwd = vim.fn.stdpath("config") }) end, + desc = "Find in nvim config" + }, + { + "fh", + function() + require("fzf-lua").helptags() + end, + desc = "Find in help", + }, + { + "fk", + function() + require("fzf-lua").keymaps() + end, + desc = "Find keymaps", + }, + { + "fB", + function() + require("fzf-lua").builtin() + end, + desc = "Find built-in FZF", + }, + { + "fw", + function() + require("fzf-lua").grep_cword() + end, + desc = "Find current word", + }, + { + "fW", + function() + require("fzf-lua").grep_cWORD() + end, + desc = "Find current WORD", + }, + { + "fd", + function() + require("fzf-lua").diagnostics_document() + end, + desc = "Find diagnostics", + }, + { + "fr", + function() + require("fzf-lua").resume() + end, + desc = "Find resume", + }, + { + "fo", + function() + require("fzf-lua").oldfiles() + end, + desc = "Find old files", + }, + { + "", + function() + require("fzf-lua").buffers() + end, + desc = "Find existing buffers", + }, + { + "/", + function() + require("fzf-lua").lgrep_curbuf() + end, + desc = "Live grep the current buffer", + }, + { + "fp", + command_palette, + desc = "Find command", + } + } }