-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7f1dda9
commit de5bd2a
Showing
8 changed files
with
132 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
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") | ||
|
||
---@param bufnr 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 class_ranges | ||
local pattern = patterns.builtin_patterns[ft] or custom_patterns[ft] | ||
|
||
if pattern then | ||
class_ranges = patterns.find_class_ranges(bufnr, pattern[1], pattern[2]) | ||
else | ||
class_ranges = tresitter.find_class_ranges(bufnr, ft) | ||
end | ||
|
||
return class_ranges | ||
end | ||
|
||
return M |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ return { | |
"vue", | ||
"svelte", | ||
"astro", | ||
"rust", | ||
"heex", | ||
"elixir", | ||
"htmldjango", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
local M = {} | ||
|
||
---@param b_start number | ||
---@param b_end number | ||
---@param bufnr number | ||
local function byte_range_to_pos(b_start, b_end, bufnr) | ||
local line_count = vim.api.nvim_buf_line_count(bufnr) | ||
local line_offsets = {} | ||
|
||
for i = 1, line_count do | ||
line_offsets[i] = vim.api.nvim_buf_get_offset(bufnr, i - 1) | ||
end | ||
|
||
local start_row, start_col, end_row, end_col | ||
|
||
for line, offset in pairs(line_offsets) do | ||
local next_offset = line_offsets[line + 1] | ||
|
||
if not next_offset or b_start >= offset and b_start < next_offset then | ||
start_row = line - 1 | ||
start_col = b_start - offset | ||
end | ||
|
||
if not next_offset or b_end >= offset and b_end < next_offset then | ||
end_row = line - 1 | ||
end_col = b_end - offset | ||
break | ||
end | ||
end | ||
|
||
return start_row, start_col, end_row, end_col | ||
end | ||
|
||
---@param bufnr number | ||
---@param pattern string | ||
---@param delimiter string | ||
M.find_class_ranges = function(bufnr, pattern, delimiter) | ||
local results = {} | ||
local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, true) | ||
local s = table.concat(lines, "\n") | ||
local offset = 1 | ||
|
||
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 | ||
local class_end = class_start + #class | ||
local pos = table.pack(byte_range_to_pos(class_start - 1, class_end - 1, bufnr)) | ||
|
||
results[#results + 1] = pos | ||
offset = offset + b_end | ||
end | ||
|
||
return results | ||
end | ||
|
||
M.builtin_patterns = { | ||
rust = { "class=[\"']([^\"']+)[\"']", "[\"']" }, | ||
} | ||
|
||
return M |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters