Skip to content

Conversation

nik-rev
Copy link
Contributor

@nik-rev nik-rev commented Dec 20, 2024

This PR adds a feature where a colorful square appears next to colors

image

We're building on top of the existing LSP by Helix by adding support for the textDocument/documentColor language server protocol method.

It's so nice, because we aren't actually hard-coding anything at all. This is not depedant on anything specific like CSS or tailwind, but rather a general implementations that works for any server which implements it. Since Helix already implements most of the LSP, we are building on top of it here.

The advantage here is that we have a lot more flexibility. For example, a popular neovim plugin that adds this functionality is nvchad-colorizer which hard-codes all the values which means if you define custom colors in your config they won't be shown. Here we won't have that problem

Closes #1115

@nik-rev nik-rev changed the title Color swatches Color swatches 🟥🟧🟨🟦🟩🟪 implementation of textDocument/documentColor Dec 20, 2024
@nik-rev nik-rev changed the title Color swatches 🟥🟧🟨🟦🟩🟪 implementation of textDocument/documentColor Color swatches, for example: 🟩 green, 🟥 #ffaaaa Dec 20, 2024
@nik-rev nik-rev changed the title Color swatches, for example: 🟩 green, 🟥 #ffaaaa Color swatches ( 🟩 green 🟥 #ffaaaa ) Dec 20, 2024
Copy link
Member

@gabydd gabydd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

most of this is good but the decorations need some changes for the first change see my comment for the second problem I am not sure how to solve it but because of how decorations are done being attached to an actual char index when softwrap is at the same char index as the color indicator then the softwrap indicator is highlighted instead see the attached screenshot
image

@gabydd
Copy link
Member

gabydd commented Dec 22, 2024

something to note here for interested parties the reason that we decided to go with decorations for now is that InlineAnnotations are styled by using theme variables(this is the Highlight newtype in the code which is an index into one of the theme styles) but for this feature we need to be able to give arbitrary styles to the text. I am sure there are other ways this could have been done but this seemed like the least invasive way to achieve this though it does still suffer a bit cause we cant differintiate from if this is the color indicator or the softwrap indicator unless we compare the grapheme.

@nik-rev nik-rev force-pushed the textDocument/documentColor branch 2 times, most recently from 23abef8 to 5f59c19 Compare December 23, 2024 02:22
@nik-rev
Copy link
Contributor Author

nik-rev commented Dec 23, 2024

something to note here for interested parties the reason that we decided to go with decorations for now is that InlineAnnotations are styled by using theme variables(this is the Highlight newtype in the code which is an index into one of the theme styles) but for this feature we need to be able to give arbitrary styles to the text. I am sure there are other ways this could have been done but this seemed like the least invasive way to achieve this though it does still suffer a bit cause we cant differintiate from if this is the color indicator or the softwrap indicator unless we compare the grapheme.

Not totally sure why but after using your latest commit for some time I noticed that the colors take a long time to update. Or they just don't update at all. For now I've reverted it

@gabydd
Copy link
Member

gabydd commented Dec 23, 2024

Ah good to know, sorry about that I'll have a better version just have been out all day

Copy link
Contributor

@TornaxO7 TornaxO7 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just some suggestions.

@nik-rev nik-rev force-pushed the textDocument/documentColor branch 2 times, most recently from efca472 to d120a35 Compare December 23, 2024 15:39
Copy link
Contributor

@TornaxO7 TornaxO7 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

peepoAwesome

@nik-rev nik-rev force-pushed the textDocument/documentColor branch from 5b205c6 to 67552f6 Compare December 25, 2024 18:29
@archseer archseer requested a review from the-mikedavis January 4, 2025 02:50
uncenter added a commit to uncenter/helix that referenced this pull request Jan 6, 2025
`patchy` is a tool which makes it easy to declaratively manage personal forks by automatically merging pull requests.

Check it out here: https://github.com/NikitaRevenco/patchy
@nik-rev nik-rev changed the title Color swatches ( 🟩 green 🟥 #ffaaaa ) feat: add colored boxes next to colors Jan 17, 2025
@kirawi kirawi added A-language-server Area: Language server client A-helix-term Area: Helix term improvements S-waiting-on-review Status: Awaiting review from a maintainer. labels Jan 19, 2025
@nik-rev nik-rev changed the title feat: add colored boxes next to colors Color swatches ( 🟩 green 🟥 #ffaaaa ) Jan 24, 2025
@nik-rev nik-rev force-pushed the textDocument/documentColor branch from 889ae97 to 5a0a773 Compare January 27, 2025 14:53
@nik-rev
Copy link
Contributor Author

nik-rev commented Jan 27, 2025

@the-mikedavis I finally fixed this PR, previously it was very hacky and it would also color the wrap indicator. Now, that bug is gone. it's ready to be reviewed now (I realise I should've put it as a draft earlier)

Since the handler type didn't have any extra information on it there's
no need for a wrapper, we can use the sender directly.

This also drops the unnecessary trigger in the completion code.
50ms is too low, it would trigger for every keypress. Instead we want to
query for these pretty lazily.
The byte-wise representation should be opaque even in the tests.
@the-mikedavis the-mikedavis merged commit 0ee5850 into helix-editor:master Mar 23, 2025
6 checks passed
@nik-rev nik-rev deleted the textDocument/documentColor branch March 23, 2025 21:11
@David-Else
Copy link
Contributor

I found a glitch:

#TableOfContents {
  background-color: #e5e7eb;
  font-size: 0.875rem; /* This is equivalent to Tailwind's text-sm, needs to change with mobile */
  line-height: 1.25rem; /* Tailwind adjusts line-height for better spacing, needs to change with mobile */
}

