init.lua 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. local commands = {
  2. prettier =
  3. [[!prettier --write --ignore-path $XDG_CONFIG_HOME/prettier/ignore --config $XDG_CONFIG_HOME/prettier/config %:p]],
  4. deno = [[!deno fmt %:p]],
  5. ruff = [[!ruff format %:p]],
  6. }
  7. local default = {
  8. json = commands.prettier,
  9. jsonc = commands.prettier,
  10. html = commands.prettier,
  11. css = commands.prettier,
  12. markdown = commands.prettier,
  13. denols = commands.deno,
  14. python = commands.ruff,
  15. typescriptreact = true,
  16. lua_ls = true,
  17. }
  18. local enabled = {}
  19. local save_format = function(source)
  20. if enabled[source] ~= nil then
  21. if enabled[source] == true then
  22. vim.api.nvim_exec("lua vim.lsp.buf.format()", true)
  23. vim.api.nvim_exec("noautocmd write", true)
  24. else
  25. vim.api.nvim_exec(enabled[source], true)
  26. end
  27. end
  28. vim.api.nvim_exec("e", true)
  29. end
  30. local global_autocmd_group = vim.api.nvim_create_augroup("SaveFormatterGlobal",
  31. { clear = true })
  32. local setup = function()
  33. vim.api.nvim_create_autocmd({ "BufWritePost" }, {
  34. pattern = { "*" },
  35. group = global_autocmd_group,
  36. callback = function()
  37. for _, lsp_data in pairs(vim.lsp.get_active_clients()) do
  38. for buf_num, buf_active in pairs(lsp_data.attached_buffers) do
  39. if buf_num == vim.api.nvim_get_current_buf() and buf_active ==
  40. true and enabled[lsp_data.name] then
  41. save_format(lsp_data.name)
  42. return
  43. end
  44. end
  45. end
  46. if enabled[vim.bo.filetype] ~= nil then
  47. save_format(vim.bo.filetype)
  48. end
  49. end
  50. })
  51. end
  52. local enable = function(items)
  53. if not items then
  54. enabled = vim.deepcopy(default)
  55. else
  56. enabled = vim.deepcopy(items)
  57. end
  58. for key, value in pairs(enabled) do
  59. if value == false then
  60. enabled[key] = nil
  61. end
  62. end
  63. return enabled
  64. end
  65. local disable = function()
  66. default = vim.deepcopy(enabled)
  67. enabled = {}
  68. return enabled
  69. end
  70. local add = function(item, value)
  71. if value == nil then value = vim.deepcopy(default[item]) end
  72. enabled[item] = vim.deepcopy(value)
  73. return value
  74. end
  75. local remove = function(item)
  76. default[item] = vim.deepcopy(enabled[item])
  77. enabled[item] = nil
  78. end
  79. local pickers = require("telescope.pickers")
  80. local themes = require("telescope.themes")
  81. local actions = require("telescope.actions")
  82. local action_state = require("telescope.actions.state")
  83. local finders = require("telescope.finders")
  84. local conf = require("telescope.config").values
  85. local entry_display = require("telescope.pickers.entry_display")
  86. local picker = function(opts)
  87. local items = {}
  88. for key, value in pairs(enabled) do
  89. if value ~= nil then
  90. table.insert(items, { key, value })
  91. end
  92. end
  93. pickers.new(themes.get_dropdown({}), {
  94. prompt_title = "Save Formatter",
  95. finder = finders.new_table({
  96. results = items,
  97. entry_maker = function(entry)
  98. local desc = "LSP Format"
  99. if type(entry[2]) == "string" then desc = entry[2] end
  100. local displayer = entry_display.create({
  101. separator = " ▏",
  102. items = { { width = 16 }, { remaining = true } }
  103. })
  104. return {
  105. value = entry,
  106. display = function() return displayer({ { entry[1] }, { desc } }) end,
  107. ordinal = entry[1],
  108. }
  109. end,
  110. }),
  111. sorter = conf.generic_sorter(opts),
  112. attach_mappings = function(prompt_bufnr, map)
  113. map("n", "d", function(_)
  114. local entry = action_state.get_selected_entry()
  115. if entry ~= nil then
  116. remove(entry.value[1])
  117. actions.close(prompt_bufnr)
  118. end
  119. end)
  120. actions.select_default:replace(function()
  121. if add(action_state.get_current_line()) ~= nil then
  122. actions.close(prompt_bufnr)
  123. end
  124. end)
  125. return true
  126. end
  127. }):find()
  128. end
  129. return {
  130. add = add,
  131. disable = disable,
  132. enable = enable,
  133. picker = picker,
  134. remove = remove,
  135. setup = setup,
  136. commands = function() return commands end,
  137. default = function() return default end,
  138. enabled = function() return enabled end,
  139. }