Skip to content

Commit

Permalink
v3.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
akinsho committed Feb 20, 2023
2 parents 84b0822 + 7d19acf commit 52d6aca
Show file tree
Hide file tree
Showing 13 changed files with 118 additions and 48 deletions.
6 changes: 0 additions & 6 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,6 @@ jobs:
wget -q https://github.com/neovim/neovim/releases/download/nightly/nvim-linux64.deb -O /tmp/nvim.deb
sudo dpkg -i /tmp/nvim.deb
- name: Fetch dependencies
run: |
git clone --depth 1 https://github.com/nvim-lua/plenary.nvim ~/.local/share/nvim/site/pack/vendor/start/plenary.nvim
git clone --depth 1 https://github.com/kyazdani42/nvim-web-devicons ~/.local/share/nvim/site/pack/vendor/start/nvim-web-devicons
ln -s $(pwd) ~/.local/share/nvim/site/pack/vendor/start
- name: Run tests
run: |
nvim --version
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.luacheckcache
/screenshots/
screenshots/
.tests/
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,17 @@ for more details on how to configure this plugin in details please see `:h buffe

#### Alternate styling

##### Slanted tabs

![slanted tabs](https://user-images.githubusercontent.com/22454918/111992989-fec39b80-8b0d-11eb-851b-010641196a04.png)

**NOTE**: some terminals require special characters to be padded so set the style to `padded_slant` if the appearance isn't right in your terminal emulator. Please keep in mind
though that results may vary depending on your terminal emulator of choice and this style might will not work for all terminals

##### Sloped tabs

![sloped tabs](https://user-images.githubusercontent.com/22454918/220115787-0ba2264f-1cf5-4f18-a322-7c7cfa3d8f42.png)

see: `:h bufferline-styling`

---
Expand Down
18 changes: 17 additions & 1 deletion doc/bufferline.txt
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,17 @@ The available configuration are:
}
},
color_icons = true | false, -- whether or not to add the filetype icon highlights
get_element_icon = function(element)
-- element consists of {filetype: string, path: string, extension: string, directory: string}
-- This can be used to change how bufferline fetches the icon
-- for an element e.g. a buffer or a tab.
-- e.g.
local icon, hl = require('nvim-web-devicons').get_icon_by_filetype(opts.filetype, { default = false })
return icon, hl
-- or
local custom_map = {my_thing_ft: {icon = "my_thing_icon", hl}}
return custom_map[element.filetype]
end,
show_buffer_icons = true | false, -- disable filetype icons for buffers
show_buffer_close_icons = true | false,
show_buffer_default_icon = true | false, -- whether or not an unrecognised filetype should show a default icon
Expand All @@ -139,7 +150,7 @@ The available configuration are:
persist_buffer_sort = true, -- whether or not custom sorted buffers should persist
-- can also be a table containing 2 custom separators
-- [focused and unfocused]. eg: { '|', '|' }
separator_style = "slant" | "thick" | "thin" | { 'any', 'any' },
separator_style = "slant" | "slope" | "thick" | "thin" | { 'any', 'any' },
enforce_regular_tabs = false | true,
always_show_bufferline = true | false,
hover = {
Expand Down Expand Up @@ -183,6 +194,7 @@ You can change the appearance of the bufferline separators by setting the
* `slant` - Use slanted/triangular separators
* `padded_slant` - Same as `slant` but with extra padding which some terminals require.
If `slant` does not render correctly for you try padded this instead.
* `slope` - Use slanted/triangular separators but slopped to the right
* `thick` - Increase the thickness of the separator characters
* `thin` - (default) Use thin separator characters
* finally you can pass in a custom list containing 2 characters which will be
Expand Down Expand Up @@ -697,6 +709,10 @@ A few of this plugins commands can be mapped for ease of use. >
nnoremap <silent><mymap> :BufferLineMoveNext<CR>
nnoremap <silent><mymap> :BufferLineMovePrev<CR>
" These commands will move the current buffer to the first or the last position in the bufferline
nnoremap <silent><mymap> :lua require'bufferline'.move_to(1)<CR>
nnoremap <silent><mymap> :lua require'bufferline'.move_to(-1)<CR>
" These commands will sort buffers by directory, language, or a custom criteria
nnoremap <silent>be :BufferLineSortByExtension<CR>
nnoremap <silent>bd :BufferLineSortByDirectory<CR>
Expand Down
2 changes: 2 additions & 0 deletions lua/bufferline.lua
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ local BUFFERLINE_GROUP = "BufferlineCmds"

local M = {
move = commands.move,
move_to = commands.move_to,
exec = commands.exec,
go_to = commands.go_to,
cycle = commands.cycle,
Expand Down Expand Up @@ -133,6 +134,7 @@ function M.group_action(name, action)
assert(name, "A name must be passed to execute a group action")
if action == "close" then
groups.command(name, function(b) api.nvim_buf_delete(b.id, { force = true }) end)
ui.refresh()
elseif action == "toggle" then
groups.toggle_hidden(nil, name)
ui.refresh()
Expand Down
17 changes: 13 additions & 4 deletions lua/bufferline/commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,14 @@ function M.get_current_element_index(current_state, opts)
end
end

--- @param direction number
function M.move(direction)
local index = M.get_current_element_index(state)
--- Move the buffer at index `from_index` (or current index if not specified) to position `to_index`
--- @param to_index number negative indices are accepted (counting from the right instead of the left, e.g. -1 for the last position, -2 for the second-last, etc.)
--- @param from_index number?
function M.move_to(to_index, from_index)
local index = from_index or M.get_current_element_index(state)
if not index then return utils.notify("Unable to find buffer to move, sorry", "warn") end
local next_index = index + direction
-- Calculate next index depending on the sign of `to_index`
local next_index = to_index > 0 and to_index or #state.components + 1 + to_index
if next_index >= 1 and next_index <= #state.components then
local item = state.components[index]
local destination_buf = state.components[next_index]
Expand All @@ -166,6 +169,12 @@ function M.move(direction)
end
end

--- @param direction number
function M.move(direction)
local index = M.get_current_element_index(state)
M.move_to(index + direction, index)
end

function M.cycle(direction)
if vim.opt.showtabline == 0 then
if direction > 0 then vim.cmd("bnext") end
Expand Down
9 changes: 5 additions & 4 deletions lua/bufferline/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,8 @@ local constants = lazy.require("bufferline.constants")

---@alias DiagnosticIndicator fun(count: number, level: number, errors: table<string, any>, ctx: table<string, any>): string

---@class HoverOptions
---@field reveal string[]
---@field delay integer
---@field enabled boolean
---@alias HoverOptions {reveal: string[], delay: integer, enabled: boolean}
---@alias IconFetcherOpts {directory: boolean, path: string, extension: string, filetype: string?}

---@class BufferlineOptions
---@field public mode BufferlineMode
Expand All @@ -62,6 +60,7 @@ local constants = lazy.require("bufferline.constants")
---@field public show_buffer_icons boolean
---@field public show_buffer_close_icons boolean
---@field public show_buffer_default_icon boolean
---@field public get_element_icon fun(opts: IconFetcherOpts): string?, string?
---@field public show_close_icon boolean
---@field public show_tab_indicators boolean
---@field public show_duplicate_prefix boolean
Expand Down Expand Up @@ -729,6 +728,8 @@ local function get_defaults()
color_icons = true,
show_buffer_icons = true,
show_buffer_close_icons = true,
get_element_icon = nil,
---@deprecated
show_buffer_default_icon = true,
show_close_icon = true,
show_tab_indicators = true,
Expand Down
2 changes: 2 additions & 0 deletions lua/bufferline/constants.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ M.sep_names = {
thin = "thin",
thick = "thick",
slant = "slant",
slope = "slope",
padded_slant = "padded_slant",
}

Expand All @@ -18,6 +19,7 @@ M.sep_chars = {
[M.sep_names.thin] = { "", "" },
[M.sep_names.thick] = { "", "" },
[M.sep_names.slant] = { "", "" },
[M.sep_names.slope] = { "", "" },
[M.sep_names.padded_slant] = { "" .. M.padding, "" .. M.padding },
}

Expand Down
5 changes: 5 additions & 0 deletions lua/bufferline/groups.lua
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,11 @@ function M.command(group_name, callback)
function(list) return list.name == group_name end,
state.components_by_group
)

if not group then
return
end

utils.for_each(callback, group)
end

Expand Down
2 changes: 1 addition & 1 deletion lua/bufferline/ui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ end
---@param style string
---@return boolean
local function is_slant(style)
return vim.tbl_contains({ sep_names.slant, sep_names.padded_slant }, style)
return vim.tbl_contains({ sep_names.slant, sep_names.padded_slant, sep_names.slope}, style)
end

--- "▍" "░"
Expand Down
28 changes: 11 additions & 17 deletions lua/bufferline/utils/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -159,17 +159,17 @@ function M.notify(msg, level, opts)
vim.schedule(function() vim.notify(msg, level, nopts) end)
end

---@class GetIconOpts
---@field directory boolean
---@field path string
---@field extension string
---@field filetype string?

---Get an icon for a filetype using either nvim-web-devicons or vim-devicons
---if using the lua plugin this also returns the icon's highlights
---@param opts GetIconOpts
---@param opts IconFetcherOpts
---@return string, string?
function M.get_icon(opts)
local user_func = config.options.get_element_icon
if user_func and vim.is_callable(user_func) then
local icon, hl = user_func(opts)
if icon then return icon, hl end
end

local loaded, webdev_icons = pcall(require, "nvim-web-devicons")
if opts.directory then
local hl = loaded and "DevIconDefault" or nil
Expand All @@ -184,18 +184,12 @@ function M.get_icon(opts)
end
if type == "terminal" then return webdev_icons.get_icon(type) end

--- TODO: Deprecate this option
local use_default = config.options.show_buffer_default_icon

local icon, hl
if M.is_truthy(opts.filetype) then
-- Don't use a default here so that we fall through to the next case if no icon is found
icon, hl = webdev_icons.get_icon_by_filetype(opts.filetype, { default = false })
end
if not icon then
icon, hl = webdev_icons.get_icon(fn.fnamemodify(opts.path, ":t"), opts.extension, {
default = use_default,
})
end
local icon, hl = webdev_icons.get_icon(fn.fnamemodify(opts.path, ":t"), opts.extension, {
default = use_default,
})

if not icon then return "", "" end
return icon, hl
Expand Down
25 changes: 17 additions & 8 deletions tests/bufferline_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,19 @@ describe("Bufferline tests:", function()
assert.is_truthy(tabline:match(icon))
end)

it("should allow specifying how icons are fetched", function()
local icon = "Q"
bufferline.setup({
options = {
get_element_icon = function() return icon end,
},
})
vim.cmd("edit test.txt")
local tabline = nvim_bufferline()
assert.truthy(tabline)
assert.is_truthy(tabline:match(icon))
end)

it("should allow formatting names", function()
bufferline.setup({
options = {
Expand Down Expand Up @@ -196,10 +209,8 @@ describe("Bufferline tests:", function()
it("should close buffers to the right of the current buffer", function()
bufferline.setup({
options = {
close_command = function(bufid)
vim.api.nvim_buf_delete(bufid, { force = true })
end
}
close_command = function(bufid) vim.api.nvim_buf_delete(bufid, { force = true }) end,
},
})
vim.cmd("file! a.txt")
vim.cmd("edit b.txt")
Expand All @@ -217,10 +228,8 @@ describe("Bufferline tests:", function()
it("should close buffers to the left of the current buffer", function()
bufferline.setup({
options = {
close_command = function(bufid)
vim.api.nvim_buf_delete(bufid, { force = true })
end
}
close_command = function(bufid) vim.api.nvim_buf_delete(bufid, { force = true }) end,
},
})
vim.cmd("edit! a.txt")
vim.cmd("edit b.txt")
Expand Down
43 changes: 37 additions & 6 deletions tests/init.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,40 @@
vim.opt.rtp:append({
".",
"../plenary.nvim",
"../nvim-web-devicons",
})
local M = {}

function M.root(root)
local f = debug.getinfo(1, "S").source:sub(2)
return vim.fn.fnamemodify(f, ":p:h:h") .. "/" .. (root or "")
end

---@param plugin string
function M.load(plugin)
local name = plugin:match(".*/(.*)")
local package_root = M.root(".tests/site/pack/deps/start/")
if not vim.loop.fs_stat(package_root .. name) then
print("Installing " .. plugin)
vim.fn.mkdir(package_root, "p")
vim.fn.system({
"git",
"clone",
"--depth=1",
"https://github.com/" .. plugin .. ".git",
package_root .. "/" .. name,
})
end
end

function M.setup()
vim.cmd([[set runtimepath=$VIMRUNTIME]])
vim.opt.runtimepath:append(M.root())
vim.opt.packpath = { M.root(".tests/site") }
M.load("nvim-lua/plenary.nvim")
M.load("nvim-tree/nvim-web-devicons")
vim.env.XDG_CONFIG_HOME = M.root(".tests/config")
vim.env.XDG_DATA_HOME = M.root(".tests/data")
vim.env.XDG_STATE_HOME = M.root(".tests/state")
vim.env.XDG_CACHE_HOME = M.root(".tests/cache")
end

vim.cmd([[runtime! plugin/plenary.vim]])
vim.o.swapfile = false
_G.__TEST = true

M.setup()

0 comments on commit 52d6aca

Please sign in to comment.