robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1--- @diagnostic disable:no-unknown 2 3--- @class (private) vim._inspector.Extmark 4--- @field id integer 5--- @field row integer 6--- @field col integer 7--- @field end_row integer 8--- @field end_col integer 9--- @field opts vim.api.keyset.extmark_details & { hl_group_link?: string } 10--- @field ns_id integer 11--- @field ns string 12 13--- @class vim._inspector.Filter 14--- @inlinedoc 15--- 16--- Include syntax based highlight groups. 17--- (default: `true`) 18--- @field syntax boolean 19--- 20--- Include treesitter based highlight groups. 21--- (default: `true`) 22--- @field treesitter boolean 23--- 24--- Include extmarks. When `all`, then extmarks without a `hl_group` will also be included. 25--- (default: true) 26--- @field extmarks boolean|"all" 27--- 28--- Include semantic token highlights. 29--- (default: true) 30--- @field semantic_tokens boolean 31local defaults ={ 32syntax =true , 33treesitter =true , 34extmarks =true , 35semantic_tokens =true , 36} 37 38---Get all the items at a given buffer position. 39--- 40---Can also be pretty-printed with `:Inspect!`. [:Inspect!]() 41--- 42---@since 11 43---@param buf? integer defaults to the current buffer 44---@param row? integer row to inspect, 0-based. Defaults to the row of the current cursor 45---@param col? integer col to inspect, 0-based. Defaults to the col of the current cursor 46---@param filter? vim._inspector.Filter Table with key-value pairs to filter the items 47---@return {treesitter:table,syntax:table,extmarks:table,semantic_tokens:table,buffer:integer,col:integer,row:integer} (table) a table with the following key-value pairs. Items are in "traversal order": 48--- - treesitter: a list of treesitter captures 49--- - syntax: a list of syntax groups 50--- - semantic_tokens: a list of semantic tokens 51--- - extmarks: a list of extmarks 52--- - buffer: the buffer used to get the items 53--- - row: the row used to get the items 54--- - col: the col used to get the items 55function vim . inspect_pos ( buf , row , col , filter ) 56filter =vim . tbl_deep_extend ( 'force' , defaults , filter or {} ) 57 58buf =buf or 0 59if row == nil or col == nil then 60-- get the row/col from the first window displaying the buffer 61local win =buf == 0 and vim . api . nvim_get_current_win () or vim . fn . bufwinid ( buf ) 62if win == - 1 then 63error ( 'row/col is required for buffers not visible in a window' ) 64end 65local cursor =vim . api . nvim_win_get_cursor ( win ) 66row , col =cursor [ 1 ] - 1 , cursor [ 2 ] 67end 68buf =vim . _resolve_bufnr ( buf ) 69---@cast row integer 70---@cast col integer 71 72local results ={ 73treesitter ={} , --- @type table[] 74syntax ={} , --- @type table[] 75extmarks ={} , 76semantic_tokens ={} , 77buffer =buf , 78row =row , 79col =col , 80} 81 82-- resolve hl links 83--- @generic T: { hl_group?: string, hl_group_link?: string } 84--- @param data T 85local function resolve_hl ( data ) 86if data . hl_group then 87local hlid =vim . api . nvim_get_hl_id_by_name ( data . hl_group ) 88local name =vim . fn . synIDattr ( vim . fn . synIDtrans ( hlid ), 'name' ) 89data . hl_group_link =name 90end 91return data 92end 93 94-- treesitter 95if filter . treesitter then 96for _ , capture in pairs ( vim . treesitter . get_captures ( buf , { row , col } )) do 97--- @diagnostic disable-next-line: inject-field 98capture . hl_group ='@' .. capture . capture .. '.' .. capture . lang 99results . treesitter [ # results . treesitter + 1 ] =resolve_hl ( capture ) 100end 101end 102 103-- syntax 104if filter . syntax and vim . api . nvim_buf_is_valid ( buf ) then 105vim . _with ( { buf =buf } , function () 106for _ , i1 in ipairs ( vim . fn . synstack ( row + 1 , col + 1 )) do 107results . syntax [ # results . syntax + 1 ] = 108resolve_hl ( { hl_group =vim . fn . synIDattr ( i1 , 'name' ) } ) 109end 110end ) 111end 112 113-- namespace id -> name map 114local nsmap ={} --- @type table<integer,string> 115for name , id in pairs ( vim . api . nvim_get_namespaces ()) do 116nsmap [ id ] =name 117end 118 119--- Convert an extmark tuple into a table 120--- @param extmark vim.api.keyset.get_extmark_item 121local function to_map ( extmark ) 122local opts =resolve_hl ( assert ( extmark [ 4 ])) 123return { 124id =extmark [ 1 ], 125row =extmark [ 2 ], 126col =extmark [ 3 ], 127end_row =opts . end_row or extmark [ 2 ], 128end_col =opts . end_col or extmark [ 3 ], 129opts =opts , 130ns_id =opts . ns_id , 131ns =nsmap [ opts . ns_id ] or '' , 132} 133end 134 135--- Exclude end_col and unpaired marks from the overlapping marks, unless 136--- filter.extmarks == 'all' (a highlight is drawn until end_col - 1). 137--- @param extmark vim._inspector.Extmark 138local function exclude_end_col ( extmark ) 139return filter . extmarks == 'all' or row < extmark . end_row or col < extmark . end_col 140end 141 142-- All overlapping extmarks at this position: 143local marks =vim . api . nvim_buf_get_extmarks ( buf , - 1 , { row , col } , { row , col } , { 144details =true , 145overlap =true , 146} ) 147local extmarks =vim . tbl_map ( to_map , marks ) 148extmarks =vim . tbl_filter ( exclude_end_col , extmarks ) 149 150if filter . semantic_tokens then 151results . semantic_tokens =vim . tbl_filter ( function ( extmark ) 152return extmark . ns : find ( 'nvim.lsp.semantic_tokens' ) == 1 153end , extmarks ) 154end 155 156if filter . extmarks then 157results . extmarks =vim . tbl_filter ( function ( extmark ) 158return extmark . ns : find ( 'nvim.lsp.semantic_tokens' ) ~= 1 159and ( filter . extmarks == 'all' or extmark . opts . hl_group ~= nil ) 160end , extmarks ) 161end 162 163return results 164end 165 166---Show all the items at a given buffer position. 167--- 168---Can also be shown with `:Inspect`. [:Inspect]() 169--- 170---See also |:marks| to list all extmarks. 171--- 172---Example: To bind this function to the vim-scriptease 173---inspired `zS` in Normal mode: 174--- 175---```lua 176---vim.keymap.set('n', 'zS', vim.show_pos) 177---``` 178--- 179---@since 11 180---@param buf? integer defaults to the current buffer 181---@param row? integer row to inspect, 0-based. Defaults to the row of the current cursor 182---@param col? integer col to inspect, 0-based. Defaults to the col of the current cursor 183---@param filter? vim._inspector.Filter 184function vim . show_pos ( buf , row , col , filter ) 185local items =vim . inspect_pos ( buf , row , col , filter ) 186 187local lines ={ {} } 188 189---@param str string 190---@param hl? string 191local function append ( str , hl ) 192table . insert ( lines [ # lines ], { str , hl } ) 193end 194 195local function nl () 196table . insert ( lines , {} ) 197end 198 199--- @param data { hl_group: string, hl_group_link: string } 200--- @param comment? string 201local function item ( data , comment ) 202append ( ' - ' ) 203append ( data . hl_group , data . hl_group ) 204append ( ' ' ) 205if data . hl_group ~= data . hl_group_link then 206append ( 'links to ' , 'MoreMsg' ) 207append ( data . hl_group_link , data . hl_group_link ) 208append ( ' ' ) 209end 210if comment then 211append ( comment , 'Dimmed' ) 212end 213nl () 214end 215 216-- treesitter 217if # items . treesitter > 0 then 218append ( 'Treesitter' , 'Title' ) 219nl () 220for _ , capture in ipairs ( items . treesitter ) do 221item ( 222capture , 223string . format ( 224'priority: %d language: %s' , 225capture . metadata . priority 226or ( capture . metadata [ capture . id ] and capture . metadata [ capture . id ]. priority ) 227or vim . hl . priorities . treesitter , 228capture . lang 229) 230) 231end 232nl () 233end 234 235-- semantic tokens 236if # items . semantic_tokens > 0 then 237append ( 'Semantic Tokens' , 'Title' ) 238nl () 239local sorted_marks =vim . fn . sort ( items . semantic_tokens , function ( left , right ) 240local left_first =left . opts . priority < right . opts . priority 241or left . opts . priority == right . opts . priority and left . opts . hl_group < right . opts . hl_group 242return left_first and - 1 or 1 243end ) 244for _ , extmark in ipairs ( sorted_marks ) do 245item ( extmark . opts , 'priority: ' .. extmark . opts . priority ) 246end 247nl () 248end 249 250-- syntax 251if # items . syntax > 0 then 252append ( 'Syntax' , 'Title' ) 253nl () 254for _ , syn in ipairs ( items . syntax ) do 255item ( syn ) 256end 257nl () 258end 259 260-- extmarks 261if # items . extmarks > 0 then 262append ( 'Extmarks' , 'Title' ) 263nl () 264for _ , extmark in ipairs ( items . extmarks ) do 265if extmark . opts . hl_group then 266item ( extmark . opts , extmark . ns ) 267else 268append ( ' - ' ) 269append ( extmark . ns , 'Dimmed' ) 270nl () 271end 272end 273nl () 274end 275 276if # lines [ # lines ] == 0 then 277table . remove ( lines ) 278end 279 280local chunks ={} 281for _ , line in ipairs ( lines ) do 282vim . list_extend ( chunks , line ) 283table . insert ( chunks , { '\n' } ) 284end 285if # chunks == 0 then 286chunks ={ 287{ 288'No items found at position ' 289.. items . row 290.. ',' 291.. items . col 292.. ' in buffer ' 293.. items . buffer , 294} , 295} 296end 297vim . api . nvim_echo ( chunks , false , { kind ='list_cmd' } ) 298end