robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1local api , nonnil =vim . api , vim . nonnil 2local shared =require ( 'vim.diagnostic._shared' ) 3local store =require ( 'vim.diagnostic._store' ) 4 5--- @class (private) vim.diagnostic._JumpOpts : vim.diagnostic.JumpOpts 6--- @field _highest? boolean 7--- @field win_id? integer 8--- @field cursor_position? [integer, integer] 9--- @field float? table|boolean 10 11--- @class (private) vim.diagnostic._jump 12local M ={} 13 14--- @param diagnostics vim.Diagnostic[] 15local function filter_highest ( diagnostics ) 16table . sort ( diagnostics , function ( a , b ) 17return shared . diagnostic_cmp ( a , b , 'severity' , false ) 18end ) 19 20-- Find the first diagnostic where the severity does not match the highest severity, and remove 21-- that element and all subsequent elements from the array 22local worst =( diagnostics [ 1 ] or {} ). severity 23for i =2 , # diagnostics do 24if diagnostics [ i ]. severity ~= worst then 25for j =i , # diagnostics do 26diagnostics [ j ] =nil 27end 28break 29end 30end 31end 32 33--- @param search_forward boolean 34--- @param opts vim.diagnostic.JumpOpts? 35--- @param use_logical_pos boolean 36--- @return vim.Diagnostic? 37local function next_diagnostic ( search_forward , opts , use_logical_pos ) 38opts =opts or {} 39--- @cast opts vim.diagnostic._JumpOpts 40 41-- Support deprecated win_id alias 42if opts . win_id then 43vim . deprecate ( 'opts.win_id' , 'opts.winid' , '0.13' ) 44opts . winid =opts . win_id 45opts . win_id =nil --- @diagnostic disable-line 46end 47 48-- Support deprecated cursor_position alias 49if opts . cursor_position then 50vim . deprecate ( 'opts.cursor_position' , 'opts.pos' , '0.13' ) 51opts . pos =opts . cursor_position 52opts . cursor_position =nil --- @diagnostic disable-line 53end 54 55local winid =opts . winid or api . nvim_get_current_win () 56local bufnr =api . nvim_win_get_buf ( winid ) 57local position =opts . pos or api . nvim_win_get_cursor ( winid ) 58 59-- Adjust row to be 0-indexed 60position [ 1 ] =position [ 1 ] - 1 61 62local wrap =nonnil ( opts . wrap , true ) 63local diagnostics =store . get_diagnostics ( bufnr , opts , true ) 64 65if opts . _highest then 66filter_highest ( diagnostics ) 67end 68 69local line_diagnostics =shared . diagnostic_lines ( diagnostics , use_logical_pos ) 70 71--- @param diagnostic vim.Diagnostic 72--- @return integer 73local function col_fn ( diagnostic ) 74return use_logical_pos and select ( 2 , shared . get_logical_pos ( diagnostic )) or diagnostic . col 75end 76 77local line_count =api . nvim_buf_line_count ( bufnr ) 78for i =0 , line_count do 79local offset =i * ( search_forward and 1 or - 1 ) 80local lnum =position [ 1 ] + offset 81if lnum < 0 or lnum >= line_count then 82if not wrap then 83return 84end 85lnum =( lnum + line_count ) % line_count 86end 87 88if line_diagnostics [ lnum ] and not vim . tbl_isempty ( line_diagnostics [ lnum ]) then 89local line_length =# api . nvim_buf_get_lines ( bufnr , lnum , lnum + 1 , true )[ 1 ] 90local sort_diagnostics , is_next --- @type function, function 91if search_forward then 92--- @param a vim.Diagnostic 93--- @param b vim.Diagnostic 94sort_diagnostics =function ( a , b ) 95return shared . diagnostic_cmp ( a , b , 'col' , false , col_fn ) 96end 97--- @param diagnostic vim.Diagnostic 98is_next =function ( diagnostic ) 99return math . min ( col_fn ( diagnostic ), math . max ( line_length - 1 , 0 )) > position [ 2 ] 100end 101else 102--- @param a vim.Diagnostic 103--- @param b vim.Diagnostic 104sort_diagnostics =function ( a , b ) 105return shared . diagnostic_cmp ( a , b , 'col' , true , col_fn ) 106end 107--- @param diagnostic vim.Diagnostic 108is_next =function ( diagnostic ) 109return math . min ( col_fn ( diagnostic ), math . max ( line_length - 1 , 0 )) < position [ 2 ] 110end 111end 112 113table . sort ( line_diagnostics [ lnum ], sort_diagnostics ) 114if i == 0 then 115for _ , diagnostic in ipairs ( line_diagnostics [ lnum ]) do 116if is_next ( diagnostic ) then 117return diagnostic 118end 119end 120else 121return line_diagnostics [ lnum ][ 1 ] 122end 123end 124end 125end 126 127--- @param diagnostic vim.Diagnostic? 128--- @param opts vim.diagnostic.JumpOpts? 129local function goto_diagnostic ( diagnostic , opts ) 130if not diagnostic then 131api . nvim_echo ( { { 'No more valid diagnostics to move to' , 'WarningMsg' } } , true ) 132return 133end 134 135opts =opts or {} 136--- @cast opts vim.diagnostic._JumpOpts 137 138-- Support deprecated win_id alias 139if opts . win_id then 140vim . deprecate ( 'opts.win_id' , 'opts.winid' , '0.13' ) 141opts . winid =opts . win_id 142opts . win_id =nil --- @diagnostic disable-line 143end 144 145local winid =opts . winid or api . nvim_get_current_win () 146local lnum , col =shared . get_logical_pos ( diagnostic ) 147 148vim . _with ( { win =winid } , function () 149-- Save position in the window's jumplist 150vim . cmd ( "normal! m'" ) 151api . nvim_win_set_cursor ( winid , { lnum + 1 , col } ) 152-- Open folds under the cursor 153vim . cmd ( 'normal! zv' ) 154end ) 155 156if opts . float then 157vim . deprecate ( 'opts.float' , 'opts.on_jump' , '0.14' ) 158local float_opts =opts . float 159float_opts =type ( float_opts ) == 'table' and float_opts or {} 160 161opts . on_jump =function ( _ , bufnr ) 162vim . diagnostic . open_float ( vim . tbl_extend ( 'keep' , float_opts , { 163bufnr =bufnr , 164scope ='cursor' , 165focus =false , 166} )) 167end 168 169opts . float =nil --- @diagnostic disable-line 170end 171 172if opts . on_jump then 173vim . schedule ( function () 174opts . on_jump ( diagnostic , api . nvim_win_get_buf ( winid )) 175end ) 176end 177end 178 179--- @param opts? vim.diagnostic.JumpOpts 180--- @return vim.Diagnostic? 181function M . get_prev ( opts ) 182return next_diagnostic ( false , opts , false ) 183end 184 185--- @param opts? vim.diagnostic.JumpOpts 186--- @return table|false 187function M . get_prev_pos ( opts ) 188vim . deprecate ( 189'vim.diagnostic.get_prev_pos()' , 190'access the lnum and col fields from get_prev() instead' , 191'0.13' 192) 193local prev =M . get_prev ( opts ) 194if not prev then 195return false 196end 197 198return { prev . lnum , prev . col } 199end 200 201--- @param opts? vim.diagnostic.JumpOpts 202function M . goto_prev ( opts ) 203vim . deprecate ( 'vim.diagnostic.goto_prev()' , 'vim.diagnostic.jump()' , '0.13' ) 204opts =opts or {} 205opts . float =nonnil ( opts . float , true ) --- @diagnostic disable-line 206goto_diagnostic ( M . get_prev ( opts ), opts ) 207end 208 209--- @param opts? vim.diagnostic.JumpOpts 210--- @return vim.Diagnostic? 211function M . get_next ( opts ) 212return next_diagnostic ( true , opts , false ) 213end 214 215--- @param opts? vim.diagnostic.JumpOpts 216--- @return table|false 217function M . get_next_pos ( opts ) 218vim . deprecate ( 219'vim.diagnostic.get_next_pos()' , 220'access the lnum and col fields from get_next() instead' , 221'0.13' 222) 223local next =M . get_next ( opts ) 224if not next then 225return false 226end 227 228return { next . lnum , next . col } 229end 230 231--- @param opts vim.diagnostic.JumpOpts 232--- @return vim.Diagnostic? 233function M . jump ( opts ) 234vim . validate ( 'opts' , opts , 'table' ) 235 236-- One of "diagnostic" or "count" must be provided 237assert ( 238opts . diagnostic or opts . count , 239'One of "diagnostic" or "count" must be specified in the options to vim.diagnostic.jump()' 240) 241 242-- Apply configuration options from vim.diagnostic.config() 243local config =vim . diagnostic . config (). jump or {} 244opts =vim . tbl_deep_extend ( 'keep' , opts , config ) 245--- @cast opts vim.diagnostic._JumpOpts 246 247if opts . diagnostic then 248goto_diagnostic ( opts . diagnostic , opts ) 249return opts . diagnostic 250end 251 252local count =assert ( opts . count ) 253if count == 0 then 254return nil 255end 256 257-- Support deprecated cursor_position alias 258if opts . cursor_position then 259vim . deprecate ( 'opts.cursor_position' , 'opts.pos' , '0.13' ) 260opts . pos =opts . cursor_position 261opts . cursor_position =nil --- @diagnostic disable-line 262end 263 264local diagnostic --- @type vim.Diagnostic? 265while count ~= 0 do 266local next =next_diagnostic ( count > 0 , opts , true ) 267if not next then 268break 269end 270 271-- Update cursor position 272opts . pos ={ next . lnum + 1 , next . col } 273 274if count > 0 then 275count =count - 1 276else 277count =count + 1 278end 279diagnostic =next 280end 281 282goto_diagnostic ( diagnostic , opts ) 283return diagnostic 284end 285 286--- @param opts? vim.diagnostic.JumpOpts 287function M . goto_next ( opts ) 288vim . deprecate ( 'vim.diagnostic.goto_next()' , 'vim.diagnostic.jump()' , '0.13' ) 289opts =opts or {} 290opts . float =nonnil ( opts . float , true ) --- @diagnostic disable-line 291goto_diagnostic ( M . get_next ( opts ), opts ) 292end 293 294return M