2022-11-06 23:46:45 -05:00
local setup , null_ls = pcall ( require , " null-ls " )
if not setup then
2022-11-07 17:33:59 -05:00
return
2022-11-06 23:46:45 -05:00
end
local formatting = null_ls.builtins . formatting
2023-10-07 15:29:18 -04:00
local diagnostics = null_ls.builtins . diagnostics
2023-01-02 16:20:08 -05:00
local conditional = function ( fn )
local utils = require ( " null-ls.utils " ) . make_conditional_utils ( )
return fn ( utils )
end
2023-02-04 18:11:52 -05:00
local lsp_formatting = function ( bufnr )
vim.lsp . buf.format ( {
timeout_ms = 2000 ,
filter = function ( client )
-- apply whatever logic you want (in this example, we'll only use null-ls)
return client.name ~= " tsserver "
end ,
bufnr = bufnr ,
} )
end
2022-11-06 23:46:45 -05:00
local augroup = vim.api . nvim_create_augroup ( " LspFormatting " , { } )
2023-02-04 18:11:52 -05:00
-- local my_rubocop_formatter = require("rahcodes.null-ls.rubocop")
local rubocop = null_ls.builtins . formatting.rubocop
2022-11-06 23:46:45 -05:00
null_ls.setup ( {
2023-10-07 15:29:18 -04:00
debug = false ,
2022-11-07 17:33:59 -05:00
sources = {
formatting.prettier ,
2023-10-07 18:01:26 -04:00
-- formatting.stylua,
2023-08-29 19:26:16 -04:00
null_ls.builtins . code_actions.gitsigns ,
2023-10-07 15:29:18 -04:00
-- setting eslint_d only if we have a ".eslintrc.js" file in the project
diagnostics.eslint_d . with ( {
condition = function ( utils )
return utils.root_has_file ( { ' .eslintrc.js ' } )
end
} ) ,
2023-01-02 16:20:08 -05:00
-- Here we set a conditional to call the rubocop formatter. If we have a Gemfile in the project, we call "bundle exec rubocop", if not we only call "rubocop".
conditional ( function ( utils )
return utils.root_has_file ( " Gemfile " )
2023-02-04 18:11:52 -05:00
and rubocop.with ( {
command = " bundle " ,
args = vim.list_extend ( { " exec " , " rubocop " } , rubocop._opts . args ) ,
} )
or rubocop
2023-01-02 16:20:08 -05:00
end ) ,
-- Same as above, but with diagnostics.rubocop to make sure we use the proper rubocop version for the project
conditional ( function ( utils )
return utils.root_has_file ( " Gemfile " )
2023-02-04 18:11:52 -05:00
and null_ls.builtins . diagnostics.rubocop . with ( {
command = " bundle " ,
args = vim.list_extend ( { " exec " , " rubocop " } , null_ls.builtins . diagnostics.rubocop . _opts.args ) ,
} )
2023-01-02 16:20:08 -05:00
or null_ls.builtins . diagnostics.rubocop
end ) ,
2022-11-07 17:33:59 -05:00
} ,
-- format on save
on_attach = function ( client , bufnr )
if 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 ( )
2023-02-04 18:11:52 -05:00
lsp_formatting ( bufnr )
2022-11-07 17:33:59 -05:00
end ,
} )
end
end ,
2022-11-06 23:46:45 -05:00
} )