From 65589309bdfa3c0fea83af164dddb147b47afbff Mon Sep 17 00:00:00 2001 From: Guennadi Maximov C Date: Sat, 21 Feb 2026 13:17:24 -0600 Subject: [PATCH 1/5] refactor!: allow live editing Signed-off-by: Guennadi Maximov C --- lua/cheaty/init.lua | 11 ++++- lua/cheaty/window.lua | 104 ++++++++++++++++++++++++++++++++---------- 2 files changed, 90 insertions(+), 25 deletions(-) diff --git a/lua/cheaty/init.lua b/lua/cheaty/init.lua index 885b15e..b333fb6 100644 --- a/lua/cheaty/init.lua +++ b/lua/cheaty/init.lua @@ -1,13 +1,22 @@ +---@class cheaty local M = {} +-- stylua: ignore start +---@class cheatyOpts +---@field width? number +---@field height? number +---@field save_file? string +---@field cheatsheet? string[] local defaults = { width = 0.6, height = 0.6, + save_file = vim.fs.joinpath(vim.fn.stdpath("data"), "cheaty.md"), cheatsheet = { "# This is a sample cheatsheet!", "Tailor it to your liking in the config!" } } -M.config = {} +M.config = {} ---@type cheatyOpts +---@param opts? cheatyOpts 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 59f6e83..974e572 100644 --- a/lua/cheaty/window.lua +++ b/lua/cheaty/window.lua @@ -1,25 +1,48 @@ +local uv = vim.uv or vim.loop + +---@class cheaty.Window local M = {} -local win_id = nil -local buf_id = nil +local win_id, buf_id = nil, nil ---@type integer|nil, integer|nil +local fd, stat = nil, nil ---@type integer|nil, uv.fs_stat.result|nil + +---@param cfg cheatyOpts +---@param flags? uv.fs_open.flags +local function open_file(cfg, flags) + if not cfg.save_file or cfg.save_file == "" then + error("No valid path!", vim.log.levels.ERROR) + end + + if vim.fn.filereadable(cfg.save_file) ~= 1 then + vim.fn.writefile(cfg.cheatsheet or {}, cfg.save_file) + end + stat = uv.fs_stat(cfg.save_file) + fd = uv.fs_open(cfg.save_file, flags or "r", tonumber("644", 8)) +end + +---@param cfg cheatyOpts local function create_window(cfg) buf_id = vim.api.nvim_create_buf(false, true) - vim.api.nvim_buf_set_lines(buf_id, 0, -1, false, cfg.cheatsheet) + open_file(cfg, "r") - -- Buffer options - local buffer = vim.bo[buf_id] + local contents = cfg.cheatsheet + if fd and stat then + contents = vim.split(uv.fs_read(fd, stat.size), "\n", { trimempty = false }) + uv.fs_close(fd) + end - buffer.buftype = "nofile" - buffer.bufhidden = "wipe" - buffer.modifiable = false - buffer.swapfile = false - buffer.filetype = "markdown" + vim.api.nvim_buf_set_lines(buf_id, 0, -1, false, contents) - vim.api.nvim_buf_call(buf_id, function() - vim.cmd("doautocmd FileType markdown") - end) + -- Buffer options + local opts = { buf = buf_id } ---@type vim.api.keyset.option + vim.api.nvim_set_option_value("buftype", "nofile", opts) + -- vim.api.nvim_set_option_value("bufhidden", "wipe", opts) + vim.api.nvim_set_option_value("modified", false, opts) + vim.api.nvim_set_option_value("modifiable", true, opts) + vim.api.nvim_set_option_value("swapfile", false, opts) + vim.api.nvim_set_option_value("filetype", "markdown", opts) local width = math.floor(vim.o.columns * cfg.width) local height = math.floor(vim.o.lines * cfg.height) @@ -29,30 +52,63 @@ local function create_window(cfg) win_id = vim.api.nvim_open_win(buf_id, true, { relative = "editor", - row = row, - col = col, - width = width, - height = height, - style = "minimal", - border = "rounded" + noautocmd = false, + title = "Cheaty", + title_pos = "center", + row = row, + col = col, + width = width, + height = height, + style = "minimal", + border = "rounded", + }) + + vim.keymap.set('n', 'q', M.close, { buffer = buf_id }) + + local augroup = vim.api.nvim_create_augroup("cheaty", { clear = true }) + vim.api.nvim_create_autocmd({ "TextChanged", "TextChangedI" }, { + group = augroup, + buffer = buf_id, + callback = function() + if not (buf_id and vim.api.nvim_buf_is_valid(buf_id)) then + return + end + + open_file(cfg, "w") + if not fd then + return + end + + local content = vim.api.nvim_buf_get_lines(buf_id, 0, -1, true) + uv.fs_write(fd, table.concat(content, "\n")) + uv.fs_close(fd) + end, }) 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 not (buf_id or win_id) then + return end + pcall(vim.api.nvim_buf_delete, buf_id, { force = true }) + pcall(vim.api.nvim_win_close, win_id, true) + + timer = nil + fd = nil + stat = nil win_id = nil buf_id = nil end +---@param cfg cheatyOpts function M.toggle(cfg) - if win_id and vim.api.nvim_win_is_valid(win_id) then + if win_id and buf_id then M.close() - else - create_window(cfg) + return end + + create_window(cfg) end return M From 2f34ecc13da8982736ab0184579b0dfbf9247a69 Mon Sep 17 00:00:00 2001 From: Guennadi Maximov C Date: Sat, 21 Feb 2026 13:20:21 -0600 Subject: [PATCH 2/5] chore: add missing StyLua ignore end Signed-off-by: Guennadi Maximov C --- lua/cheaty/init.lua | 1 + 1 file changed, 1 insertion(+) diff --git a/lua/cheaty/init.lua b/lua/cheaty/init.lua index b333fb6..fef00c6 100644 --- a/lua/cheaty/init.lua +++ b/lua/cheaty/init.lua @@ -13,6 +13,7 @@ local defaults = { save_file = vim.fs.joinpath(vim.fn.stdpath("data"), "cheaty.md"), cheatsheet = { "# This is a sample cheatsheet!", "Tailor it to your liking in the config!" } } +-- stylua: ignore end M.config = {} ---@type cheatyOpts From 511f02fe4c64f4291f60f9d2fa34980091d1b96a Mon Sep 17 00:00:00 2001 From: Guennadi Maximov C Date: Sat, 21 Feb 2026 13:21:13 -0600 Subject: [PATCH 3/5] fix: remove artifact Signed-off-by: Guennadi Maximov C --- lua/cheaty/window.lua | 1 - 1 file changed, 1 deletion(-) diff --git a/lua/cheaty/window.lua b/lua/cheaty/window.lua index 974e572..43451d1 100644 --- a/lua/cheaty/window.lua +++ b/lua/cheaty/window.lua @@ -94,7 +94,6 @@ function M.close() pcall(vim.api.nvim_buf_delete, buf_id, { force = true }) pcall(vim.api.nvim_win_close, win_id, true) - timer = nil fd = nil stat = nil win_id = nil From 3ee14d015b1077863ae7e9c45ee68817bd8e43dc Mon Sep 17 00:00:00 2001 From: Guennadi Maximov C Date: Mon, 23 Feb 2026 10:46:26 -0600 Subject: [PATCH 4/5] fix: merge caused issues Signed-off-by: Guennadi Maximov C --- lua/cheaty/init.lua | 8 +++++--- lua/cheaty/window.lua | 31 ++++++++++++++++++++++++++----- 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/lua/cheaty/init.lua b/lua/cheaty/init.lua index 0c23941..fef00c6 100644 --- a/lua/cheaty/init.lua +++ b/lua/cheaty/init.lua @@ -21,9 +21,11 @@ M.config = {} ---@type cheatyOpts function M.setup(opts) M.config = vim.tbl_deep_extend("force", defaults, opts or {}) - vim.api.nvim_create_user_command("Cheaty", function() - require("cheaty.window").toggle(M.config) - end, { desc = "Open Cheatsheet" }) + vim.api.nvim_create_user_command( + 'Cheaty', + function() require("cheaty.window").toggle(M.config) end, + { desc = "Open Cheatsheet" } + ) end return M diff --git a/lua/cheaty/window.lua b/lua/cheaty/window.lua index 44fa94e..43451d1 100644 --- a/lua/cheaty/window.lua +++ b/lua/cheaty/window.lua @@ -33,11 +33,7 @@ local function create_window(cfg) uv.fs_close(fd) end - buffer.buftype = "nofile" - buffer.bufhidden = "wipe" - buffer.modifiable = false - buffer.swapfile = false - buffer.filetype = "markdown" + vim.api.nvim_buf_set_lines(buf_id, 0, -1, false, contents) -- Buffer options local opts = { buf = buf_id } ---@type vim.api.keyset.option @@ -56,6 +52,9 @@ local function create_window(cfg) win_id = vim.api.nvim_open_win(buf_id, true, { relative = "editor", + noautocmd = false, + title = "Cheaty", + title_pos = "center", row = row, col = col, width = width, @@ -63,6 +62,28 @@ local function create_window(cfg) style = "minimal", border = "rounded", }) + + vim.keymap.set('n', 'q', M.close, { buffer = buf_id }) + + local augroup = vim.api.nvim_create_augroup("cheaty", { clear = true }) + vim.api.nvim_create_autocmd({ "TextChanged", "TextChangedI" }, { + group = augroup, + buffer = buf_id, + callback = function() + if not (buf_id and vim.api.nvim_buf_is_valid(buf_id)) then + return + end + + open_file(cfg, "w") + if not fd then + return + end + + local content = vim.api.nvim_buf_get_lines(buf_id, 0, -1, true) + uv.fs_write(fd, table.concat(content, "\n")) + uv.fs_close(fd) + end, + }) end function M.close() From 4251854180fbd030361b9cf9d19fe87a20ea2ba1 Mon Sep 17 00:00:00 2001 From: Guennadi Maximov C Date: Mon, 23 Feb 2026 10:47:18 -0600 Subject: [PATCH 5/5] chore: format with StyLua Signed-off-by: Guennadi Maximov C --- lua/cheaty/init.lua | 8 +++----- lua/cheaty/window.lua | 38 +++++++++++++++++++------------------- 2 files changed, 22 insertions(+), 24 deletions(-) diff --git a/lua/cheaty/init.lua b/lua/cheaty/init.lua index fef00c6..0c23941 100644 --- a/lua/cheaty/init.lua +++ b/lua/cheaty/init.lua @@ -21,11 +21,9 @@ M.config = {} ---@type cheatyOpts function M.setup(opts) M.config = vim.tbl_deep_extend("force", defaults, opts or {}) - vim.api.nvim_create_user_command( - 'Cheaty', - function() require("cheaty.window").toggle(M.config) end, - { desc = "Open Cheatsheet" } - ) + vim.api.nvim_create_user_command("Cheaty", function() + require("cheaty.window").toggle(M.config) + end, { desc = "Open Cheatsheet" }) end return M diff --git a/lua/cheaty/window.lua b/lua/cheaty/window.lua index 43451d1..5fddf41 100644 --- a/lua/cheaty/window.lua +++ b/lua/cheaty/window.lua @@ -63,26 +63,26 @@ local function create_window(cfg) border = "rounded", }) - vim.keymap.set('n', 'q', M.close, { buffer = buf_id }) + vim.keymap.set("n", "q", M.close, { buffer = buf_id }) local augroup = vim.api.nvim_create_augroup("cheaty", { clear = true }) vim.api.nvim_create_autocmd({ "TextChanged", "TextChangedI" }, { - group = augroup, - buffer = buf_id, - callback = function() - if not (buf_id and vim.api.nvim_buf_is_valid(buf_id)) then - return - end - - open_file(cfg, "w") - if not fd then - return - end - - local content = vim.api.nvim_buf_get_lines(buf_id, 0, -1, true) - uv.fs_write(fd, table.concat(content, "\n")) - uv.fs_close(fd) - end, + group = augroup, + buffer = buf_id, + callback = function() + if not (buf_id and vim.api.nvim_buf_is_valid(buf_id)) then + return + end + + open_file(cfg, "w") + if not fd then + return + end + + local content = vim.api.nvim_buf_get_lines(buf_id, 0, -1, true) + uv.fs_write(fd, table.concat(content, "\n")) + uv.fs_close(fd) + end, }) end @@ -94,8 +94,8 @@ function M.close() pcall(vim.api.nvim_buf_delete, buf_id, { force = true }) pcall(vim.api.nvim_win_close, win_id, true) - fd = nil - stat = nil + fd = nil + stat = nil win_id = nil buf_id = nil end