Skip to content

Commit

Permalink
fix: fix highlight not work when start neovim
Browse files Browse the repository at this point in the history
  • Loading branch information
sontungexpt committed Sep 21, 2023
1 parent 7ddae44 commit 26b6dc8
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 20 deletions.
38 changes: 19 additions & 19 deletions lua/url-open/modules/autocmd.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
5 changes: 5 additions & 0 deletions lua/url-open/modules/commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
19 changes: 18 additions & 1 deletion lua/url-open/modules/handlers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

0 comments on commit 26b6dc8

Please sign in to comment.