init.lua 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. local utils = {}
  2. -- recursive Print (structure, limit, separator)
  3. local function r_inspect_settings(structure, limit, separator)
  4. limit = limit or 100 -- default item limit
  5. separator = separator or "." -- indent string
  6. if limit < 1 then
  7. print "ERROR: Item limit reached."
  8. return limit - 1
  9. end
  10. if structure == nil then
  11. io.write("-- O", separator:sub(2), " = nil\n")
  12. return limit - 1
  13. end
  14. local ts = type(structure)
  15. if ts == "table" then
  16. for k, v in pairs(structure) do
  17. -- replace non alpha keys with ["key"]
  18. if tostring(k):match "[^%a_]" then
  19. k = '["' .. tostring(k) .. '"]'
  20. end
  21. limit = r_inspect_settings(v, limit, separator .. "." .. tostring(k))
  22. if limit < 0 then
  23. break
  24. end
  25. end
  26. return limit
  27. end
  28. if ts == "string" then
  29. -- escape sequences
  30. structure = string.format("%q", structure)
  31. end
  32. separator = separator:gsub("%.%[", "%[")
  33. if type(structure) == "function" then
  34. -- don't print functions
  35. io.write("-- lvim", separator:sub(2), " = function ()\n")
  36. else
  37. io.write("lvim", separator:sub(2), " = ", tostring(structure), "\n")
  38. end
  39. return limit - 1
  40. end
  41. function utils.generate_settings()
  42. -- Opens a file in append mode
  43. local file = io.open("lv-settings.lua", "w")
  44. -- sets the default output file as test.lua
  45. io.output(file)
  46. -- write all `lvim` related settings to `lv-settings.lua` file
  47. r_inspect_settings(lvim, 10000, ".")
  48. -- closes the open file
  49. io.close(file)
  50. end
  51. -- autoformat
  52. function utils.toggle_autoformat()
  53. if lvim.format_on_save then
  54. require("core.autocmds").define_augroups {
  55. autoformat = {
  56. {
  57. "BufWritePre",
  58. "*",
  59. ":silent lua vim.lsp.buf.formatting_sync()",
  60. },
  61. },
  62. }
  63. end
  64. if not lvim.format_on_save then
  65. vim.cmd [[
  66. if exists('#autoformat#BufWritePre')
  67. :autocmd! autoformat
  68. endif
  69. ]]
  70. end
  71. end
  72. function utils.reload_lv_config()
  73. vim.cmd "source ~/.local/share/lunarvim/lvim/lua/settings.lua"
  74. vim.cmd("source " .. USER_CONFIG_PATH)
  75. vim.cmd "source ~/.local/share/lunarvim/lvim/lua/plugins.lua"
  76. local plugins = require "plugins"
  77. local plugin_loader = require("plugin-loader").init()
  78. utils.toggle_autoformat()
  79. plugin_loader:load { plugins, lvim.plugins }
  80. vim.cmd ":PackerCompile"
  81. vim.cmd ":PackerInstall"
  82. require("keymappings").setup()
  83. -- vim.cmd ":PackerClean"
  84. end
  85. function utils.check_lsp_client_active(name)
  86. local clients = vim.lsp.get_active_clients()
  87. for _, client in pairs(clients) do
  88. if client.name == name then
  89. return true
  90. end
  91. end
  92. return false
  93. end
  94. function utils.get_active_client_by_ft(filetype)
  95. local clients = vim.lsp.get_active_clients()
  96. for _, client in pairs(clients) do
  97. if client.name == lvim.lang[filetype].lsp.provider then
  98. return client
  99. end
  100. end
  101. return nil
  102. end
  103. -- TODO: consider porting this logic to null-ls instead
  104. function utils.get_supported_linters_by_filetype(filetype)
  105. local null_ls = require "null-ls"
  106. local matches = {}
  107. for _, provider in pairs(null_ls.builtins.diagnostics) do
  108. if vim.tbl_contains(provider.filetypes, filetype) then
  109. local provider_name = provider.name
  110. table.insert(matches, provider_name)
  111. end
  112. end
  113. return matches
  114. end
  115. function utils.get_supported_formatters_by_filetype(filetype)
  116. local null_ls = require "null-ls"
  117. local matches = {}
  118. for _, provider in pairs(null_ls.builtins.formatting) do
  119. if provider.filetypes and vim.tbl_contains(provider.filetypes, filetype) then
  120. -- table.insert(matches, { provider.name, ft })
  121. table.insert(matches, provider.name)
  122. end
  123. end
  124. return matches
  125. end
  126. function utils.unrequire(m)
  127. package.loaded[m] = nil
  128. _G[m] = nil
  129. end
  130. function utils.gsub_args(args)
  131. if args == nil or type(args) ~= "table" then
  132. return args
  133. end
  134. local buffer_filepath = vim.fn.fnameescape(vim.api.nvim_buf_get_name(0))
  135. for i = 1, #args do
  136. args[i] = string.gsub(args[i], "${FILEPATH}", buffer_filepath)
  137. end
  138. return args
  139. end
  140. function utils.lvim_log(msg)
  141. if lvim.debug then
  142. vim.notify(msg, vim.log.levels.DEBUG)
  143. end
  144. end
  145. return utils
  146. -- TODO: find a new home for these autocommands