Skip to content

Commit 2e97768

Browse files
authored
feat: add highlighting of selected region (#88)
1 parent 05bd381 commit 2e97768

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,10 @@ require('gitlinker').setup({
170170
-- print message in command line
171171
message = true,
172172

173+
-- highlights the linked line(s) by the time in ms
174+
-- disable highlight by setting a value equal or less than 0
175+
highlight_duration = 100,
176+
173177
-- key mapping
174178
mapping = {
175179
["<leader>gl"] = {

lua/gitlinker.lua

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
local logger = require("gitlinker.logger")
22
local Linker = require("gitlinker.linker").Linker
3+
local highlight = require("gitlinker.highlight")
34

45
--- @alias Options table<any, any>
56
--- @type Options
@@ -9,6 +10,11 @@ local Defaults = {
910
--- @type boolean
1011
message = true,
1112

13+
-- highlight the linked region
14+
--
15+
--- @type integer
16+
highlight_duration = 500,
17+
1218
-- key mappings
1319
--
1420
--- @alias KeyMappingConfig {action:fun(url:string):nil,desc:string?}
@@ -130,6 +136,15 @@ local function setup(opts)
130136
end
131137
end
132138

139+
-- Configure highlight group
140+
if Configs.highlight_duration >= 0 then
141+
vim.api.nvim_set_hl(
142+
0,
143+
"NvimGitLinkerHighlightTextObject",
144+
{ link = "Search" }
145+
)
146+
end
147+
133148
-- logger.debug("|setup| Configs:%s", vim.inspect(Configs))
134149
end
135150

@@ -211,6 +226,10 @@ local function link(opts)
211226
if opts.action then
212227
opts.action(url)
213228
end
229+
if opts.highlight_duration >= 0 then
230+
highlight.show({ lstart = lk.lstart, lend = lk.lend })
231+
vim.defer_fn(highlight.clear, opts.highlight_duration)
232+
end
214233
if opts.message then
215234
local msg = lk.file_changed
216235
and string.format(

lua/gitlinker/highlight.lua

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
local M = {}
2+
3+
-- Highlights the text selected by the specified range.
4+
--- @param range Range?
5+
M.show = function(range)
6+
if not range then
7+
return
8+
end
9+
local namespace = vim.api.nvim_create_namespace("NvimGitLinker")
10+
local lstart, lend = range.lstart, range.lend
11+
if lend and lend < lstart then
12+
lstart, lend = lend, lstart
13+
end
14+
local pos1 = { lstart - 1, 1 }
15+
local pos2 = { (lend or lstart) - 1, vim.fn.col("$") }
16+
vim.highlight.range(
17+
0,
18+
namespace,
19+
"NvimGitLinkerHighlightTextObject",
20+
pos1,
21+
pos2,
22+
{ inclusive = true }
23+
)
24+
-- Force the screen to highlight the text immediately
25+
vim.cmd("redraw")
26+
end
27+
28+
-- Clears the gitlinker highlights for the buffer.
29+
M.clear = function()
30+
local namespace = vim.api.nvim_create_namespace("NvimGitLinker")
31+
vim.api.nvim_buf_clear_namespace(0, namespace, 0, -1)
32+
-- Force the screen to clear the highlight immediately
33+
vim.cmd("redraw")
34+
end
35+
36+
return M

0 commit comments

Comments
 (0)