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 #57 #61

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
feat: add highlighting of selected region #57
  • Loading branch information
stevanmilic committed Jul 11, 2022
commit ef5721b984215df8a9de81c40a89fb6b54c4b434
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
32 changes: 32 additions & 0 deletions lua/gitlinker/buffer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,36 @@ 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 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_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