Declare multiple time the same formatter but with different settings #238
-
I'm using nullLs.config({
sources = {
require("null-ls.helpers").conditional(function(utils)
local usesEslint = vim.fn.glob(".eslintrc*") ~= ""
or (
utils.root_has_file("package.json")
and vim.fn.filereadable("package.json")
and vim.fn.json_decode(vim.fn.readfile("package.json"))["eslintConfig"]
)
return usesEslint and nullLs.builtins.formatting.eslint_d
or nullLs.builtins.formatting.prettierd.with({
filetypes = { "javascript" },
})
end),
nullLs.builtins.formatting.prettierd.with({
filetypes = { "json", "markdown", "yaml" },
}),
},
}) The problem is that the Is there a way to perform such a task that I'm not aware of? Thanks for your help. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
It's dirty, but it's possible to do this by copying the built-in, changing the name, and setting local null_ls = require("null-ls")
local prettier = null_ls.builtins.formatting.prettierd
prettier._opts.filetypes = { "json", "markdown", "yaml" }
local prettier_javascript = vim.deepcopy(prettier)
prettier_javascript.name = "prettier_javascript"
prettier_javascript._opts.filetypes = { "javascript" }
setmetatable(prettier_javascript, getmetatable(prettier))
local sources = {
require("null-ls.helpers").conditional(function(utils)
local usesEslint = vim.fn.glob(".eslintrc*") ~= ""
or (
utils.root_has_file("package.json")
and vim.fn.filereadable("package.json")
and vim.fn.json_decode(vim.fn.readfile("package.json"))["eslintConfig"]
)
return usesEslint and null_ls.builtins.formatting.eslint_d or prettier_javascript
end),
prettier,
} Definitely open to suggestions / PRs to improve this, since it's not pleasant to look at, but it should work. Ideally each built-in would be a unique copy from the start so you could do what you wanted to do originally. |
Beta Was this translation helpful? Give feedback.
-
Also there was something that didn't work for me in the code you gave above. I had to replace |
Beta Was this translation helpful? Give feedback.
It's dirty, but it's possible to do this by copying the built-in, changing the name, and setting
_opts
directly (sincewith
references the original object):