refactor: clean up duplicate helper method

This commit is contained in:
Steven Arcangeli 2023-03-28 11:54:44 -07:00
parent d631d9fc5a
commit 080dd27474
6 changed files with 14 additions and 21 deletions

View file

@ -1,3 +1,4 @@
local layout = require("oil.layout")
local util = require("oil.util")
local SSHConnection = {}
@ -233,10 +234,10 @@ function SSHConnection:open_terminal()
end
local min_width = 120
local min_height = 20
local total_height = util.get_editor_height()
local total_height = layout.get_editor_height()
local width = math.min(min_width, vim.o.columns - 2)
local height = math.min(min_height, total_height - 3)
local row = math.floor((util.get_editor_height() - height) / 2)
local row = math.floor((total_height - height) / 2)
local col = math.floor((vim.o.columns - width) / 2)
self.term_winid = vim.api.nvim_open_win(self.term_bufnr, true, {
relative = "editor",

View file

@ -225,6 +225,7 @@ end
---@param dir nil|string When nil, open the parent of the current buffer, or the cwd if current buffer is not a file
M.open_float = function(dir)
local config = require("oil.config")
local layout = require("oil.layout")
local util = require("oil.util")
local view = require("oil.view")
local parent_url, basename = M.get_url_for_path(dir)
@ -238,7 +239,7 @@ M.open_float = function(dir)
local bufnr = vim.api.nvim_create_buf(false, true)
vim.bo[bufnr].bufhidden = "wipe"
local total_width = vim.o.columns
local total_height = util.get_editor_height()
local total_height = layout.get_editor_height()
local width = total_width - 2 * config.float.padding
if config.float.border ~= "none" then
width = width - 2 -- The border consumes 1 col on each side

View file

@ -1,4 +1,5 @@
local actions = require("oil.actions")
local layout = require("oil.layout")
local util = require("oil.util")
local M = {}
@ -84,7 +85,7 @@ M.show_help = function(keymaps)
vim.api.nvim_buf_set_option(bufnr, "bufhidden", "wipe")
local editor_width = vim.o.columns
local editor_height = util.get_editor_height()
local editor_height = layout.get_editor_height()
local winid = vim.api.nvim_open_win(bufnr, true, {
relative = "editor",
row = math.max(0, (editor_height - #lines) / 2),

View file

@ -13,10 +13,12 @@ local function calc_float(value, max_value)
end
end
---@return integer
M.get_editor_width = function()
return vim.o.columns
end
---@return integer
M.get_editor_height = function()
local editor_height = vim.o.lines - vim.o.cmdheight
-- Subtract 1 if tabline is visible

View file

@ -1,4 +1,4 @@
local util = require("oil.util")
local layout = require("oil.layout")
local ReplLayout = {}
---@param opts table
@ -19,12 +19,12 @@ ReplLayout.new = function(opts)
on_submit = { opts.on_submit, "f" },
on_cancel = { opts.on_cancel, "f", true },
})
local total_height = util.get_editor_height()
local total_height = layout.get_editor_height()
local bufnr = vim.api.nvim_create_buf(false, true)
vim.bo[bufnr].bufhidden = "wipe"
local width = math.min(opts.min_width, vim.o.columns - 2)
local height = math.min(opts.min_height, total_height - 3)
local row = math.floor((util.get_editor_height() - height) / 2)
local row = math.floor((total_height - height) / 2)
local col = math.floor((vim.o.columns - width) / 2)
local view_winid = vim.api.nvim_open_win(bufnr, false, {
relative = "editor",

View file

@ -287,20 +287,6 @@ M.is_floating_win = function(winid)
return vim.api.nvim_win_get_config(winid or 0).relative ~= ""
end
---@return integer
M.get_editor_height = function()
local total_height = vim.o.lines - vim.o.cmdheight
if vim.o.showtabline == 2 or (vim.o.showtabline == 1 and #vim.api.nvim_list_tabpages() > 1) then
total_height = total_height - 1
end
if
vim.o.laststatus >= 2 or (vim.o.laststatus == 1 and #vim.api.nvim_tabpage_list_wins(0) > 1)
then
total_height = total_height - 1
end
return total_height
end
local winid_map = {}
M.add_title_to_win = function(winid, opts)
opts = opts or {}
@ -442,6 +428,8 @@ M.get_adapter_for_action = function(action)
return adapter
end
---@param bufnr integer
---@param text string|string[]
M.render_centered_text = function(bufnr, text)
if not vim.api.nvim_buf_is_valid(bufnr) then
return