grace/dots

dotfiles/nix configs

git clone https://git.gae.moe/grace/dots

Grace Yoderhx and vcs changesddf0c79

idk
7.5 KiB221 linesraw
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 {
80  cmd = { 'vscode-eslint-language-server', '--stdio' },
81  filetypes = {
82    'javascript',
83    'javascriptreact',
84    'typescript',
85    'typescriptreact',
86    'vue',
87    'svelte',
88    'astro',
89    'htmlangular',
90  },
91  workspace_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,
105  root_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.
110    local 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
112    root_markers = vim.fn.has('nvim-0.11.3') == 1 and { root_markers, { '.git', '.jj' } }
113      or vim.list_extend(root_markers, { '.git' })
114
115    -- exclude deno
116    if vim.fs.root(bufnr, { 'deno.json', 'deno.jsonc', 'deno.lock' }) then
117      return
118    end
119
120    -- We fallback to the current working directory if no project root is found
121    local 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
142    on_dir(project_root)
143  end,
144  -- Refer to https://github.com/Microsoft/vscode-eslint#settings-options for documentation.
145  settings = {
146    validate = 'on',
147    ---@diagnostic disable-next-line: assign-type-mismatch
148    packageManager = nil,
149    useESLintClass = false,
150    experimental = {},
151    codeActionOnSave = {
152      enable = false,
153      mode = 'all',
154    },
155    format = true,
156    quiet = false,
157    onIgnoredFiles = 'off',
158    rulesCustomizations = {},
159    run = 'onType',
160    problems = {
161      shortenToSingleLine = 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.
165    nodePath = '',
166    -- use the workspace folder location or the file location (if no workspace folder is open) as the working directory
167    workingDirectory = { mode = 'auto' },
168    codeAction = {
169      disableRuleComment = {
170        enable = true,
171        location = 'separateLine',
172      },
173      showDocumentation = {
174        enable = true,
175      },
176    },
177  },
178  before_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).
182    local root_dir = config.root_dir
183
184    if root_dir then
185      config.settings = config.settings or {}
186      config.settings.workspaceFolder = {
187        uri = root_dir,
188        name = vim.fn.fnamemodify(root_dir, ':t'),
189      }
190
191      -- Support Yarn2 (PnP) projects
192      local pnp_cjs = root_dir .. '/.pnp.cjs'
193      local pnp_js = root_dir .. '/.pnp.js'
194      if type(config.cmd) == 'table' and (vim.uv.fs_stat(pnp_cjs) or vim.uv.fs_stat(pnp_js)) then
195        config.cmd = vim.list_extend({ 'yarn', 'exec' }, config.cmd --[[@as table]])
196      end
197    end
198  end,
199  handlers = {
200    ['eslint/openDoc'] = function(_, result)
201      if result then
202        vim.ui.open(result.url)
203      end
204      return {}
205    end,
206    ['eslint/confirmESLintExecution'] = function(_, result)
207      if not result then
208        return
209      end
210      return 4 -- approved
211    end,
212    ['eslint/probeFailed'] = function()
213      vim.notify('[lspconfig] ESLint probe failed.', vim.log.levels.WARN)
214      return {}
215    end,
216    ['eslint/noLibrary'] = function()
217      vim.notify('[lspconfig] Unable to find ESLint library.', vim.log.levels.WARN)
218      return {}
219    end,
220  },
221}