From ec41c8b1202942bcaee6059c44a333e3b1b852d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stevan=20Mili=C4=87?= Date: Sat, 9 Jul 2022 20:55:43 +0200 Subject: [PATCH] feat: add highlighting of selected region #57 --- README.md | 5 ++++- doc/gitlinker.txt | 3 +++ lua/gitlinker.lua | 5 +++++ lua/gitlinker/buffer.lua | 28 ++++++++++++++++++++++++++++ lua/gitlinker/opts.lua | 6 ++++++ 5 files changed, 46 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 4db4a989..1a90bef9 100644 --- a/README.md +++ b/README.md @@ -70,7 +70,7 @@ require"gitlinker".setup() - `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 @@ -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, diff --git a/doc/gitlinker.txt b/doc/gitlinker.txt index 478133c1..0d88e9f2 100644 --- a/doc/gitlinker.txt +++ b/doc/gitlinker.txt @@ -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 = "gy" }, diff --git a/lua/gitlinker.lua b/lua/gitlinker.lua index c1b64fcb..f16dad6a 100644 --- a/lua/gitlinker.lua +++ b/lua/gitlinker.lua @@ -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 diff --git a/lua/gitlinker/buffer.lua b/lua/gitlinker/buffer.lua index a2e04bb4..35562212 100644 --- a/lua/gitlinker/buffer.lua +++ b/lua/gitlinker/buffer.lua @@ -24,4 +24,32 @@ 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 pos1 = { range.lstart - 1, 1 } + local pos2 = { (range.lend or range.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 diff --git a/lua/gitlinker/opts.lua b/lua/gitlinker/opts.lua index ad58dd18..8487d8dd 100644 --- a/lua/gitlinker/opts.lua +++ b/lua/gitlinker/opts.lua @@ -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 @@ -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()