robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1local api =vim . api 2local log =require ( 'vim.lsp.log' ) 3local nvim_on =require ( 'vim._core.util' ). nvim_on 4local util =require ( 'vim.lsp.util' ) 5 6local Capability =require ( 'vim.lsp._capability' ) 7 8local M ={} 9 10---@class (private) vim.lsp.inlay_hint.LineHints 11---@field hints lsp.InlayHint[] 12---@field applied boolean whether this line's hints have had extmarks applied 13 14---@class (private) vim.lsp.inlay_hint.CurrentResult Info for current result 15---@field version? integer document version associated with this result 16---@field namespace_cleared? boolean whether the namespace was cleared for this result yet 17---@field hints? table<integer, vim.lsp.inlay_hint.LineHints> lnum -> hints 18 19---@class (private) vim.lsp.inlay_hint.ActiveRequest 20---@field request_id? integer the LSP request ID of the most recent request sent to the server 21---@field version? integer the document version associated with the most recent request 22 23---@class (private) vim.lsp.inlay_hint.ClientState Buffer local state for inlay hints 24---@field namespace integer 25---@field active_request vim.lsp.inlay_hint.ActiveRequest 26---@field current_result vim.lsp.inlay_hint.CurrentResult 27 28---@class (private) InlayHints : vim.lsp.Capability 29---@field active table<integer, InlayHints> 30---@field client_state table<integer, vim.lsp.inlay_hint.ClientState?> 31local InlayHint ={ 32name ='inlay_hint' , 33method ='textDocument/inlayHint' , 34active ={} , 35} 36InlayHint . __index =InlayHint 37setmetatable ( InlayHint , Capability ) 38Capability . all [ InlayHint . name ] =InlayHint 39 40---@package 41---@param bufnr integer 42function InlayHint : new ( bufnr ) 43self =Capability . new ( self , bufnr ) 44 45nvim_on ( 'BufWinEnter' , self . augroup , { buf =self . bufnr } , function () 46for client_id , _ in pairs ( self . client_state ) do 47self : refresh ( client_id ) 48end 49end ) 50 51return self 52end 53 54---@package 55---@param client_id integer 56function InlayHint : on_attach ( client_id ) 57if not self . client_state [ client_id ] then 58self . client_state [ client_id ] ={ 59namespace =api . nvim_create_namespace ( 'nvim.lsp.inlay_hint:' .. client_id ), 60active_request ={} , 61current_result ={} , 62} 63end 64self : refresh ( client_id ) 65end 66 67---@package 68---@param client_id integer 69function InlayHint : on_detach ( client_id ) 70local state =self . client_state [ client_id ] 71if state then 72self : reset ( client_id ) 73self . client_state [ client_id ] =nil 74end 75end 76 77---@private 78---@param client_id integer 79function InlayHint : on_close ( client_id ) 80self : reset ( client_id ) 81end 82 83---@private 84---@param client_id integer 85function InlayHint : on_change ( client_id ) 86self : refresh ( client_id ) 87end 88 89--- Reset the buffer's inlay hint state and clear the extmarks 90---@package 91---@param client_id integer 92function InlayHint : reset ( client_id ) 93local state =assert ( self . client_state [ client_id ]) 94self : cancel_active_request ( client_id ) 95api . nvim_buf_clear_namespace ( self . bufnr , state . namespace , 0 , - 1 ) 96state . current_result ={} 97end 98 99--- Refresh inlay hints by requesting them from the server 100--- 101--- Only sends a request if there is no active request in flight for the current document version. 102--- Otherwise, it cancels any previous in-progress request before sending a new one. 103--- 104---@package 105---@param client_id integer 106function InlayHint : refresh ( client_id ) 107local version =util . buf_versions [ self . bufnr ] 108local state =self . client_state [ client_id ] 109local client =vim . lsp . get_client_by_id ( client_id ) 110 111if state and client then 112local current_result =state . current_result 113local active_request =state . active_request 114 115-- Only send a request for this client if the current result is out of date and 116-- there isn't a current a request in flight for this version 117if current_result . version == version or active_request . version == version then 118return 119end 120 121-- cancel stale in-flight request 122self : cancel_active_request ( client_id ) 123 124---@type lsp.InlayHintParams 125local params ={ 126textDocument =util . make_text_document_params ( self . bufnr ), 127range =vim 128. range ( self . bufnr , 0 , 0 , api . nvim_buf_line_count ( self . bufnr ), 0 ) 129: to_lsp ( client . offset_encoding ), 130} 131 132local success , request_id =client : request ( 'textDocument/inlayHint' , params , nil , self . bufnr ) 133 134if success then 135active_request . request_id =request_id 136active_request . version =version 137end 138end 139end 140 141--- |lsp-handler| for the method `textDocument/inlayHint` 142--- Store hints for a specific buffer and client 143---@param err lsp.ResponseError? 144---@param result lsp.InlayHint[]? 145---@param ctx lsp.HandlerContext 146---@internal 147function M . on_inlayhint ( err , result , ctx ) 148local bufnr =assert ( ctx . bufnr ) 149local provider =InlayHint . active [ bufnr ] 150if not provider then 151return 152end 153 154local state =provider . client_state [ ctx . client_id ] 155if not state then 156return 157end 158 159if err then 160log . error ( 'inlay_hint' , err ) 161state . active_request ={} 162return 163end 164 165if util . buf_versions [ bufnr ] ~= ctx . version or not api . nvim_buf_is_loaded ( bufnr ) then 166return 167end 168 169-- ignore stale responses 170if state . active_request . request_id and ctx . request_id ~= state . active_request . request_id then 171return 172end 173 174-- If there's no error but the result is nil, clear existing hints. 175result =result or {} 176 177local new_lnum_hints ={} ---@type table<integer, vim.lsp.inlay_hint.LineHints> 178local num_unprocessed =# result 179if num_unprocessed == 0 then 180state . active_request ={} 181state . current_result ={} 182if vim . fn . win_gettype ( vim . fn . bufwinid ( bufnr )) == '' then 183api . nvim__redraw ( { buf =bufnr , valid =true , flush =false } ) 184end 185return 186end 187 188local lines =api . nvim_buf_get_lines ( bufnr , 0 , - 1 , false ) 189local client =assert ( vim . lsp . get_client_by_id ( ctx . client_id )) 190 191for _ , hint in ipairs ( result ) do 192local lnum =hint . position . line 193local line =lines and lines [ lnum + 1 ] or '' 194hint . position . character = 195vim . str_byteindex ( line , client . offset_encoding , hint . position . character , false ) 196if not new_lnum_hints [ lnum ] then 197new_lnum_hints [ lnum ] ={ 198hints ={} , 199applied =false , 200} 201end 202table . insert ( new_lnum_hints [ lnum ]. hints , hint ) 203end 204 205state . active_request ={} 206state . current_result ={ 207hints =new_lnum_hints , 208version =ctx . version , 209namespace_cleared =false , 210} 211 212if vim . fn . win_gettype ( vim . fn . bufwinid ( bufnr )) == '' then 213api . nvim__redraw ( { buf =bufnr , valid =true , flush =false } ) 214end 215end 216 217---@private 218---@param client_id integer 219function InlayHint : cancel_active_request ( client_id ) 220local state =assert ( self . client_state [ client_id ]) 221local client =vim . lsp . get_client_by_id ( client_id ) 222local active_request =state . active_request 223 224if client and active_request . request_id then 225client : cancel_request ( active_request . request_id ) 226active_request . request_id =nil 227active_request . version =nil 228end 229end 230 231--- |lsp-handler| for the method `workspace/inlayHint/refresh` 232---@param err lsp.ResponseError? 233---@param ctx lsp.HandlerContext 234---@internal 235function M . on_refresh ( err , _ , ctx ) 236if err then 237return vim . NIL 238end 239 240for bufnr , provider in pairs ( InlayHint . active ) do 241if provider . client_state [ ctx . client_id ] then 242provider : reset ( ctx . client_id ) 243 244if not vim . tbl_isempty ( vim . fn . win_findbuf ( bufnr )) then 245provider : refresh ( ctx . client_id ) 246end 247end 248end 249 250return vim . NIL 251end 252 253--- Optional filters |kwargs|: 254--- @class vim.lsp.inlay_hint.get.Filter 255--- @inlinedoc 256--- @field bufnr integer? 257--- @field range lsp.Range? 258 259--- @class vim.lsp.inlay_hint.get.ret 260--- @inlinedoc 261--- @field bufnr integer 262--- @field client_id integer 263--- @field inlay_hint lsp.InlayHint 264 265--- Get the list of inlay hints, (optionally) restricted by buffer or range. 266--- 267--- Example usage: 268--- 269--- ```lua 270--- local hint = vim.lsp.inlay_hint.get({ bufnr = 0 })[1] -- 0 for current buffer 271--- 272--- local client = vim.lsp.get_client_by_id(hint.client_id) 273--- local resp = client:request_sync('inlayHint/resolve', hint.inlay_hint, 100, 0) 274--- local resolved_hint = assert( 275--- resp and resp.result, 276--- resp and resp.err and vim.lsp.rpc.format_rpc_error(resp.err) or 'request failed' 277--- ) 278--- vim.lsp.util.apply_text_edits(resolved_hint.textEdits, 0, client.encoding) 279--- 280--- location = resolved_hint.label[1].location 281--- client:request('textDocument/hover', { 282--- textDocument = { uri = location.uri }, 283--- position = location.range.start, 284--- }) 285--- ``` 286--- 287--- @param filter vim.lsp.inlay_hint.get.Filter? 288--- @return vim.lsp.inlay_hint.get.ret[] 289--- @since 12 290function M . get ( filter ) 291vim . validate ( 'filter' , filter , 'table' , true ) 292filter =filter or {} 293 294local bufnr =filter . bufnr 295if not bufnr then 296return vim 297. iter ( api . nvim_list_bufs ()) 298: map ( function ( buf ) 299return M . get ( vim . tbl_extend ( 'keep' , { bufnr =buf } , filter )) 300end ) 301: flatten () 302: totable () 303else 304bufnr =vim . _resolve_bufnr ( bufnr ) 305end 306 307local provider =InlayHint . active [ bufnr ] 308if not provider then 309return {} 310end 311 312local range =filter . range 313if not range then 314range ={ 315start ={ line =0 , character =0 } , 316[ 'end' ] ={ line =api . nvim_buf_line_count ( bufnr ), character =0 } , 317} 318end 319 320--- @type vim.lsp.inlay_hint.get.ret[] 321local result ={} 322for client_id , state in pairs ( provider . client_state ) do 323local lnum_hints =state . current_result . hints 324if lnum_hints then 325for lnum =range . start . line , range [ 'end' ]. line do 326local line_hints =lnum_hints [ lnum ] or { hints ={} , applied =false } 327for _ , hint in pairs ( line_hints . hints ) do 328local line , char =hint . position . line , hint . position . character 329if 330( line > range . start . line or char >= range . start . character ) 331and ( line < range [ 'end' ]. line or char <= range [ 'end' ]. character ) 332then 333table . insert ( result , { 334bufnr =bufnr , 335client_id =client_id , 336inlay_hint =hint , 337} ) 338end 339end 340end 341end 342end 343return result 344end 345 346--- on_win handler for the decoration provider (see |nvim_set_decoration_provider|) 347---@package 348---@param topline integer 349---@param botline integer 350function InlayHint : on_win ( topline , botline ) 351for _ , state in pairs ( self . client_state ) do 352local current_result =state . current_result 353if current_result . version == util . buf_versions [ self . bufnr ] then 354if not current_result . namespace_cleared then 355api . nvim_buf_clear_namespace ( self . bufnr , state . namespace , 0 , - 1 ) 356current_result . namespace_cleared =true 357end 358 359local hints =assert ( current_result . hints ) 360 361for lnum =topline , botline do 362local hint_virtual_texts ={} --- @type table<integer, [string, string?][]> 363local line_hints =hints [ lnum ] 364if line_hints and not line_hints . applied then 365line_hints . applied =true 366for _ , hint in pairs ( line_hints . hints ) do 367local text ='' 368local label =hint . label 369if type ( label ) == 'string' then 370text =label 371else 372for _ , part in ipairs ( label ) do 373text =text .. part . value 374end 375end 376local vt =hint_virtual_texts [ hint . position . character ] or {} 377if hint . paddingLeft then 378vt [ # vt + 1 ] ={ ' ' } 379end 380vt [ # vt + 1 ] ={ text , 'LspInlayHint' } 381if hint . paddingRight then 382vt [ # vt + 1 ] ={ ' ' } 383end 384hint_virtual_texts [ hint . position . character ] =vt 385end 386end 387 388for pos , vt in pairs ( hint_virtual_texts ) do 389api . nvim_buf_set_extmark ( self . bufnr , state . namespace , lnum , pos , { 390virt_text_pos ='inline' , 391ephemeral =false , 392virt_text =vt , 393} ) 394end 395end 396end 397end 398end 399 400--- Query whether inlay hint is enabled in the {filter}ed scope 401--- @param filter? vim.lsp.capability.enable.Filter 402--- @return boolean 403--- @since 12 404function M . is_enabled ( filter ) 405return Capability . is_enabled ( 'inlay_hint' , filter ) 406end 407 408--- Enables or disables inlay hints for the {filter}ed scope. 409--- 410--- To "toggle", pass the inverse of `is_enabled()`: 411--- 412--- ```lua 413--- vim.lsp.inlay_hint.enable(not vim.lsp.inlay_hint.is_enabled()) 414--- ``` 415--- 416--- @param enable boolean? true/nil to enable, false to disable 417--- @param filter? vim.lsp.capability.enable.Filter 418--- @since 12 419function M . enable ( enable , filter ) 420Capability . enable ( 'inlay_hint' , enable , filter ) 421end 422 423return M