robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1local M ={} 2 3--- @param cmd string[] 4--- @param cwd string 5--- @param on_exit fun(out: vim.SystemCompleted) 6local function git_cmd ( cmd , cwd , on_exit ) 7cmd =vim . list_extend ( { 'git' , '-c' , 'gc.auto=0' } , cmd ) 8local env =vim . fn . environ () --- @type table<string,string> 9env . GIT_DIR , env . GIT_WORK_TREE , env . GIT_INDEX_FILE =nil , nil , nil 10local sys_opts ={ cwd =cwd , text =true , env =env , clear_env =true } 11vim . system ( cmd , sys_opts , vim . schedule_wrap ( on_exit )) 12end 13 14local capabilities ={ 15codeActionProvider =true , 16documentLinkProvider ={ resolveProvider =false } , 17documentSymbolProvider =true , 18executeCommandProvider ={ commands ={ 'delete_plugin' , 'update_plugin' , 'skip_update_plugin' } } , 19hoverProvider =true , 20} 21--- @type table<string,function> 22local methods ={} 23 24--- @param callback function 25function methods . initialize ( _ , callback ) 26return callback ( nil , { capabilities =capabilities } ) 27end 28 29--- @param callback function 30function methods . shutdown ( _ , callback ) 31return callback ( nil , nil ) 32end 33 34--- @param uri string 35local function get_confirm_bufnr ( uri ) 36return vim . _tointeger ( uri : match ( '^nvim%-pack://confirm#(%d+)$' )) 37end 38 39local group_header_pattern ='^# (%S+)' 40local plugin_header_pattern ='^## (.+)$' 41 42--- @param bufnr integer 43--- @param lnum integer 44--- @return { group: string?, name: string?, from: integer?, to: integer?, active: boolean? } 45local function get_plug_data_at_lnum ( bufnr , lnum ) 46local lines =vim . api . nvim_buf_get_lines ( bufnr , 0 , - 1 , false ) 47--- @type string, string, integer, integer 48local group , name , from , to 49for i =lnum , 1 , - 1 do 50local line =lines [ i ] 51if not line then 52return {} 53end 54group =group or line : match ( group_header_pattern ) --[[@as string]] 55-- If group is found earlier than name - `lnum` is for group header line 56-- If later - proper group header line. 57if group then 58break 59end 60name =name or line : match ( plugin_header_pattern ) --[[@as string]] 61from =( not from and name ) and i or from --[[@as integer]] 62end 63if not ( group and name and from ) then 64return {} 65end 66--- @cast group string 67--- @cast from integer 68 69for i =lnum + 1 , # lines do 70if lines [ i ]: match ( group_header_pattern ) or lines [ i ]: match ( plugin_header_pattern ) then 71-- Do not include blank line before next section 72to =i - 2 73break 74end 75end 76to =to or # lines 77 78if not ( from <= lnum and lnum <= to ) then 79return {} 80end 81return { group =group , name =name : gsub ( ' %(not active%)$' , '' ), from =from , to =to } 82end 83 84--- @alias vim.pack.lsp.Position { line: integer, character: integer } 85--- @alias vim.pack.lsp.Range { start: vim.pack.lsp.Position, end: vim.pack.lsp.Position } 86--- @alias vim.pack.lsp.DocumentLink { range: vim.pack.lsp.Range, target: string } 87 88--- Finds a line range to be linked and computes the LSP style link 89--- @param line string Buffer line to find a link in 90--- @param pattern string Pattern with three captures: `()`, the link text, and `()`, 91--- like `'^Path: +()(.+)()$'`. The empty captures return byte positions. 92--- @param link_type "commit"|"path"|"src"|"tag" 93--- @param lnum integer Line number in a buffer 94--- @param src string Plugin source 95--- @return vim.pack.lsp.DocumentLink? # A link structure according to the LSP specification 96local function match_link ( line , pattern , link_type , lnum , src ) 97local from , match , to =line : match ( pattern ) 98if type ( from ) ~= 'number' or type ( match ) ~= 'string' or type ( to ) ~= 'number' then 99return nil 100end 101 102--- @cast from integer 103--- @cast to integer 104-- Convert to UTF index used in LSP positions 105local start_col =vim . str_utfindex ( line , 'utf-16' , from - 1 , false ) 106local end_col =vim . str_utfindex ( line , 'utf-16' , to - 2 , false ) 107 108--- @type string? 109local target =match 110if link_type == 'commit' or link_type == 'tag' then 111---@diagnostic disable-next-line: param-type-mismatch 112target =require ( 'vim._core.util' ). get_forge_url ( src , match , link_type ) 113elseif link_type == 'path' then 114target =vim . uri_from_fname ( match ) 115end 116 117if target == nil then 118return nil 119end 120 121return { 122range ={ 123start ={ line =lnum - 1 , character =start_col } , 124[ 'end' ] ={ line =lnum - 1 , character =end_col } , 125} , 126target =target , 127} 128end 129 130--- @param params { textDocument: { uri: string } } 131--- @param callback function 132methods [ 'textDocument/documentLink' ] =function ( params , callback ) 133local bufnr =get_confirm_bufnr ( params . textDocument . uri ) 134if bufnr == nil then 135return callback ( nil , {} ) 136end 137 138--- @type vim.pack.lsp.DocumentLink[] 139local links ={} 140local cur_src ='' 141local lines =vim . api . nvim_buf_get_lines ( bufnr , 0 , - 1 , false ) 142for i , l in ipairs ( lines ) do 143cur_src =l : match ( '^Source: +(.+)$' ) or cur_src 144 145links [ # links + 1 ] =match_link ( l , '^Path: +()(.+)()$' , 'path' , i , cur_src ) 146links [ # links + 1 ] =match_link ( l , '^Source: +()(.+)()$' , 'src' , i , cur_src ) 147links [ # links + 1 ] =match_link ( l , '^Revision[^:]*: +()(%S+)()' , 'commit' , i , cur_src ) 148-- NOTE: Assume that short revision works in the link 149links [ # links + 1 ] =match_link ( l , '^[><] ()(%S+)()' , 'commit' , i , cur_src ) 150links [ # links + 1 ] =match_link ( l , '^• ()(.+)()$' , 'tag' , i , cur_src ) 151end 152 153return callback ( nil , links ) 154end 155 156--- @param params { textDocument: { uri: string } } 157--- @param callback function 158methods [ 'textDocument/documentSymbol' ] =function ( params , callback ) 159local bufnr =get_confirm_bufnr ( params . textDocument . uri ) 160if bufnr == nil then 161return callback ( nil , {} ) 162end 163 164--- @alias vim.pack.lsp.Symbol { 165--- name: string, 166--- kind: number, 167--- range: vim.pack.lsp.Range, 168--- selectionRange: vim.pack.lsp.Range, 169--- children: vim.pack.lsp.Symbol[]?, 170--- } 171 172--- @param name string? 173--- @param start_line integer? 174--- @param end_line integer 175--- @param kind integer 176--- @return vim.pack.lsp.Symbol? 177local function new_symbol ( name , start_line , end_line , kind ) 178if name == nil then 179return nil 180end 181local range ={ 182start ={ 183line =start_line --[[@as integer]] , 184character =0 , 185} , 186[ 'end' ] ={ line =end_line , character =0 } , 187} 188return { name =name , kind =kind , range =range , selectionRange =range } 189end 190 191local lines =vim . api . nvim_buf_get_lines ( bufnr , 0 , - 1 , false ) 192 193--- @param pattern string 194--- @param start_line integer 195--- @param end_line integer 196--- @param kind integer 197--- @return vim.pack.lsp.Symbol[] 198local function parse_headers ( pattern , start_line , end_line , kind ) 199local res , cur_match , cur_start ={} , nil , nil 200for i =start_line , end_line do 201local m =assert ( lines [ i + 1 ]): match ( pattern ) 202if m ~= nil and m ~= cur_match then 203table . insert ( res , new_symbol ( cur_match , cur_start , i , kind )) 204cur_match , cur_start =m , i 205end 206end 207table . insert ( res , new_symbol ( cur_match , cur_start , end_line , kind )) 208return res 209end 210 211local group_kind =vim . lsp . protocol . SymbolKind . Namespace 212local symbols =parse_headers ( group_header_pattern , 0 , # lines - 1 , group_kind ) 213 214local plug_kind =vim . lsp . protocol . SymbolKind . Module 215for _ , group in ipairs ( symbols ) do 216local start_line , end_line =group . range . start . line , group . range [ 'end' ]. line 217group . children =parse_headers ( plugin_header_pattern , start_line , end_line , plug_kind ) 218end 219 220return callback ( nil , symbols ) 221end 222 223--- @alias vim.pack.lsp.CodeActionContext { diagnostics: table, only: table?, triggerKind: integer? } 224 225--- @param params { textDocument: { uri: string }, range: vim.pack.lsp.Range, context: vim.pack.lsp.CodeActionContext } 226--- @param callback function 227methods [ 'textDocument/codeAction' ] =function ( params , callback ) 228local bufnr =get_confirm_bufnr ( params . textDocument . uri ) 229local empty_kind =vim . lsp . protocol . CodeActionKind . Empty 230local only =params . context . only or { empty_kind } 231if not ( bufnr and vim . tbl_contains ( only , empty_kind )) then 232return callback ( nil , {} ) 233end 234local plug_data =get_plug_data_at_lnum ( bufnr , params . range . start . line + 1 ) 235if not plug_data . name then 236return callback ( nil , {} ) 237end 238 239--- @param title string 240--- @param command string 241local function new_action ( title , command ) 242return { 243title =( '%s `%s`' ): format ( title , plug_data . name ), 244command ={ title =title , command =command , arguments ={ bufnr , plug_data } } , 245} 246end 247 248local res ={} 249if plug_data . group == 'Update' then 250vim . list_extend ( res , { 251new_action ( 'Update' , 'update_plugin' ), 252new_action ( 'Skip updating' , 'skip_update_plugin' ), 253} , 0 ) 254end 255plug_data . active =assert ( vim . pack . get ( { plug_data . name } )[ 1 ]). active 256vim . list_extend ( res , { new_action ( 'Delete' , 'delete_plugin' ) } ) 257callback ( nil , res ) 258end 259 260local commands ={ 261update_plugin =function ( plug_data ) 262vim . pack . update ( { plug_data . name } , { force =true , offline =true } ) 263end , 264skip_update_plugin =function ( _ ) end , 265delete_plugin =function ( plug_data ) 266if plug_data . active then 267local confirm_msg =( 'Plugin `%s` is active.' ): format ( plug_data . name ) 268.. ' Make sure its `vim.pack.add` call is removed from config.' 269local choice =vim . fn . confirm ( confirm_msg , 'Delete? &Yes\n&No' , 1 , 'Question' ) 270if choice ~= 1 then 271return 'skip_line_update' 272end 273end 274vim . pack . del ( { plug_data . name } , { force =true } ) 275end , 276} 277 278-- NOTE: Use `vim.schedule_wrap` to avoid hit-enter after choosing code 279-- action via built-in `vim.fn.inputlist()` 280--- @param params { command: string, arguments: table } 281--- @param callback function 282methods [ 'workspace/executeCommand' ] =vim . schedule_wrap ( function ( params , callback ) 283--- @type integer, table 284local bufnr , plug_data =unpack ( params . arguments ) 285local ok , res =pcall ( commands [ params . command ] --[[@as function]] , plug_data ) 286if not ok then 287return callback ( { code =1 , message =res } , {} ) 288end 289 290-- Remove plugin lines (including blank line) to not later act on plugin 291if res ~= 'skip_line_update' then 292vim . bo [ bufnr ]. modifiable =true 293vim . api . nvim_buf_set_lines ( bufnr , plug_data . from - 2 , plug_data . to , false , {} ) 294vim . bo [ bufnr ]. modifiable , vim . bo [ bufnr ]. modified =false , false 295end 296 297callback ( nil , {} ) 298end ) 299 300--- @param params { textDocument: { uri: string }, position: vim.pack.lsp.Position } 301--- @param callback function 302methods [ 'textDocument/hover' ] =function ( params , callback ) 303local bufnr =get_confirm_bufnr ( params . textDocument . uri ) 304if bufnr == nil then 305return 306end 307 308local lines =vim . api . nvim_buf_get_lines ( bufnr , 0 , - 1 , false ) 309local lnum =params . position . line + 1 310local line =lines [ lnum ] 311if not line then 312return 313end 314local commit =line : match ( '^[<>] (%x+) │' ) or line : match ( '^Revision.*:%s+(%x+)' ) 315local tag =line : match ( '^• (.+)$' ) 316if commit == nil and tag == nil then 317return 318end 319 320local path --- @type string? 321local path_lnum =lnum - 1 322while path == nil and path_lnum >= 1 do 323path =assert ( lines [ path_lnum ]): match ( '^Path:%s+(.+)$' ) 324path_lnum =path_lnum - 1 325end 326if path == nil then 327return 328end 329 330--- @param sys_out vim.SystemCompleted 331local on_exit =function ( sys_out ) 332local markdown ='```diff\n' .. sys_out . stdout .. '\n```' 333local res ={ contents ={ kind =vim . lsp . protocol . MarkupKind . Markdown , value =markdown } } 334callback ( nil , res ) 335end 336git_cmd ( { 'show' , '--no-color' , commit or tag } , path , on_exit ) 337end 338 339local dispatchers ={} 340 341-- TODO: Simplify after `vim.lsp.server` is a thing 342-- https://github.com/neovim/neovim/pull/24338 343--- @param disp vim.lsp.rpc.Dispatchers 344local function cmd ( disp ) 345-- Store dispatchers to use for showing progress notifications 346dispatchers =disp 347local res , closing , request_id ={} , false , 0 348 349--- @param method vim.lsp.protocol.Method.ClientToServer.Request 350--- @param params table? 351--- @param callback fun(err: lsp.ResponseError?, result: any) 352function res . request ( method , params , callback ) 353local method_impl =methods [ method ] 354if method_impl ~= nil then 355method_impl ( params , callback ) 356end 357request_id =request_id + 1 358return true , request_id 359end 360 361--- @param method vim.lsp.protocol.Method.ClientToServer.Notification 362function res . notify ( method , _ ) 363if method == 'exit' then 364dispatchers . on_exit ( 0 , 15 ) 365end 366return false 367end 368 369function res . is_closing () 370return closing 371end 372 373function res . terminate () 374closing =true 375end 376 377return res 378end 379 380M . client_id =assert ( 381vim . lsp . start ( { cmd =cmd , name ='vim.pack' , root_dir =vim . uv . cwd () } , { attach =false } ) 382) 383 384return M