diff --git a/.luarc.json b/.luarc.json index 3b9809d..f8ecc0f 100644 --- a/.luarc.json +++ b/.luarc.json @@ -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" ] diff --git a/lua/tailwind-tools/filetypes.lua b/lua/tailwind-tools/filetypes.lua new file mode 100644 index 0000000..86af8ac --- /dev/null +++ b/lua/tailwind-tools/filetypes.lua @@ -0,0 +1,14 @@ +return { + "html", + "css", + "php", + "twig", + "vue", + "svelte", + "astro", + "heex", + "elixir", + "htmldjango", + "javascriptreact", + "typescriptreact", +} diff --git a/lua/tailwind-tools/log.lua b/lua/tailwind-tools/log.lua index e4db915..72c2716 100644 --- a/lua/tailwind-tools/log.lua +++ b/lua/tailwind-tools/log.lua @@ -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 diff --git a/lua/tailwind-tools/motions.lua b/lua/tailwind-tools/motions.lua index 3598028..9d870f2 100644 --- a/lua/tailwind-tools/motions.lua +++ b/lua/tailwind-tools/motions.lua @@ -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 diff --git a/lua/tailwind-tools/treesitter.lua b/lua/tailwind-tools/treesitter.lua index d5c1a7b..0183de7 100644 --- a/lua/tailwind-tools/treesitter.lua +++ b/lua/tailwind-tools/treesitter.lua @@ -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) @@ -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