Skip to content

Commit 72c028c

Browse files
committed
feat: add highlighting of selected region #57
1 parent 782e98d commit 72c028c

File tree

5 files changed

+48
-1
lines changed

5 files changed

+48
-1
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ require"gitlinker".setup()
7070
- `<leader>gy` for normal and visual mode
7171

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

7575
- In normal mode, it will add the current line number to the url
7676
- In visual mode , it will add the line range of the visual selection to the url
@@ -126,6 +126,9 @@ require"gitlinker".setup({
126126
action_callback = require"gitlinker.actions".copy_to_clipboard,
127127
-- print the url after performing the action
128128
print_url = true,
129+
-- highlights the linked line(s) by the time in ms
130+
-- disable highlight by setting a value equal or less than 0
131+
highlight_duration = 100,
129132
},
130133
callbacks = {
131134
["github.com"] = require"gitlinker.hosts".get_github_type_url,

doc/gitlinker.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,9 @@ Here’s all the options with their defaults:
124124
action_callback = require"gitlinker.actions".copy_to_clipboard,
125125
-- print the url after performing the action
126126
print_url = true,
127+
-- highlights the linked line(s) by the time in ms
128+
-- disable highlight by setting a value equal or less than 0
129+
highlight_duration = 100,
127130
-- mapping to call url generation
128131
mappings = "<leader>gy"
129132
},

lua/gitlinker.lua

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,11 @@ function M.get_buf_range_url(mode, user_opts)
124124

125125
local url = matching_callback(url_data)
126126

127+
if user_opts.highlight_duration >= 0 then
128+
buffer.highlight_range(url_data)
129+
vim.defer_fn(buffer.clear_highlights, user_opts.highlight_duration)
130+
end
131+
127132
if user_opts.action_callback then
128133
user_opts.action_callback(url)
129134
end

lua/gitlinker/buffer.lua

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,32 @@ function M.get_range(mode, add_current_line_on_normal_mode)
2424
return { lstart = lstart, lend = lend }
2525
end
2626

27+
--[[
28+
Highlights the text selected by the specified range.
29+
]]
30+
M.highlight_range = function(range)
31+
local namespace = vim.api.nvim_create_namespace("NvimGitLinker")
32+
local pos1 = { range.lstart - 1, 1 }
33+
local pos2 = { (range.lend or range.lstart) - 1, vim.fn.col("$") }
34+
vim.highlight.range(
35+
0,
36+
namespace,
37+
"NvimGitLinkerHighlightTextObject",
38+
pos1,
39+
pos2,
40+
{ inclusive = true }
41+
)
42+
-- Force the screen to highlight the text immediately
43+
vim.cmd("redraw")
44+
end
45+
46+
--[[
47+
Clears the gitlinker highlights for the buffer.
48+
]]
49+
M.clear_highlights = function()
50+
local namespace = vim.api.nvim_create_namespace("NvimGitLinker")
51+
vim.api.nvim_buf_clear_namespace(0, namespace, 0, -1)
52+
-- Force the screen to clear the highlight immediately
53+
vim.cmd("redraw")
54+
end
2755
return M

lua/gitlinker/opts.lua

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ local defaults = {
55
add_current_line_on_normal_mode = true, -- if true adds the line nr in the url for normal mode
66
action_callback = require("gitlinker.actions").copy_to_clipboard, -- callback for what to do with the url
77
print_url = true, -- print the url after action
8+
highlight_duration = 100, -- highlight the linked region
89
}
910

1011
local opts
@@ -14,6 +15,13 @@ function M.setup(user_opts)
1415
opts = vim.tbl_deep_extend("force", {}, defaults)
1516
end
1617
opts = vim.tbl_deep_extend("force", opts, user_opts or {})
18+
19+
-- Configure highlight group
20+
if user_opts.highlight_duration >= 0 then
21+
vim.cmd([[
22+
highlight default link NvimGitLinkerHighlightTextObject Search
23+
]])
24+
end
1725
end
1826

1927
function M.get()

0 commit comments

Comments
 (0)