Skip to content

Commit

Permalink
feat: more language support
Browse files Browse the repository at this point in the history
Also made some cleanup.
  • Loading branch information
luckasRanarison committed Aug 2, 2024
1 parent da8eee8 commit 619e587
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 22 deletions.
2 changes: 1 addition & 1 deletion lua/tailwind-tools/classes.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ M.get_ranges = function(bufnr)
end

if vim.tbl_contains(query_list, ft) then
vim.list_extend(results, tresitter.find_class_ranges(bufnr, ft) or {})
vim.list_extend(results, tresitter.find_class_ranges(bufnr, ft))
end

return results
Expand Down
7 changes: 5 additions & 2 deletions lua/tailwind-tools/filetypes.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ return {
"php",
"twig",
"vue",
"svelte",
"astro",
"heex",
"astro",
"templ",
"svelte",
"elixir",
"htmldjango",
"javascript",
"typescript",
"javascriptreact",
"typescriptreact",
},
Expand Down
26 changes: 15 additions & 11 deletions lua/tailwind-tools/treesitter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,32 +28,36 @@ local function find_class_nodes(bufnr, ft)
end

---@param node TSNode
---@param bufnr number
local function get_class_range(node, bufnr)
local function get_class_range(node)
local start_row, start_col, end_row, end_col = node:range()
local children = node:named_children()

-- A special case for extracting postcss class range
if children[1] and vim.treesitter.get_node_text(children[1], bufnr) == "@apply" then
-- PostCSS @apply rules
if children[1] and children[1]:type() == "at_keyword" then
start_row, start_col, _, _ = children[2]:range()
_, _, end_row, end_col = children[#children]:range()
end

-- JS/TS function arguments
if node:type() == "arguments" then
start_row, start_col, _, _ = children[1]:range()
_, _, end_row, end_col = children[#children]:range()
end

return start_row, start_col, end_row, end_col
end

---@param bufnr number
---@param ft string
M.find_class_ranges = function(bufnr, ft)
local nodes = find_class_nodes(bufnr, ft)

if not nodes then return end

local results = {}
local nodes = find_class_nodes(bufnr, ft)

for _, node in pairs(nodes) do
local sr, sc, er, ec = get_class_range(node, bufnr)
results[#results + 1] = { sr, sc, er, ec }
if nodes then
for _, node in pairs(nodes) do
local sr, sc, er, ec = get_class_range(node)
results[#results + 1] = { sr, sc, er, ec }
end
end

return results
Expand Down
12 changes: 12 additions & 0 deletions queries/javascript/class.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
(call_expression
function: [
(identifier) @ident (#any-of? @ident "clsx" "classnames" "tw" "css")
(member_expression
object: (identifier) @object-ident (#eq? @object-ident "tw"))
]
arguments: [
(arguments
(_)+) @tailwind ; the actual class range is extracted in the code
(template_string
(string_fragment) @tailwind)
])
1 change: 1 addition & 0 deletions queries/templ/class.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
; inherits: html
5 changes: 2 additions & 3 deletions queries/tsx/class.scm
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
(jsx_attribute
(property_identifier) @_attribute_name
(#any-of? @_attribute_name "class" "className")
(#any-of? @_attribute_name "class" "className" "style" "css" "tw")
[
(string
(string_fragment) @tailwind)
(jsx_expression
(template_string
(string_fragment) @tailwind))
(_) @tailwind)
])
1 change: 1 addition & 0 deletions queries/typescript/class.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
; inherits: javascript
4 changes: 4 additions & 0 deletions tests/queries/javascript/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
clsx("p-4", "text-center");
classnames("bg-red-500", "uppercase");
tw`font-mono text-sm`
tw.input`border hover:border-black`
11 changes: 11 additions & 0 deletions tests/queries/javascript_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require("tests.queries.runner").test({
name = "javascript",
provider = "treesitter",
file = "tests/queries/javascript/test.js",
ranges = {
{ 0, 5, 0, 25 },
{ 1, 11, 1, 36 },
{ 2, 3, 2, 20 },
{ 3, 9, 3, 34 },
},
})
2 changes: 1 addition & 1 deletion tests/queries/tsx_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ require("tests.queries.runner").test({
ranges = {
{ 6, 19, 7, 55 },
{ 9, 24, 9, 47 },
{ 13, 33, 13, 45 },
{ 13, 24, 13, 46 },
},
})
6 changes: 2 additions & 4 deletions tests/queries/vue_spec.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
local spec = {
require("tests.queries.runner").test({
name = "vue",
provider = "treesitter",
file = "tests/queries/vue/test.vue",
Expand All @@ -7,6 +7,4 @@ local spec = {
{ 2, 16, 2, 43 },
{ 4, 39, 4, 49 },
},
}

require("tests.queries.runner").test(spec)
})

0 comments on commit 619e587

Please sign in to comment.