Modularize coloring and fix deprecated function calls

This commit is contained in:
2025-02-11 10:58:22 +03:00
parent 623951310e
commit d424f9bd87

View File

@@ -1,50 +1,86 @@
local M = {} local M = {}
local colors = {
w = "#ffffff",
k = "#000000",
r = "#ff0000",
g = "#00ff00",
b = "#0000ff",
c = "#00ffff",
y = "#ffff00",
m = "#ff00ff",
}
local function define_color(name, color_fg, color_bg, bold)
if bold == nil then
bold = true
end
vim.api.nvim_set_hl(0, name, { fg = colors[color_fg], bg = colors[color_bg], bold = bold })
end
local function highlight_todo_items() local function highlight_todo_items()
local lines = vim.api.nvim_buf_get_lines(0, 0, -1, false) local lines = vim.api.nvim_buf_get_lines(0, 0, -1, false)
vim.api.nvim_buf_clear_namespace(0, -1, 0, -1) local ns_id = vim.api.nvim_create_namespace("udivim_namespace")
vim.api.nvim_buf_clear_namespace(0, ns_id, 0, -1)
for i, line in ipairs(lines) do for i, line in ipairs(lines) do
if line:match("^ @") then if line:match("^%s*@") then
local idx2 = string.find(line, "@")
if line:match("") then if line:match("") then
local idx = string.find(line, "") local idx = string.find(line, "")
vim.api.nvim_buf_add_highlight(0, -1, "TodoDoneTopic", i - 1, 8, idx - 2) vim.api.nvim_buf_set_extmark(0, ns_id, i - 1, idx2 - 1, { end_col = idx - 2, hl_group = "fgkbgg" })
vim.api.nvim_buf_add_highlight(0, -1, "TodoDoneTopic", i - 1, idx + 3, -1) vim.api.nvim_buf_set_extmark(0, ns_id, i - 1, idx + 4, { end_col = #line, hl_group = "fgkbgg" })
vim.api.nvim_buf_add_highlight(0, -1, "TodoDoneHighlight", i - 1, idx - 1, idx + 3) vim.api.nvim_buf_set_extmark(0, ns_id, i - 1, idx - 1, { end_col = idx + 3, hl_group = "fgg" })
elseif line:match("") then elseif line:match("") then
local idx = string.find(line, "") local idx = string.find(line, "")
vim.api.nvim_buf_add_highlight(0, -1, "TodoPendingTopic", i - 1, 8, idx - 2) vim.api.nvim_buf_set_extmark(0, ns_id, i - 1, idx2 - 1, { end_col = idx - 2, hl_group = "fgkbgy" })
vim.api.nvim_buf_add_highlight(0, -1, "TodoPendingTopic", i - 1, idx + 3, -1) vim.api.nvim_buf_set_extmark(0, ns_id, i - 1, idx + 4, { end_col = #line, hl_group = "fgkbgy" })
vim.api.nvim_buf_add_highlight(0, -1, "TodoPendingHighlight", i - 1, idx - 1, idx + 3) vim.api.nvim_buf_set_extmark(0, ns_id, i - 1, idx - 1, { end_col = idx + 3, hl_group = "fgy" })
elseif line:match("") then elseif line:match("") then
local idx = string.find(line, "") local idx = string.find(line, "")
vim.api.nvim_buf_add_highlight(0, -1, "TodoImportantTopic", i - 1, 8, idx - 2) vim.api.nvim_buf_set_extmark(0, ns_id, i - 1, idx2 - 1, { end_col = idx - 2, hl_group = "fgkbgr" })
vim.api.nvim_buf_add_highlight(0, -1, "TodoImportantTopic", i - 1, idx + 3, -1) vim.api.nvim_buf_set_extmark(0, ns_id, i - 1, idx + 4, { end_col = #line, hl_group = "fgkbgr" })
vim.api.nvim_buf_add_highlight(0, -1, "TodoImportantHighlight", i - 1, idx - 1, idx + 3) vim.api.nvim_buf_set_extmark(0, ns_id, i - 1, idx - 1, { end_col = idx + 3, hl_group = "fgr" })
else else
vim.api.nvim_buf_add_highlight(0, -1, "Identifier", i - 1, 0, -1) vim.api.nvim_buf_set_extmark(0, ns_id, i - 1, 0, { end_col = #line, hl_group = "Identifier" })
end end
elseif line:match("^ #") then elseif line:match("^%s*#") then
vim.api.nvim_buf_add_highlight(0, -1, "Topic", i - 1, 4, -1) local idx = string.find(line, "#")
elseif line:match("^*") then vim.api.nvim_buf_set_extmark(0, ns_id, i - 1, idx - 1, { end_col = #line, hl_group = "fgkbgw" })
vim.api.nvim_buf_add_highlight(0, -1, "TopicHead", i - 1, 0, -1) elseif line:match("^%s*%*") then
local idx = string.find(line, "%*")
vim.api.nvim_buf_set_extmark(0, ns_id, i - 1, idx - 1, { end_col = #line, hl_group = "fgkbgw" })
elseif line:match("^%s*==*$") then
local idx = string.find(line, "=")
vim.api.nvim_buf_set_extmark(0, ns_id, i - 1, idx - 1, { end_col = #line, hl_group = "Comment" })
elseif line:match("^%s*%-%-") then
local idx = string.find(line, "%-")
vim.api.nvim_buf_set_extmark(0, ns_id, i - 1, idx - 1, { end_col = #line, hl_group = "Comment" })
else else
if line:match("") then if line:match("") then
local idx = string.find(line, "") local idx = string.find(line, "")
vim.api.nvim_buf_add_highlight(0, -1, "TodoDone", i - 1, 0, idx - 1) vim.api.nvim_buf_set_extmark(0, ns_id, i - 1, 0, { end_col = idx - 1, hl_group = "Comment" })
vim.api.nvim_buf_add_highlight(0, -1, "TodoDone", i - 1, idx + 2, -1) vim.api.nvim_buf_set_extmark(0, ns_id, i - 1, idx + 2, { end_col = #line, hl_group = "Comment" })
vim.api.nvim_buf_add_highlight(0, -1, "TodoDoneHighlight", i - 1, idx - 1, idx + 2) vim.api.nvim_buf_set_extmark(0, ns_id, i - 1, idx - 1, { end_col = idx + 2, hl_group = "fgg" })
elseif line:match("") then elseif line:match("") then
local idx = string.find(line, "") local idx = string.find(line, "")
vim.api.nvim_buf_add_highlight(0, -1, "TodoDone", i - 1, 0, idx - 1) vim.api.nvim_buf_set_extmark(0, ns_id, i - 1, 0, { end_col = idx - 1, hl_group = "Comment" })
vim.api.nvim_buf_add_highlight(0, -1, "TodoPending", i - 1, idx + 2, -1) vim.api.nvim_buf_set_extmark(0, ns_id, i - 1, idx + 2, { end_col = #line, hl_group = "Identifier" })
vim.api.nvim_buf_add_highlight(0, -1, "TodoPendingHighlight", i - 1, idx - 1, idx + 2) vim.api.nvim_buf_set_extmark(0, ns_id, i - 1, idx - 1, { end_col = idx + 2, hl_group = "fgy" })
elseif line:match("") then elseif line:match("") then
local idx = string.find(line, "") local idx = string.find(line, "")
vim.api.nvim_buf_add_highlight(0, -1, "TodoDone", i - 1, 0, idx - 1) vim.api.nvim_buf_set_extmark(0, ns_id, i - 1, 0, { end_col = idx - 1, hl_group = "Comment" })
vim.api.nvim_buf_add_highlight(0, -1, "TodoImportant", i - 1, idx + 2, -1) vim.api.nvim_buf_set_extmark(0, ns_id, i - 1, idx + 2, { end_col = #line, hl_group = "Error" })
vim.api.nvim_buf_add_highlight(0, -1, "TodoImportantHighlight", i - 1, idx - 1, idx + 2) vim.api.nvim_buf_set_extmark(0, ns_id, i - 1, idx - 1, { end_col = idx + 2, hl_group = "fgr" })
else else
vim.api.nvim_buf_add_highlight(0, -1, "Identifier", i - 1, 0, -1) vim.api.nvim_buf_set_extmark(0, ns_id, i - 1, 0, { end_col = #line, hl_group = "Identifier" })
end
end
for match in line:gmatch("`(.-)`") do
match = match:gsub("([^%w])", "%%%1")
local start_idx, end_idx = line:find("`" .. match .. "`")
while start_idx do
vim.api.nvim_buf_set_extmark(0, ns_id, i - 1, start_idx - 1, { end_col = end_idx, hl_group = "String" })
start_idx, end_idx = line:find("`" .. match .. "`", end_idx + 1)
end end
end end
end end
@@ -88,7 +124,7 @@ local function handle_inp()
local lines = vim.api.nvim_buf_get_lines(0, 0, -1, false) local lines = vim.api.nvim_buf_get_lines(0, 0, -1, false)
for i, line in ipairs(lines) do for i, line in ipairs(lines) do
if line:match("^%s*#") then if line:match("^%s*#") then
if not line:match("^ # ") then if not line:match("^%s*# ") then
local idx = string.find(line, "#") local idx = string.find(line, "#")
if not line:match("^%s*# ") then if not line:match("^%s*# ") then
vim.api.nvim_buf_set_text(0, i - 1, 0, i - 1, idx + 0, { " # " }) vim.api.nvim_buf_set_text(0, i - 1, 0, i - 1, idx + 0, { " # " })
@@ -108,110 +144,34 @@ local function handle_inp()
right(-1) right(-1)
end end
elseif line:match("^%s*%@") then elseif line:match("^%s*%@") then
if not line:match("^ @") then if not line:match("^%s*@ []") then
local idx = string.find(line, "@") local idx = string.find(line, "@")
if not line:match("^ @ ") then vim.api.nvim_buf_set_text(0, i - 1, 0, i - 1, idx + 0, { " @ " })
vim.api.nvim_buf_set_text(0, i - 1, 0, i - 1, idx + 0, { " @ " })
else
vim.api.nvim_buf_set_text(0, i - 1, 0, i - 1, idx + 0, { " @" })
end
right(-1) right(-1)
end end
if not line:match("[]") then
local current_pos = vim.api.nvim_win_get_cursor(0)
local line2 = vim.api.nvim_buf_get_lines(0, current_pos[1] - 1, current_pos[1], false)[1]
local end_of_line = #line2
if string.sub(line2, -2) == " " then
vim.api.nvim_buf_set_text(0, i - 1, end_of_line, i - 1, end_of_line, { "" })
elseif string.sub(line2, -2) == " " then
vim.api.nvim_buf_set_text(0, i - 1, end_of_line, i - 1, end_of_line, { "" })
else
vim.api.nvim_buf_set_text(0, i - 1, end_of_line, i - 1, end_of_line, { "" })
end
right(end_of_line)
end
elseif line:match("^%s*;") then elseif line:match("^%s*;") then
if not line:match("^ ;[]") then if not line:match("^%s*;[]") then
local idx = string.find(line, ";") local idx = string.find(line, ";")
vim.api.nvim_buf_set_text(0, i - 1, 0, i - 1, idx + 0, { " ; " }) vim.api.nvim_buf_set_text(0, i - 1, 0, i - 1, idx + 0, { " ; " })
right(-1) right(-1)
end end
end elseif line:match(" =$") then
end local idx = string.find(line, "=")
end vim.api.nvim_buf_set_text(
0,
function M.setup() i - 1,
vim.api.nvim_set_hl(0, "TodoDoneHighlight", { fg = "#00FF00", bold = true }) idx - 1,
vim.api.nvim_set_hl(0, "TodoPendingHighlight", { fg = "#FFFF00", bold = true }) i - 1,
vim.api.nvim_set_hl(0, "TodoImportantHighlight", { fg = "#FF0000", bold = true }) idx + 0,
vim.api.nvim_set_hl(0, "TodoDoneTopic", { fg = "#000000", bg = "#00FF00", bold = true }) { "==================================================" }
vim.api.nvim_set_hl(0, "TodoPendingTopic", { fg = "#000000", bg = "#FFFF00", bold = true })
vim.api.nvim_set_hl(0, "TodoImportantTopic", { fg = "#000000", bg = "#FF0000", bold = true })
vim.api.nvim_set_hl(0, "TodoDone", { link = "Comment" })
vim.api.nvim_set_hl(0, "TodoPending", { link = "Identifier" })
vim.api.nvim_set_hl(0, "TodoImportant", { link = "Error" })
vim.api.nvim_set_hl(0, "Topic", { fg = "#000000", bg = "#FFFFFF", bold = true })
vim.api.nvim_set_hl(0, "TopicHead", { fg = "#000000", bg = "#FFFFFF", bold = true })
vim.api.nvim_create_autocmd({ "BufRead", "BufNewFile" }, {
pattern = "*.udi",
callback = function()
vim.bo.filetype = "udi"
vim.opt_local.conceallevel = 2
local buf = vim.api.nvim_get_current_buf()
vim.api.nvim_buf_set_keymap(
buf,
"n",
"<leader>t",
":lua require('udivim').toggle_todo()<CR>",
{ noremap = true, silent = true }
) )
vim.api.nvim_buf_set_keymap( right(-1)
buf, elseif line:match(" %-$") then
"n", local idx = string.find(line, "%-")
"", vim.api.nvim_buf_set_text(0, i - 1, 0, i - 1, idx + 0, { " -- " })
":lua require('udivim').toggle_imp()<CR>", right(-1)
{ noremap = true, silent = true }
)
end,
})
vim.api.nvim_create_autocmd("FileType", {
pattern = "udi",
callback = function()
vim.bo.tabstop = 4
vim.bo.shiftwidth = 4
vim.bo.expandtab = true
vim.opt_local.foldmethod = "expr"
vim.opt_local.foldexpr = "v:lua.require('udivim').fold()"
vim.opt_local.foldlevel = 99
replace_task_symbols()
highlight_todo_items()
end,
})
vim.api.nvim_create_autocmd("TextChangedI", {
pattern = "*.udi",
callback = function()
local current_content = vim.api.nvim_buf_get_lines(0, 0, -1, false)
local prev_content = vim.b.prev_content or {}
local current_text = table.concat(current_content, "\n")
local prev_text = table.concat(prev_content, "\n")
if #current_text > #prev_text then
handle_inp()
end end
replace_task_symbols()
highlight_todo_items()
vim.b.prev_content = current_content
end,
})
end
function M.fold()
local lnum = vim.v.lnum
local line = vim.fn.getline(vim.v.lnum)
local indent_level = vim.fn.indent(vim.v.lnum)
if line:match("^%s*$") then
indent_level = vim.fn.indent(lnum - 1)
end end
return math.floor(indent_level / 4)
end end
function M.toggle_todo() function M.toggle_todo()
@@ -237,7 +197,7 @@ function M.toggle_imp()
local i = pos[1] local i = pos[1]
if line:match("") then if line:match("") then
local idx = string.find(line, "") local idx = string.find(line, "")
vim.api.nvim_buf_set_text(0, i - 1, idx - 1, i - 1, idx + 2, { "" }) vim.api.nvim_buf_set_text(0, i - 1, idx - 1, i - 1, idx + 2, { "" })
elseif line:match("") then elseif line:match("") then
local idx = string.find(line, "") local idx = string.find(line, "")
vim.api.nvim_buf_set_text(0, i - 1, idx - 1, i - 1, idx + 2, { "" }) vim.api.nvim_buf_set_text(0, i - 1, idx - 1, i - 1, idx + 2, { "" })
@@ -248,4 +208,82 @@ function M.toggle_imp()
highlight_todo_items() highlight_todo_items()
end end
function M.setup()
local define_grps = {
["fgg"] = { "g" },
["fgy"] = { "y" },
["fgr"] = { "r" },
["fgkbgg"] = { "k", "g" },
["fgkbgy"] = { "k", "y" },
["fgkbgr"] = { "k", "r" },
["fgkbgw"] = { "k", "w" },
}
for name, color in pairs(define_grps) do
define_color(name, color[1], color[2])
end
vim.api.nvim_create_autocmd({ "BufRead", "BufNewFile" }, {
pattern = "*.udi",
callback = function()
vim.bo.filetype = "udi"
vim.opt_local.conceallevel = 2
local buf = vim.api.nvim_get_current_buf()
vim.api.nvim_buf_set_keymap(
buf,
"n",
"<End>",
":lua require('udivim').toggle_todo()<CR>",
{ noremap = true, silent = true }
)
vim.api.nvim_buf_set_keymap(
buf,
"i",
"<End>",
"<C-o>:lua require('udivim').toggle_todo()<CR>",
{ noremap = true, silent = true }
)
vim.api.nvim_buf_set_keymap(
buf,
"n",
"<Home>",
":lua require('udivim').toggle_imp()<CR>",
{ noremap = true, silent = true }
)
vim.api.nvim_buf_set_keymap(
buf,
"i",
"<Home>",
"<C-o>:lua require('udivim').toggle_imp()<CR>",
{ noremap = true, silent = true }
)
end,
})
vim.api.nvim_create_autocmd("FileType", {
pattern = "udi",
callback = function()
vim.bo.tabstop = 4
vim.bo.shiftwidth = 4
vim.bo.expandtab = true
vim.opt_local.foldmethod = "indent"
vim.opt_local.foldlevel = 99
replace_task_symbols()
highlight_todo_items()
end,
})
vim.api.nvim_create_autocmd("TextChangedI", {
pattern = "*.udi",
callback = function()
local current_content = vim.api.nvim_buf_get_lines(0, 0, -1, false)
local prev_content = vim.b.prev_content or {}
local current_text = table.concat(current_content, "\n")
local prev_text = table.concat(prev_content, "\n")
if #current_text > #prev_text then
handle_inp()
end
replace_task_symbols()
highlight_todo_items()
vim.b.prev_content = current_content
end,
})
end
return M return M