Code Monkey home page Code Monkey logo

tailwind-sorter.nvim's Introduction

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
    • 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' }, -- 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 i && npm run build',
    config = {},
  },
})

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 i && 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 i && 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.

tailwind-sorter.nvim's People

Contributors

laytan avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.