-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
adding file icons #244
Merged
Merged
adding file icons #244
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
local M = {} | ||
|
||
---@class FileIconsProvider | ||
---@field type FileIconsProviderType | ||
---@field _lib? any | ||
---@field get_lib fun():(lib: any) | ||
---@field get_icon fun(lib: any, path: string):(icon:string, icon_hl: string) | ||
|
||
---@type FileIconsProvider[] | ||
local providers = { | ||
{ | ||
type = 'nvim-web-devicons', | ||
get_lib = function() | ||
local found, lib = pcall(require, 'nvim-web-devicons') | ||
if not found then | ||
return nil | ||
end | ||
|
||
-- check if setup() called | ||
if not lib.has_loaded() then | ||
return nil | ||
end | ||
|
||
return lib | ||
end, | ||
get_icon = function(self, path) | ||
-- first, try to match extensions like .spec.js | ||
local basename = vim.fs.basename(path) | ||
local multi_dot_extension = basename:match('[^.]*%.(.+)$') | ||
local icon, hl = self._lib.get_icon(path, multi_dot_extension, { default = false }) | ||
if icon then | ||
return icon, hl | ||
end | ||
|
||
local extension = string.match(path, '.+%.(.+)$') | ||
return self._lib.get_icon(path, extension, { default = true }) | ||
end, | ||
}, | ||
{ | ||
type = 'mini.icons', | ||
get_lib = function() | ||
local found, lib = pcall(require, 'mini.icons') | ||
if not found then | ||
return nil | ||
end | ||
-- according to mini.icons docs, need to check this | ||
-- to make sure setup has been called! | ||
-- selene: allow(global_usage) | ||
---@diagnostic disable-next-line | ||
if not _G.MiniIcons then | ||
return nil | ||
end | ||
|
||
return lib | ||
end, | ||
get_icon = function(self, path) | ||
return self._lib.get('file', path) | ||
end, | ||
}, | ||
} | ||
|
||
--- gets the icons provider | ||
---@param type FileIconsProviderType | ||
function M.getProvider(type) | ||
if type == false then | ||
return nil | ||
end | ||
|
||
for _, provider in ipairs(providers) do | ||
local lib = provider.get_lib() | ||
|
||
if lib then | ||
if type == 'first_available' or provider.type == type then | ||
local new_provider = vim.deepcopy(provider) | ||
new_provider._lib = lib | ||
return new_provider | ||
end | ||
else | ||
if provider.type == type then | ||
return nil | ||
end | ||
end | ||
end | ||
|
||
return nil | ||
end | ||
|
||
return M |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@MagicDuck, hi! Saw your Reddit post about adding support for 'mini.icons' and decided to see if it does the
_G.MiniIcons
check. It does, thanks for reading 'mini.icons' help page :)One thing I'd like to mention is that this whole
get_lib
can be compressed to a singlereturn _G.MiniIcons
. This object is both an indication that user has runrequire('mini.icons').setup()
(i.e. opted in for using it and not only has it available from the full 'mini.nvim' install) and the wholerequire('mini.icons')
table itself.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Haha, no problem! Thank you for building mini.icons! :)
I’ll make a note for myself to update this if I get a chance.