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

Code before:
      '--cmd',
      'set notermguicolors',
    }, {
      cols = 70,
    })
    screen:expect([[
                                                                            |*4
      Entering Ex mode.  Type "visual" to go to Normal mode.                |
      :^                                                                     |
      {5:-- TERMINAL --}                                                        |
    ]])
    command('set number')
    screen:expect([[
      {121:  1 }                                                                  |
      {121:  2 }                                                                  |
      {121:  3 }                                                                  |
      {121:  4 }                                                                  |
      {121:  5 }                                                                  |
      {121:  6 }:^                                                                 |
      {5:-- TERMINAL --}                                                        |
    ]])
  end

  before_each(function()
    clear()
  end)

  describe('in a line with no multibyte chars or trailing spaces,', function()
    before_each(function()
      setup_ex_register('aaaaaaaa')
    end)

    it('at the end', function()
      feed('<C-R>r')
      screen:expect([[
        {121:  1 }                                                                  |
        {121:  2 }                                                                  |
        {121:  3 }                                                                  |
        {121:  4 }                                                                  |
        {121:  5 }                                                                  |
        {121:  6 }:aaaaaaaa^                                                         |
        {5:-- TERMINAL --}                                                        |
      ]])
      eq({ 6, 9 }, eval('nvim_win_get_cursor(0)'))
      feed([[<C-\><C-N>]])
      screen:expect([[
        {121:  1 }                                                                  |
        {121:  2 }                                                                  |
        {121:  3 }                                                                  |
        {121:  4 }                                                                  |
        {121:  5 }                                                                  |

Review comment (bug):
On Windows only, this autocmd was causing some problems. As far as I could determine, it was setting `number` on the parent nvim while the child nvim was loading Ex mode and updating the colors. These simultaneous conditions caused the numbers to sometimes update to start at 2 rather than 1. That seems to me like a minor bug when it comes to computing the numbers, but it also seems to be wholly unrelated to the purpose of this test. As such, I updated the test to be slightly more deterministic in its timing by getting rid of the `TermOpen` autocmd and manually calling `set number`. This also gets rid of the "Entering Ex Mode" prompt.

Given that the set of conditions that was causing the failure seems unrealistic (because you'd have to open the terminal and start Ex mode simultaneously with that specific autocmd), I think it's okay to update the test like this. I couldn't think of a better way. The only alternative I could come up with is disabling the `vim.wait` for the bg detection in Ex mode, but updating the logic there just to make a test work felt counterproductive.

Code after:
      Entering Ex mode.  Type "visual" to go to Normal mode.                |
      :aaaaaaaa   ^                                                          |
                                                                            |
    ]])
    eq({ 6, 12 }, eval('nvim_win_get_cursor(0)'))
  end)
end)

describe('buffer cursor position is correct in terminal with number column', function()
  local screen

  local function setup_ex_register(str)
    screen = tt.setup_child_nvim({
      '-u',
      'NONE',
      '-i',
      'NONE',
      '-E',
      '--cmd',
      string.format('let @r = "%s"', str),
      -- <Left> and <Right> don't always work
      '--cmd',
      'cnoremap <C-X> <Left>',
      '--cmd',
      'cnoremap <C-O> <Right>',
      '--cmd',
      'set notermguicolors',
    }, {
      cols = 70,
    })
    screen:expect([[
      {121:  1 }                                                                  |
      {121:  2 }                                                                  |
      {121:  3 }                                                                  |
      {121:  4 }{2:                                                                  }|
      {121:  5 }Entering Ex mode.  Type "visual" to go to Normal mode.            |
      {121:  6 }:^                                                                 |
      {5:-- TERMINAL --}                                                        |
    ]])
  end

  before_each(function()
    clear()
    command('autocmd! nvim.terminal')
    -- 'number' should be set before the terminal process starts, otherwise the resize
    -- from setting 'number' may cause a redraw that removes the "Entering Ex mode".
    command('set number')
  end)

  describe('in a line with no multibyte chars or trailing spaces,', function()
    before_each(function()