Skip to content

Commit

Permalink
feat: allow enabling extensions per buftype
Browse files Browse the repository at this point in the history
Add a `buftypes` parameter to the extension table; allowing to
filter by current buftype in addition to filetype.
  • Loading branch information
martinparadiso committed Oct 24, 2023
1 parent 2248ef2 commit 6dfb48c
Show file tree
Hide file tree
Showing 3 changed files with 164 additions and 5 deletions.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -887,7 +887,7 @@ You can find a bigger list [here](https://github.com/rockerBOO/awesome-neovim#ta
### Extensions

lualine extensions change statusline appearance for a window/buffer with
specified filetypes.
specified filetypes or buffertypes.

By default no extensions are loaded to improve performance.
You can load extensions with:
Expand Down Expand Up @@ -927,6 +927,17 @@ local my_extension = { sections = { lualine_a = {'mode'} }, filetypes = {'lua'}
require('lualine').setup { extensions = { my_extension } }
```

To filter by buffer type, for instance, you can apply:

```lua
local terminal_sec = function() return 'TERMINAL' end
local my_extension = { sections = { lualine_a = {terminal_sec} }, buftypes = {'terminal'} }
require('lualine').setup { extensions = { my_extension } }
```

Note: `filetypes` takes prescedence over `buftypes`. If `filetypes` is present,
the `buftypes` value is ignored.

---

### Refreshing lualine
Expand Down
15 changes: 11 additions & 4 deletions lua/lualine.lua
Original file line number Diff line number Diff line change
Expand Up @@ -196,14 +196,19 @@ end)
-- TODO: change this so it uses a hash table instead of iteration over list
-- to improve redraws. Add buftype / bufname for extensions
-- or some kind of cond ?
local function get_extension_sections(current_ft, is_focused, sec_name)
for _, extension in ipairs(config.extensions) do
if vim.tbl_contains(extension.filetypes, current_ft) then
local function get_extension_sections(current_bt, current_ft, is_focused, sec_name)
local _get_sections = function(extension)
if is_focused then
return extension[sec_name]
else
return extension['inactive_' .. sec_name] or extension[sec_name]
end
end
for _, extension in ipairs(config.extensions) do
if extension.filetypes and vim.tbl_contains(extension.filetypes, current_ft) then
return _get_sections(extension)
elseif extension.buftypes and vim.tbl_contains(extension.buftypes, current_bt) then
return _get_sections(extension)
end
end
return nil
Expand Down Expand Up @@ -281,6 +286,8 @@ local function status_dispatch(sec_name)
local current_ft = refresh_real_curwin
and vim.api.nvim_buf_get_option(vim.api.nvim_win_get_buf(refresh_real_curwin), 'filetype')
or vim.bo.filetype
local current_bt = refresh_real_curwin and vim.api.nvim_buf_get_option(vim.api.nvim_win_get_buf(refresh_real_curwin), 'buftype')
or vim.bo.buftype
local is_focused = focused ~= nil and focused or modules.utils.is_focused()
if
vim.tbl_contains(
Expand All @@ -291,7 +298,7 @@ local function status_dispatch(sec_name)
-- disable on specific filetypes
return nil
end
local extension_sections = get_extension_sections(current_ft, is_focused, sec_name)
local extension_sections = get_extension_sections(current_bt, current_ft, is_focused, sec_name)
if extension_sections ~= nil then
retval = statusline(extension_sections, is_focused, sec_name == 'winbar')
else
Expand Down
141 changes: 141 additions & 0 deletions tests/spec/lualine_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,147 @@ describe('Lualine', function()
vim.bo.ft = old_ft
end)

describe('can filter extensions based on buftype', function ()
it('without filetypes filter being present', function()
table.insert(config.extensions, {
buftypes = { 'acwrite' },
sections = {
lualine_a = {
function()
return 'custom_extension_component'
end,
},
},
})
local old_buftype = vim.bo.buftype
-- We cannot set arbitrary values like ft, must be limited to
-- neovim known buf types
vim.bo.buftype = 'acwrite'
require('lualine').setup(config)
statusline:expect([===[
highlights = {
1: lualine_a_normal = { bg = "#a89984", bold = true, fg = "#282828" }
2: lualine_transitional_lualine_a_normal_to_lualine_c_normal = { bg = "#3c3836", fg = "#a89984" }
3: lualine_c_normal = { bg = "#3c3836", fg = "#a89984" }
}
|{1: custom_extension_component }
{2:}
{3: }|
]===])
vim.bo.buftype = old_buftype
end)

it('with empty filetypes filter', function()
table.insert(config.extensions, {
filetypes = {},
buftypes = { 'acwrite' },
sections = {
lualine_a = {
function()
return 'custom_extension_component'
end,
},
},
})
local old_buftype = vim.bo.buftype
-- We cannot set arbitrary values like ft, must be limited to
-- neovim known buf types
vim.bo.buftype = 'acwrite'
require('lualine').setup(config)
statusline:expect([===[
highlights = {
1: lualine_a_normal = { bg = "#a89984", bold = true, fg = "#282828" }
2: lualine_transitional_lualine_a_normal_to_lualine_c_normal = { bg = "#3c3836", fg = "#a89984" }
3: lualine_c_normal = { bg = "#3c3836", fg = "#a89984" }
}
|{1: custom_extension_component }
{2:}
{3: }|
]===])
vim.bo.buftype = old_buftype
end)

it('and does not apply on different type', function()
table.insert(config.extensions, {
filetypes = {},
buftypes = { 'terminal' },
sections = {
lualine_a = {
function()
return 'custom_extension_component'
end,
},
},
})
local old_buftype = vim.bo.buftype
-- We cannot set arbitrary values like ft, must be limited to
-- neovim known buf types
vim.bo.buftype = 'acwrite'
require('lualine').setup(config)
statusline:expect([===[
highlights = {
1: lualine_a_normal = { bg = "#a89984", bold = true, fg = "#282828" }
2: lualine_transitional_lualine_a_normal_to_lualine_b_normal = { bg = "#504945", fg = "#a89984" }
3: lualine_b_normal = { bg = "#504945", fg = "#ebdbb2" }
4: lualine_transitional_lualine_b_normal_to_lualine_c_normal = { bg = "#3c3836", fg = "#504945" }
5: lualine_c_normal = { bg = "#3c3836", fg = "#a89984" }
}
|{1: NORMAL }
{2:}
{3:  master }
{4:}
{5: [No Name] }
{5: }
{5:  }
{4:}
{3: Top }
{2:}
{1: 1:1 }|
]===])
vim.bo.buftype = old_buftype
end)

it('and is only checked after filetypes', function()
table.insert(config.extensions, {
buftypes = { 'quickfix' },
sections = {
lualine_a = {
function()
return 'custom_extension_component'
end,
},
},
})
table.insert(config.extensions, {
filetypes = { 'quickfix' },
sections = {
lualine_a = {
function()
return 'should_not_be_present'
end,
},
},
})

local old_buftype = vim.bo.buftype
-- We cannot set arbitrary values like ft, must be limited to
-- neovim known buf types
vim.bo.buftype = 'quickfix'
require('lualine').setup(config)
statusline:expect([===[
highlights = {
1: lualine_a_normal = { bg = "#a89984", bold = true, fg = "#282828" }
2: lualine_transitional_lualine_a_normal_to_lualine_c_normal = { bg = "#3c3836", fg = "#a89984" }
3: lualine_c_normal = { bg = "#3c3836", fg = "#a89984" }
}
|{1: custom_extension_component }
{2:}
{3: }|
]===])
vim.bo.buftype = old_buftype
end)
end)

-- TODO: figure put why some of the tablines tests fail in CI
describe('tabline', function()
local tab_conf = vim.deepcopy(config)
Expand Down

0 comments on commit 6dfb48c

Please sign in to comment.