From 6e51a9b877af4b24c43d005011f46dc6193ec813 Mon Sep 17 00:00:00 2001 From: Guennadi Maximov C Date: Tue, 23 Dec 2025 01:31:30 -0600 Subject: [PATCH 01/10] feat(ci): add StyLua config Signed-off-by: Guennadi Maximov C --- stylua.toml | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 stylua.toml diff --git a/stylua.toml b/stylua.toml new file mode 100644 index 0000000..aa28065 --- /dev/null +++ b/stylua.toml @@ -0,0 +1,12 @@ +call_parentheses = "Always" +collapse_simple_statement = "Never" +column_width = 100 +indent_type = "Tabs" +indent_width = 4 +line_endings = "Unix" +quote_style = "AutoPreferDouble" +space_after_function_names = "Never" +syntax = 'LuaJIT' + +[sort_requires] +enabled = false From 290661841f6f5bd63ab2ebb94baf92dfd350bd52 Mon Sep 17 00:00:00 2001 From: Guennadi Maximov C Date: Tue, 23 Dec 2025 01:32:59 -0600 Subject: [PATCH 02/10] chore: add EOF Vim modeline comments to enforce indentation Signed-off-by: Guennadi Maximov C --- lua/cheaty/init.lua | 1 + lua/cheaty/window.lua | 1 + 2 files changed, 2 insertions(+) diff --git a/lua/cheaty/init.lua b/lua/cheaty/init.lua index c25e5a0..ee486a2 100644 --- a/lua/cheaty/init.lua +++ b/lua/cheaty/init.lua @@ -18,3 +18,4 @@ function M.setup(opts) end return M +-- vim: set ts=4 sts=4 sw=0 noet ai si sta: diff --git a/lua/cheaty/window.lua b/lua/cheaty/window.lua index 59f6e83..241ebf6 100644 --- a/lua/cheaty/window.lua +++ b/lua/cheaty/window.lua @@ -56,3 +56,4 @@ function M.toggle(cfg) end return M +-- vim: set ts=4 sts=4 sw=0 noet ai si sta: From 0905c7db96e138c3cf2939c7d69bc973c3b8765d Mon Sep 17 00:00:00 2001 From: Guennadi Maximov C Date: Tue, 23 Dec 2025 01:35:06 -0600 Subject: [PATCH 03/10] fix: add LuaLS annotations, format with StyLua, improved modules Signed-off-by: Guennadi Maximov C --- lua/cheaty/init.lua | 21 +++++++++++------ lua/cheaty/window.lua | 53 +++++++++++++++++++++++-------------------- 2 files changed, 42 insertions(+), 32 deletions(-) diff --git a/lua/cheaty/init.lua b/lua/cheaty/init.lua index ee486a2..9f25725 100644 --- a/lua/cheaty/init.lua +++ b/lua/cheaty/init.lua @@ -1,14 +1,21 @@ -local M = {} - +---@class Cheaty.Config +---@field keymap? string +---@field width? number +---@field height? number +---@field cheatsheet? string[] local defaults = { - keymap = "cs", - width = 0.6, - height = 0.6, - cheatsheet = { "# This is a sample cheatsheet!", "Tailor it to your liking in the config!" } + keymap = "cs", + width = 0.6, + height = 0.6, + cheatsheet = { "# This is a sample cheatsheet!", "Tailor it to your liking in the config!" }, } -M.config = {} +---@class Cheaty +local M = {} + +M.config = {} ---@type Cheaty.Config +---@param opts? Cheaty.Config function M.setup(opts) M.config = vim.tbl_deep_extend("force", defaults, opts or {}) diff --git a/lua/cheaty/window.lua b/lua/cheaty/window.lua index 241ebf6..7530b2a 100644 --- a/lua/cheaty/window.lua +++ b/lua/cheaty/window.lua @@ -1,21 +1,23 @@ +---@class Cheaty.Window local M = {} -local win_id = nil -local buf_id = nil +M.win_id = nil ---@type integer +M.buf_id = nil ---@type integer -local function create_window(cfg) - buf_id = vim.api.nvim_create_buf(false, true) +---@param cfg Cheaty.Config +function M.create_window(cfg) + M.buf_id = vim.api.nvim_create_buf(false, true) - vim.api.nvim_buf_set_lines(buf_id, 0, -1, false, cfg.cheatsheet) + vim.api.nvim_buf_set_lines(M.buf_id, 0, -1, false, cfg.cheatsheet) -- Buffer options - local buffer = vim.bo[buf_id] + local buffer = vim.bo[M.buf_id] - buffer.buftype = "nofile" - buffer.bufhidden = "wipe" + buffer.buftype = "nofile" + buffer.bufhidden = "wipe" buffer.modifiable = false - buffer.swapfile = false - buffer.filetype = "markdown" + buffer.swapfile = false + buffer.filetype = "markdown" vim.api.nvim_buf_call(buf_id, function() vim.cmd("doautocmd FileType markdown") @@ -26,33 +28,34 @@ local function create_window(cfg) local row = math.floor((vim.o.lines - height) / 2) local col = math.floor((vim.o.columns - width) / 2) - - win_id = vim.api.nvim_open_win(buf_id, true, { + M.win_id = vim.api.nvim_open_win(M.buf_id, true, { relative = "editor", - row = row, - col = col, - width = width, - height = height, - style = "minimal", - border = "rounded" + row = row, + col = col, + width = width, + height = height, + style = "minimal", + border = "rounded", }) end function M.close() - if win_id and vim.api.nvim_win_is_valid(win_id) then - vim.api.nvim_win_close(win_id, true) + if M.win_id and vim.api.nvim_win_is_valid(M.win_id) then + vim.api.nvim_win_close(M.win_id, true) end - win_id = nil - buf_id = nil + M.win_id = nil + M.buf_id = nil end +---@param cfg Cheaty.Config function M.toggle(cfg) - if win_id and vim.api.nvim_win_is_valid(win_id) then + if M.win_id and vim.api.nvim_win_is_valid(M.win_id) then M.close() - else - create_window(cfg) + return end + + M.create_window(cfg) end return M From 18ed26cf0ac3686c81e59e8980737bfc486a1487 Mon Sep 17 00:00:00 2001 From: Guennadi Maximov C Date: Tue, 23 Dec 2025 01:36:34 -0600 Subject: [PATCH 04/10] fix(init): set keymap if config option is valid Signed-off-by: Guennadi Maximov C --- lua/cheaty/init.lua | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lua/cheaty/init.lua b/lua/cheaty/init.lua index 9f25725..bab7718 100644 --- a/lua/cheaty/init.lua +++ b/lua/cheaty/init.lua @@ -19,7 +19,11 @@ M.config = {} ---@type Cheaty.Config function M.setup(opts) M.config = vim.tbl_deep_extend("force", defaults, opts or {}) - vim.keymap.set('n', M.config.keymap, function() + if M.config.keymap and M.config.keymap ~= "" then + vim.keymap.set("n", M.config.keymap, function() + require("cheaty.window").toggle(M.config) + end, { desc = "Toggle cheaty.nvim cheatsheet" }) + end require("cheaty.window").toggle(M.config) end, { desc = "Toggle cheaty.nvim cheatsheet" }) end From 07c38cae0497321e23ae29df57368a40fc80e98a Mon Sep 17 00:00:00 2001 From: Guennadi Maximov C Date: Tue, 23 Dec 2025 01:37:17 -0600 Subject: [PATCH 05/10] feat(window): map `q` to exit the cheaty buffer Signed-off-by: Guennadi Maximov C --- lua/cheaty/window.lua | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lua/cheaty/window.lua b/lua/cheaty/window.lua index 7530b2a..634f6b2 100644 --- a/lua/cheaty/window.lua +++ b/lua/cheaty/window.lua @@ -37,6 +37,8 @@ function M.create_window(cfg) style = "minimal", border = "rounded", }) + + vim.keymap.set("n", "q", M.close, { buffer = M.buf_id, noremap = true, silent = true }) end function M.close() From a518410e9211dc7b74a5f31d8b5bb7772700e274 Mon Sep 17 00:00:00 2001 From: Guennadi Maximov C Date: Tue, 23 Dec 2025 01:38:02 -0600 Subject: [PATCH 06/10] fix(window): use `vim.api` instead of `vim.cmd` Signed-off-by: Guennadi Maximov C --- lua/cheaty/window.lua | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lua/cheaty/window.lua b/lua/cheaty/window.lua index 634f6b2..dd7a6d3 100644 --- a/lua/cheaty/window.lua +++ b/lua/cheaty/window.lua @@ -19,8 +19,12 @@ function M.create_window(cfg) buffer.swapfile = false buffer.filetype = "markdown" - vim.api.nvim_buf_call(buf_id, function() - vim.cmd("doautocmd FileType markdown") + vim.api.nvim_buf_call(M.buf_id, function() + -- vim.cmd("doautocmd FileType markdown") + vim.api.nvim_exec_autocmds("FileType", { + pattern = "markdown", + -- buffer = M.buf_id, + }) end) local width = math.floor(vim.o.columns * cfg.width) From 952027378cbcbd3ca2bcc96a0e113962b49043b5 Mon Sep 17 00:00:00 2001 From: Guennadi Maximov C Date: Tue, 23 Dec 2025 01:38:40 -0600 Subject: [PATCH 07/10] feat(init): add `:Cheaty` user command to toggle the window Signed-off-by: Guennadi Maximov C --- lua/cheaty/init.lua | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lua/cheaty/init.lua b/lua/cheaty/init.lua index bab7718..e9d007b 100644 --- a/lua/cheaty/init.lua +++ b/lua/cheaty/init.lua @@ -24,8 +24,10 @@ function M.setup(opts) require("cheaty.window").toggle(M.config) end, { desc = "Toggle cheaty.nvim cheatsheet" }) end + + vim.api.nvim_create_user_command("Cheaty", function() require("cheaty.window").toggle(M.config) - end, { desc = "Toggle cheaty.nvim cheatsheet" }) + end, { desc = "Toggle the Cheaty Window" }) end return M From 0989e3f8bb2955422b966b7477826d08353a4e9b Mon Sep 17 00:00:00 2001 From: Guennadi Maximov C Date: Tue, 23 Dec 2025 02:34:32 -0600 Subject: [PATCH 08/10] fix(window): addressed bad behaviour with `nvim_exec_autocmds()` Signed-off-by: Guennadi Maximov C --- lua/cheaty/window.lua | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/lua/cheaty/window.lua b/lua/cheaty/window.lua index dd7a6d3..588d288 100644 --- a/lua/cheaty/window.lua +++ b/lua/cheaty/window.lua @@ -21,10 +21,7 @@ function M.create_window(cfg) vim.api.nvim_buf_call(M.buf_id, function() -- vim.cmd("doautocmd FileType markdown") - vim.api.nvim_exec_autocmds("FileType", { - pattern = "markdown", - -- buffer = M.buf_id, - }) + vim.api.nvim_exec_autocmds("FileType", { pattern = "markdown" }) end) local width = math.floor(vim.o.columns * cfg.width) From 51d5152d5776f942d59ed316baa5452bf8bfd150 Mon Sep 17 00:00:00 2001 From: Guennadi Maximov C Date: Tue, 23 Dec 2025 02:36:25 -0600 Subject: [PATCH 09/10] refactor(window)!: remove params, store config in module Signed-off-by: Guennadi Maximov C --- lua/cheaty/init.lua | 14 ++++++++++---- lua/cheaty/window.lua | 19 +++++++++++-------- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/lua/cheaty/init.lua b/lua/cheaty/init.lua index e9d007b..e792b82 100644 --- a/lua/cheaty/init.lua +++ b/lua/cheaty/init.lua @@ -19,14 +19,20 @@ M.config = {} ---@type Cheaty.Config function M.setup(opts) M.config = vim.tbl_deep_extend("force", defaults, opts or {}) + local cheaty_win = require("cheaty.window") + cheaty_win.config = vim.deepcopy(M.config) + if M.config.keymap and M.config.keymap ~= "" then - vim.keymap.set("n", M.config.keymap, function() - require("cheaty.window").toggle(M.config) - end, { desc = "Toggle cheaty.nvim cheatsheet" }) + vim.keymap.set( + "n", + M.config.keymap, + cheaty_win.toggle, + { desc = "Toggle cheaty.nvim cheatsheet" } + ) end vim.api.nvim_create_user_command("Cheaty", function() - require("cheaty.window").toggle(M.config) + cheaty_win.toggle() end, { desc = "Toggle the Cheaty Window" }) end diff --git a/lua/cheaty/window.lua b/lua/cheaty/window.lua index 588d288..9a3f11f 100644 --- a/lua/cheaty/window.lua +++ b/lua/cheaty/window.lua @@ -1,14 +1,18 @@ ---@class Cheaty.Window +---@field config? Cheaty.Config local M = {} M.win_id = nil ---@type integer M.buf_id = nil ---@type integer ----@param cfg Cheaty.Config -function M.create_window(cfg) +function M.create_window() + if not M.config then + return + end + M.buf_id = vim.api.nvim_create_buf(false, true) - vim.api.nvim_buf_set_lines(M.buf_id, 0, -1, false, cfg.cheatsheet) + vim.api.nvim_buf_set_lines(M.buf_id, 0, -1, false, M.config.cheatsheet) -- Buffer options local buffer = vim.bo[M.buf_id] @@ -24,8 +28,8 @@ function M.create_window(cfg) vim.api.nvim_exec_autocmds("FileType", { pattern = "markdown" }) end) - local width = math.floor(vim.o.columns * cfg.width) - local height = math.floor(vim.o.lines * cfg.height) + local width = math.floor(vim.o.columns * M.config.width) + local height = math.floor(vim.o.lines * M.config.height) local row = math.floor((vim.o.lines - height) / 2) local col = math.floor((vim.o.columns - width) / 2) @@ -51,14 +55,13 @@ function M.close() M.buf_id = nil end ----@param cfg Cheaty.Config -function M.toggle(cfg) +function M.toggle() if M.win_id and vim.api.nvim_win_is_valid(M.win_id) then M.close() return end - M.create_window(cfg) + M.create_window() end return M From 8e8aa801ee3867f43e320438e515ba6668fb6b03 Mon Sep 17 00:00:00 2001 From: Guennadi Maximov C Date: Tue, 23 Dec 2025 02:38:09 -0600 Subject: [PATCH 10/10] feat(window): add title and footer to window Signed-off-by: Guennadi Maximov C --- lua/cheaty/window.lua | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lua/cheaty/window.lua b/lua/cheaty/window.lua index 9a3f11f..d193184 100644 --- a/lua/cheaty/window.lua +++ b/lua/cheaty/window.lua @@ -41,6 +41,10 @@ function M.create_window() height = height, style = "minimal", border = "rounded", + title = "Cheatsheet", + title_pos = "center", + footer_pos = "center", + footer = ": Close Window", }) vim.keymap.set("n", "q", M.close, { buffer = M.buf_id, noremap = true, silent = true })