From 4fc9cb1da395d29222e273353902b6491ac0e483 Mon Sep 17 00:00:00 2001 From: kiyan Date: Wed, 9 Feb 2022 22:26:02 +0100 Subject: [PATCH] chore: move code around - deprecate config.lua file - move icon config code into renderer/icons - move file opening config in open-file.lua --- lua/nvim-tree/actions/movements.lua | 4 +- lua/nvim-tree/actions/open-file.lua | 34 ++++++++-- lua/nvim-tree/colors.lua | 4 +- lua/nvim-tree/config.lua | 97 +---------------------------- lua/nvim-tree/renderer/icons.lua | 61 ++++++++++++++++++ lua/nvim-tree/renderer/init.lua | 5 +- lua/nvim-tree/renderer/padding.lua | 2 +- 7 files changed, 101 insertions(+), 106 deletions(-) create mode 100644 lua/nvim-tree/renderer/icons.lua diff --git a/lua/nvim-tree/actions/movements.lua b/lua/nvim-tree/actions/movements.lua index 79699ce1..41b33c32 100644 --- a/lua/nvim-tree/actions/movements.lua +++ b/lua/nvim-tree/actions/movements.lua @@ -1,7 +1,7 @@ local utils = require'nvim-tree.utils' local view = require'nvim-tree.view' local diagnostics = require'nvim-tree.diagnostics' -local config = require"nvim-tree.config" +local icons = require"nvim-tree.renderer.icons" local renderer = require"nvim-tree.renderer" local lib = function() return require'nvim-tree.lib' end @@ -106,7 +106,7 @@ function M.sibling(direction) end function M.find_git_item(where) - local icon_state = config.get_icon_state() + local icon_state = icons.get_config() local flags = where == 'next' and 'b' or '' local icons = table.concat(vim.tbl_values(icon_state.icons.git_icons), '\\|') return function() diff --git a/lua/nvim-tree/actions/open-file.lua b/lua/nvim-tree/actions/open-file.lua index d93fb03d..e2c63b79 100644 --- a/lua/nvim-tree/actions/open-file.lua +++ b/lua/nvim-tree/actions/open-file.lua @@ -1,6 +1,5 @@ local api = vim.api -local config = require'nvim-tree.config' local lib = require'nvim-tree.lib' local utils = require'nvim-tree.utils' local view = require'nvim-tree.view' @@ -9,6 +8,33 @@ local M = { quit_on_open = false, } +local function get_split_cmd() + local side = view.View.side + if side == 'right' then + return 'aboveleft' + end + if side == "left" then + return 'belowright' + end + if side == "top" then + return 'bot' + end + return 'top' +end + +local function window_picker_exclude() + if type(vim.g.nvim_tree_window_picker_exclude) == "table" then + return vim.g.nvim_tree_window_picker_exclude + end + return { + filetype = { + "notify", + "packer", + "qf" + } + } +end + ---Get user to pick a window. Selectable windows are all windows in the current ---tabpage that aren't NvimTree. ---@return integer|nil -- If a valid window was picked, return its id. If an @@ -18,7 +44,7 @@ local function pick_window() local tabpage = api.nvim_get_current_tabpage() local win_ids = api.nvim_tabpage_list_wins(tabpage) local tree_winid = view.get_winnr(tabpage) - local exclude = config.window_picker_exclude() + local exclude = window_picker_exclude() local selectable = vim.tbl_filter(function (id) local bufid = api.nvim_win_get_buf(id) @@ -160,9 +186,9 @@ function M.fn(mode, filename) if not target_winid or not vim.tbl_contains(win_ids, target_winid) then -- Target is invalid, or window does not exist in current tabpage: create -- new window - local window_opts = config.window_options() + local split_cmd = get_split_cmd() local splitside = view.is_vertical() and "vsp" or "sp" - vim.cmd(window_opts.split_command .. " " .. splitside) + vim.cmd(split_cmd .. " " .. splitside) target_winid = api.nvim_get_current_win() lib.target_winid = target_winid diff --git a/lua/nvim-tree/colors.lua b/lua/nvim-tree/colors.lua index 1ffadb85..d4a2f843 100644 --- a/lua/nvim-tree/colors.lua +++ b/lua/nvim-tree/colors.lua @@ -1,5 +1,5 @@ local api = vim.api -local config = require'nvim-tree.config' +local icons = require'nvim-tree.renderer.icons' local M = {} @@ -77,7 +77,7 @@ local function get_links() end function M.setup() - if config.get_icon_state().show_file_icon and config.get_icon_state().has_devicons then + if icons.get_config().show_file_icon and icons.get_config().has_devicons then require'nvim-web-devicons'.setup() end local higlight_groups = get_hl_groups() diff --git a/lua/nvim-tree/config.lua b/lua/nvim-tree/config.lua index fc0835a0..fa4177f4 100644 --- a/lua/nvim-tree/config.lua +++ b/lua/nvim-tree/config.lua @@ -1,103 +1,10 @@ +-- INFO: DEPRECATED FILE, DO NOT ADD ANYTHING IN THERE +-- keeping to avoid breaking user configs. Will remove during a weekend. local M = {} -function M.get_icon_state() - local show_icons = vim.g.nvim_tree_show_icons or { git = 1, folders = 1, files = 1, folder_arrows = 1 } - local icons = { - default = "", - symlink = "", - git_icons = { - unstaged = "✗", - staged = "✓", - unmerged = "", - renamed = "➜", - untracked = "★", - deleted = "", - ignored = "◌" - }, - folder_icons = { - arrow_closed = "", - arrow_open = "", - default = "", - open = "", - empty = "", - empty_open = "", - symlink = "", - symlink_open = "", - }, - } - - local user_icons = vim.g.nvim_tree_icons - if user_icons then - if user_icons.default then - icons.default = user_icons.default - icons.symlink = user_icons.default - end - if user_icons.symlink then - icons.symlink = user_icons.symlink - end - for key, val in pairs(user_icons.git or {}) do - if icons.git_icons[key] then - icons.git_icons[key] = val - end - end - for key, val in pairs(user_icons.folder or {}) do - if icons.folder_icons[key] then - icons.folder_icons[key] = val - end - end - end - - local has_devicons = pcall(require, 'nvim-web-devicons') - return { - show_file_icon = show_icons.files == 1, - show_folder_icon = show_icons.folders == 1, - show_git_icon = show_icons.git == 1, - show_folder_arrows = show_icons.folder_arrows == 1, - has_devicons = has_devicons, - icons = icons - } -end - -- TODO: remove this once the cb property is not supported in mappings function M.nvim_tree_callback(callback_name) return string.format(":lua require'nvim-tree.actions'.on_keypress('%s')", callback_name) end -function M.window_options() - local opts = {} - local side = require'nvim-tree.view'.View.side - if side == 'right' then - opts.open_command = 'h' - opts.preview_command = 'l' - opts.split_command = 'aboveleft' - elseif side == "left" then - opts.open_command = 'l' - opts.preview_command = 'h' - opts.split_command = 'belowright' - elseif side == "top" then - opts.open_command = 'j' - opts.preview_command = 'k' - opts.split_command = 'bot' - else - opts.open_command = 'k' - opts.preview_command = 'j' - opts.split_command = 'top' - end - - return opts -end - -function M.window_picker_exclude() - if type(vim.g.nvim_tree_window_picker_exclude) == "table" then - return vim.g.nvim_tree_window_picker_exclude - end - return { - filetype = { - "notify", - "packer", - "qf" - } - } -end - return M diff --git a/lua/nvim-tree/renderer/icons.lua b/lua/nvim-tree/renderer/icons.lua new file mode 100644 index 00000000..76bddb2a --- /dev/null +++ b/lua/nvim-tree/renderer/icons.lua @@ -0,0 +1,61 @@ +local M = {} + +function M.get_config() + local show_icons = vim.g.nvim_tree_show_icons or { git = 1, folders = 1, files = 1, folder_arrows = 1 } + local icons = { + default = "", + symlink = "", + git_icons = { + unstaged = "✗", + staged = "✓", + unmerged = "", + renamed = "➜", + untracked = "★", + deleted = "", + ignored = "◌" + }, + folder_icons = { + arrow_closed = "", + arrow_open = "", + default = "", + open = "", + empty = "", + empty_open = "", + symlink = "", + symlink_open = "", + }, + } + + local user_icons = vim.g.nvim_tree_icons + if user_icons then + if user_icons.default then + icons.default = user_icons.default + icons.symlink = user_icons.default + end + if user_icons.symlink then + icons.symlink = user_icons.symlink + end + for key, val in pairs(user_icons.git or {}) do + if icons.git_icons[key] then + icons.git_icons[key] = val + end + end + for key, val in pairs(user_icons.folder or {}) do + if icons.folder_icons[key] then + icons.folder_icons[key] = val + end + end + end + + local has_devicons = pcall(require, 'nvim-web-devicons') + return { + show_file_icon = show_icons.files == 1, + show_folder_icon = show_icons.folders == 1, + show_git_icon = show_icons.git == 1, + show_folder_arrows = show_icons.folder_arrows == 1, + has_devicons = has_devicons, + icons = icons + } +end + +return M diff --git a/lua/nvim-tree/renderer/init.lua b/lua/nvim-tree/renderer/init.lua index 258541c6..08af7e7d 100644 --- a/lua/nvim-tree/renderer/init.lua +++ b/lua/nvim-tree/renderer/init.lua @@ -3,6 +3,7 @@ local utils = require'nvim-tree.utils' local view = require'nvim-tree.view' local _padding = require'nvim-tree.renderer.padding' local _help = require'nvim-tree.renderer.help' +local _icons = require'nvim-tree.renderer.icons' local api = vim.api @@ -11,7 +12,7 @@ local hl = {} local index = 0 local namespace_id = api.nvim_create_namespace('NvimTreeHighlights') -local icon_state = config.get_icon_state() +local icon_state = _icons.get_config() local should_hl_opened_files = (vim.g.nvim_tree_highlight_opened_files or 0) ~= 0 @@ -382,12 +383,12 @@ function M.draw() lines = {} hl = {} + icon_state = _icons.get_config() local show_arrows = vim.g.nvim_tree_indent_markers ~= 1 and icon_state.show_folder_icon and icon_state.show_folder_arrows _padding.reload_padding_function() - icon_state = config.get_icon_state() update_draw_data(TreeExplorer, show_arrows and 2 or 0, {}) if view.is_help_ui() then diff --git a/lua/nvim-tree/renderer/padding.lua b/lua/nvim-tree/renderer/padding.lua index e9b12c7b..480a3b7a 100644 --- a/lua/nvim-tree/renderer/padding.lua +++ b/lua/nvim-tree/renderer/padding.lua @@ -33,7 +33,7 @@ local function get_padding_indent_markers(depth, idx, tree, _, markers) end function M.reload_padding_function() - local icon_state = require'nvim-tree.config'.get_icon_state() + local icon_state = require'nvim-tree.renderer.icons'.get_config() if icon_state.show_folder_icon and icon_state.show_folder_arrows then M.get_padding = get_padding_arrows(icon_state)