Skip to content

Commit

Permalink
fix: pattern search & lsp request
Browse files Browse the repository at this point in the history
  • Loading branch information
luckasRanarison committed Aug 1, 2024
1 parent de5bd2a commit 8009017
Show file tree
Hide file tree
Showing 5 changed files with 27 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 @@ -21,7 +21,7 @@ M.get_ranges = function(bufnr)
local pattern = patterns.builtin_patterns[ft] or custom_patterns[ft]

if pattern then
class_ranges = patterns.find_class_ranges(bufnr, pattern[1], pattern[2])
class_ranges = patterns.find_class_ranges(bufnr, pattern)
else
class_ranges = tresitter.find_class_ranges(bufnr, ft)
end
Expand Down
2 changes: 1 addition & 1 deletion lua/tailwind-tools/lsp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,10 @@ M.sort_classes = function()
local text = vim.api.nvim_buf_get_text(bufnr, start_row, start_col, end_row, end_col, {})

class_text[#class_text + 1] = table.concat(text, "\n")
class_ranges[#class_ranges + 1] = { start_row, start_col, end_row, end_col }
end

params.classLists = class_text

client.request("@/tailwindCSS/sortSelection", params, function(err, result, _, _)
if err then return log.error(err.message) end
if result.error then return log.error(result.error) end
Expand Down
19 changes: 10 additions & 9 deletions lua/tailwind-tools/motions.lua
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
local M = {}

local log = require("tailwind-tools.log")
local treesitter = require("tailwind-tools.treesitter")
local classes = require("tailwind-tools.classes")

---@param comp fun(a: number, b: number): boolean
local move_to_class = function(comp)
local nodes = treesitter.find_class_nodes(0, true)
local bufnr = vim.api.nvim_get_current_buf()
local class_ranges = classes.get_ranges(bufnr)

if not nodes then return end
if #nodes == 0 then return log.info("No classes") end
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))

table.sort(nodes, function(a, b)
local a_row, a_col = treesitter.get_class_range(a, 0)
local b_row, b_col = treesitter.get_class_range(b, 0)
table.sort(class_ranges, function(a, b)
local a_row, a_col = unpack(a)
local b_row, b_col = unpack(b)
return a_row == b_row and comp(b_col, a_col) or comp(b_row, a_row)
end)

for _, node in ipairs(nodes) do
local node_row, node_col = treesitter.get_class_range(node, 0)
for _, range in ipairs(class_ranges) do
local node_row, node_col = unpack(range)
local row = cursor_row - 1

if comp(node_row, row) or (node_row == row and comp(node_col, cursor_col)) then
Expand Down
23 changes: 13 additions & 10 deletions lua/tailwind-tools/patterns.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,32 +33,35 @@ end

---@param bufnr number
---@param pattern string
---@param delimiter string
M.find_class_ranges = function(bufnr, pattern, delimiter)
M.find_class_ranges = function(bufnr, pattern)
local results = {}
local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, true)
local s = table.concat(lines, "\n")
local offset = 1
local substr = table.concat(lines, "\n")
local offset = 0

while true do
local substr = s:sub(offset)
local b_start, b_end, class = substr:find(pattern)

if b_start == nil then break end

local class_start = substr:find(delimiter) + offset
substr = substr:sub(b_start)
offset = offset + b_start - 1

local match_len = b_end - b_start
local class_start = substr:find(class, 1, true) + offset - 1
local class_end = class_start + #class
local pos = table.pack(byte_range_to_pos(class_start - 1, class_end - 1, bufnr))
local sr, sc, er, ec = byte_range_to_pos(class_start, class_end, bufnr)

results[#results + 1] = pos
offset = offset + b_end
results[#results + 1] = { sr, sc, er, ec }
substr = substr:sub(match_len)
offset = offset + match_len - 1
end

return results
end

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

return M
3 changes: 2 additions & 1 deletion lua/tailwind-tools/treesitter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ M.find_class_ranges = function(bufnr, ft)
local results = {}

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

return results
Expand Down

0 comments on commit 8009017

Please sign in to comment.