feat: update preview window when cursor is moved (#42)

This commit is contained in:
Steven Arcangeli 2023-01-21 18:46:30 -08:00
parent 6c4a3dafca
commit 6c6b7673af
3 changed files with 58 additions and 18 deletions

View file

@ -1,4 +1,5 @@
local oil = require("oil")
local util = require("oil.util")
local M = {}
@ -29,15 +30,6 @@ M.select_split = {
end,
}
---@return nil|integer
local function get_preview_win()
for _, winid in ipairs(vim.api.nvim_tabpage_list_wins(0)) do
if vim.api.nvim_win_is_valid(winid) and vim.api.nvim_win_get_option(winid, "previewwindow") then
return winid
end
end
end
M.preview = {
desc = "Open the entry under the cursor in a preview window, or close the preview window if already open",
callback = function()
@ -46,7 +38,7 @@ M.preview = {
vim.notify("Could not find entry under cursor", vim.log.levels.ERROR)
return
end
local winid = get_preview_win()
local winid = util.get_preview_win()
if winid then
local cur_id = vim.w[winid].oil_entry_id
if entry.id == cur_id then
@ -61,7 +53,7 @@ M.preview = {
M.preview_scroll_down = {
desc = "Scroll down in the preview window",
callback = function()
local winid = get_preview_win()
local winid = util.get_preview_win()
if winid then
vim.api.nvim_win_call(winid, function()
vim.cmd.normal({
@ -76,7 +68,7 @@ M.preview_scroll_down = {
M.preview_scroll_up = {
desc = "Scroll up in the preview window",
callback = function()
local winid = get_preview_win()
local winid = util.get_preview_win()
if winid then
vim.api.nvim_win_call(winid, function()
vim.cmd.normal({
@ -174,7 +166,6 @@ M.open_cmdline = {
callback = function()
local config = require("oil.config")
local fs = require("oil.fs")
local util = require("oil.util")
local entry = oil.get_cursor_entry()
if not entry then
return

View file

@ -516,4 +516,13 @@ M.hack_around_termopen_autocmd = function(prev_mode)
end, 10)
end
---@return nil|integer
M.get_preview_win = function()
for _, winid in ipairs(vim.api.nvim_tabpage_list_wins(0)) do
if vim.api.nvim_win_is_valid(winid) and vim.api.nvim_win_get_option(winid, "previewwindow") then
return winid
end
end
end
return M

View file

@ -189,20 +189,60 @@ M.initialize = function(bufnr)
vim.api.nvim_buf_set_option(bufnr, k, v)
end
M.set_win_options()
vim.api.nvim_clear_autocmds({
buffer = bufnr,
group = "Oil",
})
vim.api.nvim_create_autocmd("BufHidden", {
desc = "Delete oil buffers when no longer in use",
group = "Oil",
nested = true,
buffer = bufnr,
callback = function()
vim.defer_fn(M.cleanup, 2000)
end,
nested = true,
buffer = bufnr,
})
vim.api.nvim_create_autocmd("BufDelete", {
callback = function()
session[bufnr] = nil
end,
group = "Oil",
nested = true,
once = true,
buffer = bufnr,
callback = function()
session[bufnr] = nil
end,
})
local timer
vim.api.nvim_create_autocmd("CursorMoved", {
desc = "Update oil preview window",
group = "Oil",
buffer = bufnr,
callback = function()
if timer then
timer:again()
return
end
timer = vim.loop.new_timer()
timer:start(10, 100, function()
timer:stop()
timer:close()
timer = nil
vim.schedule(function()
if vim.api.nvim_get_current_buf() ~= bufnr then
return
end
local oil = require("oil")
local entry = oil.get_cursor_entry()
if entry then
local winid = util.get_preview_win()
if winid then
if entry.id ~= vim.w[winid].oil_entry_id then
oil.select({ preview = true })
end
end
end
end)
end)
end,
})
M.render_buffer_async(bufnr, {}, function(err)
if err then