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

Code before:

  it("sets the ': mark", function()
    api.nvim_set_option_value('buftype', 'prompt', { buf = 0 })
    exec_lua(function()
      local buf = vim.api.nvim_get_current_buf()
      vim.fn.prompt_setcallback(buf, function(str)
        local last_line = vim.api.nvim_buf_line_count(buf)
        vim.api.nvim_buf_set_lines(buf, last_line-1, last_line-1, true, vim.split(str, '\n'))
      end)
    end)

    feed('asdf')
    eq({ 1, 1 }, api.nvim_buf_get_mark(0, ':'))
    feed('<cr>')
    eq({ 3, 1 }, api.nvim_buf_get_mark(0, ':'))
    -- Multiline prompt.
    feed('<s-cr>line1<s-cr>line2<s-cr>line3<cr>')
    eq({ 11, 1 }, api.nvim_buf_get_mark(0, ':'))

    -- ': mark is only available in prompt buffer.
    api.nvim_set_option_value('buftype', '', { buf = 0 })
    eq("Invalid mark name: ':'", t.pcall_err(api.nvim_buf_get_mark, 0, ':'))

    -- mark can be moved
    api.nvim_set_option_value('buftype', 'prompt', { buf = 0 })
    local last_line = api.nvim_buf_line_count(0);
    eq({ last_line, 1 }, api.nvim_buf_get_mark(0, ':'))
    eq(true, api.nvim_buf_set_mark(0, ':', 1, 1, {}))
    eq({ 1, 1 }, api.nvim_buf_get_mark(0, ':'))
  end)

  describe('prompt_getinput', function()
    it('returns current prompts text', function()
      command('new')
      local bufnr = fn('bufnr')
      api.nvim_set_option_value('buftype', 'prompt', { buf = 0 })
      eq('', fn('prompt_getinput', bufnr))
      feed('iasdf')
      eq('asdf', fn('prompt_getinput', bufnr))
      feed('<esc>dd')
      eq('', fn('prompt_getinput', bufnr))
      feed('iasdf2')
      eq('asdf2', fn('prompt_getinput', bufnr))

      -- returns empty string when called from non prompt buffer
      api.nvim_set_option_value('buftype', '', { buf = 0 })
      eq('', fn('prompt_getinput', bufnr))
    end)
  end)

  it('programmatic (non-user) edits', function()

Review comment (suggestion):
```suggestion
    local last_line = api.nvim_buf_line_count(0)
```

Code after:

  it("sets the ': mark", function()
    api.nvim_set_option_value('buftype', 'prompt', { buf = 0 })
    exec_lua(function()
      local buf = vim.api.nvim_get_current_buf()
      vim.fn.prompt_setcallback(buf, function(str)
        local last_line = vim.api.nvim_buf_line_count(buf)
        vim.api.nvim_buf_set_lines(buf, last_line - 1, last_line - 1, true, vim.split(str, '\n'))
      end)
    end)

    feed('asdf')
    eq({ 1, 1 }, api.nvim_buf_get_mark(0, ':'))
    feed('<cr>')
    eq({ 3, 1 }, api.nvim_buf_get_mark(0, ':'))
    -- Multiline prompt.
    feed('<s-cr>line1<s-cr>line2<s-cr>line3<cr>')
    eq({ 11, 1 }, api.nvim_buf_get_mark(0, ':'))

    -- ': mark is only available in prompt buffer.
    api.nvim_set_option_value('buftype', '', { buf = 0 })
    eq("Invalid mark name: ':'", t.pcall_err(api.nvim_buf_get_mark, 0, ':'))

    -- mark can be moved
    api.nvim_set_option_value('buftype', 'prompt', { buf = 0 })
    local last_line = api.nvim_buf_line_count(0)
    eq({ last_line, 1 }, api.nvim_buf_get_mark(0, ':'))
    eq(true, api.nvim_buf_set_mark(0, ':', 1, 1, {}))
    eq({ 1, 1 }, api.nvim_buf_get_mark(0, ':'))
  end)

  describe('prompt_getinput', function()
    it('returns current prompts text', function()
      command('new')
      local bufnr = fn('bufnr')
      api.nvim_set_option_value('buftype', 'prompt', { buf = 0 })
      eq('', fn('prompt_getinput', bufnr))
      feed('iasdf')
      eq('asdf', fn('prompt_getinput', bufnr))
      feed('<esc>dd')
      eq('', fn('prompt_getinput', bufnr))
      feed('iasdf2')
      eq('asdf2', fn('prompt_getinput', bufnr))

      -- returns empty string when called from non prompt buffer
      api.nvim_set_option_value('buftype', '', { buf = 0 })
      eq('', fn('prompt_getinput', bufnr))
    end)
  end)

  it('programmatic (non-user) edits', function()