fix component(filetype) proper icon based on filetype (#810)
* Refactor tests to use stubs for nvim-web-devicons * Simplify getting icon `get_icon` does not need the 2nd parameter (`ext`) as it will derive it from the given `name` (1st argument) itself. * fix component(filetype) proper icon based on filetype With https://github.com/kyazdani42/nvim-web-devicons/pull/125 a filetype based lookup was implemented upstream which can be used to get an icon when a file doesn't have an extension. fixes #578
This commit is contained in:
parent
a4bc56f6fe
commit
9d177b668c
|
@ -31,9 +31,10 @@ function M:apply_icon()
|
||||||
local icon, icon_highlight_group
|
local icon, icon_highlight_group
|
||||||
local ok, devicons = pcall(require, 'nvim-web-devicons')
|
local ok, devicons = pcall(require, 'nvim-web-devicons')
|
||||||
if ok then
|
if ok then
|
||||||
local f_name, f_extension = vim.fn.expand('%:t'), vim.fn.expand('%:e')
|
icon, icon_highlight_group = devicons.get_icon(vim.fn.expand('%:t'))
|
||||||
f_extension = f_extension ~= '' and f_extension or vim.bo.filetype
|
if icon == nil then
|
||||||
icon, icon_highlight_group = devicons.get_icon(f_name, f_extension)
|
icon, icon_highlight_group = devicons.get_icon_by_filetype(vim.bo.filetype)
|
||||||
|
end
|
||||||
|
|
||||||
if icon == nil and icon_highlight_group == nil then
|
if icon == nil and icon_highlight_group == nil then
|
||||||
icon = ''
|
icon = ''
|
||||||
|
|
|
@ -285,21 +285,24 @@ describe('Filetype component', function()
|
||||||
end)
|
end)
|
||||||
|
|
||||||
it('colors nvim-web-devicons icons', function()
|
it('colors nvim-web-devicons icons', function()
|
||||||
package.loaded['nvim-web-devicons'] = {
|
|
||||||
get_icon = function()
|
|
||||||
return '*', 'test_highlight_group'
|
|
||||||
end,
|
|
||||||
}
|
|
||||||
vim.g.actual_curwin = tostring(vim.api.nvim_get_current_win())
|
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')
|
local hl = require('lualine.highlight')
|
||||||
local utils = require('lualine.utils.utils')
|
|
||||||
stub(hl, 'create_component_highlight_group')
|
stub(hl, 'create_component_highlight_group')
|
||||||
stub(hl, 'format_highlight')
|
|
||||||
stub(utils, 'extract_highlight_colors')
|
|
||||||
hl.create_component_highlight_group.returns { name = 'MyCompHl', no_mode = false, section = 'a' }
|
hl.create_component_highlight_group.returns { name = 'MyCompHl', no_mode = false, section = 'a' }
|
||||||
|
stub(hl, 'format_highlight')
|
||||||
hl.format_highlight.returns('%#lualine_c_normal#')
|
hl.format_highlight.returns('%#lualine_c_normal#')
|
||||||
|
|
||||||
|
local utils = require('lualine.utils.utils')
|
||||||
|
stub(utils, 'extract_highlight_colors')
|
||||||
utils.extract_highlight_colors.returns('#000')
|
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')
|
||||||
|
|
||||||
local opts = build_component_opts {
|
local opts = build_component_opts {
|
||||||
component_separators = { left = '', right = '' },
|
component_separators = { left = '', right = '' },
|
||||||
hl = '%#lualine_c_normal#',
|
hl = '%#lualine_c_normal#',
|
||||||
|
@ -308,6 +311,7 @@ describe('Filetype component', function()
|
||||||
icon_only = false,
|
icon_only = false,
|
||||||
}
|
}
|
||||||
assert_component('filetype', opts, '%#MyCompHl_normal#*%#lualine_c_normal# lua%#lualine_c_normal#')
|
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(utils.extract_highlight_colors).was_called_with('test_highlight_group', 'fg')
|
assert.stub(utils.extract_highlight_colors).was_called_with('test_highlight_group', 'fg')
|
||||||
assert.stub(hl.create_component_highlight_group).was_called_with(
|
assert.stub(hl.create_component_highlight_group).was_called_with(
|
||||||
{ fg = '#000' },
|
{ fg = '#000' },
|
||||||
|
@ -315,42 +319,60 @@ describe('Filetype component', function()
|
||||||
opts,
|
opts,
|
||||||
false
|
false
|
||||||
)
|
)
|
||||||
|
assert.stub(vim.fn.expand).was_called_with('%:t')
|
||||||
|
|
||||||
|
devicons.get_icon:revert()
|
||||||
|
utils.extract_highlight_colors:revert()
|
||||||
hl.create_component_highlight_group:revert()
|
hl.create_component_highlight_group:revert()
|
||||||
hl.format_highlight:revert()
|
hl.format_highlight:revert()
|
||||||
utils.extract_highlight_colors:revert()
|
vim.fn.expand:revert()
|
||||||
package.loaded['nvim-web-devicons'] = nil
|
|
||||||
vim.g.actual_curwin = nil
|
vim.g.actual_curwin = nil
|
||||||
end)
|
end)
|
||||||
|
|
||||||
it("Doesn't color when colored is false", function()
|
it("doesn't color when colored is false", function()
|
||||||
package.loaded['nvim-web-devicons'] = {
|
stub(vim.fn, 'expand')
|
||||||
get_icon = function()
|
vim.fn.expand.on_call_with('%:t').returns('test.lua')
|
||||||
return '*', 'test_highlight_group'
|
|
||||||
end,
|
|
||||||
}
|
|
||||||
local hl = require('lualine.highlight')
|
local hl = require('lualine.highlight')
|
||||||
local utils = require('lualine.utils.utils')
|
|
||||||
stub(hl, 'create_component_highlight_group')
|
stub(hl, 'create_component_highlight_group')
|
||||||
|
|
||||||
|
local utils = require('lualine.utils.utils')
|
||||||
stub(utils, 'extract_highlight_colors')
|
stub(utils, 'extract_highlight_colors')
|
||||||
hl.create_component_highlight_group.returns('MyCompHl')
|
|
||||||
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')
|
||||||
|
|
||||||
local opts = build_component_opts {
|
local opts = build_component_opts {
|
||||||
component_separators = { left = '', right = '' },
|
component_separators = { left = '', right = '' },
|
||||||
padding = 0,
|
padding = 0,
|
||||||
colored = false,
|
colored = false,
|
||||||
}
|
}
|
||||||
assert_component('filetype', opts, '* lua')
|
assert_component('filetype', opts, '* lua')
|
||||||
hl.create_component_highlight_group:revert()
|
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()
|
utils.extract_highlight_colors:revert()
|
||||||
package.loaded['nvim-web-devicons'] = nil
|
hl.create_component_highlight_group:revert()
|
||||||
|
vim.fn.expand:revert()
|
||||||
end)
|
end)
|
||||||
|
|
||||||
it('displays only icon when icon_only is true', function()
|
it('displays only icon when icon_only is true', function()
|
||||||
package.loaded['nvim-web-devicons'] = {
|
stub(vim.fn, 'expand')
|
||||||
get_icon = function()
|
vim.fn.expand.on_call_with('%:t').returns('test.lua')
|
||||||
return '*', 'test_highlight_group'
|
|
||||||
end,
|
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 {
|
local opts = build_component_opts {
|
||||||
component_separators = { left = '', right = '' },
|
component_separators = { left = '', right = '' },
|
||||||
|
@ -359,15 +381,30 @@ describe('Filetype component', function()
|
||||||
icon_only = true,
|
icon_only = true,
|
||||||
}
|
}
|
||||||
assert_component('filetype', opts, '*')
|
assert_component('filetype', opts, '*')
|
||||||
package.loaded['nvim-web-devicons'] = nil
|
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)
|
end)
|
||||||
|
|
||||||
it('displays right aligned icon when icon.align is "right"', function()
|
it('displays right aligned icon when icon.align is "right"', function()
|
||||||
package.loaded['nvim-web-devicons'] = {
|
stub(vim.fn, 'expand')
|
||||||
get_icon = function()
|
vim.fn.expand.on_call_with('%:t').returns('test.lua')
|
||||||
return '*', 'test_highlight_group'
|
|
||||||
end,
|
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 {
|
local opts = build_component_opts {
|
||||||
component_separators = { left = '', right = '' },
|
component_separators = { left = '', right = '' },
|
||||||
|
@ -377,7 +414,51 @@ describe('Filetype component', function()
|
||||||
icon = { align = 'right' }
|
icon = { align = 'right' }
|
||||||
}
|
}
|
||||||
assert_component('filetype', opts, 'lua *')
|
assert_component('filetype', opts, 'lua *')
|
||||||
package.loaded['nvim-web-devicons'] = nil
|
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')
|
||||||
|
|
||||||
|
local opts = build_component_opts {
|
||||||
|
component_separators = { left = '', right = '' },
|
||||||
|
padding = 0,
|
||||||
|
colored = false,
|
||||||
|
icon_only = false,
|
||||||
|
}
|
||||||
|
assert_component('filetype', opts, '* lua')
|
||||||
|
assert.stub(devicons.get_icon).was_called_with('test')
|
||||||
|
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)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue