Skip to content

Commit a59a9b7

Browse files
fix(icons): update get() to work when cwd is removed from disk
Resolve #2455 Co-authored-by: Evgeni Chasnovski <evgeni.chasnovski@gmail.com>
1 parent 4236dfa commit a59a9b7

3 files changed

Lines changed: 23 additions & 1 deletion

File tree

lua/mini/icons.lua

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2216,7 +2216,12 @@ H.filetype_match = function(filename)
22162216
H.set_buf_name(buf_id, 'filetype-match-scratch')
22172217
H.scratch_buf_id = buf_id
22182218
end
2219-
return vim.filetype.match({ filename = filename, buf = H.scratch_buf_id })
2219+
2220+
-- `vim.filetype.match` can error for relative path when cwd does not exist
2221+
local ok, ft = pcall(vim.filetype.match, { filename = filename, buf = H.scratch_buf_id })
2222+
if ok then return ft end
2223+
ok, ft = pcall(vim.filetype.match, { filename = '~/' .. filename, buf = H.scratch_buf_id })
2224+
return ok and ft or nil
22202225
end
22212226

22222227
-- Utilities ------------------------------------------------------------------

tests/dir-icons/file.txt

Whitespace-only changes.

tests/test_icons.lua

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,23 @@ T['get()']['can be used without `setup()`'] = function()
653653
eq(child.lua_get('{ require("mini.icons").get("default", "file") }'), { '󰈔', 'MiniIconsGrey', false })
654654
end
655655

656+
T['get()']['works with deleted cwd'] = function()
657+
local temp_dir = 'tests/dir-icons/temp'
658+
vim.fn.mkdir(temp_dir, 'p')
659+
MiniTest.finally(function() vim.fn.delete(temp_dir, 'rf') end)
660+
661+
local file_path = child.fn.fnamemodify('tests/dir-icons/file.txt', ':p')
662+
child.fn.chdir(temp_dir)
663+
vim.fn.delete(temp_dir, 'rf')
664+
665+
-- Should use `vim.filetype.match` without errors due to missing cwd
666+
eq(get('extension', 'xpm'), { '󰍹', 'MiniIconsYellow', false })
667+
eq(get('extension', 'unknown'), { '󰈔', 'MiniIconsGrey', true })
668+
eq(get('file', 'hello.tcsh'), { '', 'MiniIconsAzure', false })
669+
eq(get('file', file_path), { '󰦪', 'MiniIconsYellow', false })
670+
eq(get('file', 'does-not-exist'), { '󰈔', 'MiniIconsGrey', true })
671+
end
672+
656673
T['get()']['handles deleting all buffers'] = function()
657674
-- As `vim.filetype.match()` requries a buffer to be more useful, make sure
658675
-- that there is a persistent helper buffer

0 commit comments

Comments
 (0)