Skip to content

Commit

Permalink
update!: config options
Browse files Browse the repository at this point in the history
  • Loading branch information
luckasRanarison committed Aug 1, 2024
1 parent 39a6431 commit fbcc476
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 11 deletions.
38 changes: 32 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ Unofficial [Tailwind CSS](https://github.com/tailwindlabs/tailwindcss) integrati

## Features

The plugin works with all languages inheriting from html, css and tsx treesitter grammars (php, astro, vue, svelte, [...](./lua/tailwind-tools/filetypes.lua)) and provides the following features:
The plugin works with all languages inheriting from html, css and tsx treesitter grammars (php, astro, vue, svelte, [...](./lua/tailwind-tools/filetypes.lua)) and Lua patterns can also be used as a fallback.

It currently provides the following features:

- Class color hints
- Class concealing
Expand Down Expand Up @@ -86,7 +88,15 @@ Here is the default configuration:
fg = "#38BDF8",
},
},
custom_filetypes = {} -- see the extension section to learn how it works
-- see the extension section to learn more
extension = {
queries = {}, -- a list of filetypes having custom `class` queries
patterns = { -- a map of filetypes to Lua pattern lists
-- exmaple:
-- rust = { "class=[\"']([^\"']+)[\"']" },
-- javascript = { "clsx%(([^)]+)%)" },
},
},
}
```

Expand Down Expand Up @@ -136,11 +146,13 @@ return {
## Extension

The plugin basically works with any language as long it has a treesitter parser and a `class` query. You can check the currently available queries and supported filetypes [here](./queries), feel free to request other languages support.
The plugin already supports many languages, but requests for additional language support and PRs are welcome.

But you can also create your own queries! If you are not familiar with treesitter queries you should check out the treesitter query documentation from [Neovim](https://neovim.io/doc/user/treesitter.html#treesitter-query) or [Treesitter](https://tree-sitter.github.io/tree-sitter/using-parsers#query-syntax).
You can also extend the language support in your configuration by using Treesitter queries or Lua patterns (or both). Treesitter queries are recommended but can be harder to write, if you are not familiar with Treesitter queries, check out the treesitter query documentation from [Neovim](https://neovim.io/doc/user/treesitter.html#treesitter-query) or [Treesitter](https://tree-sitter.github.io/tree-sitter/using-parsers#query-syntax).

To add a new filetype you first need to add it to your configuration then the plugin will search for a `class.scm` file (classexpr) associated to that filetype in your `runtimepath`. You could use your Neovim configuration folder to store queries in the following way:
### Treesitter queries

After adding a new filetype to the `queries` list in your configuration, the plugin will search for a `class.scm` file (classexpr) associated with that filetype in your `runtimepath`. You can use your Neovim configuration folder to store queries in the following way:

```
~/.config/nvim
Expand All @@ -153,7 +165,7 @@ To add a new filetype you first need to add it to your configuration then the pl
      └── class.scm
```

The `class.scm` file should contain a query used to extract the class values for a given filetype. The class value should be captured using `@tailwind` as shown in the follwing example:
The `class.scm` file should contain a query used to extract the class values for a given filetype. The class value should be captured using `@tailwind`, as shown in the follwing example:

```scheme
; queries/myfiletype/class.scm
Expand All @@ -167,6 +179,20 @@ The `class.scm` file should contain a query used to extract the class values for
> [!NOTE]
> Some class ranges cannot be precisely captured using queries alone and are handled in code. You can also check out the existing [queries](./queries) to see more examples.
### Lua patterns

[Lua patterns](https://www.lua.org/pil/20.2.html) are easier to write, but note that the underlying implementation is not completely efficient, although this inefficiency is likely negligible. Currently, there are no reliable APIs for performing pattern searches and retrieving information about capture positions.

You can define custom patterns by attaching a list of patterns to filetypes. Each pattern should have exactly **one** capture group representing the class value, as shown below:

```lua
{
patterns = {
javascript = { "clsx%(([^)]+)%)" },
}
}
```

## Related projects

Here are some related projects:
Expand Down
7 changes: 4 additions & 3 deletions lua/tailwind-tools/classes.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ local M = {}
local patterns = require("tailwind-tools.patterns")
local filetypes = require("tailwind-tools.filetypes")
local tresitter = require("tailwind-tools.treesitter")
local opts = require("tailwind-tools.config").options
local config = require("tailwind-tools.config")

---@param bufnr number
---@return number[][]
M.get_ranges = function(bufnr)
local results = {}
local ft = vim.bo[bufnr].ft
local query_list = vim.tbl_extend("force", filetypes.treesitter, opts.custom_queries)
local pattern_list = vim.tbl_extend("force", filetypes.luapattern, opts.custom_patterns)
local extension = config.options.extension
local query_list = vim.tbl_extend("force", filetypes.treesitter, extension.queries)
local pattern_list = vim.tbl_extend("force", filetypes.luapattern, extension.patterns)

for _, pattern in pairs(pattern_list[ft] or {}) do
vim.list_extend(results, patterns.find_class_ranges(bufnr, pattern))
Expand Down
6 changes: 4 additions & 2 deletions lua/tailwind-tools/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ M.options = {
fg = "#38BDF8",
},
},
custom_patterns = {},
custom_queries = {},
extension = {
queries = {},
patterns = {},
},
}

return M

0 comments on commit fbcc476

Please sign in to comment.