Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,10 @@ require('gitlinker').setup({
-- print message in command line
message = 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,

-- key mapping
mapping = {
["<leader>gl"] = {
Expand Down
19 changes: 19 additions & 0 deletions lua/gitlinker.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local logger = require("gitlinker.logger")
local Linker = require("gitlinker.linker").Linker
local highlight = require("gitlinker.highlight")

--- @alias Options table<any, any>
--- @type Options
Expand All @@ -9,6 +10,11 @@ local Defaults = {
--- @type boolean
message = true,

-- highlight the linked region
--
--- @type integer
highlight_duration = 500,

-- key mappings
--
--- @alias KeyMappingConfig {action:fun(url:string):nil,desc:string?}
Expand Down Expand Up @@ -130,6 +136,15 @@ local function setup(opts)
end
end

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

-- logger.debug("|setup| Configs:%s", vim.inspect(Configs))
end

Expand Down Expand Up @@ -211,6 +226,10 @@ local function link(opts)
if opts.action then
opts.action(url)
end
if opts.highlight_duration >= 0 then
highlight.show({ lstart = lk.lstart, lend = lk.lend })
vim.defer_fn(highlight.clear, opts.highlight_duration)
end
if opts.message then
local msg = lk.file_changed
and string.format(
Expand Down
36 changes: 36 additions & 0 deletions lua/gitlinker/highlight.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
local M = {}

-- Highlights the text selected by the specified range.
--- @param range Range?
M.show = function(range)
if not range then
return
end
local namespace = vim.api.nvim_create_namespace("NvimGitLinker")
local lstart, lend = range.lstart, range.lend
if lend and lend < lstart then
lstart, lend = lend, lstart
end
local pos1 = { lstart - 1, 1 }
local pos2 = { (lend or 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 = 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