mirror of
				https://github.com/neovim/neovim.git
				synced 2025-11-04 09:44:31 +00:00 
			
		
		
		
	fix(diagnostic): fix navigation with diagnostics placed past end of line
This commit is contained in:
		@@ -467,13 +467,14 @@ local function next_diagnostic(position, search_forward, bufnr, opts, namespace)
 | 
			
		||||
      lnum = (lnum + line_count) % line_count
 | 
			
		||||
    end
 | 
			
		||||
    if line_diagnostics[lnum] and not vim.tbl_isempty(line_diagnostics[lnum]) then
 | 
			
		||||
      local line_length = #vim.api.nvim_buf_get_lines(bufnr,  lnum, lnum + 1, true)[1]
 | 
			
		||||
      local sort_diagnostics, is_next
 | 
			
		||||
      if search_forward then
 | 
			
		||||
        sort_diagnostics = function(a, b) return a.col < b.col end
 | 
			
		||||
        is_next = function(diagnostic) return diagnostic.col > position[2] end
 | 
			
		||||
        is_next = function(d) return math.min(d.col, line_length - 1) > position[2] end
 | 
			
		||||
      else
 | 
			
		||||
        sort_diagnostics = function(a, b) return a.col > b.col end
 | 
			
		||||
        is_next = function(diagnostic) return diagnostic.col < position[2] end
 | 
			
		||||
        is_next = function(d) return math.min(d.col, line_length - 1) < position[2] end
 | 
			
		||||
      end
 | 
			
		||||
      table.sort(line_diagnostics[lnum], sort_diagnostics)
 | 
			
		||||
      if i == 0 then
 | 
			
		||||
 
 | 
			
		||||
@@ -686,6 +686,19 @@ describe('vim.diagnostic', function()
 | 
			
		||||
        return vim.diagnostic.get_prev_pos { namespace = diagnostic_ns }
 | 
			
		||||
      ]])
 | 
			
		||||
    end)
 | 
			
		||||
 | 
			
		||||
    it('works with diagnostics past the end of the line #16349', function()
 | 
			
		||||
      eq({4, 0}, exec_lua [[
 | 
			
		||||
        vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, {
 | 
			
		||||
          make_error('Diagnostic #1', 3, 9001, 3, 9001),
 | 
			
		||||
          make_error('Diagnostic #2', 4, 0, 4, 0),
 | 
			
		||||
        })
 | 
			
		||||
        vim.api.nvim_win_set_buf(0, diagnostic_bufnr)
 | 
			
		||||
        vim.api.nvim_win_set_cursor(0, {1, 1})
 | 
			
		||||
        vim.diagnostic.goto_next { float = false }
 | 
			
		||||
        return vim.diagnostic.get_next_pos { namespace = diagnostic_ns }
 | 
			
		||||
      ]])
 | 
			
		||||
    end)
 | 
			
		||||
  end)
 | 
			
		||||
 | 
			
		||||
  describe('get_prev_pos()', function()
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user