Skip to content

refactor(#1645): remove unused open_replacing_current_buffer #2570

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 9, 2023

Conversation

alex-courtis
Copy link
Member

discovered via #1645

@alex-courtis alex-courtis force-pushed the 1645-remove-open_replacing_current_buffer branch from 5808f35 to e40cc70 Compare December 9, 2023 01:18
@alex-courtis alex-courtis merged commit 27e66c2 into master Dec 9, 2023
@alex-courtis alex-courtis deleted the 1645-remove-open_replacing_current_buffer branch December 9, 2023 01:18
@usachev63
Copy link
Contributor

lmao, this broke my config since I mapped open_replacing_current_buffer on my key. Is there any quick workaround to get the same functionality?

@alex-courtis
Copy link
Member Author

See :help nvim-tree-api.node.open.replace_tree_buffer

@steliyan
Copy link

My workflow is broken as well, but currently, I don't believe it's supported.

  1. Open a file
  2. Use open_replacing_current_buffer (now removed)
  3. Nvim-tree opens in the current buffer
  4. Select a new file
  5. Press ENTER
  6. The new file is opened

Step 3 is not supported. What is described in the docs is step 4 onwards, but this re-arranges the windows in neovim which is not great.

If NvimTreeFindFile can use the current window, this will cover my (and probably @sharkov63 ) use cases.

@javiertury
Copy link
Contributor

javiertury commented Dec 20, 2023

I'm on the same boat as @sharkov63 and @steliyan, but I have to agree that the maintenance burden may not be worth it.

My motivation for introducing the function open_replacing_current_buffer was to include in the stable api a utility that allows replicating netrw behavior. The workaround is to re-define the function ourselves, which requires keeping up-to-date with changes in nvim-tree internals.

local view = require "nvim-tree.view"
local core = require "nvim-tree.core"

local function openReplacingCurrentBuffer(cwd)
  if view.is_visible() then
    return
  end

  local buf = vim.api.nvim_get_current_buf()
  local bufname = vim.api.nvim_buf_get_name(buf)

  if cwd == "" or cwd == nil then
    if bufname ~= "" and vim.loop.fs_stat(bufname) ~= nil then
      cwd = vim.fn.fnamemodify(bufname, ":p:h")
    else
      return
    end
  end

  if not core.get_explorer() or cwd ~= core.get_cwd() then
    core.init(cwd)
  end
  view.open_in_win { hijack_current_buf = false, resize = false }
  require("nvim-tree.renderer").draw()
  require("nvim-tree.actions.finders.find-file").fn(bufname)
end

-- Then use it for whatever purpose you see fit, for instance reimplementing netrw functionality

local function openInPlace(cwd)
  if cwd == "" or cwd == nil then
    local buf = vim.api.nvim_get_current_buf()
    local bufname = vim.api.nvim_buf_get_name(buf)

    if bufname ~= "" and vim.loop.fs_stat(bufname) ~= nil then
      cwd = vim.fn.fnamemodify(bufname, ":p:h")
    else
      cwd = vim.loop.cwd()
    end
  end

  openReplacingCurrentBuffer(cwd)
end

--
-- Commands
--

vim.api.nvim_create_user_command("Explore", function(res)
  openInPlace(res.args)
end, {
  desc = 'Open file explorer in current buffer',
  nargs = "?", complete = "dir"
})

@gegoune
Copy link
Collaborator

gegoune commented Dec 20, 2023

And why wouldn't you use provided api (#2570 (comment))?

@javiertury
Copy link
Contributor

@gegoune, that api function doesn't seem to work for my usecase. It requires nvim-tree to already be present in the buffer, and when you call replace_tree_buffer, it opens the selected file in the same buffer.

My goal is to replicate netrw functionality. I want to open a nvim-tree window even if it didn't exist and use the provided argument as the working directory for nvim-tree. But I don't want to open any file.

@gegoune
Copy link
Collaborator

gegoune commented Dec 20, 2023

You can use :h nvim-tree-api.tree.open() for that.

@javiertury
Copy link
Contributor

Thank you @gegoune, that is exactly what I needed!

@usachev63
Copy link
Contributor

You can use :h nvim-tree-api.tree.open() for that.

This is what I was looking for, thank you very much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants