Skip to content

Commit

Permalink
refactor: group rplugin functions
Browse files Browse the repository at this point in the history
  • Loading branch information
luckasRanarison committed Sep 24, 2024
1 parent 8a14dfc commit e66abe3
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
28 changes: 28 additions & 0 deletions lua/tailwind-tools/rplugin.lua
Original file line number Diff line number Diff line change
@@ -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
13 changes: 5 additions & 8 deletions lua/telescope/_extensions/tailwind.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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)
Expand All @@ -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 = "",
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit e66abe3

Please sign in to comment.