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

fix: Sync filetype component with filetype option #1252

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
5 changes: 1 addition & 4 deletions lua/lualine/components/filetype.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,7 @@ function M:apply_icon()
local icon, icon_highlight_group
local ok, devicons = pcall(require, 'nvim-web-devicons')
if ok then
icon, icon_highlight_group = devicons.get_icon(vim.fn.expand('%:t'))
if icon == nil then
icon, icon_highlight_group = devicons.get_icon_by_filetype(vim.bo.filetype)
end
icon, icon_highlight_group = devicons.get_icon_by_filetype(vim.bo.filetype)

if icon == nil and icon_highlight_group == nil then
icon = ''
Expand Down
83 changes: 14 additions & 69 deletions tests/spec/component_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -299,8 +299,6 @@ describe('Filetype component', function()

it('colors nvim-web-devicons icons', function()
vim.g.actual_curwin = tostring(vim.api.nvim_get_current_win())
stub(vim.fn, 'expand')
vim.fn.expand.on_call_with('%:t').returns('test.lua')

local hl = require('lualine.highlight')
stub(hl, 'create_component_highlight_group')
Expand All @@ -313,8 +311,8 @@ describe('Filetype component', function()
utils.extract_highlight_colors.returns('#000')

local devicons = require('nvim-web-devicons')
stub(devicons, 'get_icon')
devicons.get_icon.on_call_with('test.lua').returns('*', 'test_highlight_group')
stub(devicons, 'get_icon_by_filetype')
devicons.get_icon_by_filetype.on_call_with('lua').returns('*', 'test_highlight_group')

local opts = build_component_opts {
component_separators = { left = '', right = '' },
Expand All @@ -324,68 +322,58 @@ describe('Filetype component', function()
icon_only = false,
}
assert_component('filetype', opts, '%#MyCompHl_normal#* %#lualine_c_normal#lua%#lualine_c_normal#')
assert.stub(devicons.get_icon).was_called_with('test.lua')
assert.stub(devicons.get_icon_by_filetype).was_called_with('lua')
assert.stub(utils.extract_highlight_colors).was_called_with('test_highlight_group', 'fg')
assert.stub(hl.create_component_highlight_group).was_called_with(
{ fg = '#000' },
'filetype_test_highlight_group',
opts,
false
)
assert.stub(vim.fn.expand).was_called_with('%:t')

devicons.get_icon:revert()
devicons.get_icon_by_filetype:revert()
utils.extract_highlight_colors:revert()
hl.create_component_highlight_group:revert()
hl.format_highlight:revert()
vim.fn.expand:revert()
vim.g.actual_curwin = nil
end)

it("doesn't color when colored is false", function()
stub(vim.fn, 'expand')
vim.fn.expand.on_call_with('%:t').returns('test.lua')

local hl = require('lualine.highlight')
stub(hl, 'create_component_highlight_group')

local utils = require('lualine.utils.utils')
stub(utils, 'extract_highlight_colors')

local devicons = require('nvim-web-devicons')
stub(devicons, 'get_icon')
devicons.get_icon.on_call_with('test.lua').returns('*', 'test_highlight_group')
stub(devicons, 'get_icon_by_filetype')
devicons.get_icon_by_filetype.on_call_with('lua').returns('*', 'test_highlight_group')

local opts = build_component_opts {
component_separators = { left = '', right = '' },
padding = 0,
colored = false,
}
assert_component('filetype', opts, '* lua')
assert.stub(devicons.get_icon).was_called_with('test.lua')
assert.stub(devicons.get_icon_by_filetype).was_called_with('lua')
assert.stub(utils.extract_highlight_colors).was_not_called()
assert.stub(hl.create_component_highlight_group).was_not_called()
assert.stub(vim.fn.expand).was_called_with('%:t')

devicons.get_icon:revert()
devicons.get_icon_by_filetype:revert()
utils.extract_highlight_colors:revert()
hl.create_component_highlight_group:revert()
vim.fn.expand:revert()
end)

it('displays only icon when icon_only is true', function()
stub(vim.fn, 'expand')
vim.fn.expand.on_call_with('%:t').returns('test.lua')

local hl = require('lualine.highlight')
stub(hl, 'create_component_highlight_group')

local utils = require('lualine.utils.utils')
stub(utils, 'extract_highlight_colors')

local devicons = require('nvim-web-devicons')
stub(devicons, 'get_icon')
devicons.get_icon.on_call_with('test.lua').returns('*', 'test_highlight_group')
stub(devicons, 'get_icon_by_filetype')
devicons.get_icon_by_filetype.on_call_with('lua').returns('*', 'test_highlight_group')

local opts = build_component_opts {
component_separators = { left = '', right = '' },
Expand All @@ -394,63 +382,23 @@ describe('Filetype component', function()
icon_only = true,
}
assert_component('filetype', opts, '* ')
assert.stub(devicons.get_icon).was_called_with('test.lua')
assert.stub(devicons.get_icon_by_filetype).was_called_with('lua')
assert.stub(utils.extract_highlight_colors).was_not_called()
assert.stub(hl.create_component_highlight_group).was_not_called()
assert.stub(vim.fn.expand).was_called_with('%:t')

devicons.get_icon:revert()
devicons.get_icon_by_filetype:revert()
utils.extract_highlight_colors:revert()
hl.create_component_highlight_group:revert()
vim.fn.expand:revert()
end)

it('displays right aligned icon when icon.align is "right"', function()
stub(vim.fn, 'expand')
vim.fn.expand.on_call_with('%:t').returns('test.lua')

local hl = require('lualine.highlight')
stub(hl, 'create_component_highlight_group')

local utils = require('lualine.utils.utils')
stub(utils, 'extract_highlight_colors')

local devicons = require('nvim-web-devicons')
stub(devicons, 'get_icon')
devicons.get_icon.on_call_with('test.lua').returns('*', 'test_highlight_group')

local opts = build_component_opts {
component_separators = { left = '', right = '' },
padding = 0,
colored = false,
icon_only = false,
icon = { align = 'right' }
}
assert_component('filetype', opts, 'lua * ')
assert.stub(devicons.get_icon).was_called_with('test.lua')
assert.stub(utils.extract_highlight_colors).was_not_called()
assert.stub(hl.create_component_highlight_group).was_not_called()
assert.stub(vim.fn.expand).was_called_with('%:t')

devicons.get_icon:revert()
utils.extract_highlight_colors:revert()
hl.create_component_highlight_group:revert()
vim.fn.expand:revert()
end)

it('uses filetype lookup when file has no extension', function()
stub(vim.fn, 'expand')
vim.fn.expand.on_call_with('%:t').returns('test')

local hl = require('lualine.highlight')
stub(hl, 'create_component_highlight_group')

local utils = require('lualine.utils.utils')
stub(utils, 'extract_highlight_colors')

local devicons = require('nvim-web-devicons')
stub(devicons, 'get_icon')
devicons.get_icon.on_call_with('test').returns(nil)
stub(devicons, 'get_icon_by_filetype')
devicons.get_icon_by_filetype.on_call_with('lua').returns('*', 'test_highlight_group')

Expand All @@ -459,19 +407,16 @@ describe('Filetype component', function()
padding = 0,
colored = false,
icon_only = false,
icon = { align = 'right' }
}
assert_component('filetype', opts, '* lua')
assert.stub(devicons.get_icon).was_called_with('test')
assert_component('filetype', opts, 'lua * ')
assert.stub(devicons.get_icon_by_filetype).was_called_with('lua')
assert.stub(utils.extract_highlight_colors).was_not_called()
assert.stub(hl.create_component_highlight_group).was_not_called()
assert.stub(vim.fn.expand).was_called_with('%:t')

devicons.get_icon_by_filetype:revert()
devicons.get_icon:revert()
utils.extract_highlight_colors:revert()
hl.create_component_highlight_group:revert()
vim.fn.expand:revert()
end)
end)

Expand Down
Loading