nvim-config/lua/configs/null-ls.lua

112 lines
3.0 KiB
Lua
Raw Normal View History

2022-03-01 02:58:43 +00:00
local M = {}
2022-05-01 17:43:11 +00:00
2022-03-01 02:58:43 +00:00
function M.config()
2023-01-10 18:21:09 +00:00
local status_ok, null_ls = pcall(require, 'null-ls')
2022-03-01 02:58:43 +00:00
if not status_ok then
return
end
2023-01-10 18:21:09 +00:00
local augroup = vim.api.nvim_create_augroup('LspFormatting', {})
null_ls.setup({
2022-03-01 02:58:43 +00:00
debug = false,
sources = {
2023-01-10 18:21:09 +00:00
-- https://github.com/jose-elias-alvarez/null-ls.nvim/blob/main/doc/BUILTINS.md
2023-01-10 18:21:09 +00:00
-- Lua
2023-01-10 18:21:09 +00:00
-- sudo pacman -S stylua
-- https://github.com/JohnnyMorganz/StyLua/releases
null_ls.builtins.formatting.stylua,
-- Python
-- pip install black
null_ls.builtins.formatting.black,
2023-01-10 18:21:09 +00:00
-- pip install isort
null_ls.builtins.formatting.isort,
2023-01-10 18:21:09 +00:00
-- JavaScript / TypeScript
2023-01-10 18:21:09 +00:00
-- sudo pacman -S prettierd
-- sudo npm install -g @fsouza/prettierd
null_ls.builtins.formatting.prettierd.with({
2023-01-10 18:21:09 +00:00
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
2023-01-10 18:21:09 +00:00
end,
}),
-- sudo pacman -S eslint_d
-- sudo npm install -g eslint_d
null_ls.builtins.diagnostics.eslint_d.with({
2023-01-10 18:21:09 +00:00
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
2023-01-10 18:21:09 +00:00
end,
}),
2022-03-01 02:58:43 +00:00
},
2023-01-10 18:21:09 +00:00
on_attach = function(client, bufnr)
-- disable null-ls completion hints
client.server_capabilities.completionProvider = false
2023-01-10 18:21:09 +00:00
if client.supports_method('textDocument/formatting') then
2023-06-23 22:42:10 +00:00
-- 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)
2024-01-17 17:43:33 +00:00
if cwd_name == 'invsys' or cwd_name == 'invsys-b' then
2023-06-23 22:42:10 +00:00
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
2023-01-10 18:21:09 +00:00
end
end,
})
2022-03-01 02:58:43 +00:00
end
return M