Repository: neovim/neovim
File: test/functional/lua/diagnostic_spec.lua (Lua)

Code before:
        exec_lua(function()
          vim.api.nvim_win_set_cursor(0, { 1, 0 })
          vim.diagnostic.jump({ count = -1, wrap = true })
          return vim.api.nvim_win_get_cursor(0)
        end)
      )
    end)

    it('supports on_jump() handler', function()
      exec_lua(function()
        _G.jumped = false

        vim.diagnostic.jump({
          count = 1,
          on_jump = function()
            _G.jumped = true
          end,
        })
      end)

      retry(nil, nil, function()
        eq(true, exec_lua('return _G.jumped'))
      end)
    end)

    it('finds next diagnostic at a logical location after text changes before', function()
      exec_lua(function()
        vim.api.nvim_set_current_buf(_G.diagnostic_bufnr)

        vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
          _G.make_error('Diagnostic #1', 1, 4, 1, 7),
          _G.make_error('Diagnostic #2', 3, 0, 3, 3),
        })
      end)

      api.nvim_buf_set_text(0, 3, 0, 3, 0, { 'new line', 'new ' })

      eq(
        { 5, 4 },
        exec_lua(function()
          vim.api.nvim_win_set_cursor(0, { 2, 4 })
          vim.diagnostic.jump({ count = 1 })
          return vim.api.nvim_win_get_cursor(0)
        end)
      )
    end)

    it(
      'finds next diagnostic at the end of the line if diagnostic is set after last character in line',
      function()
        exec_lua(function()

Review comment (suggestion):
is it possible for these tests to re-use a shared function, to make them less verbose?

indirection can make things harder to read, but if these tests are mostly doing the same kind of thing, then using a shared function can be much more readable because it makes it obvious what is being tested in each case.

Code after:
        exec_lua(function()
          vim.api.nvim_win_set_cursor(0, { 1, 0 })
          vim.diagnostic.jump({ count = -1, wrap = true })
          return vim.api.nvim_win_get_cursor(0)
        end)
      )
    end)

    it('supports on_jump() handler', function()
      exec_lua(function()
        _G.jumped = false

        vim.diagnostic.jump({
          count = 1,
          on_jump = function()
            _G.jumped = true
          end,
        })
      end)

      retry(nil, nil, function()
        eq(true, exec_lua('return _G.jumped'))
      end)
    end)

    describe('after inserting text before diagnostic position', function()
      before_each(function()
        exec_lua(function()
          vim.api.nvim_set_current_buf(_G.diagnostic_bufnr)

          vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
            _G.make_error('Diagnostic #1', 1, 4, 1, 7),
            _G.make_error('Diagnostic #2', 3, 0, 3, 3),
          })
        end)

        api.nvim_buf_set_text(0, 3, 0, 3, 0, { 'new line', 'new ' })
      end)

      it('finds next diagnostic at a logical location', function()
        eq(
          { 5, 4 },
          exec_lua(function()
            vim.api.nvim_win_set_cursor(0, { 2, 4 })
            vim.diagnostic.jump({ count = 1 })
            return vim.api.nvim_win_get_cursor(0)
          end)
        )
      end)

      it('finds previous diagnostic at a logical location', function()