Repository: neovim/neovim
File: runtime/lua/vim/diagnostic.lua (Lua)

Code before:

    api.nvim_buf_clear_namespace(bufnr, ns.user_data.location_ns, 0, -1)

    local lines = api.nvim_buf_get_lines(bufnr, 0, -1, true)
    -- set extmarks at diagnostic locations to preserve logical positions despite text changes
    for _, diagnostic in ipairs(diagnostics) do
      local last_row = #lines - 1
      local row = math.max(0, math.min(diagnostic.lnum, last_row))
      local row_len = #lines[row + 1]
      local col = math.max(0, math.min(diagnostic.col, row_len - 1))

      local end_row = math.max(0, math.min(diagnostic.end_lnum or row, last_row))
      local end_row_len = #lines[end_row + 1]
      local end_col = math.max(0, math.min(diagnostic.end_col or col, end_row_len))

      if end_row == row then
        -- avoid starting an extmark beyond end of the line
        if end_col == col then
          end_col = math.min(end_col + 1, end_row_len)
        end
      else
        -- avoid ending an extmark before start of the line
        if end_col == 0 then
          end_row = end_row - 1

          assert(
            lines[end_row + 1],
            'Failed to adjust diagnostic position to the end of a previous line. #lines in a buffer: '
              .. #lines
              .. ', lnum: '
              .. diagnostic.lnum
              .. ', col: '
              .. diagnostic.col
              .. ', end_lnum: '
              .. diagnostic.end_lnum
              .. ', end_col: '
              .. diagnostic.end_col
          )

          end_col = #lines[end_row + 1]
        end
      end

      diagnostic._extmark_id = api.nvim_buf_set_extmark(bufnr, ns.user_data.location_ns, row, col, {
        end_row = end_row,
        end_col = end_col,
        invalidate = true,
      })
    end
  end)


Review comment (bug):
This expensive string concatenation will be done even if the assert succeeds, so adds some overhead each time this code path is taken. In cases where you need to construct an error message for an `assert` it’s better to check the condition “manually” (ie using `if`) and then call `error` if the condition fails. This way the error message is only constructed on failure. 

Code after:

    api.nvim_buf_clear_namespace(bufnr, ns.user_data.location_ns, 0, -1)

    local lines = api.nvim_buf_get_lines(bufnr, 0, -1, true)
    -- set extmarks at diagnostic locations to preserve logical positions despite text changes
    for _, diagnostic in ipairs(diagnostics) do
      local last_row = #lines - 1
      local row = math.max(0, math.min(diagnostic.lnum, last_row))
      local row_len = #lines[row + 1]
      local col = math.max(0, math.min(diagnostic.col, row_len - 1))

      local end_row = math.max(0, math.min(diagnostic.end_lnum or row, last_row))
      local end_row_len = #lines[end_row + 1]
      local end_col = math.max(0, math.min(diagnostic.end_col or col, end_row_len))

      if end_row == row then
        -- avoid starting an extmark beyond end of the line
        if end_col == col then
          end_col = math.min(end_col + 1, end_row_len)
        end
      else
        -- avoid ending an extmark before start of the line
        if end_col == 0 then
          end_row = end_row - 1

          local end_line = lines[end_row + 1]

          if not end_line then
            error(
              'Failed to adjust diagnostic position to the end of a previous line. #lines in a buffer: '
                .. #lines
                .. ', lnum: '
                .. diagnostic.lnum
                .. ', col: '
                .. diagnostic.col
                .. ', end_lnum: '
                .. diagnostic.end_lnum
                .. ', end_col: '
                .. diagnostic.end_col
            )
          end

          end_col = #end_line
        end
      end

      diagnostic._extmark_id = api.nvim_buf_set_extmark(bufnr, ns.user_data.location_ns, row, col, {
        end_row = end_row,
        end_col = end_col,
        invalidate = true,
      })