From 26b6dc8edb885658ab25fd6921c637d59a40eb0b Mon Sep 17 00:00:00 2001 From: Tran Vo Son Tung Date: Thu, 21 Sep 2023 22:25:42 +0700 Subject: [PATCH] fix: fix highlight not work when start neovim --- lua/url-open/modules/autocmd.lua | 38 +++++++++++++++---------------- lua/url-open/modules/commands.lua | 5 ++++ lua/url-open/modules/handlers.lua | 19 +++++++++++++++- 3 files changed, 42 insertions(+), 20 deletions(-) diff --git a/lua/url-open/modules/autocmd.lua b/lua/url-open/modules/autocmd.lua index bd850d5..fa608f8 100644 --- a/lua/url-open/modules/autocmd.lua +++ b/lua/url-open/modules/autocmd.lua @@ -14,18 +14,32 @@ local M = {} --- @see url-open.setup --- @see url-open.modules.handlers.highlight_cursor_url --- @see url-open.modules.handlers.set_url_effect + M.setup = function(user_opts) local highlight_url = user_opts.highlight_url if highlight_url.all_urls.enabled then - api.nvim_create_autocmd({ "BufEnter", "WinEnter" }, { + local is_loaded = false + local cursor_hold_id = 0 + -- TODO: Need to find a other way to enable command when open neovim + cursor_hold_id = api.nvim_create_autocmd({ "CursorHold" }, { desc = "URL Highlighting", - group = api.nvim_create_augroup("HighlightAllUrl", { clear = true }), + group = api.nvim_create_augroup("HighlightAllUrlWhenOpenNeovim", { clear = true }), callback = function() - handlers.set_url_effect() - M.change_color_highlight(highlight_url.all_urls, "HighlightAllUrl") + if not is_loaded then + api.nvim_command("HighlightAllUrls") + is_loaded = true + else --remove autocmd after first run + api.nvim_del_autocmd(cursor_hold_id) + end end, }) + + api.nvim_create_autocmd({ "BufEnter", "WinEnter" }, { + desc = "URL Highlighting", + group = api.nvim_create_augroup("HighlightAllUrl", { clear = true }), + command = "HighlightAllUrls", + }) end if highlight_url.cursor_move.enabled then @@ -34,24 +48,10 @@ M.setup = function(user_opts) group = api.nvim_create_augroup("HighlightCursorUrl", { clear = true }), callback = function() handlers.highlight_cursor_url(user_opts) - M.change_color_highlight(highlight_url.cursor_move, "HighlightCursorUrl") + handlers.change_color_highlight(highlight_url.cursor_move, "HighlightCursorUrl") end, }) end end ---- Change the color of the highlight ---- @tparam table opts : The user options ---- @tparam string group_name : The name of the highlight group ---- @treturn nil ---- @see url-open.modules.handlers.highlight_cursor_url ---- @see url-open.modules.handlers.set_url_effect ---- @see url-open.modules.autocmd.setup ---- @see url-open.setup -M.change_color_highlight = function(opts, group_name) - opts.enabled = nil - - api.nvim_set_hl(0, group_name, opts) -end - return M diff --git a/lua/url-open/modules/commands.lua b/lua/url-open/modules/commands.lua index 0f02014..8e19b10 100644 --- a/lua/url-open/modules/commands.lua +++ b/lua/url-open/modules/commands.lua @@ -14,6 +14,11 @@ M.setup = function(user_opts) function() handlers.open_url(user_opts) end, { nargs = 0 } ) + + vim.api.nvim_create_user_command("HighlightAllUrls", function() + handlers.set_url_effect() + handlers.change_color_highlight(user_opts.highlight_url.all_urls, "HighlightAllUrl") + end, { nargs = 0 }) end return M diff --git a/lua/url-open/modules/handlers.lua b/lua/url-open/modules/handlers.lua index 19c8fe2..bc85707 100644 --- a/lua/url-open/modules/handlers.lua +++ b/lua/url-open/modules/handlers.lua @@ -109,7 +109,8 @@ M.find_first_url_in_text = function(user_opts, text, start_pos) if user_opts.deep_pattern then local results = fn.matchstrpos(text, patterns_module.DEEP_PATTERN, start_pos) -- result[1] is url, result[2] is start_pos, result[3] is end_pos - if results[1] ~= "" and (start_found or string.len(text)) > results[2] + 1 then + -- >= to make deep_pattern has higher priority than default patterns + if results[1] ~= "" and (start_found or string.len(text)) >= results[2] + 1 then start_found, end_found, url_found = results[2] + 1, results[3], results[1] end end @@ -244,4 +245,20 @@ M.highlight_cursor_url = function(user_opts) start_pos, end_pos, url = M.find_first_url_in_text(user_opts, line, end_pos + 1) end end + +--- Change the color of the highlight +--- @tparam table opts : The user options +--- @tparam string group_name : The name of the highlight group +--- @treturn nil +--- @see url-open.modules.handlers.highlight_cursor_url +--- @see url-open.modules.handlers.set_url_effect +--- @see url-open.modules.autocmd.setup +--- @see url-open.setup +M.change_color_highlight = function(opts, group_name) + opts.enabled = nil + -- if opts.fg and opts.fg == "text" then opts.fg = nil end + + api.nvim_set_hl(0, group_name, opts) +end + return M