Move login to single function.

This commit is contained in:
CronyAkatsuki 2023-12-28 12:44:41 +01:00
parent 885e968aa1
commit 99d1871bfe
2 changed files with 40 additions and 19 deletions

View File

@ -1,30 +1,51 @@
local M = {} local M = {}
local header_regex = "^(#+) (.+)" local header_regex = "^(#+) (.+)"
M.promote = function() M.headerControl = function(action)
local current_line = vim.api.nvim_get_current_line() local current_line = vim.api.nvim_get_current_line()
local row = vim.api.nvim_win_get_cursor(0)[1] local row = vim.api.nvim_win_get_cursor(0)[1]
if current_line:match(header_regex) then if current_line:match(header_regex) then
if action == "promote" then
if current_line:match("^# (.+)") then if current_line:match("^# (.+)") then
vim.notify("You can't promote this header anymore") vim.notify("You can't promote this header anymore")
else return
vim.api.nvim_buf_set_text(0, row - 1, 0, row - 1, 1, { '' })
end end
end vim.api.nvim_buf_set_text(0, row - 1, 0, row - 1, 1, { "" })
end elseif action == "demote" then
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 if current_line:match("^###### (.+)") then
vim.notify("You can't demote this header anymore") vim.notify("You can't demote this header anymore")
else return
vim.api.nvim_buf_set_text(0, row - 1, 0, row - 1, 0, { '#' }) end
vim.api.nvim_buf_set_text(0, row - 1, 0, row - 1, 0, { "#" })
end end
end 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 return M

View File

@ -2,11 +2,11 @@ if vim.fn.exists("g:loaded_md-tools") == 0 then
local user_cmd = vim.api.nvim_create_user_command local user_cmd = vim.api.nvim_create_user_command
user_cmd("HeaderPromote", function (opts) user_cmd("HeaderPromote", function (opts)
require("md-tools").promote() require("md-tools").headerControl('promote')
end, {}) end, {})
user_cmd("HeaderDemote", function (opts) user_cmd("HeaderDemote", function (opts)
require("md-tools").demote() require("md-tools").headerControl('demote')
end, {}) end, {})
vim.api.nvim_set_var("loaded_md-tools", true) vim.api.nvim_set_var("loaded_md-tools", true)