Skip to content

Commit

Permalink
feat: class motions
Browse files Browse the repository at this point in the history
  • Loading branch information
luckasRanarison committed Mar 30, 2024
1 parent cd7de68 commit 87b507e
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 1 deletion.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ The plugin works with all languages inheriting from html, css and tsx treesitter
- Class concealing
- Class sorting (without [prettier-plugin](https://github.com/tailwindlabs/prettier-plugin-tailwindcss))
- Completion utilities (using [nvim-cmp](https://github.com/hrsh7th/nvim-cmp))
- Class motions

> [!NOTE]
> Language services like autocompletion, diagnostics and hover are already provided by [tailwindcss-language-server](https://github.com/tailwindlabs/tailwindcss-intellisense/tree/master/packages/tailwindcss-language-server).
Expand Down Expand Up @@ -100,6 +101,8 @@ Available commands:
- `TailwindColorToggle`: toggles color hints.
- `TailwindSort`: sorts all classes in the current buffer.
- `TailwindSortSelection`: 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.

## Utilities

Expand Down
3 changes: 3 additions & 0 deletions lua/tailwind-tools/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ local lsp = require("tailwind-tools.lsp")
local state = require("tailwind-tools.state")
local config = require("tailwind-tools.config")
local conceal = require("tailwind-tools.conceal")
local motions = require("tailwind-tools.motions")

---@param options TailwindTools.Option
M.setup = function(options)
Expand Down Expand Up @@ -34,6 +35,8 @@ M.setup = function(options)
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 })

vim.api.nvim_create_autocmd("LspAttach", {
group = vim.g.tailwind_tools.conceal_au,
Expand Down
49 changes: 49 additions & 0 deletions lua/tailwind-tools/motions.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
local M = {}

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

M.move_to_next_class = function()
local nodes = treesitter.get_class_nodes(0, true)

if not nodes then return 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)
return a_row == b_row and a_col < b_col or a_row < b_row
end)

for _, node in ipairs(nodes) do
local node_row, node_col = treesitter.get_class_range(node, 0)

if node_row > cursor_row - 1 or (node_row == cursor_row - 1 and node_col > cursor_col) then
return vim.api.nvim_win_set_cursor(0, { node_row + 1, node_col })
end
end
end

M.move_to_prev_class = function()
local nodes = treesitter.get_class_nodes(0, true)

if not nodes then return 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)
return a_row == b_row and a_col > b_col or a_row > b_row
end)

for _, node in ipairs(nodes) do
local node_row, node_col = treesitter.get_class_range(node, 0)

if node_row < cursor_row - 1 or (node_row == cursor_row - 1 and node_col < cursor_col) then
return vim.api.nvim_win_set_cursor(0, { node_row + 1, node_col })
end
end
end

return M
2 changes: 1 addition & 1 deletion tests/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ vim.o.swapfile = false

require("lazy").setup({
{ "nvim-lua/plenary.nvim" },
{ dir = "../", dependencies = { "nvim-treesitter/nvim-treesitter" } },
{ dir = "./", opts = {}, dependencies = { "nvim-treesitter/nvim-treesitter" } },
})
20 changes: 20 additions & 0 deletions tests/motions/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Test</title>
</head>

<body>
<div class="font-bold text-xl">Test</div>
<div class="container mx-auto p-4 bg-gray-100">
<div class="text-2xl font-bold mb-4">
Lorem ipsum dolor sit amet, qui minim labore adipisicing minim sint
cillum sint consectetur cupidatat.
</div>
</div>
</body>

</html>
31 changes: 31 additions & 0 deletions tests/motions/jump_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
local assert = require("luassert")

local function assert_cursor(expected_row, expected_col)
local row, col = unpack(vim.api.nvim_win_get_cursor(0))
assert.same(expected_row, row, "Mismatched row")
assert.same(expected_col, col, "Mismatched col")
end

describe("jump motion:", function()
vim.cmd.edit("tests/motions/index.html")

it("should go to the next class", function()
vim.cmd.TailwindNextClass()
assert_cursor(11, 14)
vim.cmd.TailwindNextClass()
assert_cursor(12, 14)
vim.cmd.TailwindNextClass()
assert_cursor(13, 16)
vim.cmd.TailwindNextClass()
assert_cursor(13, 16)
end)

it("should go to the prev class", function()
vim.cmd.TailwindPrevClass()
assert_cursor(12, 14)
vim.cmd.TailwindPrevClass()
assert_cursor(11, 14)
vim.cmd.TailwindPrevClass()
assert_cursor(11, 14)
end)
end)

0 comments on commit 87b507e

Please sign in to comment.