Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
luckasRanarison committed Mar 20, 2024
0 parents commit 06fd447
Show file tree
Hide file tree
Showing 23 changed files with 721 additions and 0 deletions.
33 changes: 33 additions & 0 deletions .github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
name: Bug report
about: Create a report to help us improve
title: ''
labels: bug
assignees: ''

---

**Describe the bug**
A clear and concise description of what the bug is.

**To Reproduce**
Steps to reproduce the behavior:
1. Go to '...'
2. Click on '....'
3. Scroll down to '....'
4. See error

**Expected behavior**
A clear and concise description of what you expected to happen.

**Screenshots**
If applicable, add screenshots to help explain your problem.

**Environment (please complete the following information):**
- Operating system
- Neovim version or commit
- Nvim-treesitter commit
- tailwind-language-server version

**Additional context**
Add any other context about the problem here.
20 changes: 20 additions & 0 deletions .github/ISSUE_TEMPLATE/feature_request.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
name: Feature request
about: Suggest an idea for this project
title: ''
labels: enhancement
assignees: ''

---

**Is your feature request related to a problem? Please describe.**
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]

**Describe the solution you'd like**
A clear and concise description of what you want to happen.

**Describe alternatives you've considered**
A clear and concise description of any alternative solutions or features you've considered.

**Additional context**
Add any other context or screenshots about the feature request here.
14 changes: 14 additions & 0 deletions .luarc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"diagnostics": {
"runtime": {
"version": "LuaJIT"
}
},
"workspace.library": [
"/usr/local/share/nvim/runtime/lua",
"~/.local/share/nvim/lazy/neodev.nvim/types/stable",
"~/.local/share/nvim/lazy/nvim-cmp",
"${3rd}/luv/library",
"${3rd}/luassert/library"
]
}
7 changes: 7 additions & 0 deletions .stylua.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
column_width = 100
line_endings = "Unix"
indent_type = "Spaces"
indent_width = 2
quote_style = "AutoPreferDouble"
call_parentheses = "Always"
collapse_simple_statement = "Always"
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 LIOKA Ranarison Fiderana

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
130 changes: 130 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
> [!IMPORTANT]
> This plugin is a community project and is **NOT** officialy supported by [Tailwind Labs](https://github.com/tailwindlabs).
# tailwind-tools.nvim

Unofficial [Tailwind CSS](https://github.com/tailwindlabs/tailwindcss) integration and tooling for [Neovim](https://github.com/neovim/neovim) using the built-in LSP client and treesitter.

![preview](https://github.com/luckasRanarison/tailwind-tools.nvim/assets/101930730/cb1c0508-8375-474f-9078-2842fb62e0b7)

## Contents

- [Features](#features)
- [Prerequisites](#prerequisites)
- [Installation](#installation)
- [Configuration](#configuration)
- [Commands](#commands)
- [Utilities](#utilities)
- [Related projects](#related-projects)
- [Contributing](#contributing)

## Features

The plugin works with all languages inheriting from html, css and tsx treesitter grammars (php, astro, vue, svelte, [...](./queries)) and provides the following features:

- Class color hints
- 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))

> [!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).
## Prerequisites

- Latest Neovim [stable](https://github.com/neovim/neovim/releases/tag/stable) or [nightly](https://github.com/neovim/neovim/releases/tag/nightly) (recommended)
- [tailwindcss-language-server](https://github.com/tailwindlabs/tailwindcss-intellisense/tree/master/packages/tailwindcss-language-server) >= `v0.0.14` (can be installed using [Mason](https://github.com/williamboman/mason.nvim))
- `html`, `css` and `tsx` treesitter grammars (can be installed using [nvim-treesitter](https://github.com/nvim-treesitter/nvim-treesitter))

> [!TIP]
> If you are not familiar with neovim LSP ecosystem check out [nvim-lspconfig](https://github.com/tailwindlabs) to learn how to setup the LSP.
## Installation

Using [lazy.nvim](https://github.com/folke/lazy.nvim):

```lua
-- tailwind-tools.lua
return {
"luckasRanarison/tailwind-tools.nvim",
opts = {}
}
```

## Configuration

> [!IMPORTANT]
> Neovim nightly is required for vscode-like inline color hints.
Here is the default configuration:

```lua
---@type TailwindTools.Option
{
document_color = {
enabled = true, -- can be toggled by commands
kind = "inline", -- "inline" | "foreground" | "background"
inline_symbol = "󰝤 ", -- only used in inline mode
debounce = 200, -- in milliseconds, only applied in insert mode
},
conceal = {
symbol = "󱏿", -- only a single character is allowed
highlight = { -- extmark highlight options, see :h 'highlight'
fg = "#38BDF8",
},
},
}
```

## Commands

Available commands:

- `TailwindConcealEnable`: enables conceal for all buffers.
- `TailwindConcealDisable`: disables conceal.
- `TailwindConcealToggle`: toggles conceal.
- `TailwindColorEnable`: enables color hints for all buffers.
- `TailwindColorDisable`: disables color hints.
- `TailwindColorToggle`: toggles color hints.
- `TailwindSort`: sorts all classes in the current buffer.
- `TailwindSortSelection`: sorts selected classes in visual mode.

## Utilities

Utility function for highlighting colors in [nvim-cmp](https://github.com/hrsh7th/nvim-cmp) using [lspkind.nvim](https://github.com/onsails/lspkind.nvim):

```lua
-- nvim-cmp.lua
return {
"hrsh7th/nvim-cmp",
dependencies = {
"luckasRanarison/tailwind-tools.nvim",
"onsails/lspkind-nvim",
-- ...
},
opts = function()
return {
-- ...
formatting = require("lspkind").cmp_format({
before = require("tailwind-tools.cmp").lspkind_format
})
}
end
},
```

## Related projects

Here are some related projects:

- [tailwindcss-intellisense](https://github.com/tailwindlabs/tailwindcss-intellisense) (official vscode extension)
- [tailwind-sorter.nvim](https://github.com/laytan/tailwind-sorter.nvim) (uses external scripts)
- [tailwind-fold](https://github.com/stivoat/tailwind-fold) (vscode extension)
- [tailwind-fold.nvim](https://github.com/razak17/tailwind-fold.nvim)
- [document-color.nvim](https://github.com/mrshmllow/document-color.nvim) (archieved)

## Contributing

Read the documentation carefully before submitting any issue.

Feature and pull requests are welcome.
20 changes: 20 additions & 0 deletions lua/tailwind-tools/cmp.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
local M = {}

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

-- Formatting utility for https://github.com/onsails/lspkind.nvim
---@param entry cmp.Entry
---@param vim_item any
---@return any
M.lspkind_format = function(entry, vim_item)
local doc = entry.completion_item.documentation

if vim_item.kind == "Color" and type(doc) == "string" then
local _, _, r, g, b = doc:find("rgba?%((%d+), (%d+), (%d+)")
if r then vim_item.kind_hl_group = utils.set_hl_from(r, g, b, "foreground") end
end

return vim_item
end

return M
81 changes: 81 additions & 0 deletions lua/tailwind-tools/conceal.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
local M = {}

local lsp = require("tailwind-tools.lsp")
local state = require("tailwind-tools.state")
local config = require("tailwind-tools.config")
local treesitter = require("tailwind-tools.treesitter")

---@param bufnr number
local function set_conceal(bufnr)
local class_nodes = treesitter.get_class_iter(bufnr)

if not class_nodes then return end

vim.wo.conceallevel = 2
vim.api.nvim_buf_clear_namespace(bufnr, vim.g.tailwind_tools.conceal_ns, 0, -1)
vim.api.nvim_buf_clear_namespace(bufnr, vim.g.tailwind_tools.color_ns, 0, -1)
table.insert(state.conceal.active_buffers, bufnr)

for _, match in class_nodes do
local node = match[2][1] or match[2]
local start_row, start_col, end_row, end_col = treesitter.get_class_range(node, bufnr)

vim.api.nvim_buf_set_extmark(bufnr, vim.g.tailwind_tools.conceal_ns, start_row, start_col, {
end_line = end_row,
end_col = end_col,
conceal = config.options.conceal.symbol,
hl_group = "TailwindConceal",
priority = 0, -- To ignore conceal hl_group when focused
})
end
end

M.enable = function()
for _, bufnr in pairs(vim.api.nvim_list_bufs()) do
if vim.api.nvim_buf_is_loaded(bufnr) then set_conceal(bufnr) end
end

vim.api.nvim_create_autocmd({ "TextChanged", "TextChangedI" }, {
group = vim.g.tailwind_tools.conceal_au,
callback = function(args) set_conceal(args.buf) end,
})
-- Workaround to reset conceallevel per buffer
vim.api.nvim_create_autocmd("BufEnter", {
group = vim.g.tailwind_tools.conceal_au,
callback = function(args)
vim.wo.conceallevel = vim.opt.conceallevel:get()
if state.conceal.enabled then set_conceal(args.buf) end
end,
})

state.conceal.enabled = true
if state.color.enabled then lsp.color_request(0) end
end

M.disable = function()
vim.wo.conceallevel = 0
vim.api.nvim_clear_autocmds({
group = vim.g.tailwind_tools.conceal_au,
event = { "TextChanged", "TextChangedI" },
})

for _, bufnr in pairs(state.conceal.active_buffers) do
if vim.api.nvim_buf_is_valid(bufnr) then
vim.api.nvim_buf_clear_namespace(bufnr, vim.g.tailwind_tools.conceal_ns, 0, -1)
end
end

state.conceal.active_buffers = {}
state.conceal.enabled = false
if state.color.enabled then lsp.color_request(0) end
end

M.toggle = function()
if state.conceal.enabled then
M.disable()
else
M.enable()
end
end

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

---@alias TailwindTools.ColorHint "foreground" | "background" | "inline"

---@class TailwindTools.Option
M.options = {
document_color = {
enabled = true,
---@type TailwindTools.ColorHint
kind = "inline",
inline_symbol = "󰝤 ",
debounce = 200,
},
conceal = {
symbol = "󱏿",
highlight = {
fg = "#38BDF8",
},
},
}

return M
40 changes: 40 additions & 0 deletions lua/tailwind-tools/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
local M = {}

local log = require("tailwind-tools.log")
local lsp = require("tailwind-tools.lsp")
local config = require("tailwind-tools.config")
local conceal = require("tailwind-tools.conceal")

---@param options TailwindTools.Option
M.setup = function(options)
config.options = vim.tbl_deep_extend("keep", options, config.options)

if vim.version().minor < 10 and config.options.document_color.kind == "inline" then
log.error("Neovim nightly is required for inline color hints, using fallback option")
config.options.document_color.kind = "background"
end

vim.g.tailwind_tools = {
color_ns = vim.api.nvim_create_namespace("tailwind_colors"),
color_au = vim.api.nvim_create_augroup("tailwind_colors", {}),
conceal_ns = vim.api.nvim_create_namespace("tailwind_conceal"),
conceal_au = vim.api.nvim_create_augroup("tailwind_conceal", {}),
}

vim.api.nvim_set_hl(0, "TailwindConceal", config.options.conceal.highlight)
vim.api.nvim_create_user_command("TailwindConcealEnable", conceal.enable, { nargs = 0 })
vim.api.nvim_create_user_command("TailwindConcealDisable", conceal.disable, { nargs = 0 })
vim.api.nvim_create_user_command("TailwindConcealToggle", conceal.toggle, { nargs = 0 })
vim.api.nvim_create_user_command("TailwindSortSelection", lsp.sort_selection, { range = "%" })
vim.api.nvim_create_user_command("TailwindSort", lsp.sort_classes, { nargs = 0 })
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_autocmd("LspAttach", {
group = vim.g.tailwind_tools.conceal_au,
callback = lsp.on_attach,
})
end

return M
Loading

0 comments on commit 06fd447

Please sign in to comment.