MunifTanjim / nui.nvim

UI Component Library for Neovim.
MIT License
1.62k stars 57 forks source link

Bulk render a list of lines #245

Closed ten3roberts closed 1 year ago

ten3roberts commented 1 year ago

Currently, you can call NuiLine:render to update a single line.

Is there a way to batch together many lines and call the api and update the buffer only once with all the line.

Alternatively, a NuiBlock or similar construct contains lines similar to how NuiLine "contains" NuiText objects.

E.g

local block = NuiBlock({
    NuiLine({NuiText("Foo")}), 
    NuiLine({NuiText("On Line 2"), NuiText("Bar)})
})

block:render(bufnr, 1)

which will do a single nvim_buf_set_lines and apply the highlights.

MunifTanjim commented 1 year ago

Is there a way to batch together many lines and call the api and update the buffer only once with all the line.

which will do a single nvim_buf_set_lines and apply the highlights.

Are you concerned about the performance? If you want to render multiple NuiLine with a single nvim_buf_set_lines call, you'd still need to apply highlights using multiple calls and also keep additional states to keep track of the boundaries to apply highlights. I don't think it's worth it.


If you want the convenience of rending multiple lines together, you can write a small wrapper:

local function render_lines(lines, bufnr, ns_id, linenr_start)
  for i, line in ipairs(lines) do
    line:render(bufnr, ns_id, linenr_start + i - 1)
  end
end

and use it as:

render_lines({
  NuiLine({ NuiText("Foo") }),
  NuiLine({ NuiText("On Line 2"), NuiText("Bar") }),
}, bufnr, ns_id, 1)