Skip to content

Commit

Permalink
feat: add layout.resize_to_content config option
Browse files Browse the repository at this point in the history
  • Loading branch information
stevearc committed Jun 21, 2023
1 parent 8b57371 commit c4714a6
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 19 deletions.
3 changes: 3 additions & 0 deletions lua/aerial/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ local default_options = {
-- window - open aerial to the right/left of the current window
placement = "window",

-- When the symbols change, resize the aerial window (within min/max constraints) to fit
resize_to_content = true,

-- Preserve window size equality with (:help CTRL-W_=)
preserve_equality = false,
},
Expand Down
6 changes: 4 additions & 2 deletions lua/aerial/render.lua
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@ local function resize_all_wins(aer_bufnr, preferred_width, preferred_height)
-- padding out whitespace. The gutter needs to adjust the total window
-- size, but it doesn't take space away from the content.
max_width = math.max(max_width, width - gutter)
vim.api.nvim_win_set_width(winid, width)
util.save_width(aer_bufnr, width)
if config.layout.resize_to_content or vim.b[aer_bufnr].aerial_width == nil then
vim.api.nvim_win_set_width(winid, width)
end
vim.b[aer_bufnr].aerial_width = width

-- Reposition floating windows
if util.is_floating_win(winid) then
Expand Down
18 changes: 2 additions & 16 deletions lua/aerial/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,8 @@ end
---@param str string
---@return string
M.remove_newlines = function(str)
return string.gsub(str, "\n", " ")
end

---@param winid integer Window to restore
---@param bufnr integer Aerial buffer
M.restore_width = function(winid, bufnr)
local ok, width = pcall(vim.api.nvim_buf_get_var, bufnr, "aerial_width")
if ok then
vim.api.nvim_win_set_width(winid, width)
end
end

---@param bufnr integer
---@param width integer
M.save_width = function(bufnr, width)
vim.api.nvim_buf_set_var(bufnr, "aerial_width", width)
local ret = string.gsub(str, "\n", " ")
return ret
end

---@param winid nil|integer
Expand Down
5 changes: 4 additions & 1 deletion lua/aerial/window.lua
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,10 @@ local function setup_aerial_win(src_winid, aer_winid, aer_bufnr)
vim.api.nvim_win_set_var(src_winid, "aerial_win", aer_winid)
-- Set the filetype only after we enter the buffer so that ftplugins behave properly
vim.api.nvim_buf_set_option(aer_bufnr, "filetype", "aerial")
util.restore_width(aer_winid, aer_bufnr)
local width = vim.b[aer_bufnr].aerial_width
if width then
vim.api.nvim_win_set_width(aer_winid, width)
end
if config.layout.preserve_equality then
vim.cmd.wincmd({ args = { "=" } })
end
Expand Down

0 comments on commit c4714a6

Please sign in to comment.