From c1744a5e8184ea74140748fb9b872a7ca72c5bed Mon Sep 17 00:00:00 2001 From: CronyAkatsuki Date: Wed, 27 Dec 2023 22:03:28 +0100 Subject: [PATCH] Clean up the logic a bit and user diff api. --- lua/md-tools/init.lua | 36 ++++++++++++++---------------------- 1 file changed, 14 insertions(+), 22 deletions(-) diff --git a/lua/md-tools/init.lua b/lua/md-tools/init.lua index 27d31da..4289bfb 100644 --- a/lua/md-tools/init.lua +++ b/lua/md-tools/init.lua @@ -2,35 +2,27 @@ local M = {} local header_regex = "^(#+) (.+)" M.promote = function() - if vim.api.nvim_get_mode().mode == "n" then - local current_line = vim.api.nvim_get_current_line() - local row, col = unpack(vim.api.nvim_win_get_cursor(0)) - local new_line = "" + local current_line = vim.api.nvim_get_current_line() + local row = vim.api.nvim_win_get_cursor(0)[1] - if current_line:match(header_regex) then - if current_line:match("^# (.+)") then - vim.notify("You can't promote this header anymore") - return - end - new_line = current_line:gsub("^#", "", 1) - vim.api.nvim_buf_set_lines(0, row - 1, row, true, { new_line }) + if current_line:match(header_regex) then + if current_line:match("^# (.+)") then + vim.notify("You can't promote this header anymore") + else + vim.api.nvim_buf_set_text(0, row - 1, 0, row - 1, 1, { '' }) end end end M.demote = function() - if vim.api.nvim_get_mode().mode == "n" then - local current_line = vim.api.nvim_get_current_line() - local row, col = unpack(vim.api.nvim_win_get_cursor(0)) - local new_line = "" + local current_line = vim.api.nvim_get_current_line() + local row = vim.api.nvim_win_get_cursor(0)[1] - if current_line:match(header_regex) then - if current_line:match("^###### (.+)") then - vim.notify("You can't demote this header anymore") - return - end - new_line = current_line:gsub("^#", "##", 1) - vim.api.nvim_buf_set_lines(0, row - 1, row, true, { new_line }) + if current_line:match(header_regex) then + if current_line:match("^###### (.+)") then + vim.notify("You can't demote this header anymore") + else + vim.api.nvim_buf_set_text(0, row - 1, 0, row - 1, 0, { '#' }) end end end