Skip to content

Commit

Permalink
feat: new highlight_on_hover option (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
stevearc committed Mar 11, 2022
1 parent d864b46 commit 8b27c45
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 4 deletions.
12 changes: 10 additions & 2 deletions lua/aerial/autocommands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,16 @@ M.on_buf_delete = function(bufnr)
data[tonumber(bufnr)] = nil
end

M.on_cursor_move = function()
window.update_position(0, true)
M.on_cursor_move = function(is_aerial_buf)
if is_aerial_buf then
render.update_highlights(util.get_source_buffer())
else
window.update_position(0, true)
end
end

M.on_leave_aerial_buf = function()
render.clear_highlights(util.get_source_buffer())
end

M.attach_autocommands = function(bufnr)
Expand Down
3 changes: 3 additions & 0 deletions lua/aerial/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ local default_options = {
-- Highlight the closest symbol if the cursor is not exactly on one.
highlight_closest = true,

-- Highlight the symbol in the source buffer when cursor is in the aerial win
highlight_on_hover = false,

-- When jumping to a symbol, highlight the line for this many ms.
-- Set to false to disable
highlight_on_jump = 300,
Expand Down
17 changes: 15 additions & 2 deletions lua/aerial/render.lua
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,7 @@ M.update_highlights = function(buf)
winids = vim.tbl_filter(function(wid)
return bufdata.positions[wid]
end, winids)
local ns = vim.api.nvim_create_namespace("aerial-line")
vim.api.nvim_buf_clear_namespace(aer_bufnr, ns, 0, -1)
local ns = M.clear_highlights(aer_bufnr)
if vim.tbl_isempty(winids) then
return
end
Expand Down Expand Up @@ -212,6 +211,20 @@ M.update_highlights = function(buf)
end_hl = end_hl + hl_width
end
end

-- If we're in the aerial buffer, update highlight line in the source buffer
if config.highlight_on_hover and aer_bufnr == vim.api.nvim_get_current_buf() then
M.clear_highlights(bufnr)
local cursor = vim.api.nvim_win_get_cursor(0)
local item = data[0]:item(cursor[1])
vim.api.nvim_buf_add_highlight(bufnr, ns, "AerialLine", item.lnum - 1, 0, -1)
end
end

M.clear_highlights = function(buf)
local ns = vim.api.nvim_create_namespace("aerial-line")
vim.api.nvim_buf_clear_namespace(buf, ns, 0, -1)
return ns
end

return M
8 changes: 8 additions & 0 deletions lua/aerial/window.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ local function create_aerial_buffer(bufnr)
api.nvim_buf_set_option(aer_bufnr, "buflisted", false)
api.nvim_buf_set_option(aer_bufnr, "swapfile", false)
api.nvim_buf_set_option(aer_bufnr, "modifiable", false)
vim.cmd(string.format(
[[
au CursorMoved <buffer=%d> lua require('aerial.autocommands').on_cursor_move(true)
au BufLeave <buffer=%d> lua require('aerial.autocommands').on_leave_aerial_buf()
]],
aer_bufnr,
aer_bufnr
))
return aer_bufnr
end

Expand Down

0 comments on commit 8b27c45

Please sign in to comment.