nvim-tree.lua
nvim-tree.lua copied to clipboard
How to open file/directory with a single mouse-click?
Right now 2-leftmouse is required to open a file/directory. I tried mapping to just leftmouse but it doesn't work properly. leftmouse does open the directory/file under the cursor with a single clock but it doesn't move the cursor to the file/directory I actually clicked.
local tree_cb = require'nvim-tree.config'.nvim_tree_callback
require'nvim-tree'.setup {
view = {
mappings = {
list = {
{ key = { '<leftmouse>' }, cb = tree_cb("edit") },
}
}
}
}
issue with only left mouse is that when clicking, the correct line is not focused. That's why double click is needed.
I understand. Therefore the correct line should first be focused an than the node opened. Its possible in netrw and nerdtree.
@kyazdani42 I have been able to achieve single mouse click using the following code:
local function action_callback_for_tree(node)
vim.cmd [[
execute "normal! \<LeftMouse>"
]]
local lib = require'nvim-tree.lib'
local node = lib.get_node_at_cursor()
if not node then return end
print(node.absolute_path)
end
The idea was copied from https://github.com/preservim/nerdtree/blob/81f3eaba295b3fceb2d032db57e5eae99ae480f8/autoload/nerdtree/ui_glue.vim#L387
You can use the execute approach for all mouse events to make them possible on a single click.
I'll see what i can do about this :) If someone want's to open a PR, it would be gladly welcome too.
@atishay can you give more details in how to use that? Should I add it as a <LeftMouse> mapping for nvim-tree? Do I need to disable the <2-LeftMouse> one?
There was a focus bug in nvim. It was able to show the menu with one click but the focus needed to be on the right pane. The concept works in vim. I will like the nvim issue to this thread once I find it.
That may explain my problem. Thanks for letting me know (and for linking the issue when you find it)
https://github.com/neovim/neovim/issues/14921
Got this to work in nvim 0.8
local function single_click_edit(node)
vim.defer_fn(function ()
local win = vim.api.nvim_get_current_win()
local view = require "nvim-tree.view"
if view.get_winnr() ~= win then return end
local actions = require'nvim-tree.actions.dispatch'
actions.dispatch("edit")
end, 10)
end
require('nvim-tree').setup({
view = {
mappings = {
list = {
{ key = "<LeftRelease>", action= "single_click_edit", action_cb = single_click_edit},
}
}
}
});
LeftRelease
Very nice! Added to :help nvim-tree-mappings.
Closing as we have a valid workaround.
For people coming here:
Setting your own mapping in the configuration will soon be deprecated, see
nvim-tree.on_attachfor experimental replacement.
For people coming here:
Setting your own mapping in the configuration will soon be deprecated, see
nvim-tree.on_attachfor experimental replacement.
Deprecated and substituted with an experimental API?
For people coming here:
Setting your own mapping in the configuration will soon be deprecated, see
nvim-tree.on_attachfor experimental replacement.Deprecated and substituted with an experimental API?
The transition will be seamless, with no action needed or expected from users:
- existing mappings will continue to work
- an optional mechanism to automatically generate and migrate your mappings to
on_attachwill be available - comprehensive transition documentation will be available
See #1579 for full details.
Thanks
El sáb., 26 nov. 2022 2:46, Alexander Courtis @.***> escribió:
For people coming here:
Setting your own mapping in the configuration will soon be deprecated, see nvim-tree.on_attach for experimental replacement.
Deprecated and substituted with an experimental API?
The transition will be seamless, with no action needed or expected from users:
- existing mappings will continue to work
- an optional mechanism to automatically generate and migrate your mappings to on_attach will be available
- comprehensive transition documentation will be available
See #1579 https://github.com/nvim-tree/nvim-tree.lua/pull/1579 for full details.
— Reply to this email directly, view it on GitHub https://github.com/nvim-tree/nvim-tree.lua/issues/731#issuecomment-1327956065, or unsubscribe https://github.com/notifications/unsubscribe-auth/AARKJWNXJ6P7AOGO75G53G3WKFTWLANCNFSM5GFSHYDQ . You are receiving this because you commented.Message ID: @.***>
How do you do it now? The mapping config thing's changed ;'(.
*edit:
Nevermind:
local function my_on_attach(bufnr)
local api = require "nvim-tree.api"
vim.keymap.set('n', '<LeftRelease>', function()
local api = require('nvim-tree.api')
local node = api.tree.get_node_under_cursor()
if node.nodes ~= nil then
api.node.open.edit()
end
end, {})
end```