From 080dd27474b4b17055b372c0377c7dcdd58e47b8 Mon Sep 17 00:00:00 2001 From: Steven Arcangeli Date: Tue, 28 Mar 2023 11:54:44 -0700 Subject: [PATCH] refactor: clean up duplicate helper method --- lua/oil/adapters/ssh/connection.lua | 5 +++-- lua/oil/init.lua | 3 ++- lua/oil/keymap_util.lua | 3 ++- lua/oil/layout.lua | 2 ++ lua/oil/repl_layout.lua | 6 +++--- lua/oil/util.lua | 16 ++-------------- 6 files changed, 14 insertions(+), 21 deletions(-) diff --git a/lua/oil/adapters/ssh/connection.lua b/lua/oil/adapters/ssh/connection.lua index 5a03eb0..a106ecf 100644 --- a/lua/oil/adapters/ssh/connection.lua +++ b/lua/oil/adapters/ssh/connection.lua @@ -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", diff --git a/lua/oil/init.lua b/lua/oil/init.lua index 6df104f..230b5bc 100644 --- a/lua/oil/init.lua +++ b/lua/oil/init.lua @@ -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 diff --git a/lua/oil/keymap_util.lua b/lua/oil/keymap_util.lua index 32db327..3a582fd 100644 --- a/lua/oil/keymap_util.lua +++ b/lua/oil/keymap_util.lua @@ -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), diff --git a/lua/oil/layout.lua b/lua/oil/layout.lua index 6c094aa..b4e3fed 100644 --- a/lua/oil/layout.lua +++ b/lua/oil/layout.lua @@ -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 diff --git a/lua/oil/repl_layout.lua b/lua/oil/repl_layout.lua index 0224ab1..7f0e385 100644 --- a/lua/oil/repl_layout.lua +++ b/lua/oil/repl_layout.lua @@ -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", diff --git a/lua/oil/util.lua b/lua/oil/util.lua index d5bef4f..581b560 100644 --- a/lua/oil/util.lua +++ b/lua/oil/util.lua @@ -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