Skip to content

Commit

Permalink
feat: add highlighting of selected region ruifm#57
Browse files Browse the repository at this point in the history
  • Loading branch information
stevanmilic committed Jul 10, 2022
1 parent 782e98d commit ec41c8b
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 1 deletion.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ require"gitlinker".setup()
- `<leader>gy` for normal and visual mode

When used, it will copy the generated url to your clipboard and print it in
`:messages`.
`:messages` with highlight of the selected region.

- In normal mode, it will add the current line number to the url
- In visual mode , it will add the line range of the visual selection to the url
Expand Down Expand Up @@ -126,6 +126,9 @@ require"gitlinker".setup({
action_callback = require"gitlinker.actions".copy_to_clipboard,
-- print the url after performing the action
print_url = true,
-- highlights the linked line(s) by the time in ms
-- disable highlight by setting a value equal or less than 0
highlight_duration = 100,
},
callbacks = {
["github.com"] = require"gitlinker.hosts".get_github_type_url,
Expand Down
3 changes: 3 additions & 0 deletions doc/gitlinker.txt
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ Here’s all the options with their defaults:
action_callback = require"gitlinker.actions".copy_to_clipboard,
-- print the url after performing the action
print_url = true,
-- highlights the linked line(s) by the time in ms
-- disable highlight by setting a value equal or less than 0
highlight_duration = 100,
-- mapping to call url generation
mappings = "<leader>gy"
},
Expand Down
5 changes: 5 additions & 0 deletions lua/gitlinker.lua
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@ function M.get_buf_range_url(mode, user_opts)

local url = matching_callback(url_data)

if user_opts.highlight_duration >= 0 then
buffer.highlight_range(url_data)
vim.defer_fn(buffer.clear_highlights, user_opts.highlight_duration)
end

if user_opts.action_callback then
user_opts.action_callback(url)
end
Expand Down
28 changes: 28 additions & 0 deletions lua/gitlinker/buffer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,32 @@ function M.get_range(mode, add_current_line_on_normal_mode)
return { lstart = lstart, lend = lend }
end

--[[
Highlights the text selected by the specified range.
]]
M.highlight_range = function(range)
local namespace = vim.api.nvim_create_namespace("NvimGitLinker")
local pos1 = { range.lstart - 1, 1 }
local pos2 = { (range.lend or range.lstart) - 1, vim.fn.col("$") }
vim.highlight.range(
0,
namespace,
"NvimGitLinkerHighlightTextObject",
pos1,
pos2,
{ inclusive = true }
)
-- Force the screen to highlight the text immediately
vim.cmd("redraw")
end

--[[
Clears the gitlinker highlights for the buffer.
]]
M.clear_highlights = function()
local namespace = vim.api.nvim_create_namespace("NvimGitLinker")
vim.api.nvim_buf_clear_namespace(0, namespace, 0, -1)
-- Force the screen to clear the highlight immediately
vim.cmd("redraw")
end
return M
6 changes: 6 additions & 0 deletions lua/gitlinker/opts.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ local defaults = {
add_current_line_on_normal_mode = true, -- if true adds the line nr in the url for normal mode
action_callback = require("gitlinker.actions").copy_to_clipboard, -- callback for what to do with the url
print_url = true, -- print the url after action
highlight_duration = 100, -- highlight the linked region
}

local opts
Expand All @@ -14,6 +15,11 @@ function M.setup(user_opts)
opts = vim.tbl_deep_extend("force", {}, defaults)
end
opts = vim.tbl_deep_extend("force", opts, user_opts or {})

-- Configure highlight group
if user_opts.highlight_duration >= 0 then
vim.api.nvim_set_hl(0, "NvimGitLinkerHighlightTextObject", {link = "Search"})
end
end

function M.get()
Expand Down

0 comments on commit ec41c8b

Please sign in to comment.