robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1local api =vim . api 2local fn =vim . fn 3local nvim_on =require ( 'vim._core.util' ). nvim_on 4 5local M ={} 6 7local skip_names ={ 'string' , 'character' , 'singlequote' , 'escape' , 'symbol' , 'comment' } 8 9---@param text string 10---@param byte_col integer 11---@return string 12local function char_at ( text , byte_col ) 13if byte_col > # text then 14return '' 15end 16return text : sub ( byte_col , byte_col + vim . str_utf_end ( text , byte_col )) 17end 18 19---@param text string 20---@param byte_col integer 21---@return string 22local function char_before ( text , byte_col ) 23local end_col =math . min ( byte_col - 1 , # text ) 24if end_col < 1 then 25return '' 26end 27return text : sub ( end_col + vim . str_utf_start ( text , end_col ), end_col ) 28end 29 30function M . enable () 31vim . g . loaded_matchparen =1 32 33if vim . g . matchparen_timeout == nil then 34vim . g . matchparen_timeout =300 35end 36if vim . g . matchparen_insert_timeout == nil then 37vim . g . matchparen_insert_timeout =60 38end 39if vim . g . matchparen_disable_cursor_hl == nil then 40vim . g . matchparen_disable_cursor_hl =0 41end 42 43local group =api . nvim_create_augroup ( 'matchparen' , { clear =true } ) 44 45-- Replace all matchparen autocommands 46nvim_on ( 47{ 'CursorMoved' , 'CursorMovedI' , 'WinEnter' , 'WinScrolled' , 'TextChanged' , 'TextChangedI' } , 48group , 49function () 50M . highlight_matching_pair () 51end 52) 53nvim_on ( 'BufWinEnter' , group , function () 54nvim_on ( 'SafeState' , group , { once =true } , function () 55M . highlight_matching_pair () 56end ) 57end ) 58nvim_on ( { 'WinLeave' , 'BufLeave' , 'TextChangedP' } , group , function () 59M . remove_matches () 60end ) 61 62-- Define commands that will disable and enable the plugin. 63api . nvim_create_user_command ( 'DoMatchParen' , function () 64M . do_matchparen () 65end , { force =true } ) 66api . nvim_create_user_command ( 'NoMatchParen' , function () 67M . no_matchparen () 68end , { force =true } ) 69end 70 71--- The function that is invoked (very often) to define a ":match" highlighting 72--- for any matching paren. 73---@param win? integer 74function M . highlight_matching_pair ( win ) 75win =win or api . nvim_get_current_win () 76local buf =api . nvim_win_get_buf ( win ) 77 78if vim . w [ win ]. matchparen_ids == nil then 79vim . w [ win ]. matchparen_ids ={} 80end 81-- Remove any previous match. 82M . remove_matches ( win ) 83 84-- Avoid that we remove the popup menu. 85if fn . pumvisible () ~= 0 then 86return 87end 88 89-- Get the character under the cursor and check if it's in 'matchpairs'. 90local cursor =api . nvim_win_get_cursor ( win ) 91local c_lnum =cursor [ 1 ] 92local c_col =cursor [ 2 ] + 1 93local before =0 94 95local text =api . nvim_buf_get_lines ( buf , c_lnum - 1 , c_lnum , false )[ 1 ] or '' 96-- Cursor columns are byte indexes, 97-- while 'matchpairs' entries may be multibyte characters. 98-- Use UTF-8 boundaries to extract the whole character around the byte column. 99local c_before =char_before ( text , c_col ) 100local c =char_at ( text , c_col ) 101---@type string[] 102local plist =vim . split ( vim . bo [ buf ]. matchpairs , '[:,]' , { trimempty =true } ) 103---@type integer? 104local i =vim . iter ( ipairs ( plist )): find ( function ( _ , item ) 105return item == c 106end ) 107if i == nil then 108-- not found, in Insert mode try character before the cursor 109local mode =api . nvim_get_mode (). mode 110if c_col > 1 and ( mode == 'i' or mode == 'R' ) then 111before =# c_before 112c =c_before 113i =vim . iter ( ipairs ( plist )): find ( function ( _ , item ) 114return item == c 115end ) 116end 117if i == nil then 118-- not found, nothing to do 119return 120end 121end 122 123-- Figure out the arguments for searchpairpos(). 124local flags ---@type string 125local c2 ---@type string 126if i % 2 == 1 then 127flags ='nW' 128c2 =assert ( plist [ i + 1 ]) 129else 130flags ='nbW' 131c2 =c 132c =assert ( plist [ i - 1 ]) 133end 134if c == '[' then 135c =[=[\[]=] 136c2 =[=[\]]=] 137end 138 139-- Find the match. When it was just before the cursor move it there for a 140-- moment. 141local save_cursor ---@type [integer, integer, integer, integer, integer]? 142if before > 0 then 143save_cursor =fn . getcurpos ( win ) 144api . nvim_win_set_cursor ( win , { c_lnum , c_col - before - 1 } ) 145end 146 147local skip ---@type fun(): boolean 148if vim . g . syntax_on == nil then 149skip =function () 150return false 151end 152elseif vim . b [ buf ]. ts_highlight ~= nil and vim . bo [ buf ]. syntax ~= 'on' then 153skip =function () 154for _ , capture in ipairs ( vim . treesitter . get_captures_at_cursor ( win )) do 155for _ , skip_name in ipairs ( skip_names ) do 156if capture : find ( skip_name , 1 , true ) ~= nil then 157return true 158end 159end 160end 161return false 162end 163else 164-- do not attempt to match when the syntax item where the cursor is 165-- indicates there does not exist a matching parenthesis, e.g. for shells 166-- case statement: "case $var in foobar)" 167-- 168-- add the check behind a filetype check, so it only needs to be 169-- evaluated for certain filetypes 170if vim . bo [ buf ]. filetype == 'sh' then 171local pos =api . nvim_win_get_cursor ( win ) 172---@type integer[] 173local stack =api . nvim_win_call ( win , function () 174return fn . synstack ( pos [ 1 ], pos [ 2 ] + 1 ) 175end ) 176for _ , id in ipairs ( stack ) do 177if fn . synIDattr ( id , 'name' ): lower (): find ( 'shsnglcase' ) ~= nil then 178if save_cursor ~= nil then 179api . nvim_win_call ( win , function () 180fn . setpos ( '.' , save_cursor ) 181end ) 182end 183return 184end 185end 186end 187-- Build an expression that detects whether the current cursor position is 188-- in certain syntax types (string, comment, etc.), for use as 189-- searchpairpos()'s skip argument. 190-- We match "escape" for special items, such as lispEscapeSpecial, and 191-- match "symbol" for lispBarSymbol. 192skip =function () 193local pos =api . nvim_win_get_cursor ( win ) 194for _ , id in ipairs ( fn . synstack ( pos [ 1 ], pos [ 2 ] + 1 )) do 195local name =fn . synIDattr ( id , 'name' ): lower () 196for _ , skip_name in ipairs ( skip_names ) do 197if name : find ( skip_name , 1 , true ) ~= nil then 198return true 199end 200end 201end 202return false 203end 204-- If executing the expression determines that the cursor is currently in 205-- one of the syntax types, then we want searchpairpos() to find the pair 206-- within those syntax types (i.e., not skip). Otherwise, the cursor is 207-- outside of the syntax types and skip should keep its value so we skip 208-- any matching pair inside the syntax types. 209if api . nvim_win_call ( win , skip ) then 210skip =function () 211return false 212end 213end 214end 215 216-- Limit the search to lines visible in the window. 217---@type integer, integer 218local stoplinetop , stoplinebottom =unpack ( api . nvim_win_call ( win , function () 219return { fn . line ( 'w0' ), fn . line ( 'w$' ) } 220end )) 221local stopline ---@type integer 222if i % 2 == 1 then 223stopline =stoplinebottom 224else 225stopline =stoplinetop 226end 227 228-- Limit the search time to 300 msec to avoid a hang on very long lines. 229local timeout ---@type integer 230local mode =api . nvim_get_mode (). mode 231if mode == 'i' or mode == 'R' then 232timeout =vim . b [ buf ]. matchparen_insert_timeout or vim . g . matchparen_insert_timeout 233else 234timeout =vim . b [ buf ]. matchparen_timeout or vim . g . matchparen_timeout 235end 236 237---@type boolean, [integer, integer]|string 238local ok , match =pcall ( api . nvim_win_call , win , function () 239return fn . searchpairpos ( c , '' , c2 , flags , skip , stopline , timeout ) 240end ) 241if not ok then ---@cast match string 242if save_cursor ~= nil then 243api . nvim_win_call ( win , function () 244fn . setpos ( '.' , save_cursor ) 245end ) 246end 247error ( match ) 248end ---@cast match [integer, integer] 249if save_cursor ~= nil then 250api . nvim_win_call ( win , function () 251fn . setpos ( '.' , save_cursor ) 252end ) 253end 254 255local m_lnum =match [ 1 ] 256local m_col =match [ 2 ] 257 258-- If a match is found setup match highlighting. 259if m_lnum > 0 and m_lnum >= stoplinetop and m_lnum <= stoplinebottom then 260local ids =vim . w [ win ]. matchparen_ids 261if tonumber ( vim . g . matchparen_disable_cursor_hl ) == 0 then 262table . insert ( 263ids , 264fn . matchaddpos ( 265'MatchParen' , 266{ { c_lnum , c_col - before } , { m_lnum , m_col } } , 26710 , 268- 1 , 269{ window =win } 270) 271) 272else 273table . insert ( 274ids , 275fn . matchaddpos ( 'MatchParen' , { { m_lnum , m_col } } , 10 , - 1 , { window =win } ) 276) 277end 278vim . w [ win ]. matchparen_ids =ids 279vim . w [ win ]. paren_hl_on =1 280end 281end 282 283---@param win? integer 284function M . remove_matches ( win ) 285win =win or api . nvim_get_current_win () 286 287if vim . w [ win ]. paren_hl_on ~= nil and vim . w [ win ]. paren_hl_on ~= 0 then 288local ids =vim . w [ win ]. matchparen_ids or {} 289while # ids > 0 do 290pcall ( fn . matchdelete , table . remove ( ids , 1 ), win ) 291end 292vim . w [ win ]. matchparen_ids =ids 293vim . w [ win ]. paren_hl_on =0 294end 295end 296 297---@param tab? integer 298function M . no_matchparen ( tab ) 299tab =tab or api . nvim_get_current_tabpage () 300for _ , win in ipairs ( api . nvim_tabpage_list_wins ( tab )) do 301M . remove_matches ( win ) 302end 303vim . g . loaded_matchparen =nil 304pcall ( api . nvim_clear_autocmds , { group ='matchparen' } ) 305end 306 307---@param tab? integer 308function M . do_matchparen ( tab ) 309if vim . g . loaded_matchparen == nil then 310M . enable () 311end 312tab =tab or api . nvim_get_current_tabpage () 313for _ , win in ipairs ( api . nvim_tabpage_list_wins ( tab )) do 314M . highlight_matching_pair ( win ) 315end 316end 317 318return M