Skip to content

Commit

Permalink
fix: set floating window win_options when buffer changes (#432)
Browse files Browse the repository at this point in the history
* fix: set floating window win_options when buffer changes

* fix: set win options even when float border is "none"

---------

Co-authored-by: Steven Arcangeli <stevearc@stevearc.com>
  • Loading branch information
icefed and stevearc authored Jul 2, 2024
1 parent ace46a4 commit b0a6cf9
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 27 deletions.
58 changes: 32 additions & 26 deletions lua/oil/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -286,29 +286,35 @@ M.open_float = function(dir)
})
)

-- Update the window title when we switch buffers
if vim.fn.has("nvim-0.9") == 1 and config.float.border ~= "none" then
local function get_title()
local src_buf = vim.api.nvim_win_get_buf(winid)
local title = vim.api.nvim_buf_get_name(src_buf)
local scheme, path = util.parse_url(title)
if config.adapters[scheme] == "files" then
assert(path)
local fs = require("oil.fs")
title = vim.fn.fnamemodify(fs.posix_to_os_path(path), ":~")
end
return title
---Recalculate the window title for the current buffer
local function get_title()
local src_buf = vim.api.nvim_win_get_buf(winid)
local title = vim.api.nvim_buf_get_name(src_buf)
local scheme, path = util.parse_url(title)
if config.adapters[scheme] == "files" then
assert(path)
local fs = require("oil.fs")
title = vim.fn.fnamemodify(fs.posix_to_os_path(path), ":~")
end
table.insert(
autocmds,
vim.api.nvim_create_autocmd("BufWinEnter", {
desc = "Update oil floating window title when buffer changes",
pattern = "*",
callback = function(params)
local winbuf = params.buf
if not vim.api.nvim_win_is_valid(winid) or vim.api.nvim_win_get_buf(winid) ~= winbuf then
return
end
return title
end

table.insert(
autocmds,
vim.api.nvim_create_autocmd("BufWinEnter", {
desc = "Reset local oil window options when buffer changes",
pattern = "*",
callback = function(params)
local winbuf = params.buf
if not vim.api.nvim_win_is_valid(winid) or vim.api.nvim_win_get_buf(winid) ~= winbuf then
return
end
for k, v in pairs(config.float.win_options) do
vim.api.nvim_set_option_value(k, v, { scope = "local", win = winid })
end

-- Update the floating window title
if vim.fn.has("nvim-0.9") == 1 and config.float.border ~= "none" then
local cur_win_opts = vim.api.nvim_win_get_config(winid)
vim.api.nvim_win_set_config(winid, {
relative = "editor",
Expand All @@ -318,10 +324,10 @@ M.open_float = function(dir)
height = cur_win_opts.height,
title = get_title(),
})
end,
})
)
end
end
end,
})
)

vim.cmd.edit({ args = { util.escape_filename(parent_url) }, mods = { keepalt = true } })
-- :edit will set buflisted = true, but we may not want that
Expand Down
2 changes: 1 addition & 1 deletion lua/oil/mutator/progress.lua
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ function Progress:show(opts)
border = config.progress.border,
})
vim.bo[self.bufnr].filetype = "oil_progress"
for k, v in pairs(config.preview.win_options) do
for k, v in pairs(config.progress.win_options) do
vim.api.nvim_set_option_value(k, v, { scope = "local", win = self.winid })
end
table.insert(
Expand Down

0 comments on commit b0a6cf9

Please sign in to comment.