robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1local log =require ( 'vim.lsp.log' ) 2local protocol =require ( 'vim.lsp.protocol' ) 3local util =require ( 'vim.lsp.util' ) 4local api =vim . api 5local completion =require ( 'vim.lsp.completion' ) 6 7--- @type table<string, lsp.Handler> 8local M ={} 9 10--- @deprecated 11--- Client to server response handlers. 12--- @type table<vim.lsp.protocol.Method.ClientToServer, lsp.Handler> 13local RCS ={} 14 15--- Server to client request handlers. 16--- @type table<vim.lsp.protocol.Method.ServerToClient, lsp.Handler> 17local RSC ={} 18 19--- Server to client notification handlers. 20--- @type table<vim.lsp.protocol.Method.ServerToClient, lsp.Handler> 21local NSC ={} 22 23--- Writes to error buffer. 24---@param ... string|number Will be concatenated before being written 25local function err_message ( ... ) 26vim . notify ( table . concat ( vim . iter ( { ... } ): flatten (): totable ()), vim . log . levels . ERROR ) 27api . nvim_command ( 'redraw' ) 28end 29 30--- @param params lsp.ShowMessageParams|lsp.ShowMessageRequestParams 31--- @param ctx lsp.HandlerContext 32local function show_message_notification ( params , ctx ) 33local message_type =params . type 34local message =params . message 35local client_id =ctx . client_id 36local client =vim . lsp . get_client_by_id ( client_id ) 37local client_name =client and client . name or string . format ( 'id=%d' , client_id ) 38if not client then 39err_message ( 'LSP[' , client_name , '] client has shut down after sending ' , message ) 40end 41message =( 'LSP[%s] %s' ): format ( client_name , message ) 42vim . notify ( message , log . _from_lsp_level ( message_type )) 43return params 44end 45 46--- @see # https://microsoft.github.io/language-server-protocol/specifications/specification-current/#workspace_executeCommand 47---@diagnostic disable-next-line: deprecated 48RCS [ 'workspace/executeCommand' ] =function ( _ , _ , _ ) 49-- Error handling is done implicitly by wrapping all handlers; see end of this file 50end 51 52--- @see # https://microsoft.github.io/language-server-protocol/specifications/specification-current/#progress 53---@param params lsp.ProgressParams 54---@param ctx lsp.HandlerContext 55---@diagnostic disable-next-line:no-unknown 56RSC [ '$/progress' ] =function ( _ , params , ctx ) 57local client =vim . lsp . get_client_by_id ( ctx . client_id ) 58if not client then 59err_message ( 'LSP[id=' , tostring ( ctx . client_id ), '] client has shut down during progress update' ) 60return vim . NIL 61end 62local kind =nil 63local value =params . value 64 65if type ( value ) == 'table' then 66kind =value . kind --- @type string 67-- Carry over title of `begin` messages to `report` and `end` messages 68-- So that consumers always have it available, even if they consume a 69-- subset of the full sequence 70if kind == 'begin' then 71client . progress . pending [ params . token ] =value . title 72else 73value . title =client . progress . pending [ params . token ] 74if kind == 'end' then 75client . progress . pending [ params . token ] =nil 76end 77end 78end 79 80client . progress : push ( params ) 81 82api . nvim_exec_autocmds ( 'LspProgress' , { 83pattern =kind , 84modeline =false , 85data ={ client_id =ctx . client_id , params =params } , 86} ) 87end 88 89--- @see # https://microsoft.github.io/language-server-protocol/specifications/specification-current/#window_workDoneProgress_create 90---@param params lsp.WorkDoneProgressCreateParams 91---@param ctx lsp.HandlerContext 92RSC [ 'window/workDoneProgress/create' ] =function ( _ , params , ctx ) 93local client =vim . lsp . get_client_by_id ( ctx . client_id ) 94if not client then 95err_message ( 'LSP[id=' , tostring ( ctx . client_id ), '] client has shut down during progress update' ) 96return vim . NIL 97end 98client . progress : push ( params ) 99return vim . NIL 100end 101 102--- @see # https://microsoft.github.io/language-server-protocol/specifications/specification-current/#window_showMessageRequest 103---@param params lsp.ShowMessageRequestParams 104RSC [ 'window/showMessageRequest' ] =function ( _ , params , ctx ) 105local actions =params . actions 106if actions and next ( actions ) then 107local co , is_main =coroutine . running () 108if co and not is_main then 109local opts ={ 110kind ='lsp_message' , 111prompt =params . message .. ': ' , 112---@param action lsp.MessageActionItem 113---@return string 114format_item =function ( action ) 115return ( action . title : gsub ( '\r\n' , '\\r\\n' ): gsub ( '\n' , '\\n' )) 116end , 117} 118vim . ui . select ( actions , opts , function ( choice ) 119-- schedule to ensure resume doesn't happen _before_ yield with 120-- default synchronous vim.ui.select 121vim . schedule ( function () 122coroutine . resume ( co , choice or vim . NIL ) 123end ) 124end ) 125-- The coroutine.running() check above guards this yield. 126---@diagnostic disable-next-line: await-in-sync 127return coroutine . yield () 128else 129local option_strings ={ params . message , '\nRequest Actions:' } 130for i , action in ipairs ( actions ) do 131local title =action . title : gsub ( '\r\n' , '\\r\\n' ) 132title =title : gsub ( '\n' , '\\n' ) 133table . insert ( option_strings , string . format ( '%d. %s' , i , title )) 134end 135local choice =vim . fn . inputlist ( option_strings ) 136if choice < 1 or choice > # actions then 137return vim . NIL 138else 139return actions [ choice ] 140end 141end 142else 143-- No actions, just show the message. 144show_message_notification ( params , ctx ) 145return vim . NIL 146end 147end 148 149--- @see # https://microsoft.github.io/language-server-protocol/specifications/specification-current/#client_registerCapability 150--- @param params lsp.RegistrationParams 151RSC [ 'client/registerCapability' ] =function ( _ , params , ctx ) 152local client =assert ( vim . lsp . get_client_by_id ( ctx . client_id )) 153client : _register ( params . registrations ) 154for bufnr in pairs ( client . attached_buffers ) do 155vim . lsp . _set_defaults ( client , bufnr ) 156end 157for _ , reg in ipairs ( params . registrations ) do 158-- The capability framework's attach loop only runs during initial Client:on_attach. 159-- When a client dynamically registers a new method, we need to manually trigger 160-- on_attach for capabilities that are now supported. 161for _ , Cap in pairs ( vim . lsp . _capability . all ) do 162if reg . method == Cap . method then 163for bufnr in pairs ( client . attached_buffers ) do 164if 165client : supports_method ( Cap . method , bufnr ) 166and vim . lsp . _capability . is_enabled ( Cap . name , { bufnr =bufnr , client_id =client . id } ) 167then 168local capability =Cap . active [ bufnr ] or Cap : new ( bufnr ) 169if not capability . client_state [ client . id ] then 170capability : on_attach ( client . id ) 171end 172end 173end 174end 175end 176end 177return vim . NIL 178end 179 180--- @see # https://microsoft.github.io/language-server-protocol/specifications/specification-current/#client_unregisterCapability 181--- @param params lsp.UnregistrationParams 182RSC [ 'client/unregisterCapability' ] =function ( _ , params , ctx ) 183local client =assert ( vim . lsp . get_client_by_id ( ctx . client_id )) 184client : _unregister ( params . unregisterations ) 185return vim . NIL 186end 187 188-- TODO(lewis6991): Do we need to notify other servers? 189--- @see # https://microsoft.github.io/language-server-protocol/specifications/specification-current/#workspace_applyEdit 190RSC [ 'workspace/applyEdit' ] =function ( _ , params , ctx ) 191assert ( 192params , 193'workspace/applyEdit must be called with `ApplyWorkspaceEditParams`. Server is violating the specification' 194) 195-- TODO(ashkan) Do something more with label? 196local client =assert ( vim . lsp . get_client_by_id ( ctx . client_id )) 197if params . label then 198( 'Workspace edit' , params . label ) 199end 200local status , result =pcall ( util . apply_workspace_edit , params . edit , client . offset_encoding ) 201return { 202applied =status , 203failureReason =result , 204} 205end 206 207---@param table table e.g., { foo = { bar = "z" } } 208---@param section string indicating the field of the table, e.g., "foo.bar" 209---@return any|nil setting value read from the table, or `nil` not found 210local function lookup_section ( table , section ) 211local keys =vim . split ( section , '.' , { plain =true } ) --- @type string[] 212return vim . tbl_get ( table , unpack ( keys )) 213end 214 215--- @see # https://microsoft.github.io/language-server-protocol/specifications/specification-current/#workspace_configuration 216--- @param params lsp.ConfigurationParams 217RSC [ 'workspace/configuration' ] =function ( _ , params , ctx ) 218local client =vim . lsp . get_client_by_id ( ctx . client_id ) 219if not client then 220err_message ( 221'LSP[' , 222ctx . client_id , 223'] client has shut down after sending a workspace/configuration request' 224) 225return 226end 227if not params . items then 228return {} 229end 230 231local response ={} 232for _ , item in ipairs ( params . items ) do 233if item . section then 234local value =lookup_section ( client . settings , item . section ) 235-- For empty sections with no explicit '' key, return settings as is 236if value == nil and item . section == '' then 237value =client . settings 238end 239if value == nil then 240value =vim . NIL 241end 242table . insert ( response , value ) 243else 244-- If no section is provided, return settings as is 245table . insert ( response , client . settings ) 246end 247end 248return response 249end 250 251--- @see # https://microsoft.github.io/language-server-protocol/specifications/specification-current/#workspace_workspaceFolders 252RSC [ 'workspace/workspaceFolders' ] =function ( _ , _ , ctx ) 253local client =vim . lsp . get_client_by_id ( ctx . client_id ) 254if not client then 255err_message ( 'LSP[id=' , ctx . client_id , '] client has shut down after sending the message' ) 256return 257end 258return client . workspace_folders or vim . NIL 259end 260 261NSC [ 'textDocument/publishDiagnostics' ] =function ( ... ) 262return vim . lsp . diagnostic . on_publish_diagnostics ( ... ) 263end 264 265--- @private 266---@diagnostic disable-next-line: deprecated 267RCS [ 'textDocument/diagnostic' ] =function ( ... ) 268return vim . lsp . diagnostic . on_diagnostic ( ... ) 269end 270 271--- @private 272---@diagnostic disable-next-line: deprecated 273RCS [ 'textDocument/inlayHint' ] =function ( ... ) 274return vim . lsp . inlay_hint . on_inlayhint ( ... ) 275end 276 277--- Return a function that converts LSP responses to list items and opens the list 278--- 279--- The returned function has an optional {config} parameter that accepts |vim.lsp.ListOpts| 280--- 281---@param map_result fun(resp: any, bufnr: integer, position_encoding: 'utf-8'|'utf-16'|'utf-32'): table to convert the response 282---@param entity string name of the resource used in a `not found` error message 283---@param title_fn fun(ctx: lsp.HandlerContext): string Function to call to generate list title 284---@return lsp.Handler 285local function response_to_list ( map_result , entity , title_fn ) 286--- @diagnostic disable-next-line:redundant-parameter 287return function ( _ , result , ctx , config ) 288if not result or vim . tbl_isempty ( result ) then 289vim . notify ( 'No ' .. entity .. ' found' ) 290return 291end 292config =config or {} 293local title =title_fn ( ctx ) 294local client =assert ( vim . lsp . get_client_by_id ( ctx . client_id )) 295local items =map_result ( result , ctx . bufnr , client . offset_encoding ) 296 297local list ={ title =title , items =items , context =ctx } 298if config . on_list then 299assert ( vim . is_callable ( config . on_list ), 'on_list is not a function' ) 300config . on_list ( list ) 301elseif config . loclist then 302vim . fn . setloclist ( 0 , {} , ' ' , list ) 303vim . cmd . lopen () 304else 305vim . fn . setqflist ( {} , ' ' , list ) 306vim . cmd ( 'botright copen' ) 307end 308end 309end 310 311--- @deprecated remove in 0.13 312--- @see # https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_documentSymbol 313---@diagnostic disable-next-line: deprecated 314RCS [ 'textDocument/documentSymbol' ] =response_to_list ( 315util . symbols_to_items , 316'document symbols' , 317function ( ctx ) 318local fname =vim . fn . fnamemodify ( vim . uri_to_fname ( ctx . params . textDocument . uri ), ':.' ) 319return string . format ( 'Symbols in %s' , fname ) 320end 321) 322 323--- @deprecated remove in 0.13 324--- @see # https://microsoft.github.io/language-server-protocol/specifications/specification-current/#workspace_symbol 325---@diagnostic disable-next-line: deprecated 326RCS [ 'workspace/symbol' ] =response_to_list ( util . symbols_to_items , 'symbols' , function ( ctx ) 327return string . format ( "Symbols matching '%s'" , ctx . params . query ) 328end ) 329 330--- @deprecated remove in 0.13 331--- @see # https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_rename 332---@diagnostic disable-next-line: deprecated 333RCS [ 'textDocument/rename' ] =function ( _ , result , ctx ) 334if not result then 335vim . notify ( "Language server couldn't provide rename result" , vim . log . levels . INFO ) 336return 337end 338local client =assert ( vim . lsp . get_client_by_id ( ctx . client_id )) 339util . apply_workspace_edit ( result , client . offset_encoding ) 340end 341 342--- @deprecated remove in 0.13 343--- @see # https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_rangeFormatting 344---@diagnostic disable-next-line: deprecated 345RCS [ 'textDocument/rangeFormatting' ] =function ( _ , result , ctx ) 346if not result then 347return 348end 349local client =assert ( vim . lsp . get_client_by_id ( ctx . client_id )) 350util . apply_text_edits ( result , assert ( ctx . bufnr ), client . offset_encoding ) 351end 352 353--- @deprecated remove in 0.13 354--- @see # https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_formatting 355---@diagnostic disable-next-line: deprecated 356RCS [ 'textDocument/formatting' ] =function ( _ , result , ctx ) 357if not result then 358return 359end 360local client =assert ( vim . lsp . get_client_by_id ( ctx . client_id )) 361util . apply_text_edits ( result , assert ( ctx . bufnr ), client . offset_encoding ) 362end 363 364--- @deprecated remove in 0.13 365--- @see # https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_completion 366---@diagnostic disable-next-line: deprecated 367RCS [ 'textDocument/completion' ] =function ( _ , result , _ ) 368if vim . tbl_isempty ( result or {} ) then 369return 370end 371local cursor =api . nvim_win_get_cursor ( 0 ) 372local row , col =cursor [ 1 ], cursor [ 2 ] 373local line =assert ( api . nvim_buf_get_lines ( 0 , row - 1 , row , false )[ 1 ]) 374local line_to_cursor =line : sub ( col + 1 ) 375local textMatch =vim . fn . match ( line_to_cursor , '\\k*$' ) 376local prefix =line_to_cursor : sub ( textMatch + 1 ) 377 378local matches =completion . _lsp_to_complete_items ( result , prefix ) 379vim . fn . complete ( textMatch + 1 , matches ) 380end 381 382--- @deprecated 383--- |lsp-handler| for the method "textDocument/hover" 384--- 385--- ```lua 386--- vim.lsp.handlers["textDocument/hover"] = vim.lsp.with( 387--- vim.lsp.handlers.hover, { 388--- -- Use a sharp border with `FloatBorder` highlights 389--- border = "single", 390--- -- add the title in hover float window 391--- title = "hover" 392--- } 393--- ) 394--- ``` 395--- 396---@param _ lsp.ResponseError? 397---@param result lsp.Hover 398---@param ctx lsp.HandlerContext 399---@param config table Configuration table. 400--- - border: (default=nil) 401--- - Add borders to the floating window 402--- - See |vim.lsp.util.open_floating_preview()| for more options. 403--- @diagnostic disable-next-line:redundant-parameter 404function M . hover ( _ , result , ctx , config ) 405config =config or {} 406config . focus_id =ctx . method 407if api . nvim_get_current_buf () ~= ctx . bufnr then 408-- Ignore result since buffer changed. This happens for slow language servers. 409return 410end 411if not ( result and result . contents ) then 412if config . silent ~= true then 413vim . notify ( 'No information available' ) 414end 415return 416end 417local format ='markdown' 418local contents ---@type string[] 419if type ( result . contents ) == 'table' and result . contents . kind == 'plaintext' then 420format ='plaintext' 421contents =vim . split ( result . contents . value or '' , '\n' , { trimempty =true } ) 422else 423contents =util . convert_input_to_markdown_lines ( result . contents ) 424end 425if vim . tbl_isempty ( contents ) then 426if config . silent ~= true then 427vim . notify ( 'No information available' ) 428end 429return 430end 431return util . open_floating_preview ( contents , format , config ) 432end 433 434--- @deprecated remove in 0.13 435--- @see # https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_hover 436--- @diagnostic disable-next-line: deprecated 437RCS [ 'textDocument/hover' ] =M . hover 438 439local sig_help_ns =api . nvim_create_namespace ( 'nvim.lsp.signature_help' ) 440 441--- @deprecated remove in 0.13 442--- |lsp-handler| for the method "textDocument/signatureHelp". 443--- 444--- The active parameter is highlighted with |hl-LspSignatureActiveParameter|. 445--- 446--- ```lua 447--- vim.lsp.handlers["textDocument/signatureHelp"] = vim.lsp.with( 448--- vim.lsp.handlers.signature_help, { 449--- -- Use a sharp border with `FloatBorder` highlights 450--- border = "single" 451--- } 452--- ) 453--- ``` 454--- 455---@param _ lsp.ResponseError? 456---@param result lsp.SignatureHelp? Response from the language server 457---@param ctx lsp.HandlerContext Client context 458---@param config table Configuration table. 459--- - border: (default=nil) 460--- - Add borders to the floating window 461--- - See |vim.lsp.util.open_floating_preview()| for more options 462--- @diagnostic disable-next-line:redundant-parameter 463function M . signature_help ( _ , result , ctx , config ) 464config =config or {} 465config . focus_id =ctx . method 466if api . nvim_get_current_buf () ~= ctx . bufnr then 467-- Ignore result since buffer changed. This happens for slow language servers. 468return 469end 470-- When use `autocmd CompleteDone <silent><buffer> lua vim.lsp.buf.signature_help()` to call signatureHelp handler 471-- If the completion item doesn't have signatures It will make noise. Change to use `print` that can use `<silent>` to ignore 472if not ( result and result . signatures and result . signatures [ 1 ]) then 473if config . silent ~= true then 474( 'No signature help available' ) 475end 476return 477end 478local client =assert ( vim . lsp . get_client_by_id ( ctx . client_id )) 479local triggers = 480vim . tbl_get ( client . server_capabilities , 'signatureHelpProvider' , 'triggerCharacters' ) 481local ft =vim . bo [ ctx . bufnr ]. filetype 482local lines , hl =util . convert_signature_help_to_markdown_lines ( result , ft , triggers ) 483if not lines or vim . tbl_isempty ( lines ) then 484if config . silent ~= true then 485( 'No signature help available' ) 486end 487return 488end 489local fbuf , fwin =util . open_floating_preview ( lines , 'markdown' , config ) 490-- Highlight the active parameter. 491if hl then 492vim . hl . range ( 493fbuf , 494sig_help_ns , 495'LspSignatureActiveParameter' , 496{ hl [ 1 ], hl [ 2 ] } , 497{ hl [ 3 ], hl [ 4 ] } 498) 499end 500return fbuf , fwin 501end 502 503--- @deprecated remove in 0.13 504--- @see # https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_signatureHelp 505--- @diagnostic disable-next-line:deprecated 506RCS [ 'textDocument/signatureHelp' ] =M . signature_help 507 508--- @deprecated remove in 0.13 509--- @see # https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_documentHighlight 510---@diagnostic disable-next-line: deprecated 511RCS [ 'textDocument/documentHighlight' ] =function ( _ , result , ctx ) 512if not result then 513return 514end 515local client_id =ctx . client_id 516local client =vim . lsp . get_client_by_id ( client_id ) 517if not client then 518return 519end 520util . buf_highlight_references ( assert ( ctx . bufnr ), result , client . offset_encoding ) 521end 522 523--- Displays call hierarchy in the quickfix window. 524--- 525--- @param direction 'from'|'to' `"from"` for incoming calls and `"to"` for outgoing calls 526--- @overload fun(direction:'from'): fun(_, result: lsp.CallHierarchyIncomingCall[]?) 527--- @overload fun(direction:'to'): fun(_, result: lsp.CallHierarchyOutgoingCall[]?) 528local function make_call_hierarchy_handler ( direction ) 529--- @param result lsp.CallHierarchyIncomingCall[]|lsp.CallHierarchyOutgoingCall[]|nil 530--- @param ctx lsp.HandlerContext 531return function ( _ , result , ctx ) 532if not result then 533return 534end 535local items ={} 536for _ , call_hierarchy_call in pairs ( result ) do 537--- @type lsp.CallHierarchyItem 538local call_hierarchy_item =call_hierarchy_call [ direction ] 539local filename =nil 540local bufnr =nil 541if direction == 'from' then 542filename =vim . uri_to_fname ( call_hierarchy_item . uri ) 543else 544bufnr =ctx . bufnr 545end 546for _ , range in pairs ( call_hierarchy_call . fromRanges ) do 547table . insert ( items , { 548filename =filename , 549bufnr =bufnr , 550text =call_hierarchy_item . name , 551lnum =range . start . line + 1 , 552col =range . start . character + 1 , 553} ) 554end 555end 556vim . fn . setqflist ( {} , ' ' , { title ='LSP call hierarchy' , items =items } ) 557vim . cmd ( 'botright copen' ) 558end 559end 560 561--- @deprecated remove in 0.13 562--- @see # https://microsoft.github.io/language-server-protocol/specifications/specification-current/#callHierarchy_incomingCalls 563---@diagnostic disable-next-line: deprecated 564RCS [ 'callHierarchy/incomingCalls' ] =make_call_hierarchy_handler ( 'from' ) 565 566--- @deprecated remove in 0.13 567--- @see # https://microsoft.github.io/language-server-protocol/specifications/specification-current/#callHierarchy_outgoingCalls 568---@diagnostic disable-next-line: deprecated 569RCS [ 'callHierarchy/outgoingCalls' ] =make_call_hierarchy_handler ( 'to' ) 570 571--- Displays type hierarchy in the quickfix window. 572local function make_type_hierarchy_handler () 573--- @param result lsp.TypeHierarchyItem[]? 574--- @param ctx lsp.HandlerContext 575return function ( _ , result , ctx , _ ) 576if not result then 577return 578end 579--- @param item lsp.TypeHierarchyItem 580local function format_item ( item ) 581if not item . detail or # item . detail == 0 then 582return item . name 583end 584return string . format ( '%s %s' , item . name , item . detail ) 585end 586local client =assert ( vim . lsp . get_client_by_id ( ctx . client_id )) 587local items ={} 588for _ , type_hierarchy_item in pairs ( result ) do 589local pos = 590vim . pos . lsp ( assert ( ctx . bufnr ), type_hierarchy_item . range . start , client . offset_encoding ) 591table . insert ( items , { 592filename =vim . uri_to_fname ( type_hierarchy_item . uri ), 593text =format_item ( type_hierarchy_item ), 594lnum =pos . row + 1 , 595col =pos . col + 1 , 596} ) 597end 598vim . fn . setqflist ( {} , ' ' , { title ='LSP type hierarchy' , items =items } ) 599vim . cmd ( 'botright copen' ) 600end 601end 602 603--- @deprecated remove in 0.13 604--- @see # https://microsoft.github.io/language-server-protocol/specifications/specification-current/#typeHierarchy_incomingCalls 605---@diagnostic disable-next-line: deprecated 606RCS [ 'typeHierarchy/subtypes' ] =make_type_hierarchy_handler () 607 608--- @deprecated remove in 0.13 609--- @see # https://microsoft.github.io/language-server-protocol/specifications/specification-current/#typeHierarchy_outgoingCalls 610---@diagnostic disable-next-line: deprecated 611RCS [ 'typeHierarchy/supertypes' ] =make_type_hierarchy_handler () 612 613--- @see: https://microsoft.github.io/language-server-protocol/specifications/specification-current/#window_logMessage 614--- @param params lsp.LogMessageParams 615NSC [ 'window/logMessage' ] =function ( _ , params , ctx ) 616local message_type =params . type 617local message =params . message 618local client_id =ctx . client_id 619local client =vim . lsp . get_client_by_id ( client_id ) 620local client_name =client and client . name or string . format ( 'id=%d' , client_id ) 621if not client then 622err_message ( 'LSP[' , client_name , '] client has shut down after sending ' , message ) 623end 624if message_type == protocol . MessageType . Error then 625log . error ( message ) 626elseif message_type == protocol . MessageType . Warning then 627log . warn ( message ) 628elseif message_type == protocol . MessageType . Info or message_type == protocol . MessageType . Log then 629log . info ( message ) 630else 631log . debug ( message ) 632end 633return params 634end 635 636--- @see # https://microsoft.github.io/language-server-protocol/specifications/specification-current/#window_showMessage 637--- @param params lsp.ShowMessageParams 638NSC [ 'window/showMessage' ] =function ( _ , params , ctx ) 639return show_message_notification ( params , ctx ) 640end 641 642--- @private 643--- @see # https://microsoft.github.io/language-server-protocol/specifications/specification-current/#window_showDocument 644--- @param params lsp.ShowDocumentParams 645RSC [ 'window/showDocument' ] =function ( _ , params , ctx ) 646local uri =params . uri 647 648if params . external then 649-- TODO(lvimuser): ask the user for confirmation 650local cmd , err =vim . ui . open ( uri ) 651local ret =cmd and cmd : wait ( 2000 ) or nil 652 653if ret == nil or ret . code ~= 0 then 654return { 655success =false , 656error ={ 657code =protocol . ErrorCodes . UnknownErrorCode , 658message =ret and ret . stderr or err , 659} , 660} 661end 662 663return { success =true } 664end 665 666local client_id =ctx . client_id 667local client =vim . lsp . get_client_by_id ( client_id ) 668local client_name =client and client . name or string . format ( 'id=%d' , client_id ) 669if not client then 670err_message ( 'LSP[' , client_name , '] client has shut down after sending ' , ctx . method ) 671return vim . NIL 672end 673 674local success =util . _show_document ( uri , params . selection , client . offset_encoding , { 675reuse_win =true , 676focus =params . takeFocus , 677} ) 678return { success =success or false } 679end 680 681---@see https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#codeLens_refresh 682RSC [ 'workspace/codeLens/refresh' ] =function ( err , result , ctx ) 683return vim . lsp . codelens . on_refresh ( err , result , ctx ) 684end 685 686---@see https://microsoft.github.io/language-server-protocol/specification/#diagnostic_refresh 687RSC [ 'workspace/diagnostic/refresh' ] =function ( err , result , ctx ) 688return vim . lsp . diagnostic . on_refresh ( err , result , ctx ) 689end 690 691---@see https://microsoft.github.io/language-server-protocol/specification/#workspace_foldingRange_refresh 692RSC [ 'workspace/foldingRange/refresh' ] =function ( err , result , ctx ) 693return vim . lsp . _folding_range . on_refresh ( err , result , ctx ) 694end 695 696---@see https://microsoft.github.io/language-server-protocol/specification/#workspace_inlayHint_refresh 697RSC [ 'workspace/inlayHint/refresh' ] =function ( err , result , ctx ) 698return vim . lsp . inlay_hint . on_refresh ( err , result , ctx ) 699end 700 701---@see https://microsoft.github.io/language-server-protocol/specifications/specification-current/#semanticTokens_refreshRequest 702RSC [ 'workspace/semanticTokens/refresh' ] =function ( err , result , ctx ) 703return vim . lsp . semantic_tokens . _refresh ( err , result , ctx ) 704end 705 706--- @nodoc 707--- @type table<string, lsp.Handler> 708---@diagnostic disable-next-line: deprecated 709M =vim . tbl_extend ( 'force' , M , RSC , NSC , RCS ) 710 711-- Add boilerplate error validation and logging for all of these. 712for k , fn in pairs ( M ) do 713--- @diagnostic disable-next-line:redundant-parameter 714M [ k ] =function ( err , result , ctx , config ) 715if log . trace () then 716log . trace ( 'default_handler' , ctx . method , { 717err =err , 718result =result , 719ctx =vim . inspect ( ctx ), 720} ) 721end 722 723-- ServerCancelled errors should be propagated to the request handler 724if err and err . code ~= protocol . ErrorCodes . ServerCancelled then 725-- LSP spec: 726-- interface ResponseError: 727-- code: integer; 728-- message: string; 729-- data?: string | number | boolean | array | object | null; 730 731-- Per LSP, don't show ContentModified error to the user. 732if err . code ~= protocol . ErrorCodes . ContentModified then 733local client =vim . lsp . get_client_by_id ( ctx . client_id ) 734local client_name =client and client . name or string . format ( 'client_id=%d' , ctx . client_id ) 735 736err_message ( client_name .. ': ' .. tostring ( err . code ) .. ': ' .. err . message ) 737end 738return 739end 740 741--- @diagnostic disable-next-line:redundant-parameter 742return fn ( err , result , ctx , config ) 743end 744end 745 746return M