Skip to content

feat: add winid parameter for api.tree.open, toggle, find_file #2213

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 3 commits into from
May 21, 2023

Conversation

alex-courtis
Copy link
Member

fixes #2189

@alex-courtis
Copy link
Member Author

@mrjones2014 I would be most grateful if you tested this branch: open, toggle, find_file

@alex-courtis
Copy link
Member Author

alex-courtis commented May 14, 2023

Here's a recipe for managing a leftmost nvim-tree above and aerial below.

I'd be grateful if you could test drive / refine this @mrjones2014

It needs some tweaking to get aerial and nvim-tree playing nicely with width.

--- find all winid for normal windows
--- @return number[]
local function normal_winids()
  return vim.tbl_filter(function(id)
    local config = vim.api.nvim_win_get_config(id)
    return config and config.relative == ""
  end, vim.api.nvim_list_wins())
end

--- find the first non-floating window containing a file type
--- @param ft string
--- @return number|nil winid
local function first_winid(ft)
  for _, winid in ipairs(normal_winids()) do
    local bufnr = vim.api.nvim_win_get_buf(winid)
    if vim.bo[bufnr] and vim.bo[bufnr].filetype == ft then
      return winid
    end
  end
end

-- open/focus nvim-tree
vim.keymap.set("n", "<space>e", function()
  -- just focus tree if it's visible
  if api.tree.is_visible() then
    api.tree.focus()
    return
  end

  -- scratch buffer to create the new window
  local scratch_bufnr = vim.api.nvim_create_buf(false, true)

  -- find the aerial window
  local aerial_winid = first_winid("aerial")
  local new_winid
  if aerial_winid then
    -- focus aerial
    vim.api.nvim_set_current_win(aerial_winid)

    -- create a horizontal split above
    vim.cmd("aboveleft sbuffer " .. scratch_bufnr)
    new_winid = vim.api.nvim_get_current_win()
  else
    -- create a full height leftmost vertical split
    vim.cmd("topleft vertical sbuffer " .. scratch_bufnr)
    new_winid = vim.api.nvim_get_current_win()
  end

  -- open the tree
  api.tree.open({ winid = new_winid })

  -- delete the scratch buffer
  vim.api.nvim_buf_delete(scratch_bufnr, { force = true })
end, { noremap = true })

-- open/focus aerial
vim.keymap.set("n", "<space>j", function()
  -- just focus aerial if it's visible
  local aerial_winid = first_winid("aerial")
  if aerial_winid then
    aerial.focus()
    return
  end

  -- source window, current
  local source_winid = vim.api.nvim_get_current_win()

  -- scratch buffer to create the new window
  local scratch_bufnr = vim.api.nvim_create_buf(false, true)

  local new_winid
  if api.tree.is_visible() then
    -- focus nvim-tree
    api.tree.focus()

    -- create a horizontal split below
    vim.cmd("belowright sbuffer " .. scratch_bufnr)
    new_winid = vim.api.nvim_get_current_win()
  else
    -- create a full height leftmost vertical split
    vim.cmd("topleft vertical sbuffer " .. scratch_bufnr)
    new_winid = vim.api.nvim_get_current_win()
  end

  -- open aerial
  aerial.open_in_win(new_winid, source_winid)

  -- delete the scratch buffer
  vim.api.nvim_buf_delete(scratch_bufnr, { force = true })
end, { noremap = true })

@mrjones2014
Copy link
Collaborator

This is almost exactly what I want 🙂 Is this something that will be built-in to nvim-tree or something?

The only things I'd want different from this are:

  • I'd want it on the right side instead of left
  • I'd also want a separate buffer list component above nvim-tree

@alex-courtis
Copy link
Member Author

This is almost exactly what I want slightly_smiling_face Is this something that will be built-in to nvim-tree or something?

No. API and a recipe are sufficient. Some convenience API could be added but needs some thought. Perhaps retrieving winid.

We could add open-nvim-tree-somewhere to nvim-tree however it doesn't really help, as the user would need to write their open-something-else anyway.

The only things I'd want different from this are:

* I'd want it on the right side instead of left

You are empowered to place it wherever you like :)

* I'd also want a separate buffer list component above nvim-tree

I wanted that once, however my workflow now is sufficient:

  • nvim-tree.renderer.highlight_opened_files
  • "Next/Prev Opened" api.node.navigate.opened.next api.node.navigate.opened.prev
  • "Toggle No Buffer" api.tree.toggle_no_buffer_filter

I'll enable the filter depending on the nature of the project I'm working on. I tend to use lsp navigation which populates the opened files.

@alex-courtis alex-courtis merged commit b1e074d into master May 21, 2023
@alex-courtis alex-courtis deleted the 2189-api.tree.open-target_window branch May 21, 2023 07:37
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.

api.tree.open target_window
2 participants