Skip to content

Instantly share code, notes, and snippets.

@jmdfm
Created February 19, 2024 23:42
Show Gist options
  • Save jmdfm/2aad107c5d96cf8ad4f3e9ccb4f666e6 to your computer and use it in GitHub Desktop.
Save jmdfm/2aad107c5d96cf8ad4f3e9ccb4f666e6 to your computer and use it in GitHub Desktop.
Setup TailwindCSS LSP in LazyVim to provide completions in Ruby / Phlex components
-- textDocument/diagnostic support until 0.10.0 is released
_timers = {}
local function setup_diagnostics(client, buffer)
if require("vim.lsp.diagnostic")._enable then
return
end
local diagnostic_handler = function()
local params = vim.lsp.util.make_text_document_params(buffer)
client.request("textDocument/diagnostic", { textDocument = params }, function(err, result)
if err then
local err_msg = string.format("diagnostics error - %s", vim.inspect(err))
vim.lsp.log.error(err_msg)
end
local diagnostic_items = {}
if result then
diagnostic_items = result.items
end
vim.lsp.diagnostic.on_publish_diagnostics(
nil,
vim.tbl_extend("keep", params, { diagnostics = diagnostic_items }),
{ client_id = client.id }
)
end)
end
diagnostic_handler() -- to request diagnostics on buffer when first attaching
vim.api.nvim_buf_attach(buffer, false, {
on_lines = function()
if _timers[buffer] then
vim.fn.timer_stop(_timers[buffer])
end
_timers[buffer] = vim.fn.timer_start(200, diagnostic_handler)
end,
on_detach = function()
if _timers[buffer] then
vim.fn.timer_stop(_timers[buffer])
end
end,
})
end
return {
-- tools
{
"williamboman/mason.nvim",
opts = function(_, opts)
vim.list_extend(opts.ensure_installed, {
"stylua",
"selene",
"luacheck",
"shellcheck",
"shfmt",
"tailwindcss-language-server",
"typescript-language-server",
"stylelint",
})
end,
},
{
"neovim/nvim-lspconfig",
---@class PluginLspOpts
opts = {
diagnostics = {
underline = false,
update_in_insert = false,
virtual_text = {
spacing = 4,
source = "if_many",
prefix = "●",
-- this will set set the prefix to a function that returns the diagnostics icon based on the severity
-- this only works on a recent 0.10.0 build. Will be set to "●" when not supported
-- prefix = "icons",
},
severity_sort = true,
},
setup = {
tailwindcss = function(_, opts)
local tw = require("lspconfig.server_configurations.tailwindcss")
opts.filetypes = opts.filetypes or {}
-- Add default filetypes
vim.list_extend(opts.filetypes, tw.default_config.filetypes)
-- Remove excluded filetypes
--- @param ft string
opts.filetypes = vim.tbl_filter(function(ft)
return not vim.tbl_contains(opts.filetypes_exclude or {}, ft)
end, opts.filetypes)
-- Add additional filetypes
vim.list_extend(opts.filetypes, opts.filetypes_include or {})
end,
},
servers = {
tailwindcss = {
filetypes = {
"css",
"scss",
"sass",
"html",
"eruby",
"ruby",
"javascript",
"javascriptreact",
"typescript",
"typescriptreact",
"rust",
"svelte",
},
init_options = {
userLanguages = {
eruby = "erb",
svelte = "html",
rust = "html",
ruby = "html",
},
},
settings = {
includeLanguages = {
typescript = "javascript",
typescriptreact = "javascript",
ruby = "html",
erb = "html",
svelte = "html",
rust = "html",
},
tailwindCSS = {
lint = {
cssConflict = "warning",
invalidApply = "error",
invalidConfigPath = "error",
invalidScreen = "error",
invalidTailwindDirective = "error",
invalidVariant = "error",
recommendedVariantOrder = "warning",
},
experimental = {
classRegex = {
[[class= "([^"]*)]],
[[class: "([^"]*)]],
'~H""".*class="([^"]*)".*"""',
'~F""".*class="([^"]*)".*"""',
},
},
validate = true,
},
},
},
ruby_ls = {
cmd = { "bundle", "exec", "ruby-lsp" },
init_options = {
formatter = "auto",
},
settings = {},
on_attach = function(client, buffer)
setup_diagnostics(client, buffer)
end,
},
stylelint_lsp = {
filetypes = { "css", "scss" },
root_dir = require("lspconfig").util.root_pattern("package.json", ".git"),
settings = {
stylelintplus = {
-- see available options in stylelint-lsp documentation
},
},
on_attach = function(client)
client.server_capabilities.document_formatting = false
end,
},
},
},
},
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment