From 760b74ee6e71c21b33d143aae59696c3c286618e Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Mon, 8 Aug 2022 18:45:22 -0400 Subject: [PATCH] refactor: extract a function for writing a line --- lua/toggle-checkbox.lua | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/lua/toggle-checkbox.lua b/lua/toggle-checkbox.lua index 8175c2c..4f8ef0e 100644 --- a/lua/toggle-checkbox.lua +++ b/lua/toggle-checkbox.lua @@ -11,18 +11,21 @@ local current_line = vim.api.nvim_buf_get_lines(bufnr, cursor[1] - 1, cursor[1], -- return string.find(line, checked_checkbox) -- end +local write_line = function(new_line) + vim.api.nvim_buf_set_lines(bufnr, cursor[1] - 1, cursor[1], false, { new_line }) + vim.api.nvim_win_set_cursor(0, cursor) +end + local M = {} M.check = function() local new_line = current_line:gsub("%[ %]", checked_checkbox) - vim.api.nvim_buf_set_lines(bufnr, cursor[1] - 1, cursor[1], false, { new_line }) - vim.api.nvim_win_set_cursor(0, cursor) + write_line(new_line) end M.uncheck = function() local new_line = current_line:gsub("%[" .. checked_character .. "%]", unchecked_checkbox) - vim.api.nvim_buf_set_lines(bufnr, cursor[1] - 1, cursor[1], false, { new_line }) - vim.api.nvim_win_set_cursor(0, cursor) + write_line(new_line) end return M