Refractor filetype and add disable_text option

This commit is contained in:
shadmansaleh 2021-05-12 15:17:51 +06:00
parent 0411f1c830
commit a560209c24
4 changed files with 99 additions and 43 deletions

View File

@ -324,7 +324,8 @@ sections = {
lualine_a = {
{
'filetype',
colored = true -- displays filetype icon in color if set to `true`
colored = true, -- displays filetype icon in color if set to `true
disable_text = false -- Display only icon for filetype
}
}
}

View File

@ -255,10 +255,12 @@ Component specific local options~
lualine_a = {
{
'filetype',
colored = true -- displays filetype icon in color if set to `true`
colored = true, -- displays filetype icon in color if set to `true`
disable_text = false -- Display only icon for filetype
}
}
}
<
* diff~
>
@ -337,4 +339,4 @@ You can disable lualine for specific filetypes
`options = {disabled_filetypes = {'lua'}}`
------------------------------------------------------------------------------
vim:tw=80:sw=4:ts=8:noet:ft=help:norl:et:
vim:tw=80:sw=4:ts=8:et:ft=help:norl:et:

View File

@ -5,45 +5,58 @@ local utils = require('lualine.utils.utils')
local FileType = require('lualine.component'):new()
FileType.update_status = function(self)
local data = vim.bo.filetype
if #data > 0 then
local ok, devicons = pcall(require, 'nvim-web-devicons')
if ok then
local f_name, f_extension = vim.fn.expand('%:t'), vim.fn.expand('%:e')
local icon, icon_highlight_group = devicons.get_icon(f_name, f_extension)
self.options.icon = icon
if self.options.icon and
(self.options.colored or self.options.colored == nil) then
self.options.colored = true
local highlight_color = utils.extract_highlight_colors(icon_highlight_group, 'fg')
local is_focused = vim.g.statusline_winid == vim.fn.win_getid()
local default_highlight = highlight.format_highlight(is_focused,
self.options.self
.section)
local icon_highlight = self.options.self.section .. '_' ..
icon_highlight_group
if not utils.highlight_exists(icon_highlight .. '_normal') then
icon_highlight = highlight.create_component_highlight_group(
{fg = highlight_color}, icon_highlight_group,
self.options)
end
self.options.icon =
highlight.component_format_highlight(icon_highlight) ..
self.options.icon .. default_highlight
end
else
ok = vim.fn.exists('*WebDevIconsGetFileTypeSymbol')
if ok ~= 0 then
self.options.icon = vim.fn.WebDevIconsGetFileTypeSymbol()
end
end
return data
function FileType:new(options, child)
local new_instance = self._parent:new(options, child or FileType)
if new_instance.options.colored == nil then
new_instance.options.colored = true
end
if new_instance.options.disable_text == nil then
new_instance.options.disable_text = false
end
return new_instance
end
function FileType.update_status() return vim.bo.filetype or '' end
function FileType:apply_icon()
if not self.options.icons_enabled then return end
local icon, icon_highlight_group
local ok, devicons = pcall(require, 'nvim-web-devicons')
if ok then
local f_name, f_extension = vim.fn.expand('%:t'), vim.fn.expand('%:e')
icon, icon_highlight_group = devicons.get_icon(f_name, f_extension)
if icon and self.options.colored then
local highlight_color = utils.extract_highlight_colors(
icon_highlight_group, 'fg')
local is_focused = vim.g.statusline_winid == vim.fn.win_getid()
local default_highlight = highlight.format_highlight(is_focused,
self.options.self
.section)
local icon_highlight = self.options.self.section .. '_' ..
icon_highlight_group
if not utils.highlight_exists(icon_highlight .. '_normal') then
icon_highlight = highlight.create_component_highlight_group(
{fg = highlight_color}, icon_highlight_group,
self.options)
end
icon = highlight.component_format_highlight(icon_highlight) .. icon ..
default_highlight
end
else
ok = vim.fn.exists('*WebDevIconsGetFileTypeSymbol')
if ok ~= 0 then icon = vim.fn.WebDevIconsGetFileTypeSymbol() end
end
if not icon then return end
if self.options.disable_text then
self.status = icon
else
self.status = icon .. ' ' .. self.status
end
return ''
end
return FileType

View File

@ -277,16 +277,56 @@ describe('Filetype component', function()
local opts = build_component_opts({
component_separators = {'', ''},
padding = 0
padding = 0,
})
assert_component('filetype', opts, '%#MyCompHl_normal#*%#lualine_c_normal# lua')
assert.stub(utils.extract_highlight_colors).was_called_with('test_highlight_group', 'fg')
opts.icon = '*'
assert.stub(hl.create_component_highlight_group).was_called_with(
{fg = '#000'}, 'test_highlight_group', opts
)
hl.create_component_highlight_group:revert()
utils.extract_highlight_colors:revert()
package.loaded['nvim-web-devicons'] = nil
end)
it('Doesn\'t color when colored is false', function()
package.loaded['nvim-web-devicons'] = {
get_icon = function()
return '*', 'test_highlight_group'
end
}
local hl = require 'lualine.highlight'
local utils = require 'lualine.utils.utils'
stub(hl, 'create_component_highlight_group')
stub(utils, 'extract_highlight_colors')
hl.create_component_highlight_group.returns('MyCompHl')
utils.extract_highlight_colors.returns('#000')
local opts = build_component_opts({
component_separators = {'', ''},
padding = 0,
colored = false,
})
assert_component('filetype', opts, '* lua')
hl.create_component_highlight_group:revert()
utils.extract_highlight_colors:revert()
package.loaded['nvim-web-devicons'] = nil
end)
it('displays only icon when disable_text is true', function()
package.loaded['nvim-web-devicons'] = {
get_icon = function()
return '*', 'test_highlight_group'
end
}
local opts = build_component_opts({
component_separators = {'', ''},
padding = 0,
colored = false,
disable_text = true,
})
assert_component('filetype', opts, '*')
package.loaded['nvim-web-devicons'] = nil
end)
end)