summaryrefslogtreecommitdiff
path: root/lua/ben/plugins/lsp/null-ls.lua
diff options
context:
space:
mode:
authorBenjamin Chausse <benjamin@chausse.xyz>2023-09-24 03:23:52 -0400
committerBenjamin Chausse <benjamin@chausse.xyz>2023-09-24 03:23:52 -0400
commitadbe65a8bd41f17398a985d4c474ba7f907bb5d4 (patch)
treef4ef9f6bd5687edf35b71aaac31ad6604ba36806 /lua/ben/plugins/lsp/null-ls.lua
Initial commit
Diffstat (limited to 'lua/ben/plugins/lsp/null-ls.lua')
-rw-r--r--lua/ben/plugins/lsp/null-ls.lua56
1 files changed, 56 insertions, 0 deletions
diff --git a/lua/ben/plugins/lsp/null-ls.lua b/lua/ben/plugins/lsp/null-ls.lua
new file mode 100644
index 0000000..9db49cc
--- /dev/null
+++ b/lua/ben/plugins/lsp/null-ls.lua
@@ -0,0 +1,56 @@
+return {
+ "jose-elias-alvarez/null-ls.nvim", -- configure formatters & linters
+ event = { "BufReadPre", "BufNewFile" },
+ config = function()
+ -- import null-ls plugin
+ local null_ls = require("null-ls")
+
+ local null_ls_utils = require("null-ls.utils")
+
+ -- for conciseness
+ local formatting = null_ls.builtins.formatting -- to setup formatters
+ local diagnostics = null_ls.builtins.diagnostics -- to setup linters
+
+ -- to setup format on save
+ local augroup = vim.api.nvim_create_augroup("LspFormatting", {})
+
+ -- configure null_ls
+ null_ls.setup({
+ -- add package.json as identifier for root (for typescript monorepos)
+ root_dir = null_ls_utils.root_pattern(".null-ls-root", "Makefile", ".git", "package.json"),
+ -- setup formatters & linters
+ sources = {
+ -- to disable file types use
+ -- "formatting.prettier.with({disabled_filetypes: {}})" (see null-ls docs)
+ formatting.prettier.with({
+ extra_filetypes = { "svelte" },
+ }), -- js/ts formatter
+ formatting.stylua, -- lua formatter
+ diagnostics.eslint_d.with({ -- js/ts linter
+ condition = function(utils)
+ return utils.root_has_file({ ".eslintrc.js", ".eslintrc.cjs" }) -- only enable if root has .eslintrc.js or .eslintrc.cjs
+ end,
+ }),
+ },
+ -- configure format on save
+ on_attach = function(current_client, bufnr)
+ if current_client.supports_method("textDocument/formatting") then
+ vim.api.nvim_clear_autocmds({ group = augroup, buffer = bufnr })
+ vim.api.nvim_create_autocmd("BufWritePre", {
+ group = augroup,
+ buffer = bufnr,
+ callback = function()
+ vim.lsp.buf.format({
+ filter = function(client)
+ -- only use null-ls for formatting instead of lsp server
+ return client.name == "null-ls"
+ end,
+ bufnr = bufnr,
+ })
+ end,
+ })
+ end
+ end,
+ })
+ end,
+}