Skip to content
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

Buffer grouping #56

Closed
akinsho opened this issue Mar 11, 2021 · 11 comments
Closed

Buffer grouping #56

akinsho opened this issue Mar 11, 2021 · 11 comments
Labels
enhancement New feature or request

Comments

@akinsho
Copy link
Owner

akinsho commented Mar 11, 2021

One thing I've thought a lot about since I discovered emacs' Centaur tabs is it's buffer groups I was initially against the idea since it seemed like an extra layer on top of vim that doesn't really exist natively. But with sorting added we already essentially have this layer on top of nvim 🤔 .

Anyway a first pass at this functionality would be added a way of first creating a group, then assigning buffers to a group. Visually groups would be clustered together +/- a way to separate them from each other or indicate that a buffer was in a particular group

The primary value of the groups would be visually seeing similar things clustered e.g. test files or some other dimension for sorting that isn't just an extension or directory. It would also have the benefit of allowing a user to interact with those buffers as a group

@akinsho
Copy link
Owner Author

akinsho commented Mar 11, 2021

@CooperAnderson potentially the filtering you had in your PR could be done on top of this e.g. certain groups could be assigned to specific tabs etc., thoughts?

@akinsho akinsho changed the title Tab grouping Buffer grouping Mar 11, 2021
@ahmed-rezk-dev
Copy link

Is there any updates on buffer groups?

@akinsho
Copy link
Owner Author

akinsho commented Jul 12, 2021

Nope. Haven't looked into it properly. A big part of the way centaur-tabs does it is that it can render the UI in a way that can't really be achieved in nvim so not super clear how this would look.

It's also a non-trivial amount of work I don't currently have time for.

@akinsho akinsho added the enhancement New feature or request label Jul 21, 2021
@siduck
Copy link

siduck commented Aug 20, 2021

Nope. Haven't looked into it properly. A big part of the way centaur-tabs does it is that it can render the UI in a way that can't really be achieved in nvim so not super clear how this would look.

It's also a non-trivial amount of work I don't currently have time for.

Idk if this sounds dumb lol , maybe you can use one buffer for grouping multiple buffers and pressing a mapping when you're in the group buffer will open a lil popup over the bottom of the grouped buffer and cycling within the popup ( of grouped buffer ) with a mapping.

image

@akinsho
Copy link
Owner Author

akinsho commented Aug 20, 2021

@siduck76 I've already started some work over in #179 I'm probably not going to go with the structure above since that is way more work/complexity i.e. hiding buffers and then trying to add a new secondary UI. The main way this is probably going to work will be different highlighting/icons for grouped buffers and just as a visual indicator, I might also allow some custom separator e.g. a space or divider between groups or a sort of header tab.

Anyway I'm trying to keep the initial implementation lean so won't be adding a bunch of complexity otherwise chances are I'll never finish anyway. You can have a look at that PR to see where it's at. It's not fully documented yet though

@siduck
Copy link

siduck commented Aug 20, 2021

@akinsho My friend has added a PR to NvChad (my config) which lets me hide/unhide specific terminal buffers through telescope.Idk if this would be useful but you could implement the same with hiding buffers and unhiding them through telescope or any other floating window like thing

image

image

@akinsho
Copy link
Owner Author

akinsho commented Aug 20, 2021

I'm sure I understand tbh or if it relates to this particular functionality at all really.

@G-Rowell
Copy link

Hi @akinsho & @siduck76,

I thought I might explain my implementation a little, though mine clearly has flaws with it's implementation as it is currently. (I intend to improve it after my exams soon!)

On Terminal creation,

  • set a local terminal variable term_type to indicate that this is a managed term & what kind of window the user created the term as
    • Eg: map("n", "<leader>w", ":execute 'terminal' | let b:term_type = 'wind' | startinsert <CR>", opt)
    • Eg: map("n", "<leader>v", ":execute 'vnew +terminal' | let b:term_type = 'vert' | startinsert <CR>", opt)
  • This could probably be done with an autocommand instead

This code is an edited excerpt from here


Closing buffers

There are several implementation choices to make:

  1. Re-map locally (in any term buffer), the normal close buffer keybind, to instead hide a term
  2. Re-map globally your close buffer keybind to a function, part of this function hide the term
  3. There is likely some way to hijack :bd! or similar with an autocommand?

We implemented # 2

map("n", "<leader>x", ":lua require('utils').close_buffer() <CR>", opt) -- close buffer
This code is an edited excerpt from here
This code is an edited excerpt from here


What hiding a term means

close_buffer()

      local chad_term, type = pcall(function()
         -- try to get our special buffer variable
         return vim.api.nvim_buf_get_var(buf, "term_type")
      end)

      if chad_term then
         -- this is a managed terminal!
         if type == "wind" then
            -- hide from bufferline
            vim.cmd(string.format("%d bufdo setlocal nobl", buf))
            -- swtich to another buff
            vim.cmd "BufferLineCycleNext"
         else
            -- this is instead a split window term
            local cur_win = vim.fn.winnr()
            -- we can close this window
            vim.cmd(string.format("%d wincmd c", cur_win))
            return
         end
      else
         -- normally close this buffer
         vim.cmd(string.format("bd! %d", buf))
      end

This code is an edited excerpt from here


How to bring back / show a term

As this implementation is designed to created many terms (instead of toggleterm.nvim's approach),
we now need some kind of selector to choose what term to show again.

I've decided to use a Telescope picker, as that already has many in-built tools and can live-preview buffers.

The general idea behind the picker:

  • only show our managed terminal buffers
  • only show hidden buffers
  • on selection, checks local buffer var term_type to see how best to re-show the terminal in a window
    -- for a window term, create a new window and set it to listed to show on the bufferline
    -- for a split term, create a split with the selected term
            if chad_term then
               if type == "wind" then
                  -- swtich to term buff & show in bufferline
                  vim.cmd(string.format("b %d | setlocal bl", buf))
               elseif type == "vert" then
                  vim.cmd(string.format("vsp #%d", buf))
               elseif type == "hori" then
                  vim.cmd(string.format("15 sp #%d ", buf))
               end

This code is an edited excerpt from here


I hope this helps, or is at least curious.
I'm more than happy to explain more, and it's totally understandable if you completely disagree with my implementation 😆

I am by all means new to plugin & proper NeoVim modification!

G

@akinsho
Copy link
Owner Author

akinsho commented Aug 21, 2021

@G-Rowell thanks for explaining the terminal functionality referenced above. Just to clarify, I think there's generally a bit of confusion about what this issue is about and what the related PR will do. It doesn't have anything to do with hiding buffers or toggling things or secondary UI's. The buffer group logic is a mechanism for users to specify how buffers get grouped, things like highlights and icons for a group and exposes functionality for interacting with a group e.g. all test files are grouped or all files matching some other criteria can be grouped and then closed together, or opened in splits all at once.

I don't think most of the recent comments really relate to this issue, just to be clear.

@G-Rowell
Copy link

G-Rowell commented Aug 21, 2021

@akinsho

Yeah, my apologies, I barely got any sleep last night 😢

I was mainly trying to supplement SiDuck's answer.

I'm slightly more awake now and agree, it's quite irrelevant.
Sorry, my bad 😆

Let me know if you want me to ping you if I do get bufferline grouping working

@akinsho
Copy link
Owner Author

akinsho commented Sep 9, 2021

closed by #179

@akinsho akinsho closed this as completed Sep 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants