neovim

Personal neovim configuration files
git clone git://gtms.dev/neovim
Log | Files | Refs

files.lua (3008B)


      1 local Loclist = require('sidebar-nvim.components.loclist')
      2 local has_devicons, devicons = pcall(require, 'nvim-web-devicons')
      3 
      4 local M = {}
      5 
      6 local icons = { --
      7   directory_closed = '',
      8   file = '',
      9 }
     10 
     11 local loclist = Loclist:new({ommit_single_group = false, show_group_count = false})
     12 
     13 local function get_fileicon(filename)
     14   if has_devicons and devicons.has_loaded() then
     15     local extension = filename:match('^.+%.(.+)$')
     16     local fileicon, _ = devicons.get_icon_color(filename, extension)
     17     local highlight = 'SidebarNvimNormal'
     18 
     19     if extension then highlight = 'DevIcon' .. extension end
     20     return {text = fileicon or icons['file'], hl = highlight}
     21   end
     22   return {text = icons['file'] .. ' '}
     23 end
     24 
     25 local function build_loclist(parent, files)
     26   local items = {}
     27 
     28   for _, file in ipairs(files) do
     29     if file.type == 'file' then
     30       local icon = get_fileicon(file.name)
     31 
     32       items[#items + 1] = {
     33         group = parent,
     34         left = { --
     35           {text = icon.text .. ' ', hl = icon.hl},
     36           {text = file.name},
     37         },
     38         name = file.name,
     39         path = file.path,
     40         type = file.type,
     41         parent = file.parent,
     42         node = file,
     43       }
     44     elseif file.type == 'directory' then
     45       local icon
     46       icon = icons['directory_closed']
     47 
     48       items[#items + 1] = {
     49         group = parent,
     50         left = { --
     51           {text = icon .. ' ' .. file.name, hl = 'SidebarNvimFilesDirectory'},
     52         },
     53         name = file.name,
     54         path = file.path,
     55         type = file.type,
     56         parent = file.parent,
     57         node = file,
     58       }
     59     end
     60   end
     61   return items
     62 end
     63 
     64 M.update = function()
     65   local ft = vim.api.nvim_buf_get_option(0, 'ft')
     66   if ft == '' then return end
     67   if ft == 'SidebarNvim' then return end
     68   if ft == 'TelescopePrompt' then return end
     69   if ft == 'help' then return end
     70 
     71   local directory = vim.fn.expand('%:p:h')
     72   local relative = vim.fn.expand('%:h')
     73 
     74   local list = {}
     75   local handle = vim.loop.fs_scandir(directory)
     76   while handle do
     77     local filename, filetype = vim.loop.fs_scandir_next(handle)
     78     if not filename then break end
     79     if filetype == 'file' then
     80       table.insert(list, { --
     81         name = filename,
     82         type = 'file',
     83         path = directory .. '/' .. filename,
     84       })
     85     elseif filetype == 'directory' then
     86       table.insert(list, { --
     87         name = filename,
     88         type = 'directory',
     89         path = directory .. '/' .. filename,
     90       })
     91     end
     92   end
     93 
     94   loclist:set_items(build_loclist(relative, list), {remove_groups = true})
     95 end
     96 
     97 M.section = {
     98   title = 'Files',
     99   setup = function()
    100     vim.api.nvim_exec([[
    101           augroup user_sidebar_files_update
    102               autocmd!
    103               autocmd BufEnter * lua require'tms.p.sidebar.files'.update()
    104           augroup END
    105           ]], false)
    106   end,
    107   update = function(_) M.update() end,
    108   draw = function(ctx)
    109     local lines = {}
    110     local hl = {}
    111 
    112     loclist:draw(ctx, lines, hl)
    113 
    114     return {lines = lines, hl = hl}
    115   end,
    116 }
    117 
    118 return M