56 lines
1.7 KiB
Lua
56 lines
1.7 KiB
Lua
-- null-ls setup
|
|
local null_ls = require("null-ls")
|
|
|
|
-- 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({
|
|
debug = true,
|
|
|
|
-- setup formatters & linters
|
|
sources = {
|
|
-- to disable file types use
|
|
-- "formatting.prettier.with({disabled_filetypes: {}})" (see null-ls docs)
|
|
formatting.prettier, -- js/ts formatter
|
|
formatting.rustfmt, -- rust formatter
|
|
formatting.stylua, -- lua formatter
|
|
formatting.gofumpt, -- go formatter
|
|
formatting.goimports, -- go auto-imports
|
|
diagnostics.eslint_d.with({
|
|
-- js/ts linter
|
|
-- only enable eslint if root has .eslintrc.js (not in youtube nvim video)
|
|
condition = function(utils)
|
|
return utils.root_has_file(".eslintrc.js") -- change file extension if you use something else
|
|
end,
|
|
}),
|
|
},
|
|
|
|
-- you can reuse a shared lspconfig on_attach callback here
|
|
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()
|
|
-- on 0.8, you should use vim.lsp.buf.format({ bufnr = bufnr }) instead
|
|
-- on later neovim version, you should use vim.lsp.buf.format({ async = false }) instead
|
|
vim.lsp.buf.format({
|
|
bufnr = bufnr,
|
|
async = false,
|
|
filter = function(client)
|
|
return client.name == "null-ls"
|
|
end,
|
|
})
|
|
-- vim.lsp.buf.formatting_sync()
|
|
end,
|
|
})
|
|
end
|
|
end,
|
|
})
|