Skip to content

Conversation

@Run1e
Copy link
Contributor

@Run1e Run1e commented Sep 9, 2025

Fixes an issue where the CursorMoved autocmd will run before the on_lines handler and set autoscroll = false in the case of the user focusing on the terminal before any output, resulting in broken autoscrolling.

Discussion: #109 (reply in thread)

Copy link
Owner

@igorlfs igorlfs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, thanks for looking into this. Made a few important comments.

@Run1e
Copy link
Contributor Author

Run1e commented Sep 17, 2025

Hi there again,

You're right my initial implementation was quite poorly tested, my bad. I've done a more thorough implementation which seems to fix all autoscrolling issues I've been seeing.

Most important changes are:

  • Keep track of autoscrolling by individual term buffers
  • Upon opening the console section, the autoscroll is re-applied if it was previously marked to be active (this seems to handle empty term buffers as well)

I also spotted a bug, namely that term buffers seem to be re-used, but the autoscroll setup seems to get called on each session restart, causing the InsertEnter/CursorMoved autocmd to be re-registered each time. I left a comment here: https://github.com/igorlfs/nvim-dap-view/pull/111/files#diff-665aacfce1a0455fca5c2033dcf97c838aafe2af15d4a75e0b588f5d10789f72R28

There's also no logic for clearing entries in the termbuf_is_autoscrolling table when buffers get deleted. I didn't find a logical place to put that, so I guess an autocmd is required. https://github.com/igorlfs/nvim-dap-view/pull/111/files#diff-665aacfce1a0455fca5c2033dcf97c838aafe2af15d4a75e0b588f5d10789f72R78

Copy link
Owner

@igorlfs igorlfs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for going the extra mile to solve this issue!

Keep track of autoscrolling by individual term buffers

That's a really nice observation by the way!

@igorlfs igorlfs merged commit 8f7d792 into igorlfs:main Oct 3, 2025
@igorlfs
Copy link
Owner

igorlfs commented Oct 3, 2025

Hey there,

I pushed some small fixes and merged soon after. Let me know if you run into any trouble with this newer version

igorlfs added a commit that referenced this pull request Oct 3, 2025
* fix: fix autoscroll in case of early terminal focus

* refactor: rename scroll functions

* feat: more proper autoscroll fix

* refactor: remove unused arg and debug log

* refactor: add arg type comments

* fix(scroll): minor tweaks

---------

Co-authored-by: Igor <igorlfs@ufmg.br>
@Run1e
Copy link
Contributor Author

Run1e commented Oct 18, 2025

Hi there,

I'm having issues where opening the termbuf after lines have appeared causes the autoscroll to not work, as in it's stuck to the top.

I had this issue in my PR, and moving the vim.schedule to the scroll_to_bottom fixed it, and it does now too.

I.e.:

---@param winnr integer
---@param bufnr integer
M.scroll_to_bottom = function(winnr, bufnr)
    if util.is_win_valid(winnr) and util.is_buf_valid(bufnr) then
        vim.schedule(function()
            api.nvim_win_set_cursor(winnr, { api.nvim_buf_line_count(bufnr), 0 })
        end)

        termbuf_is_autoscrolling[bufnr] = true
    end
end

Not familiar enough with nvim internals to understand exactly what the root cause is here, but the above code (and removing vim.schedule from the on_lines callback) fixes it.

@igorlfs
Copy link
Owner

igorlfs commented Oct 22, 2025

I'm having issues where opening the termbuf after lines have appeared causes the autoscroll to not work, as in it's stuck to the top.

Can you share an exact repro?

EDIT: I guess you're using a snippet similar to this one, with an early focus... I can actually repro with such a setup. I'll push your suggestion, because I see no reason not to. But I'm not 100% sure that's the way forward, with no regressions.

@igorlfs
Copy link
Owner

igorlfs commented Oct 22, 2025

Done with 76f219c

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.

2 participants