feat: update preview window when cursor is moved (#42)
This commit is contained in:
parent
6c4a3dafca
commit
6c6b7673af
3 changed files with 58 additions and 18 deletions
|
|
@ -1,4 +1,5 @@
|
||||||
local oil = require("oil")
|
local oil = require("oil")
|
||||||
|
local util = require("oil.util")
|
||||||
|
|
||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
|
|
@ -29,15 +30,6 @@ M.select_split = {
|
||||||
end,
|
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 = {
|
M.preview = {
|
||||||
desc = "Open the entry under the cursor in a preview window, or close the preview window if already open",
|
desc = "Open the entry under the cursor in a preview window, or close the preview window if already open",
|
||||||
callback = function()
|
callback = function()
|
||||||
|
|
@ -46,7 +38,7 @@ M.preview = {
|
||||||
vim.notify("Could not find entry under cursor", vim.log.levels.ERROR)
|
vim.notify("Could not find entry under cursor", vim.log.levels.ERROR)
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
local winid = get_preview_win()
|
local winid = util.get_preview_win()
|
||||||
if winid then
|
if winid then
|
||||||
local cur_id = vim.w[winid].oil_entry_id
|
local cur_id = vim.w[winid].oil_entry_id
|
||||||
if entry.id == cur_id then
|
if entry.id == cur_id then
|
||||||
|
|
@ -61,7 +53,7 @@ M.preview = {
|
||||||
M.preview_scroll_down = {
|
M.preview_scroll_down = {
|
||||||
desc = "Scroll down in the preview window",
|
desc = "Scroll down in the preview window",
|
||||||
callback = function()
|
callback = function()
|
||||||
local winid = get_preview_win()
|
local winid = util.get_preview_win()
|
||||||
if winid then
|
if winid then
|
||||||
vim.api.nvim_win_call(winid, function()
|
vim.api.nvim_win_call(winid, function()
|
||||||
vim.cmd.normal({
|
vim.cmd.normal({
|
||||||
|
|
@ -76,7 +68,7 @@ M.preview_scroll_down = {
|
||||||
M.preview_scroll_up = {
|
M.preview_scroll_up = {
|
||||||
desc = "Scroll up in the preview window",
|
desc = "Scroll up in the preview window",
|
||||||
callback = function()
|
callback = function()
|
||||||
local winid = get_preview_win()
|
local winid = util.get_preview_win()
|
||||||
if winid then
|
if winid then
|
||||||
vim.api.nvim_win_call(winid, function()
|
vim.api.nvim_win_call(winid, function()
|
||||||
vim.cmd.normal({
|
vim.cmd.normal({
|
||||||
|
|
@ -174,7 +166,6 @@ M.open_cmdline = {
|
||||||
callback = function()
|
callback = function()
|
||||||
local config = require("oil.config")
|
local config = require("oil.config")
|
||||||
local fs = require("oil.fs")
|
local fs = require("oil.fs")
|
||||||
local util = require("oil.util")
|
|
||||||
local entry = oil.get_cursor_entry()
|
local entry = oil.get_cursor_entry()
|
||||||
if not entry then
|
if not entry then
|
||||||
return
|
return
|
||||||
|
|
|
||||||
|
|
@ -516,4 +516,13 @@ M.hack_around_termopen_autocmd = function(prev_mode)
|
||||||
end, 10)
|
end, 10)
|
||||||
end
|
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
|
return M
|
||||||
|
|
|
||||||
|
|
@ -189,20 +189,60 @@ M.initialize = function(bufnr)
|
||||||
vim.api.nvim_buf_set_option(bufnr, k, v)
|
vim.api.nvim_buf_set_option(bufnr, k, v)
|
||||||
end
|
end
|
||||||
M.set_win_options()
|
M.set_win_options()
|
||||||
|
vim.api.nvim_clear_autocmds({
|
||||||
|
buffer = bufnr,
|
||||||
|
group = "Oil",
|
||||||
|
})
|
||||||
vim.api.nvim_create_autocmd("BufHidden", {
|
vim.api.nvim_create_autocmd("BufHidden", {
|
||||||
|
desc = "Delete oil buffers when no longer in use",
|
||||||
|
group = "Oil",
|
||||||
|
nested = true,
|
||||||
|
buffer = bufnr,
|
||||||
callback = function()
|
callback = function()
|
||||||
vim.defer_fn(M.cleanup, 2000)
|
vim.defer_fn(M.cleanup, 2000)
|
||||||
end,
|
end,
|
||||||
nested = true,
|
|
||||||
buffer = bufnr,
|
|
||||||
})
|
})
|
||||||
vim.api.nvim_create_autocmd("BufDelete", {
|
vim.api.nvim_create_autocmd("BufDelete", {
|
||||||
callback = function()
|
group = "Oil",
|
||||||
session[bufnr] = nil
|
|
||||||
end,
|
|
||||||
nested = true,
|
nested = true,
|
||||||
once = true,
|
once = true,
|
||||||
buffer = bufnr,
|
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)
|
M.render_buffer_async(bufnr, {}, function(err)
|
||||||
if err then
|
if err then
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue