Repository: neovim/neovim
File: src/nvim/statusline.c (C)

Code before:
  int len = (int)strlen(buf);
  int start_col = col;

  if (!external) {
    // Draw each snippet with the specified highlighting.
    screengrid_line_start(grid, row, 0);
  }

  char *p = buf;
  int curgroup = 0;
  int curattr = attr;
  Array content = ARRAY_DICT_INIT;
  for (stl_hlrec_t *sp = hltab;; sp++) {
    int textlen = (int)(sp->start ? sp->start - p : buf + len - p);
    // Make all characters printable. Use an empty string instead of p, if p is beyond buf + len.
    size_t tsize = transstr_buf(p >= buf + len ? "" : p, textlen, transbuf, sizeof transbuf, true);
    if (!external) {
      col += grid_line_puts(col, transbuf, (int)tsize, curattr);
    } else {
      Array chunk = ARRAY_DICT_INIT;
      ADD(chunk, INTEGER_OBJ(curattr));
      ADD(chunk, STRING_OBJ(cbuf_as_string(xmemdupz(transbuf, tsize), tsize)));
      ADD(chunk, INTEGER_OBJ(curgroup));
      ADD(content, ARRAY_OBJ(chunk));
    }
    p = sp->start;

    if (p == NULL) {
      break;
    } else if (sp->userhl == 0) {
      curattr = attr;
      curgroup = group;
    } else if (sp->userhl < 0) {
      curattr = syn_id2attr(-sp->userhl);
      curgroup = -sp->userhl;
    } else {
      int *userhl = (wp != NULL && wp != curwin && wp->w_status_height != 0)
                    ? highlight_stlnc : highlight_user;
      char userbuf[5] = "Userx";
      userbuf[4] = (char)sp->userhl + '0';
      curattr = userhl[sp->userhl - 1];
      curgroup = syn_name2id_len(userbuf, 5);
    }
    curattr = hl_combine_attr(attr, curattr);
  }

  if (external) {
    ui_call_msg_ruler(content);
    goto theend;
  }


Review comment (suggestion):
Caveat here that this does not convey combined highlight groups. Would need to adapt the "message chunks" API signature to include that.

Just making it `[attr_id, text, group_id, combine_id]` would technically be non-breaking I suppose. IDK if we would then include a 0 `combine_id` in other places. And/or if a breaking change removing `attr_id` and making `group_id` an array (so it can be passed to `nvim_buf_set_extmark()` directly) should be considered @bfredl.

Code after:
  xfree(stl);
  ewp->w_p_crb = p_crb_save;

  int len = (int)strlen(buf);
  int start_col = col;

  if (!ui_event) {
    // Draw each snippet with the specified highlighting.
    screengrid_line_start(grid, row, 0);
  }

  char *p = buf;
  int curattr = attr;
  int curgroup = (int)group;
  Array content = ARRAY_DICT_INIT;
  for (stl_hlrec_t *sp = hltab;; sp++) {
    int textlen = (int)(sp->start ? sp->start - p : buf + len - p);
    // Make all characters printable. Use an empty string instead of p, if p is beyond buf + len.
    size_t tsize = transstr_buf(p >= buf + len ? "" : p, textlen, transbuf, sizeof transbuf, true);
    if (!ui_event) {
      col += grid_line_puts(col, transbuf, (int)tsize, curattr);
    } else {
      Array chunk = ARRAY_DICT_INIT;
      ADD(chunk, INTEGER_OBJ(curattr));
      ADD(chunk, STRING_OBJ(cbuf_as_string(xmemdupz(transbuf, tsize), tsize)));
      ADD(chunk, INTEGER_OBJ(curgroup));
      ADD(content, ARRAY_OBJ(chunk));
    }
    p = sp->start;

    if (p == NULL) {
      break;
    } else if (sp->userhl == 0) {
      curattr = attr;
      curgroup = (int)group;
    } else if (sp->userhl < 0) {
      curattr = syn_id2attr(-sp->userhl);
      curgroup = -sp->userhl;
    } else {
      int *userhl = (wp != NULL && wp != curwin && wp->w_status_height != 0)
                    ? highlight_stlnc : highlight_user;
      char userbuf[5] = "User";
      userbuf[4] = (char)sp->userhl + '0';
      curattr = userhl[sp->userhl - 1];
      curgroup = syn_name2id_len(userbuf, 5);
    }
    if (curattr != attr) {
      curattr = hl_combine_attr(attr, curattr);
    }
  }