Sorts your tailwind classes, just like prettier-plugin-tailwindcss.
The plugin integrates with Treesitter to find classes. This means it can work in any language and is easy to extend to new file types.
:TailwindSort
sorts classes in the current buffer:TailwindSortOnSaveToggle
toggles automatic sorting on saveThe following is the default configuration:
require('tailwind-sorter').setup({
on_save_enabled = false, -- If `true`, automatically enables on save sorting.
on_save_pattern = { '*.html', '*.js', '*.jsx', '*.tsx', '*.twig', '*.hbs', '*.php', '*.heex', '*.astro' }, -- The file patterns to watch and sort.
node_path = 'node',
trim_spaces = false, -- If `true`, trim any extra spaces after sorting.
})
require('lazy').setup({
{
'laytan/tailwind-sorter.nvim',
dependencies = {'nvim-treesitter/nvim-treesitter', 'nvim-lua/plenary.nvim'},
build = 'cd formatter && npm ci && npm run build',
config = true,
},
})
require('packer').startup(function(use)
use {
'laytan/tailwind-sorter.nvim',
requires = {'nvim-treesitter/nvim-treesitter', 'nvim-lua/plenary.nvim'},
config = function() require('tailwind-sorter').setup() end,
run = 'cd formatter && npm ci && npm run build',
}
end)
call plug#begin()
Plug 'nvim-treesitter/nvim-treesitter'
Plug 'nvim-lua/plenary.nvim'
Plug 'laytan/tailwind-sorter.nvim', { 'do': 'cd formatter && npm ci && npm run build' }
call plug#end()
lua <<EOF
require('tailwind-sorter').setup()
EOF
I strongly recommend reading :h treesitter-query
before doing this.
TLDR: tailwind-sorter.nvim looks for tailwind.scm
files in your queries
directory and sorts any @tailwind
captures. Make sure to add them to the
on_save_pattern
config if you use the on save sort feature.
Here is how you would add support for jsx syntax (if it was not added already):
queries/javascript/tailwind.scm
(jsx_attribute
(property_identifier) @_name
(#eq? @_name "className")
(string
(string_fragment) @tailwind))
on_save_pattern
config:require('tailwind-sorter').setup({
on_save_pattern = { '*.html', '*.jsx', '*.tsx' },
})
Please consider PR'ing this extension back to the plugin.