Skip to content
This repository has been archived by the owner on Sep 20, 2023. It is now read-only.

Commit

Permalink
refactor: make component icon take precedence over provider icon
Browse files Browse the repository at this point in the history
  • Loading branch information
famiu committed Sep 16, 2021
1 parent 9b63462 commit 6961f91
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 89 deletions.
9 changes: 6 additions & 3 deletions lua/feline/generator.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ local colors = feline.colors
local separators = feline.separators
local providers = require('feline.providers')

local M ={}
M.highlights = {}
local M = {
highlights = {}
}

-- Check if current buffer is forced to have inactive statusline
local function is_forced_inactive()
Expand Down Expand Up @@ -213,7 +214,9 @@ local function parse_component(component, winid)

local hlname = parse_hl(hl)

if icon == nil and str ~= '' then
if is_component_empty then
icon = nil
elseif component.icon then
icon = component.icon
end

Expand Down
1 change: 1 addition & 0 deletions lua/feline/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ function M.reset_highlights()
M.update_inactive_windows()
end

-- Setup Feline using the provided configuration options
function M.setup(config)
-- Check if Neovim version is 0.5 or greater
if fn.has('nvim-0.5') ~= 1 then
Expand Down
1 change: 0 additions & 1 deletion lua/feline/presets/default.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ local lsp = require('feline.providers.lsp')
local vi_mode_utils = require('feline.providers.vi_mode')

local api = vim.api
local b = vim.b

