diff --git a/README.md b/README.md index a1ec6d7..6787b3e 100644 --- a/README.md +++ b/README.md @@ -304,6 +304,12 @@ file_status | true | Displays file status (readonly status, modified status) path | 0 | filename `path` option: 0 = just filename, 1 = relative path, 2 = absolute path symbols | `{modified = '[+]', readonly = '[-]'}` | changes status symbols | table containing one or more symbols | +* `filetype` component options + +Option | Default | Behaviour +:------: | :------: | :----: +colored | true | Displays filetype icon in color if set to `true` + * `diff` component options Option | Default | Behaviour | Format @@ -413,7 +419,7 @@ extensions = { 'fzf' } lualine_c = { {'filename', file_status = true} }, lualine_x = { 'encoding', 'fileformat', 'filetype' }, lualine_y = { 'progress' }, - lualine_z = { 'location' }, + lualine_z = { 'location' }, }, inactive_sections = { lualine_a = { }, @@ -421,7 +427,7 @@ extensions = { 'fzf' } lualine_c = { 'filename' }, lualine_x = { 'location' }, lualine_y = { }, - lualine_z = { } + lualine_z = { } }, extensions = { 'fzf' } } diff --git a/doc/lualine.txt b/doc/lualine.txt index 1d2fe29..7f5ed78 100644 --- a/doc/lualine.txt +++ b/doc/lualine.txt @@ -158,7 +158,7 @@ Lualine defaults~ lualine_c = { 'filename' }, lualine_x = { 'encoding', 'fileformat', 'filetype' }, lualine_y = { 'progress' }, - lualine_z = { 'location' }, + lualine_z = { 'location' }, } inactive_sections = { lualine_a = { }, @@ -166,7 +166,7 @@ Lualine defaults~ lualine_c = { 'filename' }, lualine_x = { 'location' }, lualine_y = { }, - lualine_z = { } + lualine_z = { } } < @@ -412,6 +412,10 @@ In addition, some components have unique options. • symbols (`{modified = '[+]', readonly = '[-]'}`) changes status symbols + • filetype~ + • colored (true) + Displays filetype icon in color if set to `true` + • fileformat~ • icons_enabled (true) Whether to displays icon before component. Colors diff --git a/lua/lualine/components/filetype.lua b/lua/lualine/components/filetype.lua index e63411a..473d48b 100644 --- a/lua/lualine/components/filetype.lua +++ b/lua/lualine/components/filetype.lua @@ -1,5 +1,8 @@ -- Copyright (c) 2020-2021 hoob3rt -- MIT license, see LICENSE for more details. +local highlight = require('lualine.highlight') +local utils = require('lualine.utils.utils') + local FileType = require('lualine.component'):new() FileType.update_status = function(self) @@ -8,7 +11,30 @@ FileType.update_status = function(self) local ok, devicons = pcall(require, 'nvim-web-devicons') if ok then local f_name, f_extension = vim.fn.expand('%:t'), vim.fn.expand('%:e') - self.options.icon = devicons.get_icon(f_name, f_extension) + 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 diff --git a/lua/lualine/extensions/fugitive.lua b/lua/lualine/extensions/fugitive.lua index 7488b05..bd7d7b1 100644 --- a/lua/lualine/extensions/fugitive.lua +++ b/lua/lualine/extensions/fugitive.lua @@ -3,8 +3,8 @@ local M = {} local function fugitive_branch() - local icon = '' -- e0a0 - return icon .. ' ' .. vim.fn.FugitiveHead() + local icon = '' -- e0a0 + return icon .. ' ' .. vim.fn.FugitiveHead() end M.sections = {lualine_a = {fugitive_branch}, lualine_z = {'location'}} diff --git a/lua/lualine/init.lua b/lua/lualine/init.lua index e542e44..1058cb2 100644 --- a/lua/lualine/init.lua +++ b/lua/lualine/init.lua @@ -144,7 +144,7 @@ local function setup_theme() return config.options.theme end vim.api.nvim_err_writeln('theme ' .. tostring(theme_name) .. - ' not found, defaulting to gruvbox') + ' not found, defaulting to gruvbox') return require 'lualine.themes.gruvbox' end local theme = get_theme_from_config() diff --git a/lua/tests/spec/component_spec.lua b/lua/tests/spec/component_spec.lua index e7e80af..dc30a7b 100644 --- a/lua/tests/spec/component_spec.lua +++ b/lua/tests/spec/component_spec.lua @@ -241,6 +241,55 @@ describe('Fileformat component', function() end) end) +describe('Filetype component', function() + local filetype + + before_each(function() + filetype = vim.bo.filetype + vim.bo.filetype = 'lua' + end) + + after_each(function() + vim.bo.filetype = filetype + end) + + it('does not add icon when library unavailable', function() + local opts = build_component_opts({ + component_separators = {'', ''}, + padding = 0 + }) + assert_component('filetype', opts, 'lua') + end) + + it('colors nvim-web-devicons icons', 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 + }) + 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() + end) +end) + describe('Hostname component', function() it('works', function() stub(vim.loop, 'os_gethostname')