-
Notifications
You must be signed in to change notification settings - Fork 57
perf(renderer): avoid server round-trip on display toggles and incremental fold updates #377
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
8ccdb5c
c2e3735
507514b
846ca68
34e9f8f
999a36d
d0518b9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -600,12 +600,12 @@ function M.upsert_part_now(part_id, message_id, formatted_data, previous_formatt | |
| set_part_extmark_state(part_id, formatted_data) | ||
|
|
||
| if formatted_data.fold_ranges and #formatted_data.fold_ranges > 0 then | ||
| M.set_all_folds() | ||
| M.update_part_folds(part_id) | ||
| end | ||
|
|
||
| return true | ||
| end | ||
|
|
||
| local insert_at = get_part_insertion_line(part_id, message_id) | ||
| if not insert_at then | ||
| return false | ||
|
|
@@ -635,22 +635,89 @@ end | |
|
|
||
| function M.set_all_folds() | ||
| local all_folds = {} | ||
| ctx.part_folds = {} | ||
| for part_id_iter, data in pairs(ctx.formatted_parts) do | ||
| if data.fold_ranges then | ||
| local cached_part = ctx.render_state:get_part(part_id_iter) | ||
| if cached_part and cached_part.line_start then | ||
| local part_abs_folds = {} | ||
| for _, f in ipairs(data.fold_ranges) do | ||
| table.insert(all_folds, { | ||
| local abs = { | ||
| from = cached_part.line_start + f.from - 1, | ||
| to = cached_part.line_start + f.to - 1, | ||
| }) | ||
| } | ||
| table.insert(part_abs_folds, abs) | ||
| table.insert(all_folds, abs) | ||
| end | ||
| ctx.part_folds[part_id_iter] = part_abs_folds | ||
| end | ||
| end | ||
| end | ||
| ctx.global_folds = all_folds | ||
| output_window.set_folds(all_folds) | ||
| end | ||
|
|
||
| ---Update folds for a single part during streaming, avoiding a full rebuild. | ||
| ---@param part_id string | ||
| function M.update_part_folds(part_id) | ||
| local formatted_data = ctx.formatted_parts[part_id] | ||
| if not formatted_data or not formatted_data.fold_ranges then | ||
| ctx.part_folds[part_id] = nil | ||
| M.set_all_folds() | ||
| return | ||
| end | ||
| local cached_part = ctx.render_state:get_part(part_id) | ||
| if not cached_part or not cached_part.line_start then | ||
| return | ||
| end | ||
|
|
||
| local new_folds = {} | ||
| for _, f in ipairs(formatted_data.fold_ranges) do | ||
| table.insert(new_folds, { | ||
| from = cached_part.line_start + f.from - 1, | ||
| to = cached_part.line_start + f.to - 1, | ||
| }) | ||
| end | ||
|
|
||
| local old_folds = ctx.part_folds[part_id] | ||
| if old_folds and #old_folds == #new_folds then | ||
| local same = true | ||
| for i = 1, #new_folds do | ||
| if new_folds[i].from ~= old_folds[i].from or new_folds[i].to ~= old_folds[i].to then | ||
| same = false | ||
| break | ||
| end | ||
| end | ||
| if same then | ||
| return | ||
| end | ||
| end | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could become a reusable function local function folds_equal(a, b)
if #a ~= #b then return false end
for i = 1, #a do
if a[i].from ~= b[i].from or a[i].to ~= b[i].to then return false end
end
return true
end |
||
|
|
||
| local new_global = {} | ||
| local found = false | ||
| for pid, pf in pairs(ctx.part_folds) do | ||
| if pid == part_id then | ||
| found = true | ||
| for _, nf in ipairs(new_folds) do | ||
| table.insert(new_global, nf) | ||
| end | ||
| else | ||
| for _, of in ipairs(pf) do | ||
| table.insert(new_global, of) | ||
| end | ||
| end | ||
| end | ||
| if not found then | ||
| for _, nf in ipairs(new_folds) do | ||
| table.insert(new_global, nf) | ||
| end | ||
| end | ||
|
|
||
| ctx.part_folds[part_id] = new_folds | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this whole block can be simplified. By assigning the new folds to the ctx.part_folds first then after this it's only matter of looping to the fold list. local new_global = {}
ctx.part_folds[part_id] = new_folds
for pid, part_folds in pairs(ctx.part_folds) do
for _, f in ipairs(part_folds ) do
table.insert(new_global, f)
end
end |
||
| ctx.global_folds = new_global | ||
| output_window.set_folds(new_global) | ||
| end | ||
|
|
||
|
|
||
| ---@param part_id string | ||
| ---@param extra_lines string[] | ||
|
|
@@ -676,6 +743,9 @@ function M.append_part_now(part_id, extra_lines, extra_extmarks, previous_format | |
| apply_part_actions(part_id, formatted_data, cached.line_start) | ||
| apply_extmarks(previous_formatted, formatted_data, cached.line_start, old_line_end, new_line_end) | ||
| set_part_extmark_state(part_id, formatted_data) | ||
| if formatted_data.fold_ranges then | ||
| M.update_part_folds(part_id) | ||
| end | ||
| elseif has_extmarks(extra_extmarks) then | ||
| output_window.set_extmarks(extra_extmarks, insert_at) | ||
| end | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great idea , the round trip was not necessary at all.