aboutsummaryrefslogtreecommitdiffstats
path: root/lua
diff options
context:
space:
mode:
authorJens Claes <jensclaes33@gmail.com>2021-10-12 22:25:53 +0200
committerGitHub <noreply@github.com>2021-10-12 13:25:53 -0700
commitfe65c927d53da7f7964e7ac113a9ab4eafdf96f4 (patch)
tree1a7272d10f00d13597675c41192ccf3e82c9f742 /lua
parentfix(denols): update handlers to support 0.5.1 signature (#1233) (diff)
downloadnvim-lspconfig-fe65c927d53da7f7964e7ac113a9ab4eafdf96f4.tar
nvim-lspconfig-fe65c927d53da7f7964e7ac113a9ab4eafdf96f4.tar.gz
nvim-lspconfig-fe65c927d53da7f7964e7ac113a9ab4eafdf96f4.tar.bz2
nvim-lspconfig-fe65c927d53da7f7964e7ac113a9ab4eafdf96f4.tar.lz
nvim-lspconfig-fe65c927d53da7f7964e7ac113a9ab4eafdf96f4.tar.xz
nvim-lspconfig-fe65c927d53da7f7964e7ac113a9ab4eafdf96f4.tar.zst
nvim-lspconfig-fe65c927d53da7f7964e7ac113a9ab4eafdf96f4.zip
feat: add eslint language server (#1273)
Diffstat (limited to 'lua')
-rw-r--r--lua/lspconfig/eslint.lua130
1 files changed, 130 insertions, 0 deletions
diff --git a/lua/lspconfig/eslint.lua b/lua/lspconfig/eslint.lua
new file mode 100644
index 00000000..a3ddfd17
--- /dev/null
+++ b/lua/lspconfig/eslint.lua
@@ -0,0 +1,130 @@
+local configs = require 'lspconfig/configs'
+local util = require 'lspconfig/util'
+local lsp = vim.lsp
+
+local get_eslint_client = function()
+ local active_clients = lsp.get_active_clients()
+ for _, client in ipairs(active_clients) do
+ if client.name == 'eslint' then
+ return client
+ end
+ end
+ return nil
+end
+local function fix_all(opts)
+ opts = opts or {}
+
+ local eslint_lsp_client = get_eslint_client()
+ if eslint_lsp_client == nil then
+ return
+ end
+
+ local request
+ if opts.sync or false then
+ request = function(bufnr, method, params)
+ eslint_lsp_client.request(method, params, nil, bufnr)
+ end
+ else
+ request = function(bufnr, method, params)
+ eslint_lsp_client.request_sync(method, params, nil, bufnr)
+ end
+ end
+
+ local bufnr = util.validate_bufnr(opts.bufnr or 0)
+ request(0, 'workspace/executeCommand', {
+ command = 'eslint.applyAllFixes',
+ arguments = {
+ {
+ uri = vim.uri_from_bufnr(bufnr),
+ version = lsp.util.buf_versions[bufnr],
+ },
+ },
+ })
+end
+
+local bin_name = 'vscode-eslint-language-server'
+configs.eslint = {
+ default_config = {
+ cmd = { bin_name, '--stdio' },
+ filetypes = {
+ 'javascript',
+ 'javascriptreact',
+ 'javascript.jsx',
+ 'typescript',
+ 'typescriptreact',
+ 'typescript.tsx',
+ },
+ root_dir = util.root_pattern('.eslintrc.json', '.eslintrc.js', 'package.json', 'tsconfig.json', '.git'),
+ settings = {
+ validate = 'off',
+ packageManager = 'npm',
+ useESLintClass = false,
+ codeActionOnSave = {
+ enable = false,
+ mode = 'all',
+ },
+ format = false,
+ quiet = false,
+ onIgnoredFiles = 'off',
+ rulesCustomizations = {},
+ run = 'onType',
+ nodePath = vim.NIL,
+ codeAction = {
+ disableRuleComment = {
+ enable = true,
+ location = 'separateLine',
+ },
+ showDocumentation = {
+ enable = true,
+ },
+ },
+ },
+ handlers = {
+ ['eslint/openDoc'] = function(_, result)
+ if not result then
+ return
+ end
+ vim.cmd('!open ' .. result.url)
+ return {}
+ end,
+ ['eslint/confirmESLintExecution'] = function(_, result)
+ if not result then
+ return
+ end
+ return 4 -- approved
+ end,
+ },
+ },
+ commands = {
+ EslintFixAll = {
+ function()
+ fix_all { sync = true, bufnr = 0 }
+ end,
+ description = 'Fix all eslint problems for this buffer',
+ },
+ },
+ docs = {
+ description = [[
+https://github.com/hrsh7th/vscode-langservers-extracted
+
+vscode-eslint-language-server: A linting engine for JavaScript / Typescript
+
+`vscode-eslint-language-server` can be installed via `npm`:
+```sh
+npm i -g vscode-langservers-extracted
+```
+
+vscode-eslint-language-server provides an EslintFixAll command that can be used to format document on save
+```vim
+autocmd BufWritePre <buffer> <cmd>EslintFixAll<CR>
+```
+
+See [vscode-eslint](https://github.com/microsoft/vscode-eslint/blob/55871979d7af184bf09af491b6ea35ebd56822cf/server/src/eslintServer.ts#L216-L229) for configuration options.
+
+Additional messages you can handle: eslint/probeFailed, eslint/noLibrary, eslint/noConfig
+Messages already handled in lspconfig: eslint/openDoc, eslint/confirmESLintExecution
+]],
+ },
+}
+
+configs.eslint.fix_all = fix_all