From 8009017cc42236425f5460adac4c3a67d63a205c Mon Sep 17 00:00:00 2001 From: Luckas Date: Thu, 1 Aug 2024 15:40:16 +0300 Subject: [PATCH] fix: pattern search & lsp request --- lua/tailwind-tools/classes.lua | 2 +- lua/tailwind-tools/lsp.lua | 2 +- lua/tailwind-tools/motions.lua | 19 ++++++++++--------- lua/tailwind-tools/patterns.lua | 23 +++++++++++++---------- lua/tailwind-tools/treesitter.lua | 3 ++- 5 files changed, 27 insertions(+), 22 deletions(-) diff --git a/lua/tailwind-tools/classes.lua b/lua/tailwind-tools/classes.lua index f2d12d2..d69dd82 100644 --- a/lua/tailwind-tools/classes.lua +++ b/lua/tailwind-tools/classes.lua @@ -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 diff --git a/lua/tailwind-tools/lsp.lua b/lua/tailwind-tools/lsp.lua index 549b24b..4e19b47 100644 --- a/lua/tailwind-tools/lsp.lua +++ b/lua/tailwind-tools/lsp.lua @@ -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 diff --git a/lua/tailwind-tools/motions.lua b/lua/tailwind-tools/motions.lua index 8488de9..5fd2a88 100644 --- a/lua/tailwind-tools/motions.lua +++ b/lua/tailwind-tools/motions.lua @@ -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 diff --git a/lua/tailwind-tools/patterns.lua b/lua/tailwind-tools/patterns.lua index 2958e76..671a871 100644 --- a/lua/tailwind-tools/patterns.lua +++ b/lua/tailwind-tools/patterns.lua @@ -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 diff --git a/lua/tailwind-tools/treesitter.lua b/lua/tailwind-tools/treesitter.lua index 727bfde..ccf38d9 100644 --- a/lua/tailwind-tools/treesitter.lua +++ b/lua/tailwind-tools/treesitter.lua @@ -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