Skip to content

Commit

Permalink
feat: sync sort requests
Browse files Browse the repository at this point in the history
  • Loading branch information
luckasRanarison committed Aug 12, 2024
1 parent 5afa976 commit be4eecc
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 80 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ Available commands:
- `TailwindColorEnable`: enables color hints for all buffers.
- `TailwindColorDisable`: disables color hints.
- `TailwindColorToggle`: toggles color hints.
- `TailwindSort`: sorts all classes in the current buffer.
- `TailwindSortSelection`: sorts selected classes in visual mode.
- `TailwindSort(Sync)`: sorts all classes in the current buffer.
- `TailwindSortSelection(Sync)`: sorts selected classes in visual mode.
- `TailwindNextClass`: moves the cursor to the nearest next class node.
- `TailwindPrevClass`: moves the cursor to the nearest previous class node.

Expand Down
25 changes: 15 additions & 10 deletions lua/tailwind-tools/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,21 @@ M.setup = function(options)
}

vim.api.nvim_set_hl(0, "TailwindConceal", config.options.conceal.highlight)
vim.api.nvim_create_user_command("TailwindConcealEnable", conceal.enable, { nargs = 0 })
vim.api.nvim_create_user_command("TailwindConcealDisable", conceal.disable, { nargs = 0 })
vim.api.nvim_create_user_command("TailwindConcealToggle", conceal.toggle, { nargs = 0 })
vim.api.nvim_create_user_command("TailwindSortSelection", lsp.sort_selection, { range = "%" })
vim.api.nvim_create_user_command("TailwindSort", lsp.sort_classes, { nargs = 0 })
vim.api.nvim_create_user_command("TailwindColorEnable", lsp.enable_color, { nargs = 0 })
vim.api.nvim_create_user_command("TailwindColorDisable", lsp.disable_color, { nargs = 0 })
vim.api.nvim_create_user_command("TailwindColorToggle", lsp.toggle_colors, { nargs = 0 })
vim.api.nvim_create_user_command("TailwindNextClass", motions.move_to_next_class, { nargs = 0 })
vim.api.nvim_create_user_command("TailwindPrevClass", motions.move_to_prev_class, { nargs = 0 })

local cmd = vim.api.nvim_create_user_command

cmd("TailwindConcealEnable", conceal.enable, { nargs = 0 })
cmd("TailwindConcealDisable", conceal.disable, { nargs = 0 })
cmd("TailwindConcealToggle", conceal.toggle, { nargs = 0 })
cmd("TailwindSort", lsp.sort_classes, { nargs = 0 })
cmd("TailwindSortSelection", lsp.sort_selection, { range = "%" })
cmd("TailwindColorEnable", lsp.enable_color, { nargs = 0 })
cmd("TailwindColorDisable", lsp.disable_color, { nargs = 0 })
cmd("TailwindColorToggle", lsp.toggle_colors, { nargs = 0 })
cmd("TailwindNextClass", motions.move_to_next_class, { nargs = 0 })
cmd("TailwindPrevClass", motions.move_to_prev_class, { nargs = 0 })
cmd("TailwindSortSync", function() lsp.sort_classes(true) end, { nargs = 0 })
cmd("TailwindSortSelectionSync", function() lsp.sort_selection(true) end, { range = "%" })

vim.api.nvim_create_autocmd("LspAttach", {
group = vim.g.tailwind_tools.color_au,
Expand Down
116 changes: 58 additions & 58 deletions lua/tailwind-tools/lsp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,55 @@ local function debounced_color_request(bufnr)
)
end

---@param ranges number[][]
---@param bufnr number
---@param sync boolean
local function sort_classes(ranges, bufnr, sync)
local client = get_tailwindcss()

if not client then return log.error("tailwind-language-server is not running") end
if #ranges == 0 then return end

local class_text = {}

for _, range in pairs(ranges) do
local start_row, start_col, end_row, end_col = unpack(range)
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")
end

local params = vim.tbl_extend("error", vim.lsp.util.make_text_document_params(bufnr), {
classLists = class_text,
})

local handler = function(err, result, _, _)
if err then return log.error(err.message) end
if result.error then return log.error(result.error) end
if not result or not vim.api.nvim_buf_is_valid(bufnr) then return end

for i, edit in pairs(result.classLists) do
local lines = vim.split(edit, "\n")
local s_row, s_col, e_row, e_col = unpack(ranges[i])

-- Dismiss useless error messages when undoing in nightly
pcall(function() vim.api.nvim_buf_set_text(bufnr, s_row, s_col, e_row, e_col, lines) end)
end
end

if sync then
local response = client.request_sync("@/tailwindCSS/sortSelection", params, 2000, bufnr)

if response then
handler(response.err, response.result)
else
log.error("LSP request timed out")
end
else
client.request("@/tailwindCSS/sortSelection", params, handler, bufnr)
end
end

M.on_attach = function(args)
local bufnr = args.buf
local client = get_tailwindcss()
Expand Down Expand Up @@ -142,70 +191,21 @@ M.toggle_colors = function()
end
end

M.sort_selection = function()
local client = get_tailwindcss()

if not client then return log.warn("tailwind-language-server is not running") end

---@param sync boolean
M.sort_selection = function(sync)
local bufnr = vim.api.nvim_get_current_buf()
local start_col = vim.fn.col("'<") - 1
local end_col = vim.fn.col("'>")
local start_row = vim.fn.line("'<") - 1
local end_row = vim.fn.line("'>") - 1
local class = vim.api.nvim_buf_get_text(bufnr, start_row, start_col, end_row, end_col, {})

if class then
local params = vim.lsp.util.make_text_document_params(bufnr)

params.classLists = { table.concat(class, "\n") }
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
if not vim.api.nvim_buf_is_valid(bufnr) then return end
local formatted = vim.split(result.classLists[1], "\n")

vim.api.nvim_buf_set_text(bufnr, start_row, start_col, end_row, end_col, formatted)
end, bufnr)
end
end

