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

Code before:
      if (win == NULL) {
        // File window is not found. Find the location list window.
        win = qf_find_win(qi);
      }
      if (win == NULL) {
        return;
      }
    }
    qf_winid = (int)win->handle;
  }

  // autocommands may cause trouble
  incr_quickfix_busy();

  aco_save_T aco;

  if (old_last == NULL) {
    // set curwin/curbuf to buf and save a few things
    aucmd_prepbuf(&aco, buf);
  }

  qf_update_win_titlevar(qi);

  qf_fill_buffer(qf_get_curlist(qi), buf, old_last, qf_winid);

  linenr_T new_line_count = buf->b_ml.ml_line_count;
  colnr_T new_endcol = ml_get_buf_len(buf, new_line_count);
  bcount_t new_byte_count;

  if (old_last == NULL) {
    new_byte_count = get_region_bytecount(buf, 1, new_line_count, 0, new_endcol);
    extmark_splice(buf, 0, 0, old_line_count - 1, 0, old_byte_count, new_line_count - 1,
                   new_endcol, new_byte_count, kExtmarkNoUndo);

    changed_lines(buf, 1, 0, old_line_count > 0 ? old_line_count + 1 : 1,
                  new_line_count - old_line_count, true);
  } else {
    linenr_T start_lnum = old_line_count + 1;
    new_byte_count = get_region_bytecount(buf, start_lnum, new_line_count, 0, new_endcol);
    extmark_splice(buf, old_line_count - 1, old_endcol,
                   0, 0, 0,
                   new_line_count - old_line_count, new_endcol,
                   new_byte_count, kExtmarkNoUndo);

    changed_lines(buf, start_lnum, 0, new_line_count + 1, new_line_count - old_line_count + 1,
                  true);
  }
  buf->b_changed = false;

  if (old_last == NULL) {
    qf_win_pos_update(qi, 0);

Review comment (suggestion):
Instead of all the new code (below), could we maybe change `qf_fill_buffer` to use `nvim_buf_set_lines` or `nvim_buf_set_text`, which already call `extmark_splice` + `changed_lines` in the expected way?

I guess not, because it looks like `qf_fill_buffer` is doing extra behavior per-line...

Code after:
      if (win == NULL) {
        // File window is not found. Find the location list window.
        win = qf_find_win(qi);
      }
      if (win == NULL) {
        return;
      }
    }
    qf_winid = (int)win->handle;
  }

  // autocommands may cause trouble
  incr_quickfix_busy();

  aco_save_T aco;

  if (old_last == NULL) {
    // set curwin/curbuf to buf and save a few things
    aucmd_prepbuf(&aco, buf);
  }

  qf_update_win_titlevar(qi);

  qf_fill_buffer(qf_get_curlist(qi), buf, old_last, qf_winid);

  linenr_T new_line_count = buf->b_ml.ml_line_count;
  colnr_T new_endcol = ml_get_buf_len(buf, new_line_count);
  bcount_t new_byte_count;

  linenr_T delta = new_line_count - old_line_count;
  if (old_last == NULL) {
    new_byte_count = get_region_bytecount(buf, 1, new_line_count, 0, new_endcol);
    extmark_splice(buf, 0, 0, old_line_count - 1, 0, old_bytecount, new_line_count - 1, new_endcol,
                   new_byte_count, kExtmarkNoUndo);
    changed_lines(buf, 1, 0, old_line_count > 0 ? old_line_count + 1 : 1, delta, true);
  } else {
    linenr_T start_lnum = old_line_count + 1;
    new_byte_count = get_region_bytecount(buf, start_lnum, new_line_count, 0, new_endcol);
    extmark_splice(buf, old_line_count - 1, old_endcol, 0, 0, 0, delta, new_endcol, new_byte_count,
                   kExtmarkNoUndo);
    changed_lines(buf, start_lnum, 0, new_line_count + 1, delta + 1, true);
  }
  buf->b_changed = false;

  if (old_last == NULL) {
    qf_win_pos_update(qi, 0);

    // restore curwin/curbuf and a few other things
    aucmd_restbuf(&aco);
  }