Skip to content

Keep following text visible when opening previews with wrapped lines - #81

Open
denny0223 wants to merge 1 commit into
delphinus:mainfrom
denny0223:fix/preview-wrapped-context
Open

denny0223 wants to merge 1 commit into
delphinus:mainfrom
denny0223:fix/preview-wrapped-context

Conversation

@denny0223

Copy link
Copy Markdown
Contributor

Opening a preview near the end of a document with wrapped lines can push the current paragraph to the bottom of the window and hide the next paragraph. This change keeps that following text visible in this case while preserving #80's short-document fix.

#80 fixed unnecessary scrolling when a short document already fits the window. Its EOF limit uses buffer line counts, but a buffer line can occupy several screen rows. The limit can therefore pull the view too far toward the start of the document. Neovim then scrolls only enough to bring the mapped cursor back into view at the bottom.

This change measures the screen rows remaining from the proposed starting position. If they fill the window, it applies that position; otherwise it uses Neovim's native bottom alignment. It then places the cursor on the same mapped line. The height query and bottom-alignment command are already used by split synchronization.

The new regression test attaches its own 120×40 UI and checks that the following paragraph is actually drawn. With the same test, #80 fails and this change passes on Neovim 0.12.0 and 0.12.5. The test also covers short-document behavior, EOF alignment, and cursor/source preservation.

Run from this PR's repository root:

nvim --headless -u NONE --noplugin -l tests/preview_scroll_test.lua
Verify the failure on #80 and the fix with the same test

Run these commands from this PR's repository root. Both runs use the PR version of the test; the baseline worktree supplies the unchanged plugin code. The baseline run is expected to exit with status 1 and report the paragraph after the cursor is off screen.

pr_dir=$PWD
base_dir=$(mktemp -d)
git worktree add --detach "$base_dir" 4f6d73a276356bc23dbffa594d2a54dc978e98bb
(cd "$base_dir" && nvim --headless -u NONE --noplugin -l "$pr_dir/tests/preview_scroll_test.lua")
nvim --headless -u NONE --noplugin -l tests/preview_scroll_test.lua
git worktree remove "$base_dir"
Plugin version Result with the same PR test
#80 / 4f6d73a 8 passed, 1 failed: following paragraph off screen
This change 9 passed, 0 failed

The test creates its own UI, so its result does not depend on your terminal size.

Validation against that upstream base:

  • make -k test passes on Neovim 0.12.0 and 0.12.5 (28 Lua test files). The optional PlantUML renderer test is skipped because no renderer is available locally.
  • StyLua 2.5.2 and Luacheck 1.2.0 pass.

The change retains the existing initial centering estimate and leaves the floating-window height issue noted in #80 separate.

Reproduce the visible behavior interactively

Save this snippet as a temporary .vim file outside the repository. It generates 13 numbered test paragraphs; Para11 and Para12 are simply the labels for paragraphs 11 and 12:

enew
setlocal filetype=markdown
set scrolloff=0 laststatus=2
lua << EOF
local lines = {}
for i = 1, 13 do
  lines[#lines + 1] = ("Para%02d "):format(i) .. string.rep("x", 145)
  lines[#lines + 1] = ""
end
vim.api.nvim_buf_set_lines(0, 0, -1, false, lines)
vim.api.nvim_win_set_cursor(0, { 21, 0 })
EOF
MdRender tab

From the repository root of either revision, start a fresh Neovim:

nvim --clean --cmd 'set runtimepath^=.'

Resize the terminal so :echo &columns &lines reports 120 40, then run :source /path/to/your/repro.vim. The script places the source cursor in paragraph 11 and opens the tab preview. Before the change, the cursor is on the bottom row and the following paragraph is off screen; with the change, that paragraph is visible below the cursor. Repeat in the other checkout at the same terminal size. Exit each reproduction with :qa!.

Capping the topline by buffer line count moves the cursor to the bottom
when following wrapped content already fills the screen. Measure the
remaining screen rows, stopping before later buffer lines once a window
is filled, and use the existing native EOF alignment only when those
rows would leave a gap.

Keep the original centering path when enough screen rows remain. Add EOF
and attached-UI wrapping regressions alongside the upstream tests.

Upstream-PR: delphinus#80
(cherry picked from commit 86c7c95)
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