Luigit
repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by admin

neovim/lua/bugabinga/file_explorer.lua

Raw
local puk = require 'std.puk'
local map = require 'std.map'
local auto = require 'std.auto'
local rename = require 'std.rename'

local oil = puk 'oil'

-- Declare a global function to retrieve the current directory
function _G.get_oil_winbar()
  local dir = require("oil").get_current_dir()
  if dir then
    return vim.fn.fnamemodify(dir, ":~")
  else
    -- If there is no current directory (e.g. over ssh), just show the buffer name
    return vim.api.nvim_buf_get_name(0)
  end
end

oil.setup {
  default_file_explorer = true,
  -- Set to true to watch the file system for changes and reload oil
  watch_for_changes = true,
  skip_confirm_for_simple_edits = true,
  prompt_save_on_select_new_entry = false,
  delete_to_trash = true,

  columns = { 'icon' },
  win_options = {
    wrap = true,
    cursorline = true,
    -- show CWD in winbar. needs global function, see above
    winbar = "%!v:lua.get_oil_winbar()",
  },
  keymaps = {
    ['q'] = 'actions.close',
    ['<C-v>'] = 'actions.select_vsplit',
    ['<C-s>'] = 'actions.select_split',
    ['`'] = 'actions.tcd',
    ['~'] = '<cmd>edit $HOME<CR>',
    gs = {
      callback = function()
        local prefills = { paths = require('oil').get_current_dir() }
        local grug_far = require 'grug-far'
        if not grug_far.has_instance 'explorer' then
          grug_far.open { instanceName = 'explorer', prefills = prefills, staticTitle = 'Find and Replace from Explorer' }
        else
          grug_far.get_instance('explorer'):open()
          grug_far.get_instance('explorer'):update_input_values(prefills, false)
        end
      end,
      desc = 'oil: Search in directory',
    },
  },
  float = {
    max_width = 69,
    min_width = 69,
    border = vim.g.border_style,
    padding = 2,
  },
  -- Configuration for the floating action confirmation window
  confirmation = {
    border = vim.g.border_style,
  },
  -- Configuration for the floating progress window
  progress = {
    border = vim.g.border_style,
  },
  -- Configuration for the floating SSH window
  ssh = {
    border = vim.g.border_style,
  },
  -- Configuration for the floating keymaps help window
  keymaps_help = {
    border = vim.g.border_style,
  },
  view_options = {
    show_hidden = true,
    natural_order = true,
    is_always_hidden = function(name) return name == '..' end,
  },
}

map.normal {
  description = 'Open parent directory in buffer.',
  category = 'files',
  keys = '-',
  command = oil.open,
}

auto 'notify_lsp_on_rename' {
  description = '',
  events = 'User',
  pattern = 'OilActionsPost',
  command = function(event)
    for _, action in ipairs(event.data.actions) do
      if action.type == "move" then
        rename.on_rename_file(action.src_url, action.dest_url)
      end
    end
  end,
}