nvim-tree.lua icon indicating copy to clipboard operation
nvim-tree.lua copied to clipboard

How to open file/directory with a single mouse-click?

Open astier opened this issue 4 years ago • 8 comments
trafficstars

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") },
      }
    }
  }
}

astier avatar Oct 18 '21 03:10 astier

issue with only left mouse is that when clicking, the correct line is not focused. That's why double click is needed.

kyazdani42 avatar Oct 23 '21 14:10 kyazdani42

I understand. Therefore the correct line should first be focused an than the node opened. Its possible in netrw and nerdtree.

astier avatar Oct 23 '21 15:10 astier

@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.

atishay avatar Jan 23 '22 07:01 atishay

I'll see what i can do about this :) If someone want's to open a PR, it would be gladly welcome too.

kyazdani42 avatar Feb 03 '22 12:02 kyazdani42

@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?

danielo515 avatar Jun 07 '22 07:06 danielo515

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.

atishay avatar Jun 07 '22 12:06 atishay

That may explain my problem. Thanks for letting me know (and for linking the issue when you find it)

danielo515 avatar Jun 07 '22 16:06 danielo515

https://github.com/neovim/neovim/issues/14921

atishay avatar Jun 20 '22 10:06 atishay

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},
      }
    }
  }
});

atishay avatar Oct 22 '22 06:10 atishay

LeftRelease

Very nice! Added to :help nvim-tree-mappings.

alex-courtis avatar Oct 22 '22 23:10 alex-courtis

Closing as we have a valid workaround.

alex-courtis avatar Oct 22 '22 23:10 alex-courtis

For people coming here:

Setting your own mapping in the configuration will soon be deprecated, see nvim-tree.on_attach for experimental replacement.

louwers avatar Nov 24 '22 11:11 louwers

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?

danielo515 avatar Nov 24 '22 18:11 danielo515

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 for full details.

alex-courtis avatar Nov 26 '22 01:11 alex-courtis

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: @.***>

danielo515 avatar Nov 27 '22 15:11 danielo515

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```

ZeChArtiahSaher avatar Oct 26 '23 19:10 ZeChArtiahSaher