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

feat(ui): Tab Renaming #848

Merged
merged 4 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
akinsho marked this conversation as resolved.
Show resolved Hide resolved
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
muhmud marked this conversation as resolved.
Show resolved Hide resolved
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
Loading