Skip to content

update PreviewImageNvimTree function to work with updated nvim-tree #20

@MarGustafsson

Description

@MarGustafsson

Hi, i installed the plugin and noticed that i got only errors when trying it out in nvim-tree.

After some digging through the nvim-tree docs, I noticed that the command get_node_at_cursor() was no longer present, however there was a command called get_node_under_cursor() which seemed to do the same thing.

I tested it out in my local keymaps file just to see if it would work, and it did, here is the example of what i tested:

nnoremap("<leader>P", function ()
  local nvimtree = require("nvim-tree.api")
  local img_preview = require("image_preview")
  local path = nvimtree.tree.get_node_under_cursor().absolute_path
  img_preview.PreviewImage(path)
end, { desc = "Preview image under cursor (NvimTree)" })

nnoremap is just a personal helper that sets up the keybind for normal mode btw.

So i think the plugin should work for nvim-tree again with just 2 changes, here is what i think the PreviewImageNvimTree function should look like instead:

function M.PreviewImageNvimTree()
-  local use, imported = pcall(require, "nvim-tree.lib")
+  local use, imported = pcall(require, "nvim-tree.api")
    if use then
-      local absolutePath = imported.get_node_at_cursor().absolute_path
+      local absolutePath = imported.tree.get_node_under_cursor().absolute_path
        M.PreviewImage(absolutePath)
    else
        return ''
    end
end

I didn't have time to make a proper pull request, but if you ever get to it i hope this issue helps!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions