Open Nvim-Tree with multiple options #2751
Replies: 2 comments 2 replies
-
You can definitely do this - using API is preferable to a command as it allows you to specify the behaviour you need. Your configuration looks very rich and well thought out and your What exactly is not working?
These modules are not API and should not be used as they are internal and subject to change at any time. Please use Take a look at |
Beta Was this translation helpful? Give feedback.
-
its my neovim distro, which i plan to publicize once there are some startup improvements: https://github.com/daUnknownCoder/NeutronVim i am not really great at
idk how to put this, but i dont really know how to do it, so if you can it will be great, after i saw the i reaaly wanna replace the
local api = require("nvim-tree.api")
local openfile = require("nvim-tree.actions.node.open-file")
local actions = require("telescope.actions")
local action_state = require("telescope.actions.state")
local M = {}
local view_selection = function(prompt_bufnr, map)
actions.select_default:replace(function()
actions.close(prompt_bufnr)
local selection = action_state.get_selected_entry()
local filename = selection.filename
if filename == nil then
filename = selection[1]
end
openfile.fn("preview", filename)
end)
return true
end
function M.launch_live_grep(opts)
return M.launch_telescope("live_grep", opts)
end
function M.launch_find_files(opts)
return M.launch_telescope("find_files", opts)
end
function M.launch_telescope(func_name, opts)
local telescope_status_ok, _ = pcall(require, "telescope")
if not telescope_status_ok then
return
end
local node = api.tree.get_node_under_cursor()
local is_folder = node.fs_stat and node.fs_stat.type == "directory" or false
local basedir = is_folder and node.absolute_path or vim.fn.fnamemodify(node.absolute_path, ":h")
if node.name == ".." and TreeExplorer ~= nil then
basedir = TreeExplorer.cwd
end
opts = opts or {}
opts.cwd = basedir
opts.search_dirs = { basedir }
opts.attach_mappings = view_selection
return require("telescope.builtin")[func_name](opts)
end
function M.stage_file()
local node = api.tree.get_node_under_cursor()
local gs = node.git_status.file
-- If the current node is a directory get children status
if gs == nil then
gs = (node.git_status.dir.direct ~= nil and node.git_status.dir.direct[1])
or (node.git_status.dir.indirect ~= nil and node.git_status.dir.indirect[1])
end
-- If the file is untracked, unstaged or partially staged, we stage it
if gs == "??" or gs == "MM" or gs == "AM" or gs == " M" then
vim.cmd("silent !git add " .. node.absolute_path)
-- If the file is staged, we unstage
elseif gs == "M " or gs == "A " then
vim.cmd("silent !git restore --staged " .. node.absolute_path)
end
api.tree.reload()
end
function M.open_nvim_tree(data)
-- buffer is a real file on the disk
local real_file = vim.fn.filereadable(data.file) == 1
-- buffer is a [No Name]
local no_name = data.file == "" and vim.bo[data.buf].buftype == ""
if not real_file and not no_name then
return
end
-- open the tree, find the file but don't focus it
require("nvim-tree.api").tree.toggle({ focus = false, find_file = true })
end
return M this was all copied from all over the wiki i want to add this: local function open_nvim_tree(data)
-- buffer is a real file on the disk
local real_file = vim.fn.filereadable(data.file) == 1
-- buffer is a [No Name]
local no_name = data.file == "" and vim.bo[data.buf].buftype == ""
if not real_file and not no_name then
return
end
-- open the tree, find the file but don't focus it
require("nvim-tree.api").tree.toggle({ focus = false, find_file = true, })
end and any1 of the below two idk whats the diff or maybe both of them (current/ new window) local function open_nvim_tree(data)
-- buffer is a directory
local directory = vim.fn.isdirectory(data.file) == 1
if not directory then
return
end
-- change to the directory
vim.cmd.cd(data.file)
-- open the tree
require("nvim-tree.api").tree.open()
end or local function open_nvim_tree(data)
-- buffer is a directory
local directory = vim.fn.isdirectory(data.file) == 1
if not directory then
return
end
-- create a new, empty buffer
vim.cmd.enew()
-- wipe the directory buffer
vim.cmd.bw(data.buf)
-- change to the directory
vim.cmd.cd(data.file)
-- open the tree
require("nvim-tree.api").tree.open()
end i might have also tried a few times, but i always fail at the |
Beta Was this translation helpful? Give feedback.
-
Right Now, im editing my config, and wanted to know real quick, if i can use a few opts from Nvim-Tree's wiki of
Open On Startup
?rn, i want these:
No name Buffers
Open for current/new window
so what i want to use is a single
open_nvim_tree(data)
function instead ofNvimTreeFindFileToggle
current config:
treeutils.lua
Beta Was this translation helpful? Give feedback.
All reactions