From a560209c2478cd4c40e329135a13267e3f70ddcf Mon Sep 17 00:00:00 2001 From: shadmansaleh Date: Wed, 12 May 2021 15:17:51 +0600 Subject: [PATCH] Refractor filetype and add disable_text option --- README.md | 3 +- doc/lualine.txt | 6 +- lua/lualine/components/filetype.lua | 89 +++++++++++++++++------------ lua/tests/spec/component_spec.lua | 44 +++++++++++++- 4 files changed, 99 insertions(+), 43 deletions(-) diff --git a/README.md b/README.md index 5e88ffb..d91915b 100644 --- a/README.md +++ b/README.md @@ -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 } } } diff --git a/doc/lualine.txt b/doc/lualine.txt index c1a3a31..e5676d9 100644 --- a/doc/lualine.txt +++ b/doc/lualine.txt @@ -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: diff --git a/lua/lualine/components/filetype.lua b/lua/lualine/components/filetype.lua index 473d48b..4536933 100644 --- a/lua/lualine/components/filetype.lua +++ b/lua/lualine/components/filetype.lua @@ -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 diff --git a/lua/tests/spec/component_spec.lua b/lua/tests/spec/component_spec.lua index dc30a7b..6c4845a 100644 --- a/lua/tests/spec/component_spec.lua +++ b/lua/tests/spec/component_spec.lua @@ -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)