From 99d1871bfe4dfc519db58dadde939bce82e3569f Mon Sep 17 00:00:00 2001 From: CronyAkatsuki Date: Thu, 28 Dec 2023 12:44:41 +0100 Subject: [PATCH] Move login to single function. --- lua/md-tools/init.lua | 55 ++++++++++++++++++++++++++++++------------- plugin/init.lua | 4 ++-- 2 files changed, 40 insertions(+), 19 deletions(-) diff --git a/lua/md-tools/init.lua b/lua/md-tools/init.lua index 4289bfb..9c2dc57 100644 --- a/lua/md-tools/init.lua +++ b/lua/md-tools/init.lua @@ -1,30 +1,51 @@ local M = {} local header_regex = "^(#+) (.+)" -M.promote = function() +M.headerControl = function(action) 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") - else - vim.api.nvim_buf_set_text(0, row - 1, 0, row - 1, 1, { '' }) + if action == "promote" then + if current_line:match("^# (.+)") then + vim.notify("You can't promote this header anymore") + return + end + vim.api.nvim_buf_set_text(0, row - 1, 0, row - 1, 1, { "" }) + elseif action == "demote" then + if current_line:match("^###### (.+)") then + vim.notify("You can't demote this header anymore") + return + end + vim.api.nvim_buf_set_text(0, row - 1, 0, row - 1, 0, { "#" }) end end end -M.demote = function() - 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") - else - vim.api.nvim_buf_set_text(0, row - 1, 0, row - 1, 0, { '#' }) - end - end -end +-- M.promote = function() +-- 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") +-- else +-- vim.api.nvim_buf_set_text(0, row - 1, 0, row - 1, 1, { "" }) +-- end +-- end +-- end +-- +-- M.demote = function() +-- 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") +-- else +-- vim.api.nvim_buf_set_text(0, row - 1, 0, row - 1, 0, { "#" }) +-- end +-- end +-- end return M diff --git a/plugin/init.lua b/plugin/init.lua index 3aa11d5..7993bb9 100644 --- a/plugin/init.lua +++ b/plugin/init.lua @@ -2,11 +2,11 @@ if vim.fn.exists("g:loaded_md-tools") == 0 then local user_cmd = vim.api.nvim_create_user_command user_cmd("HeaderPromote", function (opts) - require("md-tools").promote() + require("md-tools").headerControl('promote') end, {}) user_cmd("HeaderDemote", function (opts) - require("md-tools").demote() + require("md-tools").headerControl('demote') end, {}) vim.api.nvim_set_var("loaded_md-tools", true)