diff options
| author | christianchiarulli <[email protected]> | 2021-08-10 15:01:26 -0400 | 
|---|---|---|
| committer | christianchiarulli <[email protected]> | 2021-08-10 15:01:26 -0400 | 
| commit | a6a10e3fdbcfea9d8600772b91e45fb19084a390 (patch) | |
| tree | d73dd44962792cd3eb86b2a5bd3e2878f768e648 | |
| parent | 9a68500333660af76a2b7e75c3d7dcd38f8481b2 (diff) | |
bring back behavior we used to get from astronauta
| -rw-r--r-- | lua/core/autocmds.lua | 5 | ||||
| -rw-r--r-- | lua/utils/ft.lua | 45 | 
2 files changed, 50 insertions, 0 deletions
diff --git a/lua/core/autocmds.lua b/lua/core/autocmds.lua index 62c05802..c00e4ba7 100644 --- a/lua/core/autocmds.lua +++ b/lua/core/autocmds.lua @@ -3,6 +3,11 @@ local autocommands = {}  lvim.autocommands = {    _general_settings = {      { +      "Filetype", +      "*", +      "lua require('utils.ft').do_filetype(vim.fn.expand(\"<amatch>\"))", +    }, +    {        "TextYankPost",        "*",        "lua require('vim.highlight').on_yank({higroup = 'Search', timeout = 200})", diff --git a/lua/utils/ft.lua b/lua/utils/ft.lua new file mode 100644 index 00000000..fcebd1ea --- /dev/null +++ b/lua/utils/ft.lua @@ -0,0 +1,45 @@ +local ft = {} + +ft.find_lua_ftplugins = function(filetype) +  local patterns = { +    string.format("ftplugin/%s.lua", filetype), + +    -- Looks like we don't need this, because the first one works +    -- string.format("after/ftplugin/%s.lua", filetype), +  } + +  local result = {} +  for _, pat in ipairs(patterns) do +    vim.list_extend(result, vim.api.nvim_get_runtime_file(pat, true)) +  end + +  return result +end + +ft.do_filetype = function(filetype) +  local ftplugins = ft.find_lua_ftplugins(filetype) + +  local f_env = setmetatable({ +    -- Override print, so the prints still go through, otherwise it's confusing for people +    print = vim.schedule_wrap(print), +  }, { +    -- Buf default back read/write to whatever is going on in the global landscape +    __index = _G, +    __newindex = _G, +  }) + +  for _, file in ipairs(ftplugins) do +    local f = loadfile(file) +    if not f then +      vim.api.nvim_err_writeln("Unable to load file: " .. file) +    else +      local ok, msg = pcall(setfenv(f, f_env)) + +      if not ok then +        vim.api.nvim_err_writeln("Error while processing file: " .. file .. "\n" .. msg) +      end +    end +  end +end + +return ft  | 
