Skip to content

Commit

Permalink
refactor: cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
luckasRanarison committed Jul 31, 2024
1 parent 79b2a48 commit bda72f3
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 53 deletions.
2 changes: 0 additions & 2 deletions .luarc.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
},
"workspace.library": [
"/usr/local/share/nvim/runtime/lua",
"~/.local/share/nvim/lazy/neodev.nvim/types/stable",
"~/.local/share/nvim/lazy/nvim-cmp",
"~/.local/share/nvim/lazy/nvim-treesitter",
"${3rd}/luv/library",
"${3rd}/luassert/library"
]
Expand Down
14 changes: 14 additions & 0 deletions lua/tailwind-tools/filetypes.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
return {
"html",
"css",
"php",
"twig",
"vue",
"svelte",
"astro",
"heex",
"elixir",
"htmldjango",
"javascriptreact",
"typescriptreact",
}
13 changes: 5 additions & 8 deletions lua/tailwind-tools/log.lua
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
local M = {}

local levels = vim.log.levels

local notify_fn = function(level)
return
---@param message string
return ---@param message string
function(message) vim.notify("[tailwind-tools] " .. message, level) end
end

M.debug = notify_fn(levels.DEBUG)
M.info = notify_fn(levels.INFO)
M.warn = notify_fn(levels.WARN)
M.error = notify_fn(levels.ERROR)
M.debug = notify_fn(vim.log.levels.DEBUG)
M.info = notify_fn(vim.log.levels.INFO)
M.warn = notify_fn(vim.log.levels.WARN)
M.error = notify_fn(vim.log.levels.ERROR)

return M
34 changes: 12 additions & 22 deletions lua/tailwind-tools/motions.lua
Original file line number Diff line number Diff line change
@@ -1,49 +1,39 @@
local M = {}

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

M.move_to_next_class = function()
---@param comp fun(a: number, b: number): boolean
local move_to_class = function(comp)
local nodes = treesitter.get_class_nodes(0, true)

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

if node_row > cursor_row - 1 or (node_row == cursor_row - 1 and node_col > cursor_col) then
if comp(node_row, row) or (node_row == row and comp(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)
M.move_to_next_class = function()
move_to_class(function(a, b) return a > b end)
end

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
M.move_to_prev_class = function()
move_to_class(function(a, b) return a < b end)
end

return M
27 changes: 6 additions & 21 deletions lua/tailwind-tools/treesitter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,20 @@ local M = {}

local log = require("tailwind-tools.log")
local config = require("tailwind-tools.config")
local parsers = require("nvim-treesitter.parsers")

local supported_filetypes = {
"html",
"css",
"php",
"twig",
"vue",
"svelte",
"astro",
"heex",
"elixir",
"htmldjango",
"javascriptreact",
"typescriptreact",
}
local filetypes = require("tailwind-tools.filetypes")

---@param bufnr number
---@param all boolean?
M.get_class_nodes = function(bufnr, all)
local ft = vim.bo[bufnr].ft
local filetypes = vim.tbl_extend("keep", config.options.custom_filetypes, supported_filetypes)
local results = {}
local supported_filetypes = vim.tbl_extend("keep", filetypes, config.options.custom_filetypes)

if not vim.tbl_contains(filetypes, ft) then return end
if not vim.tbl_contains(supported_filetypes, ft) then return end

local parser = parsers.get_parser(bufnr)
local results = {}
local parser = vim.treesitter.get_parser(bufnr)

if not parser then return log.warn("No parser available for " .. ft) end

if all and vim.version().minor >= 10 then parser:parse(true) end

parser:for_each_tree(function(tree, lang_tree)
Expand All @@ -40,6 +24,7 @@ M.get_class_nodes = function(bufnr, all)
local query = vim.treesitter.query.get(lang, "class")

if query then
---@diagnostic disable-next-line: redundant-parameter
for id, node in query:iter_captures(root, bufnr, 0, -1, { all = true }) do
if query.captures[id] == "tailwind" then results[#results + 1] = node end
end
Expand Down

0 comments on commit bda72f3

Please sign in to comment.