Skip to content

Commit

Permalink
feat: add excludes list (closes #151)
Browse files Browse the repository at this point in the history
  • Loading branch information
romgrk committed Aug 2, 2021
1 parent 347689a commit 28319dd
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 13 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,10 @@ let bufferline.closable = v:true
" - middle-click: delete buffer
let bufferline.clickable = v:true
" Excludes buffers from the tabline
let bufferline.exclude_ft = ['javascript']
let bufferline.exclude_name = ['package.json']
" Enable/disable icons
" if set to 'numbers', will show buffer index in the tabline
" if set to 'both', will show buffer index and icons in the tabline
Expand Down Expand Up @@ -277,6 +281,10 @@ vim.g.bufferline = {
-- - middle-click: delete buffer
clickable = true,

-- Excludes buffers from the tabline
exclude_ft = ['javascript'],
exclude_name = ['package.json'],

-- Enable/disable icons
-- if set to 'numbers', will show buffer index in the tabline
-- if set to 'both', will show buffer index and icons in the tabline
Expand Down
12 changes: 0 additions & 12 deletions autoload/bufferline.vim

This file was deleted.

16 changes: 16 additions & 0 deletions doc/barbar.txt
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,22 @@ Here are the groups that you should define if you'd like to style Barbar.
let g:bufferline.clickable = v:true
<

*g:bufferline.exclude_ft*
`g:bufferline.exclude_ft` string[] (default v:null)

Excludes filetypes from appearing in the tabs.
>
let g:bufferline.exclude_ft = ['javascript']
<

*g:bufferline.exclude_name*
`g:bufferline.exclude_name` string[] (default v:null)

Excludes buffers matching name from appearing in the tabs.
>
let g:bufferline.exclude_name = ['package.json']
<

*g:bufferline.maximum_padding*
`g:bufferline.maximum_padding` int (default 4)

Expand Down
39 changes: 38 additions & 1 deletion lua/bufferline/state.lua
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,43 @@ end

-- Update state

local function get_buffer_list()
local opts = vim.g.bufferline
local buffers = nvim.list_bufs()
local result = {}

local exclude_ft = opts.exclude_ft
local exclude_name = opts.exclude_name

for i, buffer in ipairs(buffers) do

if not nvim.buf_get_option(buffer, 'buflisted') then
goto continue
end

if exclude_ft ~= vim.NIL then
local ft = nvim.buf_get_option(buffer, 'filetype')
if utils.has(exclude_ft, ft) then
goto continue
end
end

if exclude_name ~= vim.NIL then
local fullname = nvim.nvim_buf_get_name(buffer)
local name = utils.basename(fullname)
if utils.has(exclude_name, name) then
goto continue
end
end

table.insert(result, buffer)

::continue::
end

return result
end

function m.update_names()
local opts = vim.g.bufferline
local buffer_index_by_name = {}
Expand Down Expand Up @@ -238,7 +275,7 @@ function m.update_names()
end

function m.get_updated_buffers(update_names)
local current_buffers = vim.fn['bufferline#filter']('&buflisted')
local current_buffers = get_buffer_list()
local new_buffers =
filter(
function(b) return not includes(m.buffers, b) end,
Expand Down
6 changes: 6 additions & 0 deletions lua/bufferline/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ local function index(tbl, n)
return nil
end

local function has(tbl, n)
return index(tbl, n) ~= nil
end

local function slice(tbl, first, last)
if type(tbl) == 'string' then
return string.sub(tbl, first, last)
Expand Down Expand Up @@ -127,9 +131,11 @@ end
return {
len = len,
index = index,
has = has,
slice = slice,
reverse = reverse,
collect = collect,
basename = basename,
get_buffer_name = get_buffer_name,
get_unique_name = get_unique_name,
}
2 changes: 2 additions & 0 deletions plugin/bufferline.vim
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ let s:DEFAULT_OPTIONS = {
\ 'auto_hide': v:false,
\ 'clickable': v:true,
\ 'closable': v:true,
\ 'exclude_ft': v:null,
\ 'exclude_name': v:null,
\ 'icon_close_tab': '',
\ 'icon_close_tab_modified': '',
\ 'icon_separator_active': '',
Expand Down

0 comments on commit 28319dd

Please sign in to comment.