ft.lua 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. local ft = {}
  2. ft.find_lua_ftplugins = function(filetype)
  3. local patterns = {
  4. string.format("ftplugin/%s.lua", filetype),
  5. -- Looks like we don't need this, because the first one works
  6. -- string.format("after/ftplugin/%s.lua", filetype),
  7. }
  8. local result = {}
  9. for _, pat in ipairs(patterns) do
  10. vim.list_extend(result, vim.api.nvim_get_runtime_file(pat, true))
  11. end
  12. return result
  13. end
  14. ft.do_filetype = function(filetype)
  15. local ftplugins = ft.find_lua_ftplugins(filetype)
  16. local f_env = setmetatable({
  17. -- Override print, so the prints still go through, otherwise it's confusing for people
  18. print = vim.schedule_wrap(print),
  19. }, {
  20. -- Buf default back read/write to whatever is going on in the global landscape
  21. __index = _G,
  22. __newindex = _G,
  23. })
  24. for _, file in ipairs(ftplugins) do
  25. local f = loadfile(file)
  26. if not f then
  27. vim.api.nvim_err_writeln("Unable to load file: " .. file)
  28. else
  29. local ok, msg = pcall(setfenv(f, f_env))
  30. if not ok then
  31. vim.api.nvim_err_writeln("Error while processing file: " .. file .. "\n" .. msg)
  32. end
  33. end
  34. end
  35. end
  36. return ft