Repository: neovim/neovim
File: runtime/lua/vim/_core/ui2/messages.lua (Lua)

Code before:
    end
  end
  M.set_pos(tar)
end

-- Keep track of the current message column to be able to
-- append or overwrite messages for :echon or carriage returns.
local col = 0
local cmd_timer ---@type uv.uv_timer_t? Timer resetting cmdline state next event loop.
---@param tar 'cmd'|'dialog'|'msg'|'pager'
---@param content MsgContent
---@param replace_last boolean
---@param append boolean
---@param id integer|string
function M.show_msg(tar, content, replace_last, append, id)
  local mark, msg, cr, dupe, buf = {}, '', false, 0, ui.bufs[tar]

  if M[tar] then -- tar == 'cmd'|'msg'
    local extid = M[tar].ids[id] and M[tar].ids[id].extid
    if tar == ui.cfg.msg.target then
      -- Save the concatenated message to identify repeated messages.
      for _, chunk in ipairs(content) do
        msg = msg .. chunk[2]
      end
      dupe = (
        not extid and msg == M.prev_msg and ui.cmd.srow == 0 and not append and M.dupe + 1 or 0
      )
    end

    cr = next(M[tar].ids) ~= nil and msg:sub(1, 1) == '\r'
    replace_last = next(M[tar].ids) ~= nil and not extid and (replace_last or dupe > 0)
    extid = extid or replace_last and M[tar].ids[M.prev_id] and M[tar].ids[M.prev_id].extid
    mark = extid and api.nvim_buf_get_extmark_by_id(buf, ui.ns, extid, { details = true }) or {}

    -- Ensure cmdline is clear when writing the first message.
    if tar == 'cmd' and dupe == 0 and not next(M.cmd.ids) and ui.cmd.srow == 0 then
      api.nvim_buf_set_lines(buf, 0, -1, false, {})
    end
  end

  -- Filter out empty newline messages. TODO: don't emit them.
  if msg == '\n' then
    return
  end

  local line_count = api.nvim_buf_line_count(buf)
  ---@type integer Start row after last line in the target buffer, unless
  ---this is the first message, or in case of a repeated or replaced message.
  local row = mark[1]
    or (M[tar] and not next(M[tar].ids) and ui.cmd.srow == 0 and 0)
    or (line_count - ((replace_last or cr or append) and 1 or 0))

Review comment (suggestion):
```suggestion
        not extid and not append and msg == M.prev_msg and ui.cmd.srow == 0 and M.dupe + 1 or 0
```

Code after:
    end
  end
  M.set_pos(tar)
end

-- Keep track of the current message column to be able to
-- append or overwrite messages for :echon or carriage returns.
local col = 0
local cmd_timer ---@type uv.uv_timer_t? Timer resetting cmdline state next event loop.
---@param tar 'cmd'|'dialog'|'msg'|'pager'
---@param content MsgContent
---@param replace_last boolean
---@param append boolean
---@param id integer|string
function M.show_msg(tar, content, replace_last, append, id)
  local mark, msg, cr, dupe, buf = {}, '', false, 0, ui.bufs[tar]

  if M[tar] then -- tar == 'cmd'|'msg'
    local extid = M[tar].ids[id] and M[tar].ids[id].extid
    if tar == ui.cfg.msg.target then
      -- Save the concatenated message to identify repeated messages.
      for _, chunk in ipairs(content) do
        msg = msg .. chunk[2]
      end
      dupe = (
        not extid and not append and msg == M.prev_msg and ui.cmd.srow == 0 and M.dupe + 1 or 0
      )
    end

    cr = next(M[tar].ids) ~= nil and msg:sub(1, 1) == '\r'
    replace_last = next(M[tar].ids) ~= nil and not extid and (replace_last or dupe > 0)
    extid = extid or replace_last and M[tar].ids[M.prev_id] and M[tar].ids[M.prev_id].extid
    mark = extid and api.nvim_buf_get_extmark_by_id(buf, ui.ns, extid, { details = true }) or {}

    -- Ensure cmdline is clear when writing the first message.
    if tar == 'cmd' and dupe == 0 and not next(M.cmd.ids) and ui.cmd.srow == 0 then
      api.nvim_buf_set_lines(buf, 0, -1, false, {})
    end
  end

  -- Filter out empty newline messages. TODO: don't emit them.
  if msg == '\n' then
    return
  end

  local line_count = api.nvim_buf_line_count(buf)
  ---@type integer Start row after last line in the target buffer, unless
  ---this is the first message, or in case of a repeated or replaced message.
  local row = mark[1]
    or (M[tar] and not next(M[tar].ids) and ui.cmd.srow == 0 and 0)
    or (line_count - ((replace_last or cr or append) and 1 or 0))