karb94 / neoscroll.nvim

Smooth scrolling neovim plugin written in lua
MIT License
1.44k stars 36 forks source link

Add support for `gg`/`G` #23

Open ouuan opened 3 years ago

ouuan commented 3 years ago

I added these in my config:

t['gg']    = {'scroll', {'1 - vim.api.nvim_win_get_cursor(0)[1]', 'true', '1', '5', e}}
t['G']     = {'scroll', {'vim.api.nvim_buf_line_count(0) - vim.api.nvim_win_get_cursor(0)[1]', 'true', '1', '5', e}}

And it turns out that it doesn't work well when there are wrapped lines, because neoscroll uses gj and gk, which is good for other commands like <c-u>/<c-d> but bad for gg and G.

Or is there any way to get the display line number? I didn't find it in help or online.

karb94 commented 3 years ago

Yes gg and G have some complications. The good news is that it is relatively easy to implement inside Neoscroll so the best solution is for me to make wrapper functions like I did for zt/zz/zb.

karb94 commented 3 years ago

as a temporary solution you could do something like:

t['gg']    = {'scroll', {'-2*vim.api.nvim_buf_line_count(0)', 'true', '1', '5', e}}
t['G']     = {'scroll', {'2*vim.api.nvim_buf_line_count(0)', 'true', '1', '5', e}}
vext01 commented 3 years ago

Similarly, it would be good if pageup and pagedown were captured by neoscroll.

karb94 commented 3 years ago

I finally had some time to look at this. I'm not entirely sure what would be the expected behaviour when an easing function is enabled. There are two parts for this animation:

  1. The window scrolls till the edge of the buffer
  2. The cursor scrolls till the edge of the buffer

The cursor has to scroll after the window reaches the edge of the buffer because the cursor cannot be at the bottom/top of the window if scrolloff is greater than 0.

Having said that, when an easing function is enabled do we want the easing function to apply to both movements, only on the window scroll movement or only on the cursor movement?

maxwelljens commented 2 years ago

@vext01 You can add map <PageUp> <C-b> and map <PageDown> <C-f> to your config. That will make page up and page down do smooth scrolling.

veyxov commented 2 years ago

@vext01 You can add map <PageUp> <C-b> and map <PageDown> <C-f> to your config. That will make page up and page down do smooth scrolling.

Didn't work for me.

lawrence-laz commented 2 years ago

@vext01 You can add map <PageUp> <C-b> and map <PageDown> <C-f> to your config. That will make page up and page down do smooth scrolling.

Didn't work for me.

I just duplicated mapping for PageUp and PageDown and it seems to work nicely:

t['<C-b>'] = {'scroll', {'-vim.api.nvim_win_get_height(0)', 'true', '100'}}
t['<PageUp>'] = {'scroll', {'-vim.api.nvim_win_get_height(0)', 'true', '100'}}
t['<C-f>'] = {'scroll', { 'vim.api.nvim_win_get_height(0)', 'true', '100'}}
t['<PageDown>'] = {'scroll', { 'vim.api.nvim_win_get_height(0)', 'true', '100'}}
benwoodward commented 1 year ago

as a temporary solution you could do something like:

t['gg']    = {'scroll', {'-2*vim.api.nvim_buf_line_count(0)', 'true', '1', '5', e}}
t['G']     = {'scroll', {'2*vim.api.nvim_buf_line_count(0)', 'true', '1', '5', e}}

This is great, thanks. The only issue with this for me is that if the file is shorter than the height of the window, then the file will scroll to the last line leaving only the last line of the file visible at the top of the window.

My workaround for this: (only smooth scroll if buffer is longer than window height)

local api = vim.api

vim.keymap.set('n', 'G', [[<Cmd> lua maybe_smooth_scroll()<CR>]], { noremap = true })

function _G.maybe_smooth_scroll()
  local win_height = api.nvim_win_get_height(0)
  local buf_line_count = api.nvim_buf_line_count(0)

  if buf_line_count < win_height then
    api.nvim_command('normal! G')
  else
    require('neoscroll').scroll(1 * buf_line_count, true, 1, 5)
  end
end
peter-cardenas-ai commented 5 months ago

i have the above in addition to a post hook to set the cursor to the last and first lines for G and gg respectively. i believe this is the desired workflow for these commands?

  post_hook = function(info)
    if info == nil then
      return
    end
    if info.kind == 'gg' then
      vim.api.nvim_win_set_cursor(info.winid, { 1, 0 })
    elseif info.kind == 'G' then
      local line = vim.api.nvim_buf_line_count(info.bufnr)
      vim.api.nvim_win_set_cursor(info.winid, { line, 0 })
    end
  end,