TailwindSort on save #15
-
I want to be able to run TailwindSort on save, but the sorting procedure is asynchronous so it's not guaranteed to resolve before the buffer is written. Not a big deal for me - so not ready to make a feature request - but would be nice if the api could easily handle it. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
Yeah, I deliberately didn't add sort on save because of the asynchronous nature of the function, but I'm confused what kind of API do you want? |
Beta Was this translation helpful? Give feedback.
-
Been a while but since I just solved this for myself I thought I'll share if anyone wants to get sorting to work. Prerequisites: using conform.nvim for managing formatters or I guess writing own autocmd logic I placed the following key in my conform's opts: format_after_save = function()
local t_attached = vim.tbl_contains(
vim.tbl_map(function(c)
return c.name
end, vim.lsp.get_clients()),
"tailwindcss"
)
if not t_attached or not pcall(require, "tailwind-tools") then
return
end
vim.cmd("TailwindSort")
-- These options will be passed to conform.format()
return { lsp_format = "fallback" }
end, and it runs the format for me just fine. |
Beta Was this translation helpful? Give feedback.
The problem with sorting on save is that we don't know how the users handle BufWrite events, do they use format on save? If so is it asynchronous or not? And in the case where both are async we need a callback API on one side, the default
format
function from neovim doesn't provide a callback argument so I think it's resonable to a callback function on our side or add a sync version (I personally prefer async), but currently there's no way to do that if both are async.