summary refs log tree commit diff
path: root/.vim/lua/internal/exrc.lua
blob: 6b7742b9c015eb70bbfa44f500da447e7b1977ff (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
local root_pattern = require('lspconfig.util').root_pattern

local M = {
  config = {
    filenames = {'.nvimrc'}
  }
}

M.setup = function (opt)
  vim.validate {
    filenames = { opt.filenames, 'table', true },
  }

  M.config = vim.tbl_extend('force', M.config, opt)

  local group = vim.api.nvim_create_augroup('ExrcLoad', { clear = true })
  vim.api.nvim_create_autocmd('VimEnter', {
    callback = M.load,
    group = group,
  })
end

local load = function (root, filename)
  local fpath = root .. '/' .. filename
  if root and vim.loop.fs_stat(fpath) then
    vim.cmd('luafile ' .. fpath)
    vim.notify('[exrc] loaded')
  else
    error(string.format('[exrc] %s not found', fpath))
  end
end

M.load = function ()
  local root = vim.fn.getcwd()
  for _, fname in ipairs(M.config.filenames) do
    local ok = false
    ok, _ = pcall(load, root, fname)
    if ok then return true end

    -- try harder by searching up the dir tree
    root = root_pattern(fname)(root)
    ok, _ = pcall(load, root, fname)
    if ok then return true end
  end

  return false
end

return M