Skip to content
Rogin Farrer edited this page Nov 1, 2021 · 8 revisions

One of the features of lir.nvim is that it allows you to easily define and map your own actions.

NOTE: Feel free to add your own actions!

home()

local function home()
  vim.cmd('edit ' .. vim.fn.expand('$HOME'))
end

gomi()

Use b4b4r07/gomi

local function gomi()
  local ctx = lir.get_context()

  local marked_items = ctx:get_marked_items()
  if #marked_items == 0 then
    utils.error('Please mark one or more.')
    return
  end

  local path_list = vim.tbl_map(function(items)
    return esc_path(items.fullpath)
  end, marked_items)

  if vim.fn.confirm("Delete files?", "&Yes\n&No", 2) ~= 1 then
    return
  end
  vim.fn.system('gomi ' .. vim.fn.join(path_list))
  actions.reload()
end

nop()

local function nop()
end

goto_git_root()

local function goto_git_root()
  local dir = require'lspconfig.util'.find_git_ancestor(vim.fn.getcwd())
  if dir == nil or dir == "" then
    return
  end
  vim.cmd ('e ' .. dir)
end

rename_and_jump()

local function rename_and_jump()
  local ctx = lir.get_context()
  local old = string.gsub(ctx:current_value(), '/$', '')
  local new = vim.fn.input('Rename: ', old)
  if new == '' or new == old then
    return
  end

  if new == '.' or new == '..' or string.match(new, '[/\\]') then
    utils.error('Invalid name: ' .. new)
    return
  end

  if not vim.loop.fs_rename(ctx.dir .. old, ctx.dir .. new) then
    utils.error('Rename failed')
  end

  actions.reload()

  local lnum = lir.get_context():indexof(new)
  if lnum then
    vim.cmd(tostring(lnum))
  end
end

input_newfile()

  • If the input value ends with '/', the directory will be created and
  • If the input value contains '/', and the directory does not exist, it will be created recursively
  • If the input file name does not contain '.' or '/', check if it is a directory.
  • If the first string is '.' and show_hidden_files is false, set it to true and display it again.
local lir = require('lir')
local utils = lir.utils
local config = lir.config
local Path = require('plenary.path')

local function lcd(path)
  vim.cmd(string.format(':lcd %s', path))
end

local no_confirm_patterns = {
  '^LICENSE$',
  '^Makefile$'
}

local need_confirm = function(filename)
  for _, pattern in ipairs(no_confirm_patterns) do
    if filename:match(pattern) then
      return false
    end
  end
  return true
end

local function input_newfile()
  local save_curdir = vim.fn.getcwd()
  lcd(lir.get_context().dir)
  local name = vim.fn.input('New file: ', '', 'file')
  lcd(save_curdir)

  if name == '' then
    return
  end

  if name == '.' or name == '..' then
    utils.error('Invalid file name: ' .. name)
    return
  end

  -- If I need to check, I will.
  if need_confirm(name) then
    -- '.' is not included or '/' is not included, then
    -- I may have entered it as a directory, I'll check.
    if not name:match('%.') and not name:match('/') then
      if vim.fn.confirm("Directory?", "&No\n&Yes", 1) == 2 then
        name = name .. '/'
      end
    end
  end

  local path = Path:new(lir.get_context().dir .. name)
  if string.match(name, '/$') then
    -- mkdir
    name = name:gsub('/$', '')
    path:mkdir({
      parents = true,
      mode = tonumber('700', 8),
      exists_ok = false
    })
  else
    -- touch
    path:touch({
      parents = true,
      mode = tonumber('644', 8),
    })
  end

  -- If the first character is '.' and show_hidden_files is false, set it to true
  if name:match([[^%.]]) and not config.values.show_hidden_files then
    config.values.show_hidden_files = true
  end

  actions.reload()

  -- Jump to a line in the parent directory of the file you created.
  local lnum = lir.get_context():indexof(name:match('^[^/]+'))
  if lnum then
    vim.cmd(tostring(lnum))
  end
end
Clone this wiki locally