brenoprata10 / nvim-highlight-colors

Highlight colors for neovim
724 stars 33 forks source link

Support LSP through `textDocument/documentColor` requests #59

Closed mehalter closed 7 months ago

mehalter commented 9 months ago

The LSP specification defines a capability for a language server to return elements that are colors (Document Color Requests). This is supported by language servers like tailwindcss.

I started looking at the source code here and doing a bit of an investigation, but haven't gotten it organized into a pull request yet. I'm not sure if I will have time, so I figured I would leave the information I have gathered so far for what would need to go into a pull request:

  1. You can easily do asynchronous LSP buffer requests with this format:
local param = { textDocument = vim.lsp.util.make_text_document_params() }
vim.lsp.buf_request_all(active_buffer_id, "textDocument/documentColor", param, function(resps)
  for _, resp in pairs(resps) do
    if resp.error == nil and resp.result then
      for _, color in pairs(resp.result) do
        local r, g, b, a =
          color.color.red or 0, color.color.green or 0, color.color.blue or 0, color.color.alpha or 0

        utils.create_highlight(
          active_buffer_id,
          ns_id,
          color.range.start.line - 1,
          color.range.start.character,
          color.range["end"].character,
          string.format("#%02x%02x%02x", r * a * 255, g * a * 255, b * a * 255),
          options.render,
          options.custom_colors
        )
      end
    end
  end
end)
  1. create_highlight should be updated to support multi-line highlights. with a start row/end row as these are possible (color.range.start.line and color.range["end"].line)
  2. create_highlight could be modified to also take in a color table rather than just a string and decoding it.
  3. the tailwind regex patterns become unnecessary as the conversion and calculations of these are fully handled by the language server (resolves #48)

I hope this helps provide some guidance to getting this implemented! Thanks so much for maintaining this plugin!

brenoprata10 commented 9 months ago

Hey o/ Thanks for spending your time with this. I will totally support LSP in future updates, so no need to open a PR for me. I will use your code as a basis for the feature :)

brenoprata10 commented 7 months ago

Hey o/ Just providing feedback. I've started working on the LSP integration and so far it's looking good! Hopefully, i will have something to show soon.

mehalter commented 7 months ago

Thanks for the update @brenoprata10 ! Very exciting stuff!! Let me know if you want any help testing or any work you want to offload on a random stranger on the interwebs 😂

brenoprata10 commented 7 months ago

@mehalter Do you know which LSPs support color highlighting? The only one I know of is tailwindcss. I would like to test my solution with more LSPs before release

brenoprata10 commented 7 months ago

PR is created with the initial version, Could you give it a try?

mehalter commented 7 months ago

Just tested and it works great! Thanks so much!