local M = {
components = {
Expand Down
1 change: 0 additions & 1 deletion lua/feline/presets/noicon.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ local lsp = require('feline.providers.lsp')
local vi_mode_utils = require('feline.providers.vi_mode')

local api = vim.api
local b = vim.b

local M = {
components = {
Expand Down
27 changes: 8 additions & 19 deletions lua/feline/providers/file.lua
Original file line number Diff line number Diff line change
Expand Up @@ -94,28 +94,17 @@ function M.file_info(component, winid)
local extension = fn.fnamemodify(filename, ':e')
local readonly_str, modified_str

local icon = component.icon

if not icon then
local ic, hl_group = require("nvim-web-devicons").get_icon(
filename, extension, { default = true }
)

local colored_icon
local icon_str, icon_hlname = require("nvim-web-devicons").get_icon(
filename, extension, { default = true }
)

if component.colored_icon == nil then
colored_icon = true
else
colored_icon = component.colored_icon
end
local icon = { str = icon_str }

icon = { str = ic }
if component.colored_icon == nil or component.colored_icon then
local fg = api.nvim_get_hl_by_name(icon_hlname, true).foreground

if colored_icon then
local fg = api.nvim_get_hl_by_name(hl_group, true)['foreground']
if fg then
icon["hl"] = { fg = string.format('#%06x', fg) }
end
if fg then
icon.hl = { fg = string.format('#%06x', fg) }
end
end

Expand Down
56 changes: 15 additions & 41 deletions lua/feline/providers/git.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,63 +2,37 @@ local api = vim.api

local M = {}

function M.git_branch(component, winid)
local icon
local str = ''
function M.git_branch(_, winid)
local ok, head = pcall(api.nvim_buf_get_var, api.nvim_win_get_buf(winid), 'gitsigns_head')

if not ok then head = vim.g.gitsigns_head or '' end

if head ~= '' then
icon = component.icon or ''
str = str .. head
end

return str, icon
return head, ''
end

function M.git_diff_added(component, winid)
-- Common function used by the git providers
local function git_diff(winid, type)
local ok, gsd = pcall(api.nvim_buf_get_var, api.nvim_win_get_buf(winid), 'gitsigns_status_dict')

local icon
local str = ''

if ok and gsd['added'] and gsd['added'] > 0 then
icon = component.icon or ''
str = str .. gsd.added
if ok and gsd[type] and gsd[type] > 0 then
return tostring(gsd[type])
end

return str, icon
return ''
end

function M.git_diff_removed(component, winid)
local ok, gsd = pcall(api.nvim_buf_get_var, api.nvim_win_get_buf(winid), 'gitsigns_status_dict')

local icon
local str = ''

if ok and gsd['removed'] and gsd['removed'] > 0 then
icon = component.icon or ''
str = str .. gsd.removed
end

return str, icon
function M.git_diff_added(_, winid)
return git_diff(winid, 'added'), ''
end

function M.git_diff_changed(component, winid)
local ok, gsd = pcall(api.nvim_buf_get_var, api.nvim_win_get_buf(winid), 'gitsigns_status_dict')

local icon
local str = ''

if ok and gsd['changed'] and gsd['changed'] > 0 then
icon = component.icon or ''
str = str .. gsd.changed
end
function M.git_diff_removed(_, winid)
return git_diff(winid, 'removed'), ''
end

return str, icon
function M.git_diff_changed(_, winid)
return git_diff(winid, 'changed'), ''
end

-- Utility function to check if git provider information is available
function M.git_info_exists(winid)
local ok, _ = pcall(api.nvim_buf_get_var, api.nvim_win_get_buf(winid), 'gitsigns_status_dict')
return ok
Expand Down
36 changes: 18 additions & 18 deletions lua/feline/providers/lsp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ function M.get_diagnostics_count(severity, bufnr)
if not active_clients then return nil end

local count = 0

for _, client in pairs(active_clients) do
count = count + vim.lsp.diagnostic.get_count(bufnr, severity, client.id)
end
Expand All @@ -30,39 +31,38 @@ function M.diagnostics_exist(severity, bufnr)
return diagnostics_count and diagnostics_count > 0
end

function M.lsp_client_names(component, winid)
function M.lsp_client_names(_, winid)
local clients = {}
local icon = component.icon or ''

for _, client in pairs(vim.lsp.buf_get_clients(api.nvim_win_get_buf(winid))) do
clients[#clients+1] = client.name
end

return table.concat(clients, ' '), icon
return table.concat(clients, ' '), ''
end

function M.diagnostic_errors(component, winid)
local count = M.get_diagnostics_count('Error', api.nvim_win_get_buf(winid))
-- Common function used by the diagnostics providers
local function diagnostics(winid, severity)
local count = M.get_diagnostics_count(severity, api.nvim_win_get_buf(winid))

if not count or count == 0 then return '' end
return tostring(count), (component.icon or '')
return tostring(count)
end

function M.diagnostic_warnings(component, winid)
local count = M.get_diagnostics_count('Warning', api.nvim_win_get_buf(winid))
if not count or count == 0 then return '' end
return tostring(count), (component.icon or '')
function M.diagnostic_errors(_, winid)
return diagnostics(winid, 'Error'), ''
end

function M.diagnostic_hints(component, winid)
local count = M.get_diagnostics_count('Hint', api.nvim_win_get_buf(winid))
if not count or count == 0 then return '' end
return tostring(count), (component.icon or '')
function M.diagnostic_warnings(_, winid)
return diagnostics(winid, 'Warning'), ''
end

function M.diagnostic_info(component, winid)
local count = M.get_diagnostics_count('Information', api.nvim_win_get_buf(winid))
if not count or count == 0 then return '' end
return tostring(count), (component.icon or '')
function M.diagnostic_hints(_, winid)
return diagnostics(winid, 'Hint'), ''
end

function M.diagnostic_info(_, winid)
return diagnostics(winid, 'Information'), ''
end

return M
10 changes: 4 additions & 6 deletions lua/feline/providers/vi_mode.lua
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,10 @@ function M.get_mode_highlight_name()
end

function M.vi_mode(component)
if component.icon then
if component.icon == '' then
return M.get_vim_mode()
else
return component.icon
end
if component.icon == '' then
return M.get_vim_mode()
elseif component.icon ~= nil then
return component.icon
else
return ''
end
Expand Down

2 comments on commit 6961f91

@Olical
Copy link

@Olical Olical commented on 6961f91 Sep 17, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Heads up, this commit breaks the plugin for anyone that doesn't have nvim-web-devicons installed! It's no longer an optional dependency. I'll open an issue.

@famiu
Copy link
Owner Author

@famiu famiu commented on 6961f91 Sep 17, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Heads up, this commit breaks the plugin for anyone that doesn't have nvim-web-devicons installed! It's no longer an optional dependency. I'll open an issue.

that was just fixed in the latest develop commit

Please sign in to comment.