0
0

init.lua 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. local M = {
  2. formatters = {
  3. lsp = {},
  4. filetype = {}
  5. },
  6. commands = {}
  7. }
  8. M.setup = function()
  9. local group = vim.api.nvim_create_augroup("SaveFormatter", { clear = true })
  10. vim.api.nvim_create_autocmd({ "BufWritePost" }, {
  11. group = group,
  12. pattern = { "*" },
  13. callback = function(args) M.format(args.buf, true) end
  14. })
  15. vim.api.nvim_create_autocmd({ "BufWritePre" }, {
  16. group = group,
  17. pattern = { "*" },
  18. callback = function(args) M.format(args.buf, false) end
  19. })
  20. vim.api.nvim_create_autocmd({ "LspAttach" }, {
  21. group = group,
  22. callback = function(args)
  23. local client = vim.lsp.get_client_by_id(args.data.client_id)
  24. if client ~= nil and client:supports_method('textDocument/format') then
  25. M.add("lsp", client.name, nil, false)
  26. end
  27. end
  28. })
  29. end
  30. --- @param buf integer
  31. --- @param did_write boolean
  32. M.format = function(buf, did_write)
  33. local filetype = vim.bo[buf].filetype
  34. local lsps = vim.lsp.get_clients({ bufnr = buf })
  35. for _, client in pairs(lsps) do
  36. if M.formatters.lsp[client.name] ~= nil and M.formatters.lsp[client.name].enabled then
  37. if not did_write then
  38. vim.lsp.buf.format({ bufnr = buf })
  39. end
  40. return
  41. end
  42. end
  43. if M.formatters.filetype[filetype] ~= nil and M.formatters.filetype[filetype].enabled and M.commands[M.formatters.filetype[filetype].command] ~= nil then
  44. M.commands[M.formatters.filetype[filetype].command](buf)
  45. if did_write then
  46. vim.api.nvim_buf_call(buf, function()
  47. vim.api.nvim_exec2("noautocmd edit", {})
  48. end)
  49. end
  50. end
  51. end
  52. ---@param type "lsp"|"filename"
  53. ---@param name string
  54. ---@param enabled boolean
  55. M.toggle = function(type, name, enabled)
  56. if M.formatters[type] == nil or M.formatters[type][name] == nil then return end
  57. if enabled == nil then
  58. M.formatters[type][name].enabled = not M.formatters[type][name].enabled
  59. else
  60. M.formatters[type][name].enabled = enabled
  61. end
  62. end
  63. ---@param type "lsp"|"filetype"
  64. ---@param name string
  65. ---@param command string|nil
  66. ---@param enable boolean|nil
  67. M.add = function(type, name, command, enable)
  68. if M.formatters[type] == nil then return end
  69. local formatter = { enabled = false }
  70. if type == "filetype" then
  71. if command == nil then return end
  72. formatter.command = command
  73. end
  74. if enable then formatter.enabled = true end
  75. if M.formatters[type][name] ~= nil then
  76. formatter.enabled = M.formatters[type][name].enabled
  77. end
  78. M.formatters[type][name] = formatter
  79. end
  80. ---@param name string
  81. ---@param value function
  82. M.command = function(name, value)
  83. M.commands[name] = value
  84. end
  85. M.picker = function()
  86. if pcall(require, "telescope.pickers") then else return end
  87. local pickers = require("telescope.pickers")
  88. local themes = require("telescope.themes")
  89. local actions = require("telescope.actions")
  90. local action_state = require("telescope.actions.state")
  91. local finders = require("telescope.finders")
  92. local conf = require("telescope.config").values
  93. local entry_display = require("telescope.pickers.entry_display")
  94. local items = {}
  95. for key, value in pairs(M.formatters.lsp) do
  96. table.insert(items, {
  97. type = "lsp",
  98. name = key,
  99. enabled = value.enabled
  100. })
  101. end
  102. for key, value in pairs(M.formatters.filetype) do
  103. table.insert(items, {
  104. type = "filetype",
  105. name = key,
  106. enabled = value.enabled,
  107. command = value.command
  108. })
  109. end
  110. pickers.new(themes.get_dropdown({}), {
  111. prompt_title = "Save Formatter",
  112. finder = finders.new_table({
  113. results = items,
  114. entry_maker = function(entry)
  115. local displayer = entry_display.create({
  116. separator = " ▏",
  117. items = { { width = 32 }, { width = 8 }, { remaining = true } }
  118. })
  119. local command = "LSP"
  120. if entry.command ~= nil then command = entry.command end
  121. return {
  122. value = entry,
  123. display = function()
  124. return displayer({ { entry.name }, { entry.enabled }, { command } })
  125. end,
  126. ordinal = entry.name
  127. }
  128. end
  129. }),
  130. sorter = conf.generic_sorter({}),
  131. attach_mappings = function(buf, _)
  132. actions.select_default:replace(function()
  133. local t = action_state.get_selected_entry().value
  134. M.formatters[t.type][t.name].enabled = not t.enabled
  135. actions.close(buf)
  136. M.picker()
  137. end)
  138. return true
  139. end
  140. }):find()
  141. end
  142. return M