diff options
| author | HyBer <59885141+BBboy01@users.noreply.github.com> | 2026-03-26 17:54:19 +0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-03-26 05:54:19 -0400 |
| commit | 0624a7434ccb1fa27802b80e4a032ff71deea700 (patch) | |
| tree | eb6e1f621f125c69159805ed5d404207a9345ab9 | |
| parent | docs: update configs.md (diff) | |
| download | nvim-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)
| -rw-r--r-- | .github/ci/lint.sh | 2 | ||||
| -rw-r--r-- | lsp/stylelint_lsp.lua | 90 | ||||
| -rw-r--r-- | scripts/gen_json_schemas.lua | 2 |
3 files changed, 66 insertions, 28 deletions
diff --git a/.github/ci/lint.sh b/.github/ci/lint.sh index 8efeac73..8e4403ec 100644 --- a/.github/ci/lint.sh +++ b/.github/ci/lint.sh @@ -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 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' }, + }, + }, } diff --git a/scripts/gen_json_schemas.lua b/scripts/gen_json_schemas.lua index 9f62ff48..825824fc 100644 --- a/scripts/gen_json_schemas.lua +++ b/scripts/gen_json_schemas.lua @@ -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', |
