Beyond try_node_modules -- an option to *only* format when the formatter is installed
Opened this issue · 2 comments
snoblenet commented
I'd like to only automatically apply Prettier formatting when it is installed in the current repository's node modules. I've found try_node_modules
, but I'm looking for something like only_node_modules
. How can I solve this problem using the current functionality of formatter.nvim? If it can't be solved using the current functionality of formatter.nvim, can this issue be considered a feature request? Thank you.
snoblenet commented
For now, I'm experimenting with this:
local prettierConfig = function(ext)
return function()
local node_modules = vim.inspect(vim.fs.find('node_modules'))
local prettier = vim.fs.find(node_modules .. '/.bin/prettier')
if not prettier then
return null
end
return {
exe = 'prettier',
args = {
'--stdin-filepath',
vim.fn.shellescape(vim.api.nvim_buf_get_name(0)),
},
stdin = true,
try_node_modules = true,
}
end
end
snoblenet commented
currently
local Path = require('plenary.path')
return {
'mhartington/formatter.nvim',
dependencies = { 'nvim-lua/plenary.nvim' },
config = function()
local prettierConfig = function(ext)
return function()
local prettier = Path:new('.')
:find_upwards('node_modules')
:find_upwards('.bin')
:find_upwards('prettier')
:absolute()
if not prettier then
return null
end
return {
exe = prettier,
args = {
'--stdin-filepath',
vim.fn.shellescape(vim.api.nvim_buf_get_name(0)),
},
stdin = true,
}
end
end
-- etc
require('formatter').setup({
filetype = {
typescript = { prettierConfig('ts') },
-- etc
},
logging = false,
})
vim.api.nvim_create_autocmd({ 'BufWritePost' }, {
command = 'silent! FormatWrite',
pattern = {
'/Users/steven/code/*/*',
-- etc
},
})
end,
}