b0o / nvim-tree-preview.lua

🍃 Floating preview windows for nvim-tree
MIT License
59 stars 2 forks source link

[Bug]: `on_attach`, keymapping bugs #4

Open daUnknownCoder opened 2 months ago

daUnknownCoder commented 2 months ago

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

b0o commented 2 months ago

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.

daUnknownCoder commented 2 months ago

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
    ...
b0o commented 2 months ago

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),
daUnknownCoder commented 2 months ago

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

👍🏻

b0o commented 2 months ago

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

Can you elaborate on this or share a screencast?