ft.lua 1.2 KB

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