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

[Bug]: `on_attach`, keymapping bugs

Open daUnknownCoder opened this issue 1 year ago • 5 comments

im not able to use the nvim-tree on_attach properly,

        on_attach = function(bufnr)
          vim.keymap.set("n", "P", "<cmd>lua require('nvim-tree-preview').watch()<CR>", { desc = "Preview (Watch)" })
          vim.keymap.set(
            "n",
            "<Esc>",
            "<cmd>lua require('nvim-tree-preview').unwatch()<CR>",
            { desc = "Close Preview/Unwatch" }
          )
          vim.keymap.set("n", "<Tab>", function()
            local ok, node = pcall(require("nvim-tree.api").tree.get_node_under_cursor)
            if ok and node then
              if node.type == "directory" then
                require("nvim-tree.api").node.open.edit()
              else
                require("nvim-tree-preview").node(node, { toggle_focus = true })
              end
            end
          end, { desc = "Preview" })
        end,

so what happens is my keymaps are getting switched like my <Esc> is mapped to :nohl which gets remapped to unwatch() even when nvim-tree is not on so idk but on attach doesnt work good for me so if you can help me...

and there's this thing: i use nvimtree in a centered floating window and the preview comes below the nvim-tree layer idk why: image so if it can come adjacent to my nvim-tree buffer and that too on top of it it will be good

daUnknownCoder avatar Jun 07 '24 11:06 daUnknownCoder

Hey there! I just updated the README to hopefully improve the setup instructions. I think if you change your config as follows, it should fix the issue with the keymappings:

  on_attach = function(bufnr)
    local api = require('nvim-tree.api')
    local preview = require('nvim-tree-preview')

    local function opts(desc)
      return { desc = 'nvim-tree: ' .. desc, buffer = bufnr, noremap = true, silent = true, nowait = true }
    end

    vim.keymap.set('n', 'P', preview.watch, opts 'Preview (Watch)')
    vim.keymap.set('n', '<Esc>', preview.unwatch, opts 'Close Preview/Unwatch')

    vim.keymap.set('n', '<Tab>', function()
      local ok, node = pcall(api.tree.get_node_under_cursor)
      if ok and node then
        if node.type == "directory" then
          api.node.open.edit()
        else
          preview.node(node, { toggle_focus = true })
        end
      end
    end, opts 'Preview')
  end,

As for why the preview is showing below the nvim-tree window, I just pushed a commit (23b00821d226a30ab80d66ea5457da916cf267b4) that makes the preview window's zindex configurable, and I also increased it from the default of 50 up to 100. Can you test again to see if it shows up on top? If not, try increasing it to 150 or 200 to see if that fixes it.

b0o avatar Jun 07 '24 14:06 b0o

It should fix the issue with the keymappings

It fixed the issue for your plugin keymappings, but since i use float, i have to use float.quit_on_focus_loss = false which comes with consequences like after opening a file to edit, the nvim-tree has to be closed again by the keymap or focus the tree and then q it

and yeah i m not able to use <C-t>, C-v, C-h,

and if possible, i want to close the preview thing with q but if i map it to q, then it wont close my normal nvim-tree buffer tho i tried this without luck:

          vim.keymap.set("n", "q", function()
            local ok, node = pcall(api.tree.get_node_under_cursor)
            if ok and node then
              api.tree.close()
            else
              preview.unwatch()
            end
          end, opts("Close Preview/Close Nvim-Tree"))

Can you test again to see if it shows up on top?

Yes it did, ~arigato~ Thank you

This is how im using this plugin:

return {
  {
    "nvim-tree/nvim-tree.lua",
    cmd = "NvimTreeFindFileToggle",
    lazy = false,
    dependencies = {
      { "b0o/nvim-tree-preview.lua", lazy = true, config = true },
    },
    ...
    config = function()
      nvimtree.setup({
        ...
        on_attach = function(bufnr)
          local api = require("nvim-tree.api")
          local preview = require("nvim-tree-preview")

          local function opts(desc)
            return { desc = "NvimTree " .. desc, buffer = bufnr, noremap = true, silent = true, nowait = true }
          end

          -- Default Mappings
          api.config.mappings.default_on_attach(bufnr)

          -- Nvim-Tree Preview Plugin Mappings
          vim.keymap.set("n", "P", preview.watch, opts("Preview (Watch)"))
          vim.keymap.set("n", "<Esc>", preview.unwatch, opts("Close Preview/Unwatch"))
          vim.keymap.set("n", "q", function()
            local ok, node = pcall(api.tree.get_node_under_cursor)
            if ok and node then
              api.tree.close()
            else
              preview.unwatch()
            end
          end, opts("Close Preview/Unwatch"))
          vim.keymap.set("n", "<Tab>", function()
            local ok, node = pcall(api.tree.get_node_under_cursor)
            if ok and node then
              if node.type == "directory" then
                api.node.open.edit()
              else
                preview.node(node, { toggle_focus = true })
              end
            end
          end, opts("Preview"))
        end,
      })
    end
    ...

daUnknownCoder avatar Jun 07 '24 15:06 daUnknownCoder

I added two new API functions: preview.is_open() and preview.is_focused(). After you update the plugin, I think you should be able to use is_open() to accomplish this:

on_attach = function(bufnr)
  -- ...
  vim.keymap.set('n', 'q', function()
    if preview.is_watching() or preview.is_open() then
      preview.unwatch()
    else
      api.tree.close()
    end
  end, opts 'Close Preview/Unwatch')
  -- ...
end),

b0o avatar Jun 07 '24 16:06 b0o

unsolved stuff:

but since i use float, i have to use float.quit_on_focus_loss = false which comes with consequences like after opening a file to edit, the nvim-tree has to be closed again by the keymap or focus the tree and then q it

i m not able to use <C-t>, <C-v>, <C-h>

solved stuff:

After you update the plugin, I think you should be able to use is_open() to accomplish this

👍🏻

daUnknownCoder avatar Jun 08 '24 09:06 daUnknownCoder

i m not able to use <C-t>, <C-v>, <C-h>

Can you elaborate on this or share a screencast?

b0o avatar Jun 08 '24 10:06 b0o