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

Code before:
      cmd: asdf^                |
      {1:~                        }|*3
      {3:[Prompt] [+]             }|
      other buffer             |
      {1:~                        }|*3
      {5:-- INSERT --}             |
    ]])

    feed('<esc>cc')
    screen:expect([[
      cmd: ^                    |
      {1:~                        }|*3
      {3:[Prompt] [+]             }|
      other buffer             |
      {1:~                        }|*3
      {5:-- INSERT --}             |
    ]])
  end)

  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()

Review comment (suggestion):
```suggestion
        vim.api.nvim_buf_set_lines(buf, last_line - 1, last_line - 1, true, vim.split(str, '\n'))
```

Code after:
      cmd: asdf^                |
      {1:~                        }|*3
      {3:[Prompt] [+]             }|
      other buffer             |
      {1:~                        }|*3
      {5:-- INSERT --}             |
    ]])

    feed('<esc>cc')
    screen:expect([[
      cmd: ^                    |
      {1:~                        }|*3
      {3:[Prompt] [+]             }|
      other buffer             |
      {1:~                        }|*3
      {5:-- INSERT --}             |
    ]])
  end)

  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()