Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add highlighting of selected region #88

Merged
merged 1 commit into from
Nov 13, 2023
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 @@
--- @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 @@
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 @@
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)

Check warning on line 231 in lua/gitlinker.lua

View check run for this annotation

Codecov / codecov/patch

lua/gitlinker.lua#L229-L231

Added lines #L229 - L231 were not covered by tests
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

Check warning on line 7 in lua/gitlinker/highlight.lua

View check run for this annotation

Codecov / codecov/patch

lua/gitlinker/highlight.lua#L6-L7

Added lines #L6 - L7 were not covered by tests
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

Check warning on line 12 in lua/gitlinker/highlight.lua

View check run for this annotation

Codecov / codecov/patch

lua/gitlinker/highlight.lua#L9-L12

Added lines #L9 - L12 were not covered by tests
end
local pos1 = { lstart - 1, 1 }
local pos2 = { (lend or lstart) - 1, vim.fn.col("$") }
vim.highlight.range(

Check warning on line 16 in lua/gitlinker/highlight.lua

View check run for this annotation

Codecov / codecov/patch

lua/gitlinker/highlight.lua#L14-L16

Added lines #L14 - L16 were not covered by tests
0,
namespace,
"NvimGitLinkerHighlightTextObject",
pos1,
pos2,
{ inclusive = true }
)
-- Force the screen to highlight the text immediately
vim.cmd("redraw")

Check warning on line 25 in lua/gitlinker/highlight.lua

View check run for this annotation

Codecov / codecov/patch

lua/gitlinker/highlight.lua#L25

Added line #L25 was not covered by tests
end

-- Clears the gitlinker highlights for the buffer.
M.clear = function()
local namespace = vim.api.nvim_create_namespace("NvimGitLinker")
linrongbin16 marked this conversation as resolved.
Show resolved Hide resolved
vim.api.nvim_buf_clear_namespace(0, namespace, 0, -1)

Check warning on line 31 in lua/gitlinker/highlight.lua

View check run for this annotation

Codecov / codecov/patch

lua/gitlinker/highlight.lua#L30-L31

Added lines #L30 - L31 were not covered by tests
-- Force the screen to clear the highlight immediately
vim.cmd("redraw")

Check warning on line 33 in lua/gitlinker/highlight.lua

View check run for this annotation

Codecov / codecov/patch

lua/gitlinker/highlight.lua#L33

Added line #L33 was not covered by tests
end

return M
Loading