conditionally enable/disable prettier based on .pretierrc existance in root #629
-
Vim and Lua are not my forte but I've been banging my head with this for hours. Using condition on formatting service works ok if I start and finish work on a single project in Nvim. However, switching sessions (and projects) require some more code. First idea that came to my mind was to register Prettier formatting in So I did:
After many tries, I realized Any ideas how to implement something like this? Point is, some projects use prettier and some dont. Also, I manage projects through Nvim, with sessions or nvim-tree... I don't want to open/exit nvim every time I switch projects. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
This is mentioned in the documentation, but local prettier = null_ls.builtins.formatting.prettier.with({
runtime_condition = function(params)
local utils = require("null-ls.utils")
-- use whatever root markers you want to check - these are the defaults
local root = utils.root_pattern(".null-ls-root", "Makefile", ".git")(params.bufname)
return root and utils.path.exists(utils.path.join(root, ".prettierrc"))
end,
}) |
Beta Was this translation helpful? Give feedback.
-
If anybody needs it, or just to tell me if I did something stupid (this is my first nvim/load scripting), here is how I did this:
My complete NulLs setup is:
and for icons in Lualine config:
|
Beta Was this translation helpful? Give feedback.
This is mentioned in the documentation, but
condition
will only run once. Your best bet is to useruntime_condition
, which does run on each invocation and so can be used to check the current buffer's root directory. Note that in your case,params.root
will still reflect the original root, so you'd have to do something like this: