themaxmarchuk / tailwindcss-colors.nvim

Highlights Tailwind CSS class names when @tailwindcss/language-server is connected
MIT License
55 stars 1 forks source link

Add `on_attach` Method to avoid vim notifications on non `tailwindcss-lsp` files. #5

Open abasnfarah opened 9 months ago

abasnfarah commented 9 months ago

Add on_attach Method to avoid vim notifications on non tailwindcss-lsp files

Overview

This pull request introduces a new on_attach(client, bufnr) method to the plugin. The primary goal of this update is to integrate Tailwind CSS LSP client, that our buf_attach method is only invoked when the Tailwind CSS LSP is attached to a buffer.

Motivation

Avoids tailwindcss-colors notifications on non tailwindcss-lsp buffers. I would frequently get the tailwindcss-colors: current buffer is not attached to tailwindcss lsp client notification on non tailwindcss-lsp filetypes. It got very distracting so I went ahead and updated it to work on my end.

I choose to make it a new method instead of updating the buf_attach method for 2 reasons.

  1. Not to introduce breaking changes to the plugin for those who use it.
  2. To allow people to configure it more easily with nvim-lsp and mason. Avoiding doing the client check in their config.

I also updated the public facing methods to more closely follow Lua Documentation Guidlines



---This function attaches to a buffer, updating highlights on change
---It filters for tailwindcss lsp client
---@param client table A |vim.lsp.client| object
---@param bufnr number
function M.on_attach(client, bufnr)
   if not client then
    vim.notify_once "tailwindcss-colors: on_attach client tried to attach to a nil client"
    return
   end

   if client.name == "tailwindcss" then
      M.buf_attach(bufnr)
   end
end