diff --git a/lua/tailwind-tools/rplugin.lua b/lua/tailwind-tools/rplugin.lua new file mode 100644 index 0000000..d2fb4d7 --- /dev/null +++ b/lua/tailwind-tools/rplugin.lua @@ -0,0 +1,28 @@ +local M = {} + +---@class TailwindTools.ClassEntry +---@field name string +---@field value any + +local function nil_wrap(fn) + return function(args) + local result = fn(args) + if result ~= vim.NIL then return result end + end +end + +---@type fun(utilities: string[]): string? +---Expands utility classes to CSS using tailwindcss library from the current project. +---It uses PostCSS from the current project for TailwindCSS v3. +--- +---TODO: Support for v4 +M.expand_utilities = nil_wrap(vim.fn.TailwindExpandUtilities) + +---@type fun(): TailwindTools.ClassEntry[]? +---Gets available utility classes for the current project. +---It is done by reading and processing the tailwind configuration file. +--- +---TODO: Support for v4 +M.get_utilities = nil_wrap(vim.fn.TailwindGetUtilities) + +return M diff --git a/lua/telescope/_extensions/tailwind.lua b/lua/telescope/_extensions/tailwind.lua index 26c5d1b..50d507c 100644 --- a/lua/telescope/_extensions/tailwind.lua +++ b/lua/telescope/_extensions/tailwind.lua @@ -2,6 +2,7 @@ local log = require("tailwind-tools.log") local utils = require("tailwind-tools.utils") local classes = require("tailwind-tools.classes") local plugin_config = require("tailwind-tools.config") +local rplugin = require("tailwind-tools.rplugin") local actions = require("telescope.actions") local action_state = require("telescope.actions.state") @@ -11,10 +12,6 @@ local previewers = require("telescope.previewers") local entry_display = require("telescope.pickers.entry_display") local config = require("telescope.config").values ----@class TailwindTools.ClassEntry ----@field name string ----@field value any - local fg_prefixes = { "text", "border", "outline" } local function get_hl_kind(class_name) @@ -26,9 +23,9 @@ local function get_hl_kind(class_name) end local function utility_picker() - local utilities = vim.fn.TailwindGetUtilities() --[[@as TailwindTools.ClassEntry[] | nil]] + local utilities = rplugin.get_utilities() - if utilities == vim.NIL then return log.error("No project found") end + if not utilities then return log.error("No project found") end local displayer = entry_display.create({ separator = "", @@ -64,9 +61,9 @@ local function utility_picker() title = "CSS Output", define_preview = function(self, entry) local bufnr = self.state.bufnr - local css = vim.fn.TailwindExpandUtilities({ entry.value.name }) + local css = rplugin.expand_utilities({ entry.value.name }) - if type(css) == "string" then + if css then vim.bo[bufnr].ft = "css" vim.api.nvim_buf_set_text(bufnr, 0, -1, 0, -1, vim.split(css, "\n")) entry.value.css = css