grace/dots
dotfiles/nix configs
git clone https://git.gae.moe/grace/dots
ddf0c79
wafrn-testing
1--- @brief 2--- 3--- https://github.com/hrsh7th/vscode-langservers-extracted 4--- 5--- `vscode-eslint-language-server` is a linting engine for JavaScript / Typescript. 6--- It can be installed via `npm`: 7--- 8--- ```sh 9--- npm i -g vscode-langservers-extracted 10--- ``` 11--- 12--- The default `on_attach` config provides the `LspEslintFixAll` command that can be used to format a document on save: 13--- ```lua 14--- local base_on_attach = vim.lsp.config.eslint.on_attach 15--- vim.lsp.config("eslint", { 16--- on_attach = function(client, bufnr) 17--- if not base_on_attach then return end 18--- 19--- base_on_attach(client, bufnr) 20--- vim.api.nvim_create_autocmd("BufWritePre", { 21--- buffer = bufnr, 22--- command = "LspEslintFixAll", 23--- }) 24--- end, 25--- }) 26--- ``` 27--- 28--- See [vscode-eslint](https://github.com/microsoft/vscode-eslint/blob/55871979d7af184bf09af491b6ea35ebd56822cf/server/src/eslintServer.ts#L216-L229) for configuration options. 29--- 30--- Messages handled in lspconfig: `eslint/openDoc`, `eslint/confirmESLintExecution`, `eslint/probeFailed`, `eslint/noLibrary` 31--- 32--- Additional messages you can handle: `eslint/noConfig` 33--- 34--- ### Monorepo support 35--- 36--- `vscode-eslint-language-server` supports monorepos by default. It will automatically find the config file corresponding to the package you are working on. You can use different configs in different packages. 37--- This works without the need of spawning multiple instances of `vscode-eslint-language-server`. 38--- You can use a different version of ESLint in each package, but it is recommended to use the same version of ESLint in all packages. The location of the ESLint binary will be determined automatically. 39--- 40--- /!\ When using flat config files, you need to use them across all your packages in your monorepo, as it's a global setting for the server. 41--- 42--- ### Flat config in ESLint versions prior to 10.0 43--- 44--- If you're using a ESLint version that supports both flat config and eslintrc (>= 8.21, < 10.0) and want to change 45--- the [default behavior](https://eslint.org/blog/2023/10/flat-config-rollout-plans/), you'll need to set 46--- `experimental.useFlatConfig` accordingly: 47--- ```lua 48--- vim.lsp.config("eslint", { 49--- settings = { 50--- experimental = { 51--- -- If you want to use flat config on >= 8.21, < 9.0 52--- useFlatConfig = true, 53--- -- Or if you want to use eslintrc on 9.* 54--- -- useFlatConfig = false, 55--- } 56--- } 57--- }) 58--- ``` 59 60-- local util = require 'lspconfig.util' 61local lsp =vim . lsp 62 63local eslint_config_files ={ 64'.eslintrc' , 65'.eslintrc.js' , 66'.eslintrc.cjs' , 67'.eslintrc.yaml' , 68'.eslintrc.yml' , 69'.eslintrc.json' , 70'eslint.config.js' , 71'eslint.config.mjs' , 72'eslint.config.cjs' , 73'eslint.config.ts' , 74'eslint.config.mts' , 75'eslint.config.cts' , 76} 77 78---@type vim.lsp.Config 79return { 80cmd ={ 'vscode-eslint-language-server' , '--stdio' } , 81filetypes ={ 82'javascript' , 83'javascriptreact' , 84'typescript' , 85'typescriptreact' , 86'vue' , 87'svelte' , 88'astro' , 89'htmlangular' , 90} , 91workspace_required =true , 92-- on_attach = function(client, bufnr) 93-- vim.api.nvim_buf_create_user_command(bufnr, 'LspEslintFixAll', function() 94-- client:request_sync('workspace/executeCommand', { 95-- command = 'eslint.applyAllFixes', 96-- arguments = { 97-- { 98-- uri = vim.uri_from_bufnr(bufnr), 99-- version = lsp.util.buf_versions[bufnr], 100-- }, 101-- }, 102-- }, nil, bufnr) 103-- end, {}) 104-- end, 105root_dir =function ( bufnr , on_dir ) 106-- The project root is where the LSP can be started from 107-- As stated in the documentation above, this LSP supports monorepos and simple projects. 108-- We select then from the project root, which is identified by the presence of a package 109-- manager lock file. 110local root_markers ={ 'package-lock.json' , 'yarn.lock' , 'pnpm-lock.yaml' , 'bun.lockb' , 'bun.lock' } 111-- Give the root markers equal priority by wrapping them in a table 112root_markers =vim . fn . has ( 'nvim-0.11.3' ) == 1 and { root_markers , { '.git' , '.jj' } } 113or vim . list_extend ( root_markers , { '.git' } ) 114 115-- exclude deno 116if vim . fs . root ( bufnr , { 'deno.json' , 'deno.jsonc' , 'deno.lock' } ) then 117return 118end 119 120-- We fallback to the current working directory if no project root is found 121local project_root =vim . fs . root ( bufnr , root_markers ) or vim . fn . getcwd () 122 123-- We know that the buffer is using ESLint if it has a config file 124-- in its directory tree. 125-- 126-- Eslint used to support package.json files as config files, but it doesn't anymore. 127-- We keep this for backward compatibility. 128-- local filename = vim.api.nvim_buf_get_name(bufnr) 129-- local eslint_config_files_with_package_json = 130-- util.insert_package_json(eslint_config_files, 'eslintConfig', filename) 131-- local is_buffer_using_eslint = vim.fs.find(eslint_config_files_with_package_json, { 132-- path = filename, 133-- type = 'file', 134-- limit = 1, 135-- upward = true, 136-- stop = vim.fs.dirname(project_root), 137-- })[1] 138-- if not is_buffer_using_eslint then 139-- return 140-- end 141 142on_dir ( project_root ) 143end , 144-- Refer to https://github.com/Microsoft/vscode-eslint#settings-options for documentation. 145settings ={ 146validate ='on' , 147---@diagnostic disable-next-line: assign-type-mismatch 148packageManager =nil , 149useESLintClass =false , 150experimental ={} , 151codeActionOnSave ={ 152enable =false , 153mode ='all' , 154} , 155format =true , 156quiet =false , 157onIgnoredFiles ='off' , 158rulesCustomizations ={} , 159run ='onType' , 160problems ={ 161shortenToSingleLine =false , 162} , 163-- nodePath configures the directory in which the eslint server should start its node_modules resolution. 164-- This path is relative to the workspace folder (root dir) of the server instance. 165nodePath ='' , 166-- use the workspace folder location or the file location (if no workspace folder is open) as the working directory 167workingDirectory ={ mode ='auto' } , 168codeAction ={ 169disableRuleComment ={ 170enable =true , 171location ='separateLine' , 172} , 173showDocumentation ={ 174enable =true , 175} , 176} , 177} , 178before_init =function ( _ , config ) 179-- The "workspaceFolder" is a VSCode concept. It limits how far the 180-- server will traverse the file system when locating the ESLint config 181-- file (e.g., .eslintrc). 182local root_dir =config . root_dir 183 184if root_dir then 185config . settings =config . settings or {} 186config . settings . workspaceFolder ={ 187uri =root_dir , 188name =vim . fn . fnamemodify ( root_dir , ':t' ), 189} 190 191-- Support Yarn2 (PnP) projects 192local pnp_cjs =root_dir .. '/.pnp.cjs' 193local pnp_js =root_dir .. '/.pnp.js' 194if type ( config . cmd ) == 'table' and ( vim . uv . fs_stat ( pnp_cjs ) or vim . uv . fs_stat ( pnp_js )) then 195config . cmd =vim . list_extend ( { 'yarn' , 'exec' } , config . cmd --[[@as table]] ) 196end 197end 198end , 199handlers ={ 200[ 'eslint/openDoc' ] =function ( _ , result ) 201if result then 202vim . ui . open ( result . url ) 203end 204return {} 205end , 206[ 'eslint/confirmESLintExecution' ] =function ( _ , result ) 207if not result then 208return 209end 210return 4 -- approved 211end , 212[ 'eslint/probeFailed' ] =function () 213vim . notify ( '[lspconfig] ESLint probe failed.' , vim . log . levels . WARN ) 214return {} 215end , 216[ 'eslint/noLibrary' ] =function () 217vim . notify ( '[lspconfig] Unable to find ESLint library.' , vim . log . levels . WARN ) 218return {} 219end , 220} , 221}