diff --git a/README.md b/README.md index 9367634..756f66c 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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%(([^)]+)%)" }, + }, + }, } ``` @@ -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 @@ -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 @@ -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: diff --git a/lua/tailwind-tools/classes.lua b/lua/tailwind-tools/classes.lua index 904a061..c6229e6 100644 --- a/lua/tailwind-tools/classes.lua +++ b/lua/tailwind-tools/classes.lua @@ -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)) diff --git a/lua/tailwind-tools/config.lua b/lua/tailwind-tools/config.lua index e291d64..2898e76 100644 --- a/lua/tailwind-tools/config.lua +++ b/lua/tailwind-tools/config.lua @@ -19,8 +19,10 @@ M.options = { fg = "#38BDF8", }, }, - custom_patterns = {}, - custom_queries = {}, + extension = { + queries = {}, + patterns = {}, + }, } return M