aerial.nvim icon indicating copy to clipboard operation
aerial.nvim copied to clipboard

Scrolling the source window while keeping Aerial focused

Open jaywonchung opened this issue 1 year ago • 0 comments

Great plugin!

My current primary use case is to have the Aerial window open and go through a long source file method-by-method to grasp its structure. The autojump feature really makes this convenient.

But sometimes, I would like to look into individual methods/functions a bit more by scrolling (<C-e>, <C-y>, <C-d>, <C-u>), but when I have Aerial focused, I need to move my cursor back into the source window, scroll around, and then come back. This is mildly annoying, so I wanted to define a keymap that lets me scroll the source buffer without moving focus away from Aerial.

The following is what I currently have:

["e"] = {
  desc = "Scroll the source window down by one line",
  callback = function()
    local source_win = require("aerial.util").get_winids(0)
    if source_win then
      vim.api.nvim_win_call(source_win, function()
        local scroll_down = vim.api.nvim_replace_termcodes("<C-e>", true, false, true)
        vim.cmd("normal! " .. scroll_down)
      end)
    end
  end
},
["y"] = {
  desc = "Scroll the source window up by one line",
  callback = function()
    local source_win = require("aerial.util").get_winids(0)
    if source_win then
      vim.api.nvim_win_call(source_win, function()
        local scroll_up = vim.api.nvim_replace_termcodes("<C-y>", true, false, true)
        vim.cmd("normal! " .. scroll_up)
      end)
    end
  end
},
["d"] = {
  desc = "Scroll the source window down by half window",
  callback = function()
    local source_win = require("aerial.util").get_winids(0)
    if source_win then
      vim.api.nvim_win_call(source_win, function()
        local scroll_down = vim.api.nvim_replace_termcodes("<C-d>", true, false, true)
        vim.cmd("normal! " .. scroll_down)
      end)
    end
  end
},
["u"] = {
  desc = "Scroll the source window up by half window",
  callback = function()
    local source_win = require("aerial.util").get_winids(0)
    if source_win then
      vim.api.nvim_win_call(source_win, function()
        local scroll_up = vim.api.nvim_replace_termcodes("<C-u>", true, false, true)
        vim.cmd("normal! " .. scroll_up)
      end)
    end
  end
},

If I directly override stuff like <C-e> then I won't be able to scroll Aerial itself, so I instead override the keys corresponding alphabet keys. Fortunately, it's highly unlikely that I'll need these keys (e, y, d, u) ever inside Aerial.

I'm just writing this up in case someone else meets the same need. I'd be happy to contribute these as built in actions if this looks like a significant enough use case & useful enough built in for the plugin. Only lua/aerial/actions.lua will need to be modified.

CC @stevearc

Thanks.

jaywonchung avatar Feb 23 '25 22:02 jaywonchung