G migration mechanism (#1030)

This commit is contained in:
Alexander Courtis 2022-03-02 05:54:12 +11:00 committed by GitHub
parent 3d8912ca53
commit 97717d8d23
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 244 additions and 43 deletions

View File

@ -601,7 +601,7 @@ INFORMATIONS *nvim-tree-info*
- <C-x> will open the file in a horizontal split
- <C-t> will open the file in a new tab
- <Tab> will open the file as a preview (keeps the cursor in the tree)
- `I` will toggle visibility of folders hidden via |g:nvim_tree_ignore|
- `I` will toggle visibility of folders hidden via |git.ignore| option
- `H` will toggle visibility of dotfiles (files/folders starting with a `.`)
- `R` will refresh the tree
- Double left click acts like <CR>

View File

@ -7,6 +7,7 @@ local renderer = require'nvim-tree.renderer'
local view = require'nvim-tree.view'
local utils = require'nvim-tree.utils'
local change_dir = require'nvim-tree.actions.change-dir'
local legacy = require'nvim-tree.legacy'
local _config = {}
@ -348,7 +349,7 @@ local DEFAULT_OPTS = {
},
filters = {
dotfiles = false,
custom_filter = {},
custom = {},
exclude = {}
},
git = {
@ -364,9 +365,7 @@ local DEFAULT_OPTS = {
open_file = {
quit_on_open = vim.g.nvim_tree_quit_on_open == 1,
window_picker = {
enable = vim.g.nvim_tree_disable_window_picker ~= 1,
chars = vim.g.nvim_tree_window_picker_chars,
exclude = vim.g.nvim_tree_window_picker_exclude,
enable = true,
}
}
},
@ -381,6 +380,8 @@ local function merge_options(conf)
end
function M.setup(conf)
legacy.migrate_legacy_options(conf)
local opts = merge_options(conf)
local netrw_disabled = opts.disable_netrw or opts.hijack_netrw

182
lua/nvim-tree/legacy.lua Normal file
View File

@ -0,0 +1,182 @@
local utils = require'nvim-tree.utils'
local M = {}
-- TODO update git.io/JPhyt when adding a migration
-- migrate the g: to o if the user has not specified that when calling setup
local migrations = {
nvim_tree_disable_netrw = function(o)
if o.disable_netrw == nil then
o.disable_netrw = vim.g.nvim_tree_disable_netrw ~= 0
end
end,
nvim_tree_hijack_netrw = function(o)
if o.hijack_netrw == nil then
o.hijack_netrw = vim.g.nvim_tree_hijack_netrw ~= 0
end
end,
nvim_tree_auto_open = function(o)
if o.open_on_setup == nil then
o.open_on_setup = vim.g.nvim_tree_auto_open ~= 0
end
end,
nvim_tree_auto_close = function(o)
if o.auto_close == nil then
o.auto_close = vim.g.nvim_tree_auto_close ~= 0
end
end,
nvim_tree_tab_open = function(o)
if o.open_on_tab == nil then
o.open_on_tab = vim.g.nvim_tree_tab_open ~= 0
end
end,
nvim_tree_update_cwd = function(o)
if o.update_cwd == nil then
o.update_cwd = vim.g.nvim_tree_update_cwd ~= 0
end
end,
nvim_tree_hijack_cursor = function(o)
if o.hijack_cursor == nil then
o.hijack_cursor = vim.g.nvim_tree_hijack_cursor ~= 0
end
end,
nvim_tree_system_open_command = function(o)
utils.table_create_missing(o, "system_open")
if o.system_open.cmd == nil then
o.system_open.cmd = vim.g.nvim_tree_system_open_command
end
end,
nvim_tree_system_open_command_args = function(o)
utils.table_create_missing(o, "system_open")
if o.system_open.args == nil then
o.system_open.args = vim.g.nvim_tree_system_open_command_args
end
end,
nvim_tree_follow = function(o)
utils.table_create_missing(o, "update_focused_file")
if o.update_focused_file.enable == nil then
o.update_focused_file.enable = vim.g.nvim_tree_follow ~= 0
end
end,
nvim_tree_follow_update_path = function(o)
utils.table_create_missing(o, "update_focused_file")
if o.update_focused_file.update_cwd == nil then
o.update_focused_file.update_cwd = vim.g.nvim_tree_follow_update_path ~= 0
end
end,
nvim_tree_lsp_diagnostics = function(o)
utils.table_create_missing(o, "diagnostics")
if o.diagnostics.enable == nil then
o.diagnostics.enable = vim.g.nvim_tree_lsp_diagnostics ~= 0
if o.diagnostics.show_on_dirs == nil then
o.diagnostics.show_on_dirs = vim.g.nvim_tree_lsp_diagnostics ~= 0
end
end
end,
nvim_tree_auto_resize = function(o)
utils.table_create_missing(o, "view")
if o.view.auto_resize == nil then
o.view.auto_resize = vim.g.nvim_tree_auto_resize ~= 0
end
end,
nvim_tree_bindings = function(o)
utils.table_create_missing(o, "view.mappings")
if o.view.mappings.list == nil then
o.view.mappings.list = vim.g.nvim_tree_bindings
end
end,
nvim_tree_disable_keybindings = function(o)
utils.table_create_missing(o, "view.mappings")
if o.view.mappings.custom_only == nil then
if vim.g.nvim_tree_disable_keybindings ~= 0 then
o.view.mappings.custom_only = true
-- specify one mapping so that defaults do not apply
o.view.mappings.list = {
{ key = "g?", action = "" },
}
end
end
end,
nvim_tree_disable_default_keybindings = function(o)
utils.table_create_missing(o, "view.mappings")
if o.view.mappings.custom_only == nil then
o.view.mappings.custom_only = vim.g.nvim_tree_disable_default_keybindings ~= 0
end
end,
nvim_tree_hide_dotfiles = function(o)
utils.table_create_missing(o, "filters")
if o.filters.dotfiles == nil then
o.filters.dotfiles = vim.g.nvim_tree_hide_dotfiles ~= 0
end
end,
nvim_tree_ignore = function(o)
utils.table_create_missing(o, "filters")
if o.filters.custom == nil then
o.filters.custom = vim.g.nvim_tree_ignore
end
end,
nvim_tree_gitignore = function(o)
utils.table_create_missing(o, "git")
if o.git.ignore == nil then
o.git.ignore = vim.g.nvim_tree_gitignore ~= 0
end
end,
nvim_tree_disable_window_picker = function(o)
utils.table_create_missing(o, "actions.open_file.window_picker")
if o.actions.open_file.window_picker.enable == nil then
o.actions.open_file.window_picker.enable = vim.g.nvim_tree_disable_window_picker == 0
end
end,
nvim_tree_window_picker_chars = function(o)
utils.table_create_missing(o, "actions.open_file.window_picker")
if o.actions.open_file.window_picker.chars == nil then
o.actions.open_file.window_picker.chars = vim.g.nvim_tree_window_picker_chars
end
end,
nvim_tree_window_picker_exclude = function(o)
utils.table_create_missing(o, "actions.open_file.window_picker")
if o.actions.open_file.window_picker.exclude == nil then
o.actions.open_file.window_picker.exclude = vim.g.nvim_tree_window_picker_exclude
end
end,
}
function M.migrate_legacy_options(opts)
local msg = nil
for g, m in pairs(migrations) do
if vim.fn.exists('g:'..g) ~= 0 then
m(opts)
msg = (msg and msg .. ", " or "Following options were moved to setup, see git.io/JPhyt: ") .. g
end
end
if msg then
require'nvim-tree.utils'.warn(msg)
end
end
return M

View File

@ -225,4 +225,60 @@ function M.canonical_path(path)
return path
end
-- Create empty sub-tables if not present
-- @param tbl to create empty inside of
-- @param sub dot separated string of sub-tables
-- @return deepest sub-table
function M.table_create_missing(tbl, sub)
if tbl == nil then
return nil
end
local t = tbl
for s in string.gmatch(sub, "([^%.]+)%.*") do
if t[s] == nil then
t[s] = {}
end
t = t[s]
end
return t
end
-- Serialise a table as a string
-- @param val table to serialise
-- @param name optional
-- @param skipnewlines optional
-- @param spaces to indent, for internal use
function M.table_tostring(val, name, skipnewlines, depth)
skipnewlines = skipnewlines or false
depth = depth or 0
local tmp = string.rep(" ", depth)
if name then
tmp = tmp .. name .. " = "
end
if type(val) == "table" then
tmp = tmp .. "{" .. (not skipnewlines and "\n" or "")
for k, v in pairs(val) do
tmp = tmp .. M.table_tostring(v, k, skipnewlines, depth + 1) .. "," .. (not skipnewlines and "\n" or "")
end
tmp = tmp .. string.rep(" ", depth) .. "}"
elseif type(val) == "number" then
tmp = tmp .. tostring(val)
elseif type(val) == "string" then
tmp = tmp .. string.format("%q", val)
elseif type(val) == "boolean" then
tmp = tmp .. tostring(val)
else
tmp = tmp .. "\"[" .. type(val) .. "]\""
end
return tmp
end
return M

View File

@ -1,38 +0,0 @@
local is_initialized = false
if is_initialized then
return
end
-- luacheck: ignore
is_initialized = true
local out_config = {
"nvim_tree_disable_netrw",
"nvim_tree_hijack_netrw",
"nvim_tree_auto_open",
"nvim_tree_auto_close",
"nvim_tree_tab_open",
"nvim_tree_update_cwd",
"nvim_tree_hijack_cursor",
"nvim_tree_system_open_command",
"nvim_tree_system_open_command_args",
"nvim_tree_follow",
"nvim_tree_follow_update_path",
"nvim_tree_lsp_diagnostics",
"nvim_tree_auto_resize",
"nvim_tree_bindings",
"nvim_tree_disable_keybindings",
"nvim_tree_disable_default_keybindings",
"nvim_tree_hide_dotfiles",
"nvim_tree_ignore",
"nvim_tree_gitignore"
}
local x = vim.tbl_filter(function(v)
return vim.fn.exists('g:'..v) ~= 0
end, out_config)
if #x > 0 then
local msg = "Following options were moved to setup, see git.io/JPhyt: "
require'nvim-tree.utils'.warn(msg..table.concat(x, ", "))
end