Skip to content

Commit

Permalink
Fix/tailwind issues (#128)
Browse files Browse the repository at this point in the history
* fix: do not add tailwind colors to trie if tailwind == "lsp"

* ref: refactors tailwind module

* ref: update options parameter to ud_options to indicate it satisfies `user_default_options`

* fix: before applying tailwind lsp highlights, clear default namespace for line

* ref: removes colorizer.default_namespace.  namespaces are available in constants module

* ref: refactors tailwind module

* feat: creates tailwind_names_parser, which will be used to parse tailwind class names from a data table in it's own namespace

* ref: create namespace for tailwind names, which is cleared upon tailwind lsp highlighting when tailwind = "both"

* feat: tailwind lsp only highlights visible rows

* ref: rename utils.view_range to utils.visible_line_range

* ref: use client:supports_method

* feat: tailwind lsp results can be configured to be added to tailwind_names color map

* ref: validate options during applying aliases

* perf: tailwind LSP results are cached to be returned from WinScrolled event

* feat: update tailwind named Trie from LSP results

* doc: updates readme with tailwind information

* fix(sass): pass in correct matcher.make function

* fix(sass): use correct field name (COLOR_PARSER -> color_parser)

* fix(sass): use updated hl_opts argument for colorizer.highlight

* ref(sass): rename options key to ud_opts to remain consistent with plugin parameters

* doc: updates readme
  • Loading branch information
catgoose authored Jan 10, 2025
1 parent 359d456 commit dd643c1
Show file tree
Hide file tree
Showing 36 changed files with 1,733 additions and 824 deletions.
86 changes: 56 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
- [Why another highlighter?](#why-another-highlighter)
- [Customization](#customization)
- [Updating color even when buffer is not focused](#updating-color-even-when-buffer-is-not-focused)
- [Tailwind](#tailwind)
- [Testing](#testing)
- [Extras](#extras)
- [TODO](#todo)
- [Similar projects](#similar-projects)
<!--toc:end-->

[![luadoc](https://img.shields.io/badge/luadoc-0.1-blue)](https://catgoose.github.io/nvim-colorizer.lua/)
Expand All @@ -27,15 +27,11 @@ dependencies**! Written in performant Luajit.
As long as you have `malloc()` and `free()` on your system, this will work.
Which includes Linux, OSX, and Windows.

![Demo.gif](https://raw.githubusercontent.com/norcalli/github-assets/master/nvim-colorizer.lua-demo-short.gif)
![Demo.gif](https://github.com/catgoose/screenshots/blob/51466fa599efe6d9821715616106c1712aad00c3/nvim-colorizer.lua/demo-short.gif)

## Installation and Usage

Requires Neovim >= 0.7.0 and `set termguicolors`.
If you don't have true color for your terminal or are
unsure, [read this excellent guide](https://github.com/termstandard/colors).

Use your plugin manager or clone directly into your package.

### Plugin managers

Expand All @@ -58,33 +54,33 @@ use("catgoose/nvim-colorizer.lua")

#### Manual

One line setup. This will create an `autocmd` for `FileType *` to highlight
every filetype.

> [!NOTE]
> You should add this line after/below where your plugins are setup.
```lua
require("colorizer").setup()
```

This will create an `autocmd` for `FileType *` to highlight
every filetype.

### User commands

> [!NOTE]
> User commands can be enabled/disabled in setup opts
| Command | Description |
| ----------------------------- | ----------------------------------------------------------- |
| **ColorizerAttachToBuffer** | Attach to the current buffer with given or default settings |
| **ColorizerDetachFromBuffer** | Stop highlighting the current buffer |
| **ColorizerReloadAllBuffers** | Reload all buffers that are being highlighted currently |
| **ColorizerToggle** | Toggle highlighting of the current buffer |

> [!NOTE]
> User commands can be enabled/disabled in setup opts
### Lua API

```lua
-- All options that can be passed to `user_default_options` in setup() can be
-- passed here
-- passed to `attach_to_buffer`
-- Similar for other functions

-- Attach to buffer
Expand All @@ -107,10 +103,6 @@ is that _this only works for Neovim_, and that will never change.
Apart from that, it only applies the highlights to the current visible contents,
so even if a big file is opened, the editor won't just choke on a blank screen.

This idea was copied from
[brenoprata10/nvim-highlight-colors](https://github.com/brenoprata10/nvim-highlight-colors)
Credits to [brenoprata10](https://github.com/brenoprata10)

Additionally, having a Lua API that's available means users can use this as a
library to do custom highlighting themselves.

Expand All @@ -122,6 +114,10 @@ library to do custom highlighting themselves.
```lua
require("colorizer").setup({
filetypes = { "*" },
-- all the sub-options of filetypes apply to buftypes
buftypes = {},
-- Boolean | List of usercommands to enable. See User commands section.
user_commands = true, -- Enable all or some usercommands
user_default_options = {
names = true, -- "Name" codes like Blue or red. Added from `vim.api.nvim_get_color_map()`
names_opts = { -- options for mutating/filtering names.
Expand Down Expand Up @@ -149,6 +145,9 @@ library to do custom highlighting themselves.
mode = "background", -- Set the display mode
-- Tailwind colors. boolean|'normal'|'lsp'|'both'. True is same as normal
tailwind = false, -- Enable tailwind colors
tailwind_opts = { -- Options for highlighting tailwind names
update_names = false, -- When using tailwind = 'both', update tailwind names from LSP results. See tailwind section
},
-- parsers can contain values used in |user_default_options|
sass = { enable = false, parsers = { "css" } }, -- Enable sass colors
-- Virtualtext character to use
Expand All @@ -161,20 +160,18 @@ library to do custom highlighting themselves.
-- example use: cmp_menu, cmp_docs
always_update = false,
},
-- all the sub-options of filetypes apply to buftypes
buftypes = {},
-- Boolean | List of usercommands to enable
user_commands = true, -- Enable all or some usercommands
})
```

MODES:
Highlighting modes:

- `background`: sets the background text color.
- `foreground`: sets the foreground text color.
- `virtualtext`: indicate the color behind the virtualtext.

For basic setup, you can use a command like the following.
Virtualtext symbol can be displayed at end of line, or

Setup examples:

```lua
-- Attaches to every FileType with default options
Expand Down Expand Up @@ -260,9 +257,9 @@ In `user_default_options`, there are 2 types of options

1. Alias options - `css`, `css_fn`

If `css_fn` is true, then `hsl_fn`, `rgb_fn` becomes `true`
If `css_fn` is true `hsl_fn`, `rgb_fn` becomes `true`

If `css` is true, then `names`, `RGB`, `RGBA`, `RRGGBB`, `RRGGBBAA`, `hsl_fn`, `rgb_fn`
If `css` is true `names`, `RGB`, `RGBA`, `RRGGBB`, `RRGGBBAA`, `hsl_fn`, `rgb_fn`
becomes `true`

These options have a priority, Individual options have the highest priority,
Expand Down Expand Up @@ -332,6 +329,38 @@ For lower level interface, see
[LuaDocs for API details](https://catgoose.github.io/nvim-colorizer.lua/modules/colorizer.html)
or use `:h colorizer` once installed.

### Tailwind

Tailwind colors can either be parsed from the Tailwind colors file (found in
`lua/colorizer/data/tailwind_colors.lua`) or by requesting
`textDocument/documentColor` from the LSP.

When using `tailwind="normal"`, only standard color names/values are parsed.

Using the `tailwind_opts.update_names` configuration option, the `tailwind_names`
color mapping will be updated with results from Tailwind LSP, including custom
colors defined in `tailwind.config.{js,ts}`.

This can be useful if you are highlighting `cmp_menu` filetype.

```typescript
// tailwind.config.ts
extend: {
colors: {
gray: {
600: '#2CEF6F',
700: '#AC50EF',
800: '#2ECFF6',
},
},
},
```

![tailwind.update_names](https://github.com/catgoose/screenshots/blob/51466fa599efe6d9821715616106c1712aad00c3/nvim-colorizer.lua/tailwind_update_names.png)

To improve highlighting performance with the slow Tailwind LSP, results from LSP
are cached and returned on `WinScrolled` event.

## Testing

For troubleshooting use `test/minimal.lua`.
Expand All @@ -350,7 +379,7 @@ to conveniently reattach Colorizer to `test/expect.lua` on save.

## Extras

Documentaion is generated using ldoc. See
Documentation is generated using ldoc. See
[scripts/gen_docs.sh](https://github.com/colorizer/nvim-colorizer.lua/blob/master/scripts/gen_docs.sh)

## TODO
Expand All @@ -359,7 +388,4 @@ Documentaion is generated using ldoc. See
- [ ] Add more display modes. E.g - sign column
- [ ] Use a more space efficient trie implementation.
- [ ] Support custom parsers

## Similar projects

[nvim-highlight-colors](https://github.com/brenoprata10/nvim-highlight-colors)
- [ ] Options support providing function to enable/disable instead of just boolean
Loading

0 comments on commit dd643c1

Please sign in to comment.