Skip to content

Commit

Permalink
feat(ui): tab renaming (#848)
Browse files Browse the repository at this point in the history
* ported over changes from previous pr

* Update lua/bufferline/commands.lua

Co-authored-by: Akin <[email protected]>

* Update lua/bufferline/tabpages.lua

Co-authored-by: Akin <[email protected]>

* change to use ui.refresh

---------

Co-authored-by: Muhmud Ahmad <[email protected]>
Co-authored-by: Akin <[email protected]>
  • Loading branch information
3 people committed Jan 22, 2024
1 parent e48ce18 commit f2e6c86
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 1 deletion.
8 changes: 8 additions & 0 deletions doc/bufferline.txt
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,14 @@ has a lot of the same features/styling but not all. A few things to note are
* Sorting doesn't work yet as that needs to be thought through.
* Grouping doesn't work yet as that also needs to be thought through.

`BufferLineTabRename`

Tabs can be renamed using the `BufferLineTabRename` command:

e.g.

- Rename the current tab: `BufferLineTabRename Code`
- Rename a tab based on it's `tabnr`: `BufferLineTabRename 1 Code`

==============================================================================
NUMBERS *bufferline-numbers*
Expand Down
2 changes: 2 additions & 0 deletions lua/bufferline.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ local M = {
get_elements = commands.get_elements,
close_with_pick = commands.close_with_pick,
close_in_direction = commands.close_in_direction,
rename_tab = commands.rename_tab,
close_others = commands.close_others,
unpin_and_close = commands.unpin_and_close,

Expand Down Expand Up @@ -168,6 +169,7 @@ local function setup_commands()
command("BufferLineSortByTabs", function() M.sort_by("tabs") end)
command("BufferLineGoToBuffer", function(opts) M.go_to(opts.args) end, { nargs = 1 })
command("BufferLineTogglePin", function() groups.toggle_pin() end, { nargs = 0 })
command("BufferLineTabRename", function(opts) M.rename_tab(opts.fargs) end, { nargs = '*' })
command("BufferLineGroupClose", function(opts) groups.action(opts.args, "close") end, {
nargs = 1,
complete = groups.complete,
Expand Down
12 changes: 12 additions & 0 deletions lua/bufferline/commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ local config = lazy.require("bufferline.config") ---@module "bufferline.config"
local groups = lazy.require("bufferline.groups") ---@module "bufferline.groups"
local sorters = lazy.require("bufferline.sorters") ---@module "bufferline.sorters"
local pick = lazy.require("bufferline.pick") ---@module "bufferline.pick"
local tabpage = lazy.require("bufferline.tabpages") ---@module "bufferline.tabpages"

local M = {}

Expand Down Expand Up @@ -264,6 +265,17 @@ function M.sort_by(sort_by)
ui.refresh()
end

function M.rename_tab(args)
if #args == 0 then return end
local tabnr = tonumber(args[1])
local name = table.concat(args, " ", 2)
if not tabnr then
name = table.concat(args, " ")
tabnr = 0
end
tabpage.rename_tab(tabnr, name)
end

_G.___bufferline_private.handle_close = handle_close
_G.___bufferline_private.handle_click = handle_click
_G.___bufferline_private.handle_group_click = handle_group_click
Expand Down
13 changes: 12 additions & 1 deletion lua/bufferline/tabpages.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ local function render(tabpage, is_active, style, highlights)
local hl = is_active and h.tab_selected.hl_group or h.tab.hl_group
local separator_hl = is_active and h.tab_separator_selected.hl_group or h.tab_separator.hl_group
local chars = constants.sep_chars[style] or constants.sep_chars.thin
local name = padding .. tabpage.tabnr .. padding
local name = padding .. (tabpage.variables.name or tabpage.tabnr) .. padding
local char_order = ({ thick = { 1, 2 }, thin = { 1, 2 } })[style] or { 2, 1 }
return {
{ highlight = separator_hl, text = chars[char_order[1]] },
Expand All @@ -34,6 +34,17 @@ local function render(tabpage, is_active, style, highlights)
}
end

function M.rename_tab(tabnr, name)
if tabnr == 0 then
tabnr = vim.fn.tabpagenr()
end
if name == "" then
name = string(tabnr)
end
api.nvim_tabpage_set_var(tabnr, "name", name)
ui.refresh()
end

function M.get()
local tabs = vim.fn.gettabinfo()
local current_tab = vim.fn.tabpagenr()
Expand Down

0 comments on commit f2e6c86

Please sign in to comment.