Added prettier back and limited it

This commit is contained in:
2024-03-25 22:30:51 +01:00
parent 9366cff430
commit b7e9d9fa45
3 changed files with 35 additions and 8 deletions

View File

@@ -32,6 +32,7 @@ return {
mason_null_ls.setup({ mason_null_ls.setup({
ensure_installed = { ensure_installed = {
"stylua", "stylua",
"prettier",
}, },
automatic_installation = true, automatic_installation = true,
}) })

View File

@@ -9,9 +9,29 @@ return {
local formatting = null_ls.builtins.formatting local formatting = null_ls.builtins.formatting
local diagnostics = null_ls.builtins.diagnostics local diagnostics = null_ls.builtins.diagnostics
local root_has_file = function(files)
return function(utils)
return utils.root_has_file(files)
end
end
local prettier_root_files = {
".prettierrc",
".prettierrc.json",
".prettierrc.js",
".prettierrc.mjs",
".prettierrc.cjs",
"prettier.config.cjs",
"prettier.config.js",
"prettier.config.mjs",
}
local formatting_with_prettier = formatting.prettier.with({
condition = root_has_file(prettier_root_files),
})
null_ls.setup({ null_ls.setup({
sources = { sources = {
formatting.stylua, formatting.stylua,
formatting.prettier.with(formatting_with_prettier),
}, },
on_attach = function(client, bufnr) on_attach = function(client, bufnr)
if client.supports_method("textDocument/formatting") then if client.supports_method("textDocument/formatting") then

View File

@@ -24,22 +24,28 @@ return {
else else
cmd = "typescript-language-server" cmd = "typescript-language-server"
end end
local prettier_config = vim.fn.glob(cwd .. "/*prettierrc*", false, true)
typescript_tools.setup({ typescript_tools.setup({
capabilities = capabilities, capabilities = capabilities,
on_attach = function(client, bufnr) on_attach = function(client, bufnr)
on_attach(client, bufnr) on_attach(client, bufnr)
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({ async = false })
end,
})
if cmd == "typescript-language-server" then if cmd == "typescript-language-server" then
print("Using global tsserver") print("Using global tsserver")
end end
if prettier_config == "" then
print("Using prettier to format")
return
else
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({ async = false })
end,
})
end
end, end,
settings = { settings = {
cmd = { cmd, "--stdio" }, cmd = { cmd, "--stdio" },