M.sort_classes = function()
local client = get_tailwindcss()
local s_row, s_col, e_row, e_col = utils.get_visual_range()
local class_ranges = { { s_row, s_col, e_row, e_col } }

if not client then return log.warn("tailwind-language-server is not running") end
sort_classes(class_ranges, bufnr, sync)
end

---@param sync boolean
M.sort_classes = function(sync)
local bufnr = vim.api.nvim_get_current_buf()
local params = vim.lsp.util.make_text_document_params(bufnr)
local class_ranges = classes.get_ranges(bufnr)

if #class_ranges == 0 then return end

local class_text = {}

for _, range in pairs(class_ranges) do
local start_row, start_col, end_row, end_col = unpack(range)
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")
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
if not result or not vim.api.nvim_buf_is_valid(bufnr) then return end

for i, edit in pairs(result.classLists) do
local lines = vim.split(edit, "\n")
local start_row, start_col, end_row, end_col = unpack(class_ranges[i])
local set_text = function()
vim.api.nvim_buf_set_text(bufnr, start_row, start_col, end_row, end_col, lines)
end
-- Dismiss useless error messages when undoing in nightly
pcall(set_text)
end
end, bufnr)
sort_classes(class_ranges, bufnr, sync)
end

return M
10 changes: 10 additions & 0 deletions lua/tailwind-tools/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,14 @@ M.extract_color = function(s)
if _r then return tonumber(_r, base), tonumber(_g, base), tonumber(_b, base) end
end

---Returns the 0-based range of the visual selection (start_row, start_col, end_row, end_col)
M.get_visual_range = function()
local s_row = vim.fn.line("'<") - 1
local s_col = vim.fn.col("'<") - 1
local e_row = vim.fn.line("'>") - 1
local e_col = vim.fn.col("'>")

return s_row, s_col, e_row, e_col
end

return M
15 changes: 5 additions & 10 deletions tests/lsp/sort_spec.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local assert = require("luassert")
local utils = require("tailwind-tools.utils")

describe("sort:", function()
it("Should initialize project", function()
Expand Down Expand Up @@ -30,24 +31,18 @@ describe("sort:", function()
it("Should sort selection", function()
vim.cmd.TailwindNextClass()
vim.cmd.normal('vi"\28\14')
vim.cmd([['<,'>TailwindSortSelection]])
vim.wait(2000, function() return vim.bo.modified end)

local start_col = vim.fn.col("'<") - 1
local end_col = vim.fn.col("'>")
local start_row = vim.fn.line("'<") - 1
local end_row = vim.fn.line("'>") - 1
local class = vim.api.nvim_buf_get_text(0, start_row, start_col, end_row, end_col, {})
vim.cmd([['<,'>TailwindSortSelectionSync]])

local s_row, s_col, e_row, e_col = utils.get_visual_range()
local class = vim.api.nvim_buf_get_text(0, s_row, s_col, e_row, e_col, {})
local expected = "flex h-screen w-screen items-center justify-center bg-[#111827]"

assert.same(expected, class[1])
end)

it("Should sort all classes", function()
vim.cmd.normal("u")
vim.cmd.TailwindSort()
vim.wait(2000, function() return vim.bo.modified end)
vim.cmd.TailwindSortSync()

local lines = vim.api.nvim_buf_get_lines(0, 0, -1, true)

Expand Down

0 comments on commit be4eecc

Please sign in to comment.