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

shift buffer to left / right ( tabufline ) #8

Closed
kola-web opened this issue Aug 12, 2022 · 41 comments
Closed

shift buffer to left / right ( tabufline ) #8

kola-web opened this issue Aug 12, 2022 · 41 comments

Comments

@kola-web
Copy link

Is your feature request related to a problem? Please describe.
A clear and concise description of what the problem was.

Describe the solution you'd like
Like bufferline.nvim's BufferLineMoveNext & BufferLineMovePrev commands

Describe alternatives you've considered
A clear and concise description of any alternative solutions or features you've considered.

Additional context
Add any other context or screenshots about the feature request here.

Screenshot
Maybe a screenshot of the feature

@siduck siduck changed the title Move the buffer left and right shift buffer to left / right ( tabufline ) Aug 12, 2022
@siduck siduck transferred this issue from NvChad/NvChad Aug 12, 2022
@siduck
Copy link
Member

siduck commented Aug 25, 2022

ok i'll add some functions for this in sept

@kola-web
Copy link
Author

Thank you, I am willing to actively cooperate with the test

@kola-web
Copy link
Author

image
I saw this in an update today, very pleasant surprise

@siduck
Copy link
Member

siduck commented Aug 25, 2022

Thank you, I am willing to actively cooperate with the test

or you can make your own functions for this :))

@siduck
Copy link
Member

siduck commented Aug 25, 2022

image

current tab buffer numbers are stored in a table called vim.t.bufs

@siduck
Copy link
Member

siduck commented Aug 25, 2022

So in the above image, init.lua is at index 1 , stylua at index 2 and so on.

gitignore file's buffer name is 5

@siduck
Copy link
Member

siduck commented Aug 25, 2022

local move_buf_left = function()
  local bufs = vim.t.bufs

  for index, bufnr in ipairs(bufs) do

    if bufnr == vim.api.nvim_get_current_buf() then
      if index == 1 then
        bufs[1] = bufs[#bufs]
        bufs[#bufs] = bufnr
      else
        bufs[index] = bufs[index - 1]
        bufs[index - 1] = bufnr
      end

      break
    end
  end

  vim.t.bufs = bufs
  vim.cmd "redrawtabline"
end

make a command out of it & test it

@siduck
Copy link
Member

siduck commented Aug 25, 2022

i will clean the function later

@kola-web
Copy link
Author

ok,I'll give it a try

@kola-web
Copy link
Author

vim.t.bufs not fond

@siduck
Copy link
Member

siduck commented Aug 30, 2022

vim.t.bufs not fond

do you use nvchad's tabufline? and what did you do till now

@kola-web
Copy link
Author

I bound it to a keymap

@kola-web
Copy link
Author

I will study again

@siduck
Copy link
Member

siduck commented Aug 30, 2022

vim.t.bufs not fond

impossible

imageimage

@kola-web
Copy link
Author

kola-web commented Aug 30, 2022

Can be used normally

local M = {}

function moveAllEleTest(originTbl, n)
	if type(originTbl) ~= "table" then
		return
	end
	if #originTbl == 0 then
		return
	end

	local newTbl = {}
	for i = 1, #originTbl do
		newTbl[i] = originTbl[(n - 1 + i) % #originTbl + 1]
	end
	return newTbl
end

M.move_buf_left = function()
	local bufs = vim.t.bufs

	bufs = moveAllEleTest(bufs, 1)

	vim.t.bufs = bufs
	vim.cmd("redrawtabline")
end

M.move_buf_right = function()
	local bufs = vim.t.bufs
	bufs = moveAllEleTest(bufs, -1)

	vim.t.bufs = bufs
	vim.cmd("redrawtabline")
end

return M

@siduck
Copy link
Member

siduck commented Aug 30, 2022

hmm then if it works for u, i'll close this issue.

@siduck siduck closed this as completed Aug 30, 2022
@kola-web
Copy link
Author

ok

@kola-web
Copy link
Author

thank you

@kola-web
Copy link
Author

local M = {}

function moveItemRight(originTbl, n)
	local index = nil
	for k, v in ipairs(originTbl) do
    print(v)
		if v == n then
			index = k
		end
	end
	table.remove(originTbl, index)
	if index == #originTbl + 1 then
		table.insert(originTbl, 1, n)
	else
		table.insert(originTbl, index + 1, n)
	end
  return originTbl
end

function moveItemLeft(originTbl, n)
	local index = nil
	for k, v in ipairs(originTbl) do
		if v == n then
			index = k
		end
	end
	table.remove(originTbl, index)
	if index == 1 then
		table.insert(originTbl, #originTbl + 1, n)
	else
		table.insert(originTbl, index - 1, n)
	end
  return originTbl
end

M.move_buf_left = function()
	local bufs = vim.t.bufs
	bufs = moveItemLeft(bufs, vim.api.nvim_get_current_buf())
	vim.t.bufs = bufs
	vim.cmd("redrawtabline")
end

M.move_buf_right = function()
	local bufs = vim.t.bufs
	bufs = moveItemRight(bufs, vim.api.nvim_get_current_buf())
	vim.t.bufs = bufs
	vim.cmd("redrawtabline")
end

return M

@siduck
Copy link
Member

siduck commented Sep 18, 2022

nice!

@martin-braun
Copy link

martin-braun commented Oct 11, 2022

@siduck I think it would be nice to implement @kola-web's implementation right into NvChad/ui, so it's easier to map this functionality in the own custom tabufline mappings.

(I can make a PR, if you like, let me know. I would also like to suggest to re-open this issue for the implementation of such)

@siduck
Copy link
Member

siduck commented Oct 11, 2022

@martin-braun agreed, however i'll try to make the function small, i'll push today

@martin-braun
Copy link

@siduck it's not on main yet, is it?

@siduck
Copy link
Member

siduck commented Oct 27, 2022

@siduck it's not on main yet, is it?

oh oops! i totally forgot about it, i'll push it in main tomorrow. A lil busy these days!

@martin-braun
Copy link

@siduck No worries :) Thanks a lot!

@siduck
Copy link
Member

siduck commented Oct 30, 2022

really sorry for the delay, its been added now!

954a07f

@martin-braun @kola-web

@martin-braun
Copy link

@siduck Thanks, however, the logic still misbehaves on the edges when trying to move further after it's rolling over:

M.tabufline = {
    n = {
        ["<C-j>"] = {
            function()
                require("nvchad_ui.tabufline").tabuflineNext()
            end, "goto next buffer",
        },
        ["<C-S-j>"] = {
            function()
                require("nvchad_ui.tabufline").move_buf(1)
            end, "move buffer forward",
        },
        ["<C-k>"] = {
            function()
                require("nvchad_ui.tabufline").tabuflinePrev()
            end,
            "goto prev buffer",
        },
        ["<C-S-k>"] = {
            function()
                require("nvchad_ui.tabufline").move_buf(-1)
            end,
            "move buffer backwards",
        },
        ["<leader>c"] = {
            function()
                require("nvchad_ui.tabufline").close_buffer()
            end,
            "close buffer",
        },
    },
}

2022-10-30 2 17 23 PM

You cannot just do if i == 1 or i == #bufs then and move it to a different side, because you have to take the direction into consideration.

Instead use if n < 0 and i == 1 or n > 0 and i == #bufs then and it will work for -1 and 1.

However, if you choose to provide a variable n as an input, you have to account for things like -2 or less and 2 or more on the edges, so simply moving it to the opposite side would not behave correctly.

My suggestion was to simply implement two functions instead (tabuflineMoveNext() and tabuflineMovePrev()). Then you would not face this inconsistency at all and it would even play nicely with tabuflineNext() and tabuflinePrev() when it comes to name consistencies.

Please tell me what you think. :)

@siduck
Copy link
Member

siduck commented Oct 30, 2022

@martin-braun hmm i thought it was normal to swap out indexes 1 and the last value. I made this function based on this function #8 (comment)

@kola-web
Copy link
Author

kola-web commented Nov 9, 2022

@siduck @martin-braun This is the move method I'm using now, I don't know how to make this streamlined without this problem

local M = {}

function moveItemRight(originTbl, n)
	local index = nil
	for k, v in ipairs(originTbl) do
    print(v)
		if v == n then
			index = k
		end
	end
	table.remove(originTbl, index)
	if index == #originTbl + 1 then
		table.insert(originTbl, 1, n)
	else
		table.insert(originTbl, index + 1, n)
	end
  return originTbl
end

function moveItemLeft(originTbl, n)
	local index = nil
	for k, v in ipairs(originTbl) do
		if v == n then
			index = k
		end
	end
	table.remove(originTbl, index)
	if index == 1 then
		table.insert(originTbl, #originTbl + 1, n)
	else
		table.insert(originTbl, index - 1, n)
	end
  return originTbl
end

M.move_buf_left = function()
	local bufs = vim.t.bufs
	bufs = moveItemLeft(bufs, vim.api.nvim_get_current_buf())
	vim.t.bufs = bufs
	vim.cmd("redrawtabline")
end

M.move_buf_right = function()
	local bufs = vim.t.bufs
	bufs = moveItemRight(bufs, vim.api.nvim_get_current_buf())
	vim.t.bufs = bufs
	vim.cmd("redrawtabline")
end

return M

@siduck
Copy link
Member

siduck commented Nov 9, 2022

whats the problem? @kola-web

@kola-web
Copy link
Author

kola-web commented Nov 9, 2022

whats the problem? @kola-web

Left and right movements are not in sequence

@siduck
Copy link
Member

siduck commented Nov 9, 2022

oh yea, i probably forgot to push the fix. Will catch up tonight!

@kola-web
Copy link
Author

kola-web commented Nov 9, 2022

thanks for your effort

@siduck
Copy link
Member

siduck commented Nov 9, 2022

@martin-braun @kola-web ok it should be fixed now!

@siduck siduck closed this as completed Nov 9, 2022
@kola-web
Copy link
Author

Here are the three tabs that are initially opened
image
Called in a loop require("nvchad_ui.tabufline").move_buf(1) will be presented separately as
image
image

The order is out of order

@kola-web
Copy link
Author

@siduck Is it possible to reopen this question

@siduck siduck reopened this Nov 10, 2022
@siduck
Copy link
Member

siduck commented Nov 10, 2022

@kola-web why loop?

@kola-web
Copy link
Author

kola-web commented Nov 10, 2022

repeated calls require("nvchad_ui.tabufline").move_buf(1) Tab order is shuffled

@siduck
Copy link
Member

siduck commented Nov 10, 2022

repeated calls require("nvchad_ui.tabufline").move_buf(1) Tab order is shuffled

you cant use it that way at the moment

@siduck
Copy link
Member

siduck commented Nov 10, 2022

@kola-web i tested it now, it works well

vid.mp4

@siduck siduck closed this as completed Nov 14, 2022
@kola-web
Copy link
Author

Thanks a lot for your work, I'll test it again

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

No branches or pull requests

3 participants