| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- local commands = {
- prettier =
- [[!prettier --write --ignore-path $XDG_CONFIG_HOME/prettier/ignore --config $XDG_CONFIG_HOME/prettier/config %:p]],
- deno = [[!deno fmt %:p]],
- ruff = [[!ruff format %:p]],
- }
- local default = {
- json = commands.prettier,
- jsonc = commands.prettier,
- html = commands.prettier,
- css = commands.prettier,
- markdown = commands.prettier,
- denols = commands.deno,
- python = commands.ruff,
- typescriptreact = true,
- lua_ls = true,
- }
- local enabled = {}
- local save_format = function(source)
- if enabled[source] ~= nil then
- if enabled[source] == true then
- vim.api.nvim_exec("lua vim.lsp.buf.format()", true)
- vim.api.nvim_exec("noautocmd write", true)
- else
- vim.api.nvim_exec(enabled[source], true)
- end
- end
- vim.api.nvim_exec("e", true)
- end
- local global_autocmd_group = vim.api.nvim_create_augroup("SaveFormatterGlobal",
- { clear = true })
- local setup = function()
- vim.api.nvim_create_autocmd({ "BufWritePost" }, {
- pattern = { "*" },
- group = global_autocmd_group,
- callback = function()
- for _, lsp_data in pairs(vim.lsp.get_active_clients()) do
- for buf_num, buf_active in pairs(lsp_data.attached_buffers) do
- if buf_num == vim.api.nvim_get_current_buf() and buf_active ==
- true and enabled[lsp_data.name] then
- save_format(lsp_data.name)
- return
- end
- end
- end
- if enabled[vim.bo.filetype] ~= nil then
- save_format(vim.bo.filetype)
- end
- end
- })
- end
- local enable = function(items)
- if not items then
- enabled = vim.deepcopy(default)
- else
- enabled = vim.deepcopy(items)
- end
- for key, value in pairs(enabled) do
- if value == false then
- enabled[key] = nil
- end
- end
- return enabled
- end
- local disable = function()
- default = vim.deepcopy(enabled)
- enabled = {}
- return enabled
- end
- local add = function(item, value)
- if value == nil then value = vim.deepcopy(default[item]) end
- enabled[item] = vim.deepcopy(value)
- return value
- end
- local remove = function(item)
- default[item] = vim.deepcopy(enabled[item])
- enabled[item] = nil
- end
- local pickers = require("telescope.pickers")
- local themes = require("telescope.themes")
- local actions = require("telescope.actions")
- local action_state = require("telescope.actions.state")
- local finders = require("telescope.finders")
- local conf = require("telescope.config").values
- local entry_display = require("telescope.pickers.entry_display")
- local picker = function(opts)
- local items = {}
- for key, value in pairs(enabled) do
- if value ~= nil then
- table.insert(items, { key, value })
- end
- end
- pickers.new(themes.get_dropdown({}), {
- prompt_title = "Save Formatter",
- finder = finders.new_table({
- results = items,
- entry_maker = function(entry)
- local desc = "LSP Format"
- if type(entry[2]) == "string" then desc = entry[2] end
- local displayer = entry_display.create({
- separator = " ▏",
- items = { { width = 16 }, { remaining = true } }
- })
- return {
- value = entry,
- display = function() return displayer({ { entry[1] }, { desc } }) end,
- ordinal = entry[1],
- }
- end,
- }),
- sorter = conf.generic_sorter(opts),
- attach_mappings = function(prompt_bufnr, map)
- map("n", "d", function(_)
- local entry = action_state.get_selected_entry()
- if entry ~= nil then
- remove(entry.value[1])
- actions.close(prompt_bufnr)
- end
- end)
- actions.select_default:replace(function()
- if add(action_state.get_current_line()) ~= nil then
- actions.close(prompt_bufnr)
- end
- end)
- return true
- end
- }):find()
- end
- return {
- add = add,
- disable = disable,
- enable = enable,
- picker = picker,
- remove = remove,
- setup = setup,
- commands = function() return commands end,
- default = function() return default end,
- enabled = function() return enabled end,
- }
|