nvim-tree/nvim-tree.lua

add view.cursorlineopt

eeeXun opened this issue · 11 comments

I used to have

require("nvim-tree.view").View.winopts.cursorlineopt = "number"

to change cursorlineopt. Since cursorlineopt is not available in nvim-tree-opts-view.

How should I do after 0a06f65?

Apologies, View is not API and has been refactored from a singleton to instances.

We can add an option for the hardcoded cursorlineopt.

In the interests of speed I've merged the feature. Please update and advise whether it works as expected.

Yes, it works. Thx

I was using the nvim-tree.view to calculate the width of the nvim-tree explorer View in order to always offset my bufferline the same width as the nvim-tree explorer. However, after updating this morning, I'm having issues requiring that nvim-tree.view. It's telling me module not found?

local nvim_tree_events = require('nvim-tree.events')
local nvim_tree_view = require('nvim-tree.view')
local bufferline_api = require('bufferline.api')

local function get_tree_size()
  return nvim_tree_view.View.width
end

nvim_tree_events.subscribe('TreeOpen', function()
  bufferline_api.set_offset(get_tree_size())
end)

nvim_tree_events.subscribe('TreeClose', function()
  bufferline_api.set_offset(0)
end)

nvim_tree_events.subscribe('Resize', function()
  if nvim_tree_view.is_visible() then
    bufferline_api.set_offset(get_tree_size())
  else
    bufferline_api.set_offset(0)
  end
end)

Is this related to this issue? The View is no longer accessible as an API?

View was never API.

The Resize event should pass you the width as the size parameter. See :help nvim-tree-events

Is there another (new) way to get the size without an event? I had another keyboard shortcut that would also check for the window width without a resize:

map({ "n" }, "<C-h>", "<Cmd>TmuxResizeLeft<CR> | <CMD>lua require('bufferline.api').set_offset(require('nvim-tree.view').View.width)<CR>", { remap = true, silent = true, desc = 'Resize Neovim pane horizontally' })

But it looks like the code for View changed or was moved?

I tried to refactor the code I shared earlier:

local events = require('nvim-tree.events')
local api = require('nvim-tree.api')
local bufferline_api = require('bufferline.api')

api.events.subscribe(events.TreeOpen, function()
  bufferline_api.set_offset(30)
end)

api.events.subscribe(events.TreeClose, function()
  bufferline_api.set_offset(0)
end)

api.events.subscribe(events.Resize, function(size)
  bufferline_api.set_offset(size)
end)

That works without requiring View. However, now anytime I resize I get the following error:

[NvimTree] Handler for event Resize errored. ".../nvim/lazy/nvim-tree.lua/lua/nvim-tree/explorer/view.lua:566: attempt to index local 'self' (a nil value)"

I think the examples/recipes that use the require('nvim-tree.view') are going to be broken?
https://github.com/nvim-tree/nvim-tree.lua/wiki/Recipes#remember-nvimtree-window-size

Is there another (new) way to get the size without an event? I had another keyboard shortcut that would also check for the window width without a resize:

Please note Caution: API and Internal Modules

You can get the width via tree then nvim api.

  -- find the winid in the current tab  :help nvim-tree-api.tree.winid()
  local winid = api.tree.winid()
  print("winid=" .. winid)

  -- get the width  :help nvim_win_get_width
  local width = vim.api.nvim_win_get_width(winid)
  print("width=" .. width)

I think the examples/recipes that use the require('nvim-tree.view') are going to be broken?
https://github.com/nvim-tree/nvim-tree.lua/wiki/Recipes#remember-nvimtree-window-size

Thanks for finding those! I'd be most grateful if you could take the time to update those view examples.

Thanks, I got it working with the following:

local nvim_tree_events = require('nvim-tree.events')
local api = require('nvim-tree.api')
local bufferline_api = require('bufferline.api')

local function get_tree_size()
  -- Get the ID of the nvim-tree window
  local winid = api.tree.winid()
  -- Return the width of the nvim-tree window
  if (winid) then
    return vim.api.nvim_win_get_width(winid)
  else
    return 0
  end
end

-- Set the offset for bufferline when nvim-tree is opened
nvim_tree_events.subscribe('TreeOpen', function()
  bufferline_api.set_offset(30)
end)

-- Set the offset to 0 when nvim-tree is closed
nvim_tree_events.subscribe('TreeClose', function()
  bufferline_api.set_offset(0)
end)

-- Resize the bufferline offset to match the nvim-tree window width
autocmd('WinResized', {
  pattern = '*',
  callback = function()
    bufferline_api.set_offset(get_tree_size())
  end,
})

I've refactored the examples using nvim-tree.view with this similar workaround, thanks for you help. Please double-check my work. I didn't have a chance to test, but it looks right the way I updated in the wiki.

Many thanks @l00sed !

Great fix for the double toggle.