aboutsummaryrefslogtreecommitdiffstats
path: root/lsp
diff options
context:
space:
mode:
authorHyBer <59885141+BBboy01@users.noreply.github.com>2026-03-26 17:54:19 +0800
committerGitHub <noreply@github.com>2026-03-26 05:54:19 -0400
commit0624a7434ccb1fa27802b80e4a032ff71deea700 (patch)
treeeb6e1f621f125c69159805ed5d404207a9345ab9 /lsp
parentdocs: update configs.md (diff)
downloadnvim-lspconfig-0624a7434ccb1fa27802b80e4a032ff71deea700.tar
nvim-lspconfig-0624a7434ccb1fa27802b80e4a032ff71deea700.tar.gz
nvim-lspconfig-0624a7434ccb1fa27802b80e4a032ff71deea700.tar.bz2
nvim-lspconfig-0624a7434ccb1fa27802b80e4a032ff71deea700.tar.lz
nvim-lspconfig-0624a7434ccb1fa27802b80e4a032ff71deea700.tar.xz
nvim-lspconfig-0624a7434ccb1fa27802b80e4a032ff71deea700.tar.zst
nvim-lspconfig-0624a7434ccb1fa27802b80e4a032ff71deea700.zip
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)
Diffstat (limited to 'lsp')
-rw-r--r--lsp/stylelint_lsp.lua90
1 files changed, 64 insertions, 26 deletions
diff --git a/lsp/stylelint_lsp.lua b/lsp/stylelint_lsp.lua
index fbb25585..ed780b25 100644
--- a/lsp/stylelint_lsp.lua
+++ b/lsp/stylelint_lsp.lua
@@ -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' },
+ },
+ },
}