robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1local api =vim . api 2 3local M ={} 4 5--- Table with default priorities used for highlighting: 6--- - `syntax`: `50`, used for standard syntax highlighting 7--- - `treesitter`: `100`, used for treesitter-based highlighting 8--- - `semantic_tokens`: `125`, used for LSP semantic token highlighting 9--- - `diagnostics`: `150`, used for code analysis such as diagnostics 10--- - `user`: `200`, used for user-triggered highlights such as LSP document 11--- symbols or `hl_op` autocommands 12M . priorities ={ 13syntax =50 , 14treesitter =100 , 15semantic_tokens =125 , 16diagnostics =150 , 17user =200 , 18} 19 20--- @class vim.hl.range.Opts 21--- @inlinedoc 22--- 23--- Type of range. See [getregtype()] 24--- (default: `'v'` i.e. charwise) 25--- @field regtype? string 26--- 27--- Indicates whether the range is end-inclusive 28--- (default: `false`) 29--- @field inclusive? boolean 30--- 31--- Highlight priority 32--- (default: `vim.hl.priorities.user`) 33--- @field priority? integer 34--- 35--- Time in ms before highlight is cleared 36--- (default: -1 no timeout) 37--- @field timeout? integer 38 39--- Apply highlight group to range of text. 40--- 41---@param buf integer Buffer number to apply highlighting to 42---@param ns integer Namespace to add highlight to 43---@param hlgroup integer|integer[]|string|string[] Highlight group used for the text range. 44--- See the `hl_group` option in |nvim_buf_set_extmark()|. 45---@param start [integer,integer]|string Start of region as a (line, column) tuple or string accepted by |getpos()| 46---@param finish [integer,integer]|string End of region as a (line, column) tuple or string accepted by |getpos()| 47---@param opts? vim.hl.range.Opts 48--- @return uv.uv_timer_t? range_timer A timer which manages how much time the 49--- highlight has left 50--- @return fun()? range_clear A function which allows clearing the highlight manually. 51--- nil is returned if timeout is not specified 52function M . range ( buf , ns , hlgroup , start , finish , opts ) 53-- Resolve buf=0 so the deferred `range_hl_clear` works correctly even after buffer-switch. 54buf =vim . _resolve_bufnr ( buf ) 55opts =opts or {} 56local regtype =opts . regtype or 'v' 57local inclusive =opts . inclusive or false 58local priority =opts . priority or M . priorities . user 59local timeout =opts . timeout or - 1 60 61local v_maxcol =vim . v . maxcol 62 63local pos1 --- @type [integer, integer, integer, integer] 64if type ( start ) == 'string' then 65pos1 =vim . fn . getpos ( start ) 66else 67pos1 ={ 68buf , 69start [ 1 ] + 1 , 70start [ 2 ] ~= - 1 and start [ 2 ] ~= v_maxcol and start [ 2 ] + 1 or v_maxcol , 710 , 72} 73end 74 75local pos2 ---@type [integer, integer, integer, integer] 76if type ( finish ) == 'string' then 77pos2 =vim . fn . getpos ( finish ) 78else 79pos2 ={ 80buf , 81finish [ 1 ] + 1 , 82finish [ 2 ] ~= - 1 and finish [ 2 ] ~= v_maxcol and finish [ 2 ] + 1 or v_maxcol , 830 , 84} 85end 86 87local buf_line_count =api . nvim_buf_line_count ( buf ) 88pos1 [ 2 ] =math . min ( pos1 [ 2 ], buf_line_count ) 89pos2 [ 2 ] =math . min ( pos2 [ 2 ], buf_line_count ) 90 91if pos1 [ 2 ] <= 0 or pos1 [ 3 ] <= 0 or pos2 [ 2 ] <= 0 or pos2 [ 3 ] <= 0 then 92return 93end 94 95vim . _with ( { buf =buf } , function () 96if pos1 [ 3 ] ~= v_maxcol then 97local max_col1 =vim . fn . col ( { pos1 [ 2 ], '$' } ) 98pos1 [ 3 ] =math . min ( pos1 [ 3 ], max_col1 ) 99end 100if pos2 [ 3 ] ~= v_maxcol then 101local max_col2 =vim . fn . col ( { pos2 [ 2 ], '$' } ) 102pos2 [ 3 ] =math . min ( pos2 [ 3 ], max_col2 ) 103end 104end ) 105 106-- For non-blockwise selection, use a single extmark and only compute its bounds. 107local bounds =regtype == 'v' or regtype == 'V' 108local region =vim . fn . getregionpos ( pos1 , pos2 , { 109type =regtype , 110exclusive =not inclusive , 111eol =true , 112bounds =bounds , 113} ) 114if bounds then 115local region1 =assert ( region [ 1 ]) 116if 117regtype == 'V' 118or region1 [ 2 ][ 2 ] == pos1 [ 2 ] and pos1 [ 3 ] == v_maxcol 119or region1 [ 2 ][ 2 ] == pos2 [ 2 ] and pos2 [ 3 ] == v_maxcol 120then 121region1 [ 2 ][ 2 ] =region1 [ 2 ][ 2 ] + 1 122region1 [ 2 ][ 3 ] =0 123end 124end 125 126local extmarks ={} --- @type integer[] 127for _ , res in ipairs ( region ) do 128local start_row =res [ 1 ][ 2 ] - 1 129local start_col =res [ 1 ][ 3 ] - 1 130local end_row =res [ 2 ][ 2 ] - 1 131local end_col =res [ 2 ][ 3 ] 132table . insert ( 133extmarks , 134api . nvim_buf_set_extmark ( buf , ns , start_row , start_col , { 135hl_group =hlgroup , 136end_row =end_row , 137end_col =end_col , 138priority =priority , 139strict =false , 140} ) 141) 142end 143 144local range_hl_clear =function () 145if not api . nvim_buf_is_valid ( buf ) then 146return 147end 148for _ , mark in ipairs ( extmarks ) do 149api . nvim_buf_del_extmark ( buf , ns , mark ) 150end 151end 152 153if timeout ~= - 1 then 154local range_timer =vim . defer_fn ( range_hl_clear , timeout ) 155return range_timer , range_hl_clear 156end 157end 158 159---@class (private) vim.hl.OnEventState 160---@field timer? uv.uv_timer_t Timer to clear the highlight. 161---@field clear? fun() Function to clear the highlight immediately. 162 163--- @type { [string]: vim.hl.OnEventState } 164local hl_op_state ={} 165 166local events_ns =api . nvim_create_namespace ( 'nvim.hl.events' ) 167 168--- Highlights the affected text region during a |TextYankPost| or |TextPutPost| event. 169--- 170--- Add this to your `init.vim`: 171--- 172--- ```vim 173--- autocmd TextYankPost * silent! lua vim.hl.hl_op {higroup='Visual', timeout=300} 174--- autocmd TextPutPost * silent! lua vim.hl.hl_op {higroup='Visual', timeout=300} 175--- ``` 176--- 177--- @param opts table|nil Optional parameters 178--- - event (default: vim.v.event) Event structure. 179--- - higroup (default: "IncSearch") Highlight group for the text region. 180--- - on_macro (default: false) Highlight during |macro| execution. 181--- - on_visual (default: true) Highlight during |Visual| mode. 182--- - priority (default: |vim.hl.priorities|`.user`) Integer priority. 183--- - timeout (default: 150) Time in ms before highlight is cleared. 184function M . hl_op ( opts ) 185vim . validate ( 'opts' , opts , 'table' , true ) 186opts =opts or {} 187local event =opts . event or vim . v . event 188local on_macro =opts . on_macro or false 189local on_visual =( opts . on_visual ~= false ) 190 191if not on_macro and vim . fn . reg_executing () ~= '' then 192return 193elseif 194event . regtype == '' 195or ( event . operator ~= 'y' and event . operator ~= 'p' and event . operator ~= 'P' ) 196then 197return 198elseif not on_visual and event . visual then 199return 200end 201 202local state_key =event . operator == 'y' and 'yank' or 'put' 203local hlgroup =opts . higroup or 'IncSearch' 204 205local bufnr =api . nvim_get_current_buf () 206local winid =api . nvim_get_current_win () 207 208local state =hl_op_state [ state_key ] 209-- Multicursor cascade: accumulate per-cursor, don't cancel the previous event's highlight. 210local cascading =api . nvim__mcursor_cascading () 211if state ~= nil and state . timer and not state . timer : is_closing () and not cascading then 212state . timer : close () 213assert ( state . clear ) 214state . clear () 215end 216 217api . nvim__ns_set ( events_ns , { wins ={ winid } } ) 218local timer , clear =M . range ( bufnr , events_ns , hlgroup , "'[" , "']" , { 219regtype =event . regtype , 220inclusive =true , 221priority =opts . priority or M . priorities . user , 222timeout =opts . timeout or 150 , 223} ) 224 225hl_op_state [ state_key ] ={ 226timer =timer , 227clear =clear , 228} 229end 230 231--- @deprecated Use |vim.hl.hl_op()| instead. 232--- @param opts? table 233function M . on_yank ( opts ) 234vim . deprecate ( 'vim.hl.on_yank' , 'vim.hl.hl_op' , '0.14' ) 235return M . hl_op ( opts ) 236end 237 238return M