Skip to content

refactor: replace win_execute string injection in _adjust_scroll - #1

Open
leolaurindo wants to merge 1 commit into
cvlmtg:mainfrom
leolaurindo:feat/scroll-adjust-cleanup
Open

refactor: replace win_execute string injection in _adjust_scroll#1
leolaurindo wants to merge 1 commit into
cvlmtg:mainfrom
leolaurindo:feat/scroll-adjust-cleanup

Conversation

@leolaurindo

Copy link
Copy Markdown

Summary

This is a behavior-preserving refactor of the internal _adjust_scroll implementation.

_adjust_scroll handles deleted virtual lines at the edges of a buffer:

  • Deleted lines before the first buffer line use virt_lines_above and require topfill.
  • Deleted lines after the final buffer line require scrolling past EOF so they become visible.

The current implementation performs these operations through dynamically constructed Ex-command strings passed to win_execute.

What changed

Before, the top-of-file case built and evaluated Lua source code:

vim.fn.win_execute(
  winid,
  "lua vim.fn.winrestview({topfill=" .. count .. "})"
)

It is now a direct window-scoped function call:

vim.api.nvim_win_call(winid, function()
  vim.fn.winrestview({ topfill = count })
end)
The bottom-of-file case previously built a normal-mode command string:
vim.fn.win_execute(winid, "normal! " .. needed .. "\5")

It now uses the documented window-call API:

vim.api.nvim_win_call(winid, function()
  vim.cmd.normal(needed .. "\5")
end)

Why

The previous code constructed executable Lua/Vimscript as strings. That makes the operation:

  • Harder for static analysis and linters to inspect
  • More fragile during future refactors
  • Dependent on manual string construction and escaping
  • Less explicit about which window receives the operation

nvim_win_call() directly expresses the intended operation: execute this function with a specific window as the current window.

The CTRL-E bottom scroll is intentionally retained. winrestview({ topline = ... }) clamps at the final buffer line and cannot reveal virtual lines positioned past EOF. The normal-mode scroll remains necessary for that case.

User-visible behavior

None intended. This is an internal refactor only.

The existing scroll tests pass unchanged:

Success: 23

Failed: 0

Errors: 0

The combined demo branch containing this and the other proposed changes is available at:
https://github.com/leolaurindo/inline-diff.nvim/tree/dev/all

…_adjust_scroll

The top-fill and bottom-scroll adjustments previously built Vimscript/Lua
strings at runtime and evaluated them via win_execute, e.g.
'win_execute(winid, "lua vim.fn.winrestview({topfill=...})")'. This is
eval-style and fragile to internal changes.

Replace both with direct vim.api.nvim_win_call calls:
- winrestview({ topfill = count }) for the top case
- vim.cmd.normal(needed .. "\5") for the bottom case

The N<C-e> scroll itself is retained: winrestview topline clamps to the
last buffer line and cannot reveal past-EOF virt_lines, so CTRL-E remains
the only stable mechanism. No behavior change.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant