-
-
Notifications
You must be signed in to change notification settings - Fork 622
feat: add winid parameter for api.tree.open, toggle, find_file #2213
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
@mrjones2014 I would be most grateful if you tested this branch: open, toggle, find_file |
Here's a recipe for managing a leftmost nvim-tree above and aerial below. I'd be grateful if you could test drive / refine this @mrjones2014 It needs some tweaking to get aerial and nvim-tree playing nicely with width. --- find all winid for normal windows
--- @return number[]
local function normal_winids()
return vim.tbl_filter(function(id)
local config = vim.api.nvim_win_get_config(id)
return config and config.relative == ""
end, vim.api.nvim_list_wins())
end
--- find the first non-floating window containing a file type
--- @param ft string
--- @return number|nil winid
local function first_winid(ft)
for _, winid in ipairs(normal_winids()) do
local bufnr = vim.api.nvim_win_get_buf(winid)
if vim.bo[bufnr] and vim.bo[bufnr].filetype == ft then
return winid
end
end
end
-- open/focus nvim-tree
vim.keymap.set("n", "<space>e", function()
-- just focus tree if it's visible
if api.tree.is_visible() then
api.tree.focus()
return
end
-- scratch buffer to create the new window
local scratch_bufnr = vim.api.nvim_create_buf(false, true)
-- find the aerial window
local aerial_winid = first_winid("aerial")
local new_winid
if aerial_winid then
-- focus aerial
vim.api.nvim_set_current_win(aerial_winid)
-- create a horizontal split above
vim.cmd("aboveleft sbuffer " .. scratch_bufnr)
new_winid = vim.api.nvim_get_current_win()
else
-- create a full height leftmost vertical split
vim.cmd("topleft vertical sbuffer " .. scratch_bufnr)
new_winid = vim.api.nvim_get_current_win()
end
-- open the tree
api.tree.open({ winid = new_winid })
-- delete the scratch buffer
vim.api.nvim_buf_delete(scratch_bufnr, { force = true })
end, { noremap = true })
-- open/focus aerial
vim.keymap.set("n", "<space>j", function()
-- just focus aerial if it's visible
local aerial_winid = first_winid("aerial")
if aerial_winid then
aerial.focus()
return
end
-- source window, current
local source_winid = vim.api.nvim_get_current_win()
-- scratch buffer to create the new window
local scratch_bufnr = vim.api.nvim_create_buf(false, true)
local new_winid
if api.tree.is_visible() then
-- focus nvim-tree
api.tree.focus()
-- create a horizontal split below
vim.cmd("belowright sbuffer " .. scratch_bufnr)
new_winid = vim.api.nvim_get_current_win()
else
-- create a full height leftmost vertical split
vim.cmd("topleft vertical sbuffer " .. scratch_bufnr)
new_winid = vim.api.nvim_get_current_win()
end
-- open aerial
aerial.open_in_win(new_winid, source_winid)
-- delete the scratch buffer
vim.api.nvim_buf_delete(scratch_bufnr, { force = true })
end, { noremap = true }) |
This is almost exactly what I want 🙂 Is this something that will be built-in to nvim-tree or something? The only things I'd want different from this are:
|
No. API and a recipe are sufficient. Some convenience API could be added but needs some thought. Perhaps retrieving winid. We could add open-nvim-tree-somewhere to nvim-tree however it doesn't really help, as the user would need to write their open-something-else anyway.
You are empowered to place it wherever you like :)
I wanted that once, however my workflow now is sufficient:
I'll enable the filter depending on the nature of the project I'm working on. I tend to use lsp navigation which populates the opened files. |
fixes #2189