-
Notifications
You must be signed in to change notification settings - Fork 162
/
window.lua
372 lines (336 loc) 路 9.74 KB
/
window.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
local Main = require("trouble.view.main")
local Util = require("trouble.util")
---@class trouble.Window.split
---@field type "split"
---@field relative "editor" | "win" cursor is only valid for float
---@field size number | {width:number, height:number} when a table is provided, either the width or height is used based on the position
---@field position "top" | "bottom" | "left" | "right"
---@class trouble.Window.float
---@field type "float"
---@field relative "editor" | "win" | "cursor" cursor is only valid for float
---@field size {width: number, height: number}
---@field position {[1]: number, [2]: number}
---@field anchor? string
---@field focusable? boolean
---@field zindex? integer
---@field border? any
---@field title? string|{[1]: string, [2]: string}
---@field title_pos? string
---@field footer? string|{[1]: string, [2]: string}
---@field footer_pos? string
---@field fixed? boolean
---@class trouble.Window.main
---@field type "main"
---@class trouble.Window.base
---@field padding? {top?:number, left?:number}
---@field wo? vim.wo
---@field bo? vim.bo
---@field minimal? boolean (defaults to true)
---@field win? number
---@field on_mount? fun(self: trouble.Window)
---@field on_close? fun(self: trouble.Window)
---@alias trouble.Window.opts trouble.Window.base|trouble.Window.split|trouble.Window.float|trouble.Window.main
---@class trouble.Window
---@field opts trouble.Window.opts
---@field win? number
---@field buf number
---@field id number
---@field keys table<string, string>
local M = {}
M.__index = M
local _id = 0
local function next_id()
_id = _id + 1
return _id
end
local split_commands = {
editor = {
top = "topleft",
right = "vertical botright",
bottom = "botright",
left = "vertical topleft",
},
win = {
top = "aboveleft",
right = "vertical rightbelow",
bottom = "belowright",
left = "vertical leftabove",
},
}
local float_options = {
"anchor",
"border",
"bufpos",
"col",
"external",
"fixed",
"focusable",
"footer",
"footer_pos",
"height",
"hide",
"noautocmd",
"relative",
"row",
"style",
"title",
"title_pos",
"width",
"win",
"zindex",
}
---@type trouble.Window.opts
local defaults = {
padding = { top = 0, left = 1 },
bo = {
bufhidden = "wipe",
filetype = "trouble",
buftype = "nofile",
},
wo = {},
}
---@type trouble.Window.opts
local minimal = {
wo = {
cursorcolumn = false,
cursorline = true,
cursorlineopt = "both",
fillchars = "eob: ",
foldcolumn = "0",
foldenable = false,
foldlevel = 99,
foldmethod = "manual",
list = false,
number = false,
relativenumber = false,
signcolumn = "no",
spell = false,
statuscolumn = "",
winfixheight = true,
winfixwidth = true,
winhighlight = "Normal:TroubleNormal,NormalNC:TroubleNormalNC,EndOfBuffer:TroubleNormal",
wrap = false,
},
}
---@param opts? trouble.Window.opts
function M.new(opts)
local self = setmetatable({}, M)
self.id = next_id()
opts = opts or {}
if opts.minimal == nil then
opts.minimal = opts.type ~= "main"
end
opts = vim.tbl_deep_extend("force", {}, defaults, opts.minimal and minimal or {}, opts or {})
opts.type = opts.type or "split"
if opts.type == "split" then
opts.relative = opts.relative or "editor"
opts.position = opts.position or "bottom"
opts.size = opts.size or (opts.position == "bottom" or opts.position == "top") and 10 or 30
opts.win = opts.win or vim.api.nvim_get_current_win()
elseif opts.type == "float" then
opts.relative = opts.relative or "editor"
opts.size = opts.size or { width = 0.8, height = 0.8 }
opts.position = opts.position or { 0.5, 0.5 }
elseif opts.type == "main" then
opts.type = "float"
opts.relative = "win"
opts.position = { 0, 0 }
opts.size = { width = 1, height = 1 }
opts.wo.winhighlight = ""
end
self.opts = opts
return self
end
---@param clear? boolean
function M:augroup(clear)
return vim.api.nvim_create_augroup("trouble.window." .. self.id, { clear = clear == true })
end
function M:parent_size()
if self.opts.relative == "editor" or self.opts.relative == "cursor" then
return { width = vim.o.columns, height = vim.o.lines }
end
return { width = vim.api.nvim_win_get_width(self.opts.win), height = vim.api.nvim_win_get_height(self.opts.win) }
end
---@param type "win" | "buf"
function M:set_options(type)
local opts = type == "win" and self.opts.wo or self.opts.bo
---@diagnostic disable-next-line: no-unknown
for k, v in pairs(opts or {}) do
---@diagnostic disable-next-line: no-unknown
local ok, err = pcall(vim.api.nvim_set_option_value, k, v, type == "win" and {
scope = "local",
win = self.win,
} or { buf = self.buf })
if not ok then
Util.error("Error setting option `" .. k .. "=" .. v .. "`\n\n" .. err)
end
end
end
function M:mount()
self.keys = {}
self.buf = vim.api.nvim_create_buf(false, true)
self:set_options("buf")
if self.opts.type == "split" then
---@diagnostic disable-next-line: param-type-mismatch
self:mount_split(self.opts)
else
---@diagnostic disable-next-line: param-type-mismatch
self:mount_float(self.opts)
end
vim.w[self.win].trouble = {
type = self.opts.type,
relative = self.opts.relative,
position = self.opts.position,
}
self:set_options("win")
self:on({ "BufWinLeave" }, vim.schedule_wrap(self.check_alien))
self:on("WinClosed", function()
if self.opts.on_close then
self.opts.on_close(self)
end
self:augroup(true)
end, { win = true })
if self.opts.on_mount then
self.opts.on_mount(self)
end
end
function M:set_buf(buf)
self.buf = buf
if self.win and vim.api.nvim_win_is_valid(self.win) then
vim.api.nvim_win_set_buf(self.win, buf)
end
end
function M:check_alien()
if self.win and vim.api.nvim_win_is_valid(self.win) then
local buf = vim.api.nvim_win_get_buf(self.win)
if buf ~= self.buf then
-- move the alien buffer to another window
local main = Main:get()
if main then
vim.api.nvim_win_set_buf(main.win, buf)
-- restore the trouble window
self:close()
self:open()
end
end
end
end
function M:close()
self:augroup(true)
pcall(vim.api.nvim_win_close, self.win, true)
self.win = nil
end
function M:open()
if self:valid() then
return
end
self:close()
self:mount()
return self
end
function M:valid()
return self.win
and vim.api.nvim_win_is_valid(self.win)
and self.buf
and vim.api.nvim_buf_is_valid(self.buf)
and vim.api.nvim_win_get_buf(self.win) == self.buf
end
---@param opts trouble.Window.split|trouble.Window.base
function M:mount_split(opts)
if self.opts.win and not vim.api.nvim_win_is_valid(self.opts.win) then
self.opts.win = 0
end
local parent_size = self:parent_size()
local size = opts.size
if type(size) == "table" then
size = opts.position == "left" or opts.position == "right" and size.width or size.height
end
if size <= 1 then
local vertical = opts.position == "left" or opts.position == "right"
size = math.floor(parent_size[vertical and "width" or "height"] * size)
end
local cmd = split_commands[opts.relative][opts.position]
Util.noautocmd(function()
vim.api.nvim_win_call(opts.win, function()
vim.cmd("silent noswapfile " .. cmd .. " " .. size .. "split")
vim.api.nvim_win_set_buf(0, self.buf)
self.win = vim.api.nvim_get_current_win()
end)
end)
end
---@param opts trouble.Window.float|trouble.Window.base
function M:mount_float(opts)
local parent_size = self:parent_size()
---@type vim.api.keyset.win_config
local config = {}
for _, v in ipairs(float_options) do
---@diagnostic disable-next-line: no-unknown
config[v] = opts[v]
end
config.focusable = true
config.height = opts.size.height <= 1 and math.floor(parent_size.height * opts.size.height) or opts.size.height
config.width = opts.size.width <= 1 and math.floor(parent_size.width * opts.size.width) or opts.size.width
config.row = math.abs(opts.position[1]) <= 1 and math.floor((parent_size.height - config.height) * opts.position[1])
or opts.position[1]
config.row = config.row < 0 and (parent_size.height + config.row) or config.row
config.col = math.abs(opts.position[2]) <= 1 and math.floor((parent_size.width - config.width) * opts.position[2])
or opts.position[2]
config.col = config.col < 0 and (parent_size.width + config.col) or config.col
if config.relative ~= "win" then
config.win = nil
end
self.win = vim.api.nvim_open_win(self.buf, false, config)
end
function M:focus()
if self:valid() then
vim.api.nvim_set_current_win(self.win)
end
end
---@param events string|string[]
---@param fn fun(self:trouble.Window, event:{buf:number}):boolean?
---@param opts? vim.api.keyset.create_autocmd | {buffer: false, win?:boolean}
function M:on(events, fn, opts)
opts = opts or {}
if opts.win then
opts.pattern = self.win .. ""
opts.win = nil
elseif opts.buffer == nil then
opts.buffer = self.buf
elseif opts.buffer == false then
opts.buffer = nil
end
if opts.pattern then
opts.buffer = nil
end
local _self = Util.weak(self)
opts.callback = function(e)
local this = _self()
if not this then
-- delete the autocmd
return true
end
return fn(this, e)
end
opts.group = self:augroup()
vim.api.nvim_create_autocmd(events, opts)
end
---@param key string
---@param fn fun(self: trouble.Window)
---@param desc? string
function M:map(key, fn, desc)
if not self:valid() then
error("Cannot create a keymap for an invalid window")
end
self.keys[key] = desc or key
local weak_self = Util.weak(self)
vim.keymap.set("n", key, function()
if weak_self() then
fn(weak_self())
end
end, {
nowait = true,
buffer = self.buf,
desc = desc,
})
end
return M