local M = {} function M.config() local status_ok, null_ls = pcall(require, 'null-ls') if not status_ok then return end local augroup = vim.api.nvim_create_augroup('LspFormatting', {}) null_ls.setup({ debug = false, sources = { -- https://github.com/jose-elias-alvarez/null-ls.nvim/blob/main/doc/BUILTINS.md -- Lua -- sudo pacman -S stylua -- https://github.com/JohnnyMorganz/StyLua/releases null_ls.builtins.formatting.stylua, -- Python -- pip install black null_ls.builtins.formatting.black, -- pip install isort null_ls.builtins.formatting.isort, -- JavaScript / TypeScript -- sudo pacman -S prettierd -- sudo npm install -g @fsouza/prettierd null_ls.builtins.formatting.prettierd.with({ disabled_filetypes = { 'html', 'markdown' }, condition = function() -- https://github.com/jose-elias-alvarez/null-ls.nvim/discussions/1269 return require('null-ls.utils').root_pattern( '.prettierrc', '.prettierrc.json', '.prettierrc.yml', '.prettierrc.yaml', '.prettierrc.json5', '.prettierrc.js', '.prettierrc.cjs', '.prettier.config.js', '.prettier.config.cjs', '.prettierrc.toml' )(vim.api.nvim_buf_get_name(0)) ~= nil end, }), -- sudo pacman -S eslint_d -- sudo npm install -g eslint_d null_ls.builtins.diagnostics.eslint_d.with({ diagnostic_config = { virtual_text = false, severity_sort = true, }, root_pattern = function() return require('null-ls.utils').root_pattern( -- file names copied from -- https://github.com/jose-elias-alvarez/null-ls.nvim/blob/main/lua/null-ls/builtins/diagnostics/eslint.lua 'eslint.config.js', '.eslintrc', '.eslintrc.js', '.eslintrc.cjs', '.eslintrc.yaml', '.eslintrc.yml', '.eslintrc.json' )(vim.api.nvim_buf_get_name(0)) end, condition = function() return require('null-ls.utils').root_pattern( -- file names copied from -- https://github.com/jose-elias-alvarez/null-ls.nvim/blob/main/lua/null-ls/builtins/diagnostics/eslint.lua 'eslint.config.js', '.eslintrc', '.eslintrc.js', '.eslintrc.cjs', '.eslintrc.yaml', '.eslintrc.yml', '.eslintrc.json' )(vim.api.nvim_buf_get_name(0)) ~= nil end, }), }, on_attach = function(client, bufnr) -- disable null-ls completion hints client.server_capabilities.completionProvider = false if client.supports_method('textDocument/formatting') then -- Format before save *only* in invsys local cwd = vim.fn.getcwd() -- substr the cwd starting at the last "/" character local cwd_name = string.sub(cwd, -cwd:reverse():find('/') + 1) if cwd_name == 'invsys' or cwd_name == 'invsys-b' 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({ bufnr = bufnr }) end, }) end end end, }) end return M