lua/josean/plugins/lsp/none-ls.lua (2514B)
1 return {
2 "nvimtools/none-ls.nvim", -- configure formatters & linters
3 lazy = true,
4 -- event = { "BufReadPre", "BufNewFile" }, -- to enable uncomment this
5 dependencies = {
6 "jay-babu/mason-null-ls.nvim",
7 },
8 config = function()
9 local mason_null_ls = require("mason-null-ls")
10
11 local null_ls = require("null-ls")
12
13 local null_ls_utils = require("null-ls.utils")
14
15 mason_null_ls.setup({
16 ensure_installed = {
17 "prettier", -- prettier formatter
18 "stylua", -- lua formatter
19 "black", -- python formatter
20 "pylint", -- python linter
21 "eslint_d", -- js linter
22 },
23 })
24
25 -- for conciseness
26 local formatting = null_ls.builtins.formatting -- to setup formatters
27 local diagnostics = null_ls.builtins.diagnostics -- to setup linters
28
29 -- to setup format on save
30 local augroup = vim.api.nvim_create_augroup("LspFormatting", {})
31
32 -- configure null_ls
33 null_ls.setup({
34 -- add package.json as identifier for root (for typescript monorepos)
35 root_dir = null_ls_utils.root_pattern(".null-ls-root", "Makefile", ".git", "package.json"),
36 -- setup formatters & linters
37 sources = {
38 -- to disable file types use
39 -- "formatting.prettier.with({disabled_filetypes: {}})" (see null-ls docs)
40 formatting.prettier.with({
41 extra_filetypes = { "svelte" },
42 }), -- js/ts formatter
43 formatting.stylua, -- lua formatter
44 formatting.isort,
45 formatting.black,
46 diagnostics.pylint,
47 diagnostics.eslint_d.with({ -- js/ts linter
48 condition = function(utils)
49 return utils.root_has_file({ ".eslintrc.js", ".eslintrc.cjs" }) -- only enable if root has .eslintrc.js or .eslintrc.cjs
50 end,
51 }),
52 },
53 -- configure format on save
54 on_attach = function(current_client, bufnr)
55 if current_client.supports_method("textDocument/formatting") then
56 vim.api.nvim_clear_autocmds({ group = augroup, buffer = bufnr })
57 vim.api.nvim_create_autocmd("BufWritePre", {
58 group = augroup,
59 buffer = bufnr,
60 callback = function()
61 vim.lsp.buf.format({
62 filter = function(client)
63 -- only use null-ls for formatting instead of lsp server
64 return client.name == "null-ls"
65 end,
66 bufnr = bufnr,
67 })
68 end,
69 })
70 end
71 end,
72 })
73 end,
74 }