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

Code before:
      -- replace
      api.nvim_buf_set_lines(0, 0, 1, true, { 'asdf', 'fdsa' })
      check_events {
        { 'test1', 'bytes', 1, 5, 0, 0, 0, 1, 0, 5, 2, 0, 10 },
      }
    end)

    it('flushes delbytes on substitute', function()
      local check_events = setup_eventcheck(verify, { 'AAA', 'BBB', 'CCC' })

      feed('gg0')
      command('s/AAA/GGG/')

      check_events {
        { 'test1', 'bytes', 1, 3, 0, 0, 0, 0, 3, 3, 0, 3, 3 },
      }

      -- check that byte updates for :delete (which uses curbuf->deleted_bytes2)
      -- are correct
      command('delete')
      check_events {
        { 'test1', 'bytes', 1, 4, 0, 0, 0, 1, 0, 4, 0, 0, 0 },
      }
    end)

    it('on_bytes sees modified buffer during substitute', function()
      api.nvim_buf_set_lines(0, 0, -1, true, { 'Hello' })

      local buffer_lines = exec_lua(function()
        local lines
        vim.api.nvim_buf_attach(0, false, {
          on_bytes = function()
            lines = vim.api.nvim_buf_get_lines(0, 0, -1, true)
          end,
        })
        vim.cmd('s/llo/y/')
        return lines
      end)

      -- Make sure on_bytes is called after the buffer is modified.
      eq({ 'Hey' }, buffer_lines)
    end)

    it('on_bytes called multiple times for multiple substitutions on same line', function()
      api.nvim_buf_set_lines(0, 0, -1, true, { 'Hello Hello' })

      local call_count, args = exec_lua(function()
        local count = 0
        local args = {}
        vim.api.nvim_buf_attach(0, false, {
          on_bytes = function(

Review comment (suggestion):
The word "during" is raising questions for me. Is this PR intended to fix buffer updates emitted *after a substitution is confirmed*? Or *during an 'inccommand' preview*?

We should not send *any* buffer updates *during* a substitution (like during an 'inccommand' preview). The 'inccommand' preview is intentionally NOT an "edit" semantically or technically, and should not emit buffer updates. If it is emitting buffer updates before the user "confirms" the substitution, then that should be fixed (it should NOT emit updates).

Code after:
      -- replace
      api.nvim_buf_set_lines(0, 0, 1, true, { 'asdf', 'fdsa' })
      check_events {
        { 'test1', 'bytes', 1, 5, 0, 0, 0, 1, 0, 5, 2, 0, 10 },
      }
    end)

    it('flushes delbytes on substitute', function()
      local check_events = setup_eventcheck(verify, { 'AAA', 'BBB', 'CCC' })

      feed('gg0')
      command('s/AAA/GGG/')

      check_events {
        { 'test1', 'bytes', 1, 3, 0, 0, 0, 0, 3, 3, 0, 3, 3 },
      }

      -- check that byte updates for :delete (which uses curbuf->deleted_bytes2)
      -- are correct
      command('delete')
      check_events {
        { 'test1', 'bytes', 1, 4, 0, 0, 0, 1, 0, 4, 0, 0, 0 },
      }
    end)

    it('on_bytes sees modified buffer after substitute', function()
      api.nvim_buf_set_lines(0, 0, -1, true, { 'Hello' })

      local buffer_lines = exec_lua(function()
        local lines
        vim.api.nvim_buf_attach(0, false, {
          on_bytes = function()
            lines = vim.api.nvim_buf_get_lines(0, 0, -1, true)
          end,
        })
        vim.cmd('s/llo/y/')
        return lines
      end)

      -- Make sure on_bytes is called after the buffer is modified.
      eq({ 'Hey' }, buffer_lines)
    end)

    it('on_bytes called multiple times for multiple substitutions on same line', function()
      api.nvim_buf_set_lines(0, 0, -1, true, { 'Hello Hello' })

      local call_count, args = exec_lua(function()
        local count = 0
        local args = {}
        vim.api.nvim_buf_attach(0, false, {
          on_bytes = function(