Skip to content

Commit

Permalink
fix: class query logic
Browse files Browse the repository at this point in the history
  • Loading branch information
luckasRanarison committed Aug 1, 2024
1 parent dfa3e97 commit 39a6431
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 35 deletions.
23 changes: 9 additions & 14 deletions lua/tailwind-tools/classes.lua
Original file line number Diff line number Diff line change
@@ -1,30 +1,25 @@
local M = {}

local config = require("tailwind-tools.config")
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

---@param bufnr number
---@return number[][]
M.get_ranges = function(bufnr)
local ft = vim.bo[bufnr].ft
local custom_patterns = config.options.custom_patterns
local pattern_ft = vim.tbl_keys(custom_patterns)
local query_ft = vim.tbl_keys(config.options.custom_queries)

vim.list_extend(filetypes, query_ft)
vim.list_extend(filetypes, pattern_ft)

if not vim.tbl_contains(filetypes, ft) then return end

local results = {}
local pattern_list = patterns.builtin_patterns[ft] or custom_patterns[ft]
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)

for _, pattern in pairs(pattern_list or {}) do
for _, pattern in pairs(pattern_list[ft] or {}) do
vim.list_extend(results, patterns.find_class_ranges(bufnr, pattern))
end

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

return results
end
Expand Down
2 changes: 1 addition & 1 deletion lua/tailwind-tools/conceal.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ local classes = require("tailwind-tools.classes")
local function set_conceal(bufnr)
local class_ranges = classes.get_ranges(bufnr)

if not class_ranges then return end
if #class_ranges == 0 then return end

vim.wo.conceallevel = 2
vim.api.nvim_buf_clear_namespace(bufnr, vim.g.tailwind_tools.conceal_ns, 0, -1)
Expand Down
30 changes: 17 additions & 13 deletions lua/tailwind-tools/filetypes.lua
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
return {
"html",
"css",
"php",
"twig",
"vue",
"svelte",
"astro",
"rust",
"heex",
"elixir",
"htmldjango",
"javascriptreact",
"typescriptreact",
treesitter = {
"html",
"css",
"php",
"twig",
"vue",
"svelte",
"astro",
"heex",
"elixir",
"htmldjango",
"javascriptreact",
"typescriptreact",
},
luapattern = {
rust = { "class=[\"']([^\"']+)[\"']" },
},
}
2 changes: 1 addition & 1 deletion lua/tailwind-tools/lsp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ M.sort_classes = function()
local params = vim.lsp.util.make_text_document_params(bufnr)
local class_ranges = classes.get_ranges(bufnr)

if not class_ranges then return end
if #class_ranges == 0 then return end

local class_text = {}

Expand Down
1 change: 0 additions & 1 deletion lua/tailwind-tools/motions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ local move_to_class = function(comp)
local bufnr = vim.api.nvim_get_current_buf()
local class_ranges = classes.get_ranges(bufnr)

if not class_ranges then return end
if #class_ranges == 0 then return log.info("No classes") end

local cursor_row, cursor_col = unpack(vim.api.nvim_win_get_cursor(0))
Expand Down
4 changes: 0 additions & 4 deletions lua/tailwind-tools/patterns.lua
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,4 @@ M.find_class_ranges = function(bufnr, pattern)
return results
end

M.builtin_patterns = {
rust = { "class=[\"']([^\"']+)[\"']" },
}

return M
2 changes: 1 addition & 1 deletion tests/queries/runner.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ M.test = function(spec)
vim.cmd.edit(spec.file)
vim.bo.filetype = spec.filetype or vim.bo.filetype

local ranges = assert(classes.get_ranges(0), "Expected range list, got nil")
local ranges = classes.get_ranges(0)

it(
"Should get class count",
Expand Down

0 comments on commit 39a6431

Please sign in to comment.