feat(stylelint)!: migrate to stylelint-language-server #4351

stylelint now has an official LS:
[@stylelint/language-server](https://github.com/stylelint/vscode-stylelint/tree/main/packages/language-server)
This commit is contained in:
HyBer 2026-03-26 17:54:19 +08:00 committed by GitHub
parent 46204c8fda
commit 0624a7434c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 66 additions and 28 deletions

2
.github/ci/lint.sh vendored
View File

@ -53,7 +53,7 @@ _check_lsp_cmd_prefix() {
# Enforce client:exec_cmd().
_check_exec_cmd() {
local exclude='eslint\|pyright\|basedpyright'
local exclude='eslint\|stylelint\|pyright\|basedpyright'
if git grep -P 'workspace.executeCommand' -- 'lsp/*.lua' | grep -v "$exclude" ; then
_fail 'Use client:exec_cmd() instead of calling request("workspace/executeCommand") directly. Example: lsp/pyright.lua'
fi

View File

@ -1,28 +1,14 @@
---@brief
---
--- https://github.com/bmatcuk/stylelint-lsp
--- https://github.com/stylelint/vscode-stylelint/tree/main/packages/language-server
---
--- `stylelint-lsp` can be installed via `npm`:
---
--- ```sh
--- npm i -g stylelint-lsp
--- ```
---
--- Can be configured by passing a `settings.stylelintplus` object to vim.lsp.config('stylelint_lsp'):
---
--- ```lua
--- vim.lsp.config('stylelint_lsp', {
--- settings = {
--- stylelintplus = {
--- -- see available options in stylelint-lsp documentation
--- }
--- }
--- })
--- `stylelint-language-server` can be installed via npm `npm install -g @stylelint/language-server`.
--- ```
local util = require 'lspconfig.util'
local lsp = vim.lsp
local root_file = {
local stylelint_config_files = {
'.stylelintrc',
'.stylelintrc.mjs',
'.stylelintrc.cjs',
@ -35,22 +21,74 @@ local root_file = {
'stylelint.config.js',
}
root_file = util.insert_package_json(root_file, 'stylelint')
---@type vim.lsp.Config
return {
cmd = { 'stylelint-lsp', '--stdio' },
cmd = { 'stylelint-language-server', '--stdio' },
filetypes = {
'astro',
'css',
'html',
'less',
'scss',
'sugarss',
'vue',
'wxss',
},
root_markers = root_file,
---@type lspconfig.settings.stylelint_lsp
settings = {},
root_dir = function(bufnr, on_dir)
-- The project root is where the LSP can be started from
-- As stated in the documentation above, this LSP supports monorepos and simple projects.
-- We select then from the project root, which is identified by the presence of a package
-- manager lock file.
local root_markers = { 'package-lock.json', 'yarn.lock', 'pnpm-lock.yaml', 'bun.lockb', 'bun.lock' }
-- Give the root markers equal priority by wrapping them in a table
root_markers = vim.fn.has('nvim-0.11.3') == 1 and { root_markers, { '.git' } }
or vim.list_extend(root_markers, { '.git' })
-- exclude deno
if vim.fs.root(bufnr, { 'deno.json', 'deno.jsonc', 'deno.lock' }) then
return
end
-- We fallback to the current working directory if no project root is found
local project_root = vim.fs.root(bufnr, root_markers) or vim.fn.getcwd()
-- We know that the buffer is using Stylelint if it has a config file
-- in its directory tree.
--
-- Stylelint support package.json files as config files.
local filename = vim.api.nvim_buf_get_name(bufnr)
local stylelint_config_files_with_package_json =
util.insert_package_json(stylelint_config_files, 'stylelintConfig', filename)
local is_buffer_using_stylelint = vim.fs.find(stylelint_config_files_with_package_json, {
path = filename,
type = 'file',
limit = 1,
upward = true,
stop = vim.fs.dirname(project_root),
})[1]
if not is_buffer_using_stylelint then
return
end
on_dir(project_root)
end,
on_attach = function(client, bufnr)
vim.api.nvim_buf_create_user_command(bufnr, 'LspStylelintFixAll', function()
client:request_sync('workspace/executeCommand', {
command = 'stylelint.applyAutoFix',
arguments = {
{
uri = vim.uri_from_bufnr(bufnr),
version = lsp.util.buf_versions[bufnr],
},
},
}, nil, bufnr)
end, {})
end,
-- Refer to https://github.com/stylelint/vscode-stylelint?tab=readme-ov-file#extension-settings for documentation.
---@type lspconfig.settings.stylelint_language_server
settings = {
stylelint = {
validate = { 'css', 'postcss' },
snippet = { 'css', 'postcss' },
},
},
}

View File

@ -49,7 +49,7 @@ local index = {
sorbet = 'https://raw.githubusercontent.com/sorbet/sorbet/master/vscode_extension/package.json',
sourcekit = 'https://raw.githubusercontent.com/swift-server/vscode-swift/main/package.json',
spectral = 'https://raw.githubusercontent.com/stoplightio/vscode-spectral/master/package.json',
stylelint_lsp = 'https://raw.githubusercontent.com/bmatcuk/coc-stylelintplus/master/package.json',
stylelint_lsp = 'https://raw.githubusercontent.com/stylelint/vscode-stylelint/main/package.json',
svelte = 'https://raw.githubusercontent.com/sveltejs/language-tools/master/packages/svelte-vscode/package.json',
svlangserver = 'https://raw.githubusercontent.com/eirikpre/VSCode-SystemVerilog/master/package.json',
tailwindcss = 'https://raw.githubusercontent.com/tailwindlabs/tailwindcss-intellisense/master/packages/vscode-tailwindcss/package.json',