• Stars
    star
    112
  • Rank 311,464 (Top 7 %)
  • Language
    Lua
  • License
    MIT License
  • Created over 1 year ago
  • Updated 7 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Easily sort Tailwind classes in Neovim.

Tailwind Sorter for Neovim

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.

Features

  • Works in more file types than prettier does (using a treesitter integration), confirmed to work with:
    • jsx
    • tsx
    • html
    • twig
    • handlebars
    • elixir/heex
    • astro
    • haskell
    • any languages that inject any of the above languages
  • Not having to pull in prettier just to have your classes sorted
  • Easier/faster than prettier if all you want is tailwind sorting
  • Easy to extend to other languages or use-cases

Usage

Commands

  • :TailwindSort sorts classes in the current buffer
  • :TailwindSortOnSaveToggle toggles automatic sorting on save

Requirements

Configuration

The 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',
})

lazy.nvim

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,
  },
})

packer.nvim

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)

vim-plug

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

Extending

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):

  1. Create the file queries/javascript/tailwind.scm
  2. We will paste the following to target any string inside the className attribute:
(jsx_attribute
  (property_identifier) @_name
    (#eq? @_name "className")
  (string
    (string_fragment) @tailwind))
  1. Add any file extension for jsx in the on_save_pattern config:
require('tailwind-sorter').setup({
  on_save_pattern = { '*.html', '*.jsx', '*.tsx' },
})

Please consider PR'ing this extension back to the plugin.

More Repositories

1

cloak.nvim

Cloak allows you to overlay *'s over defined patterns in defined files.
Lua
264
star
2

odin-http

A HTTP/1.1 client/server implementation for Odin.
Odin
64
star
3

phpls

PHP language server written in Go.
Go
24
star
4

back

Backtraces for Odin on the major platforms (MacOS, Linux, Windows).
Odin
13
star
5

odin-ini-parser

INI file format parser for odin.
Odin
12
star
6

todomvc-odin-htmx

An implementation of Todo MVC using my in development Odin web stack.
Odin
9
star
7

setup-odin

JavaScript
9
star
8

odin-pattern

Lua's pattern matching (alternative to regex) natively in Odin
Odin
7
star
9

odin-mysql

MySQL client bindings for Odin
Odin
7
star
10

temple

An experimental in development templating engine for Odin
Odin
7
star
11

odin-tree-sitter

Tree Sitter API bindings, wrappers and convenience for use in the Odin programming language.
Odin
5
star
12

tizen-librelink-up

A TizenOS Wearable app containing a client for the LibreLinkUp API which graphs all glucose readings for each connection on a shared circular graph.
JavaScript
5
star
13

dotfiles

My dotfiles
Lua
3
star
14

odin-postgresql

Complete & documented Odin bindings for libpq (PostgreSQL)
Odin
3
star
15

composer-language-server

A language server (LSP) for composer.json files.
TypeScript
2
star
16

odin-snowflake

Twitter Snowflake ID generation for Odin
Odin
2
star
17

aoc-2023

Advent Of Code 2023
Odin
1
star
18

site-checker

A telegram bot that checks sites for appropiate status codes and messages if a site is down.
JavaScript
1
star
19

critical

A plugin for Wordpress that lets you easily generate, add and manage Critical CSS. It also has the option to preload CSS files so they are non render blocking.
PHP
1
star