-
-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathutils.lua
177 lines (137 loc) · 5.02 KB
/
utils.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
local utils = require("nui.utils")
local layout_utils = require("nui.layout.utils")
local u = {
defaults = utils.defaults,
get_editor_size = utils.get_editor_size,
get_window_size = utils.get_window_size,
is_type = utils.is_type,
normalize_dimension = utils._.normalize_dimension,
size = layout_utils.size,
}
local mod = {}
---@param size number|string|nui_split_option_size
---@param position nui_split_option_position
---@return number|string size
local function to_split_size(size, position)
if not u.is_type("table", size) then
---@cast size number|string
return size
end
if position == "left" or position == "right" then
return size.width
end
return size.height
end
---@param options table
---@return table options
function mod.merge_default_options(options)
options.relative = u.defaults(options.relative, "win")
options.position = u.defaults(options.position, vim.go.splitbelow and "bottom" or "top")
options.enter = u.defaults(options.enter, true)
options.buf_options = u.defaults(options.buf_options, {})
options.win_options = vim.tbl_extend("force", {
winfixwidth = true,
winfixheight = true,
}, u.defaults(options.win_options, {}))
return options
end
---@param options nui_split_options
function mod.normalize_layout_options(options)
if utils.is_type("string", options.relative) then
options.relative = {
---@diagnostic disable-next-line: assign-type-mismatch
type = options.relative,
}
end
return options
end
---@param options nui_split_options
function mod.normalize_options(options)
options = mod.normalize_layout_options(options)
return options
end
local function parse_relative(relative, fallback_winid)
local winid = u.defaults(relative.winid, fallback_winid)
return {
type = relative.type,
win = winid,
}
end
---@param relative _nui_split_internal_relative
---@return { size: { height: integer, width: integer }, type: 'editor'|'window' }
local function get_container_info(relative)
if relative.type == "editor" then
local size = u.get_editor_size()
-- best effort adjustments
size.height = size.height - vim.api.nvim_get_option("cmdheight")
if vim.api.nvim_get_option("laststatus") >= 2 then
size.height = size.height - 1
end
if vim.api.nvim_get_option("showtabline") == 2 then
size.height = size.height - 1
end
return {
size = size,
type = "editor",
}
end
return {
size = u.get_window_size(relative.win),
type = "window",
}
end
---@param position nui_split_option_position
---@param size number|string
---@param container_size { width: number, height: number }
---@return { width?: number, height?: number }
function mod.calculate_window_size(position, size, container_size)
if not size then
return {}
end
if position == "left" or position == "right" then
return {
width = u.normalize_dimension(size, container_size.width),
}
end
return {
height = u.normalize_dimension(size, container_size.height),
}
end
function mod.update_layout_config(component_internal, config)
local internal = component_internal
local options = mod.normalize_layout_options({
relative = config.relative,
position = config.position,
size = config.size,
})
if internal.relative and internal.relative.win and not vim.api.nvim_win_is_valid(internal.relative.win) then
internal.relative.win = vim.api.nvim_get_current_win()
internal.win_config.win = internal.relative.win
internal.win_config.pending_changes.relative = true
end
if options.relative then
local fallback_winid = internal.relative and internal.relative.win or vim.api.nvim_get_current_win()
internal.relative = parse_relative(options.relative, fallback_winid)
local prev_relative = internal.win_config.relative
local prev_win = internal.win_config.win
internal.win_config.relative = internal.relative.type
internal.win_config.win = internal.relative.type == "win" and internal.relative.win or nil
internal.win_config.pending_changes.relative = internal.win_config.relative ~= prev_relative
or internal.win_config.win ~= prev_win
end
if options.position or internal.win_config.pending_changes.relative then
local prev_position = internal.win_config.position
internal.position = options.position or internal.position
internal.win_config.position = internal.position
internal.win_config.pending_changes.position = internal.win_config.position ~= prev_position
end
if options.size or internal.win_config.pending_changes.position or internal.win_config.pending_changes.relative then
internal.layout.size = to_split_size(options.size or internal.layout.size, internal.position)
internal.container_info = get_container_info(internal.relative)
internal.size = mod.calculate_window_size(internal.position, internal.layout.size, internal.container_info.size)
internal.win_config.width = internal.size.width
internal.win_config.height = internal.size.height
internal.win_config.pending_changes.size = true
end
end
return mod