Insert the cursor right in front of the color block and enter insert mode, autocomplete appears, go through the colors, you will get this:

Screenshot From 2025-03-24 18-43-25

@nik-rev
Copy link
Contributor Author

nik-rev commented Mar 24, 2025

I found a glitch:

#TableOfContents {
  background-color: #e5e7eb;
  font-size: 0.875rem; /* This is equivalent to Tailwind's text-sm, needs to change with mobile */
  line-height: 1.25rem; /* Tailwind adjusts line-height for better spacing, needs to change with mobile */
}

Insert the cursor right in front of the color block and enter insert mode, autocomplete appears, go through the colors, you will get this:

Screenshot From 2025-03-24 18-43-25

Hmm this is weird. Likely the positions of the color swatches aren't being updated when doing the auto complete transaction

@q60
Copy link

q60 commented Mar 25, 2025

sorry for interrupting, but here i've made a tiny language server that sends documentColor on colors like "#ABC", "#AABBCC", "#AABBCCDD" - https://github.com/q60/uwu_colors. i guess it's a bit janky but it works

cc @DoctorRyner

@nik-rev
Copy link
Contributor Author

nik-rev commented Mar 25, 2025

sorry for interrupting, but here i've made a tiny language server that sends documentColor on colors like "#ABC", "#AABBCC", "#AABBCCDD" - https://github.com/q60/uwu_colors. i guess it's a bit janky but it works

cc @DoctorRyner

wow, that is really cool! Thanks for sharing. I like how simple it is, makes it a good starting point for learning how to create your own language server

@q60
Copy link

q60 commented Mar 25, 2025

sorry for interrupting, but here i've made a tiny language server that sends documentColor on colors like "#ABC", "#AABBCC", "#AABBCCDD" - https://github.com/q60/uwu_colors. i guess it's a bit janky but it works

wow, that is really cool! Thanks for sharing. I like how simple it is, makes it a good starting point for learning how to create your own language server

thanks for the kind words! i didn't think of making it an example on how to write a language server... which is ironic considering how i struggled because of the bad example code in tower_lsp docs with no information about handling files and their contents
i was looking looking at some language servers written in rust, and they do this in many different ways, that's a bit frustrating for me

i guess i will add comments to my code to make it more readable and understandable for someone who tries to write their own langserver! i am also planning to improve the regex situation and probably make some cli arguments so it'll work perfectly in different scenarios

@DoctorRainer
Copy link

DoctorRainer commented Mar 25, 2025

sorry for interrupting, but here i've made a tiny language server that sends documentColor on colors like "#ABC", "#AABBCC", "#AABBCCDD" - https://github.com/q60/uwu_colors. i guess it's a bit janky but it works

cc @DoctorRyner

Wow, this is exactly what the world needed, you are a hero. And we don't deserve you

rhogenson pushed a commit to rhogenson/helix that referenced this pull request Mar 29, 2025
sarabande pushed a commit to sarabande/helix that referenced this pull request Apr 11, 2025
EriKWDev pushed a commit to EriKWDev/helix that referenced this pull request Apr 24, 2025
darshanCommits pushed a commit to darshanCommits/helix that referenced this pull request May 1, 2025
Trivernis pushed a commit to Trivernis/helix-plus that referenced this pull request Jun 10, 2025
bmwiedemann pushed a commit to bmwiedemann/openSUSE that referenced this pull request Jul 17, 2025
https://build.opensuse.org/request/show/1293872
by user alessio.biancalana + anag_factory
- Update to version 25.07:
  Features:
  * Add a picker which explores directories ([#11285](helix-editor/helix#11285), [d4aed40](helix-editor/helix@d4aed40))
  * Allow cycling through multiple LSP Hover responses with `A-n`/`A-p` ([#10122](helix-editor/helix#10122), [2367b20](helix-editor/helix@2367b20))
  * Add support for incomplete LSP completions ([5c1f3f8](helix-editor/helix@5c1f3f8))
  * Add support for EditorConfig ([#13056](helix-editor/helix#13056), [#13443](helix-editor/helix#13443))
  * Add support for LSP document colors ([#12308](helix-editor/helix#12308), [d43de14](https://github.c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-helix-term Area: Helix term improvements A-language-server Area: Language server client
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Display the color of color-hex-codes, color functions