From eb7cf24b00c3288e824f4022dec5fd42840d99b7 Mon Sep 17 00:00:00 2001 From: Mika Vilpas Date: Fri, 19 Apr 2024 21:22:01 +0300 Subject: [PATCH] refactor(window)!: greatly simplify the code This change removes the extra logic for the window border. I'm not sure why it was there in the first place, but as far as I could see, neovim is able to support the border without any extra logic. --- lua/yazi/window.lua | 106 +------------------------------------------- 1 file changed, 2 insertions(+), 104 deletions(-) diff --git a/lua/yazi/window.lua b/lua/yazi/window.lua index 170abc3e..449a0a68 100644 --- a/lua/yazi/window.lua +++ b/lua/yazi/window.lua @@ -2,7 +2,6 @@ local M = {} ---@class YaziFloatingWindow ---@field win integer floating_window_id ----@field border_window integer ---@field content_buffer integer ---@field config YaziConfig local YaziFloatingWindow = {} @@ -30,10 +29,6 @@ function YaziFloatingWindow:close() if vim.api.nvim_win_is_valid(self.win) then vim.api.nvim_win_close(self.win, true) end - - if vim.api.nvim_win_is_valid(self.border_window) then - vim.api.nvim_win_close(self.border_window, true) - end end function YaziFloatingWindow:open_and_display() @@ -46,15 +41,7 @@ function YaziFloatingWindow:open_and_display() local row = math.ceil(vim.o.lines - height) / 2 local col = math.ceil(vim.o.columns - width) / 2 - local border_opts = { - style = 'minimal', - relative = 'editor', - row = row - 1, - col = col - 1, - width = width + 2, - height = height + 2, - } - + ---@type vim.api.keyset.win_config local opts = { style = 'minimal', relative = 'editor', @@ -62,28 +49,9 @@ function YaziFloatingWindow:open_and_display() col = col, width = width, height = height, + border = 'rounded', } - local topleft, top, topright, right, botright, bot, botleft, left = - '╭', '─', '╮', '│', '╯', '─', '╰', '│' - - local border_lines = { topleft .. string.rep(top, width) .. topright } - local middle_line = left .. string.rep(' ', width) .. right - for _ = 1, height do - table.insert(border_lines, middle_line) - end - table.insert(border_lines, botleft .. string.rep(bot, width) .. botright) - - -- create a unlisted scratch buffer for the border - local border_buffer = vim.api.nvim_create_buf(false, true) - - -- set border_lines in the border buffer from start 0 to end -1 and strict_indexing false - vim.api.nvim_buf_set_lines(border_buffer, 0, -1, true, border_lines) - -- create border window - local border_window = vim.api.nvim_open_win(border_buffer, true, border_opts) - vim.api.nvim_set_hl(0, 'YaziBorder', { link = 'Normal', default = true }) - vim.cmd('set winhl=NormalFloat:YaziBorder') - local yazi_buffer = vim.api.nvim_create_buf(false, true) -- create file window, enter the window, and use the options defined in opts local win = vim.api.nvim_open_win(yazi_buffer, true, opts) @@ -104,79 +72,9 @@ function YaziFloatingWindow:open_and_display() }) self.win = win - self.border_window = border_window self.content_buffer = yazi_buffer return self end ---- open a floating window with nice borders ----@param config YaziConfig ----@return integer, integer, integer -function M.open_floating_window(config) - local height = math.ceil(vim.o.lines * config.floating_window_scaling_factor) - - 1 - local width = math.ceil(vim.o.columns * config.floating_window_scaling_factor) - - local row = math.ceil(vim.o.lines - height) / 2 - local col = math.ceil(vim.o.columns - width) / 2 - - local border_opts = { - style = 'minimal', - relative = 'editor', - row = row - 1, - col = col - 1, - width = width + 2, - height = height + 2, - } - - local opts = { - style = 'minimal', - relative = 'editor', - row = row, - col = col, - width = width, - height = height, - } - - local topleft, top, topright, right, botright, bot, botleft, left = - '╭', '─', '╮', '│', '╯', '─', '╰', '│' - - local border_lines = { topleft .. string.rep(top, width) .. topright } - local middle_line = left .. string.rep(' ', width) .. right - for _ = 1, height do - table.insert(border_lines, middle_line) - end - table.insert(border_lines, botleft .. string.rep(bot, width) .. botright) - - -- create a unlisted scratch buffer for the border - local border_buffer = vim.api.nvim_create_buf(false, true) - - -- set border_lines in the border buffer from start 0 to end -1 and strict_indexing false - vim.api.nvim_buf_set_lines(border_buffer, 0, -1, true, border_lines) - -- create border window - local border_window = vim.api.nvim_open_win(border_buffer, true, border_opts) - vim.api.nvim_set_hl(0, 'YaziBorder', { link = 'Normal', default = true }) - vim.cmd('set winhl=NormalFloat:YaziBorder') - - local yazi_buffer = vim.api.nvim_create_buf(false, true) - -- create file window, enter the window, and use the options defined in opts - local win = vim.api.nvim_open_win(yazi_buffer, true, opts) - - vim.bo[yazi_buffer].filetype = 'yazi' - - vim.cmd('setlocal bufhidden=hide') - vim.cmd('setlocal nocursorcolumn') - vim.api.nvim_set_hl(0, 'YaziFloat', { link = 'Normal', default = true }) - vim.cmd('setlocal winhl=NormalFloat:YaziFloat') - vim.cmd('set winblend=' .. config.yazi_floating_window_winblend) - - -- use autocommand to ensure that the border_buffer closes at the same time as the main buffer - vim.cmd("autocmd WinLeave silent! execute 'hide'") - local cmd = [[autocmd WinLeave silent! execute 'silent bdelete! %s']] - vim.cmd(cmd:format(border_buffer)) - - return win, border_window, yazi_buffer -end - return M