|
| 1 | +local config = require("nvim-tree.config") |
| 2 | + |
| 3 | +local M = {} |
| 4 | + |
| 5 | +local DEFAULT_MIN_WIDTH = 30 |
| 6 | +local DEFAULT_MAX_WIDTH = -1 |
| 7 | +local DEFAULT_LINES_EXCLUDED = { |
| 8 | + "root", |
| 9 | +} |
| 10 | +local DEFAULT_PADDING = 1 |
| 11 | + |
| 12 | +M.Active = { |
| 13 | + adaptive_size = false, |
| 14 | + tabpages = {}, |
| 15 | + cursors = {}, |
| 16 | + winopts = { |
| 17 | + relativenumber = false, |
| 18 | + number = false, |
| 19 | + list = false, |
| 20 | + foldenable = false, |
| 21 | + winfixwidth = true, |
| 22 | + winfixheight = true, |
| 23 | + spell = false, |
| 24 | + signcolumn = "yes", |
| 25 | + foldmethod = "manual", |
| 26 | + foldcolumn = "0", |
| 27 | + cursorcolumn = false, |
| 28 | + cursorline = true, |
| 29 | + cursorlineopt = "both", |
| 30 | + colorcolumn = "0", |
| 31 | + wrap = false, |
| 32 | + winhl = table.concat({ |
| 33 | + "EndOfBuffer:NvimTreeEndOfBuffer", |
| 34 | + "CursorLine:NvimTreeCursorLine", |
| 35 | + "CursorLineNr:NvimTreeCursorLineNr", |
| 36 | + "LineNr:NvimTreeLineNr", |
| 37 | + "WinSeparator:NvimTreeWinSeparator", |
| 38 | + "StatusLine:NvimTreeStatusLine", |
| 39 | + "StatusLineNC:NvimTreeStatuslineNC", |
| 40 | + "SignColumn:NvimTreeSignColumn", |
| 41 | + "Normal:NvimTreeNormal", |
| 42 | + "NormalNC:NvimTreeNormalNC", |
| 43 | + "NormalFloat:NvimTreeNormalFloat", |
| 44 | + "FloatBorder:NvimTreeNormalFloatBorder", |
| 45 | + }, ","), |
| 46 | + }, |
| 47 | +} |
| 48 | + |
| 49 | +---@param size (fun():integer)|integer|string |
| 50 | +---@return integer |
| 51 | +function M.get_size(size) |
| 52 | + if type(size) == "number" then |
| 53 | + return size |
| 54 | + elseif type(size) == "function" then |
| 55 | + return M.get_size(size()) |
| 56 | + end |
| 57 | + local size_as_number = tonumber(size:sub(0, -2)) |
| 58 | + local percent_as_decimal = size_as_number / 100 |
| 59 | + return math.floor(vim.o.columns * percent_as_decimal) |
| 60 | +end |
| 61 | + |
| 62 | +---@param size (fun():integer)|integer|nil |
| 63 | +---@return integer |
| 64 | +function M.get_width(size) |
| 65 | + if size then |
| 66 | + return M.get_size(size) |
| 67 | + else |
| 68 | + return M.get_size(M.Active.width) |
| 69 | + end |
| 70 | +end |
| 71 | + |
| 72 | +---Configure width-related config |
| 73 | +---@param width string|function|number|table|nil |
| 74 | +local function configure_width(width) |
| 75 | + if type(width) == "table" then |
| 76 | + M.Active.adaptive_size = true |
| 77 | + M.Active.width = width.min or DEFAULT_MIN_WIDTH |
| 78 | + M.Active.max_width = width.max or DEFAULT_MAX_WIDTH |
| 79 | + local lines_excluded = width.lines_excluded or DEFAULT_LINES_EXCLUDED |
| 80 | + M.Active.root_excluded = vim.tbl_contains(lines_excluded, "root") |
| 81 | + M.Active.padding = width.padding or DEFAULT_PADDING |
| 82 | + elseif width == nil then |
| 83 | + if config.g.view.width ~= nil then |
| 84 | + -- if we had input config - fallback to it |
| 85 | + M.configure_width(config.g.view.width) |
| 86 | + else |
| 87 | + -- otherwise - restore initial width |
| 88 | + M.Active.width = M.Active.initial_width |
| 89 | + end |
| 90 | + else |
| 91 | + M.Active.adaptive_size = false |
| 92 | + M.Active.width = width |
| 93 | + end |
| 94 | +end |
| 95 | + |
| 96 | +---Apply global configuration to Active |
| 97 | +function M.initialize() |
| 98 | + M.Active.winopts.cursorline = config.g.view.cursorline |
| 99 | + M.Active.winopts.cursorlineopt = config.g.view.cursorlineopt |
| 100 | + M.Active.winopts.number = config.g.view.number |
| 101 | + M.Active.winopts.relativenumber = config.g.view.relativenumber |
| 102 | + M.Active.winopts.signcolumn = config.g.view.signcolumn |
| 103 | + |
| 104 | + configure_width(config.g.view.width) |
| 105 | + |
| 106 | + M.Active.initial_width = M.get_width() |
| 107 | +end |
| 108 | + |
| 109 | +return M |
0 commit comments