Skip to content

Commit b5cc6e1

Browse files
committed
feat: autoresize when opening / closing other windows (#190)
1 parent 499f31a commit b5cc6e1

4 files changed

Lines changed: 28 additions & 60 deletions

File tree

lua/dap-view/actions.lua

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -145,19 +145,24 @@ M.open = function(hide_terminal)
145145

146146
local term_size = term_size__ < 1 and math.floor(go_max * term_size__) or math.floor(term_size__)
147147

148+
-- Oh lord
149+
local height = (
150+
is_win_valid and (term_is_vertical and ((is_vertical and size or go.lines) - term_size) or size)
151+
or (is_vertical and size or nil)
152+
)
153+
local width = (
154+
is_win_valid and (not term_is_vertical and ((not is_vertical and size or go.columns) - term_size) or size)
155+
or (not is_vertical and size or nil)
156+
)
157+
158+
state.og_height = height
159+
state.og_width = width
160+
148161
local winnr = api.nvim_open_win(bufnr, false, {
149162
split = is_win_valid and inv_term_position or win_pos,
150163
win = is_anchor_win_valid and anchor_win or is_term_win_valid and term_winnr or -1,
151-
-- Oh lord
152-
height = (
153-
is_win_valid and (term_is_vertical and ((is_vertical and size or go.lines) - term_size) or size)
154-
or (is_vertical and size or nil)
155-
),
156-
width = (
157-
is_win_valid
158-
and (not term_is_vertical and ((not is_vertical and size or go.columns) - term_size) or size)
159-
or (not is_vertical and size or nil)
160-
),
164+
height = height,
165+
width = width,
161166
})
162167

163168
-- Restore fixed size

lua/dap-view/autocmds.lua

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,22 @@ local winbar = require("dap-view.options.winbar")
99

1010
local api = vim.api
1111

12-
api.nvim_create_autocmd("WinClosed", {
13-
callback = function(args)
14-
local w = vim.w[tonumber(args.file)]
15-
16-
-- Resize main window on term close to
17-
if w.dapview_win_term and util.is_win_valid(state.winnr) then
18-
require("dap-view.util.window").resize(state.winnr)
19-
end
20-
21-
-- Similarly for closing main
22-
if w.dapview_win and util.is_win_valid(state.term_winnr) then
23-
require("dap-view.util.window").resize(state.term_winnr)
24-
end
25-
end,
26-
})
27-
2812
api.nvim_create_autocmd({ "WinClosed", "WinNew" }, {
2913
callback = function()
3014
vim.schedule(function()
3115
if util.is_win_valid(state.winnr) then
16+
-- Recalculate function labels
3217
winbar.refresh_winbar()
18+
19+
-- Resize when closing unrelated windows (#190)
20+
-- We assume that keeping the original size is more important than recalculating
21+
-- (if the size was a function)
22+
if state.og_height then
23+
api.nvim_win_set_height(state.winnr, state.og_height)
24+
end
25+
if state.og_width then
26+
api.nvim_win_set_width(state.winnr, state.og_width)
27+
end
3328
end
3429
end)
3530
end,

lua/dap-view/state.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
---@field current_adapter? string
3636
---@field subtle_frames boolean
3737
---@field current_section? dapview.Section
38+
---@field og_height? integer
39+
---@field og_width? integer
3840
---@field last_section? dapview.Section
3941
---@field last_session_buf? integer
4042
---@field exceptions_options table<string,dapview.ExceptionsOption[]>

lua/dap-view/util/window.lua

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
local state = require("dap-view.state")
2-
local setup = require("dap-view.setup")
3-
41
local M = {}
52

63
local api = vim.api
@@ -29,35 +26,4 @@ M.fetch_window = function(opts)
2926
end
3027
end
3128

32-
---Restores the configured size for a given window
33-
---@param winnr integer
34-
M.resize = function(winnr)
35-
local wo = vim.wo[winnr]
36-
37-
local size_ = setup.config.windows.size
38-
local size__ = ((type(size_) == "function" and size_(state.win_pos)) or size_)
39-
40-
---@cast size__ number
41-
42-
if wo.winfixheight then
43-
wo.winfixheight = false
44-
45-
local size = math.floor(size__ < 1 and size__ * vim.go.lines or size__)
46-
47-
api.nvim_win_set_height(state.term_winnr, size)
48-
49-
wo.winfixheight = true
50-
end
51-
52-
if wo.winfixwidth then
53-
wo.winfixwidth = false
54-
55-
local size = math.floor(size__ < 1 and size__ * vim.go.columns or size__)
56-
57-
api.nvim_win_set_width(state.term_winnr, size)
58-
59-
wo.winfixwidth = true
60-
end
61-
end
62-
6329
return M

0 commit comments

Comments
 (0)