Hi everyone and thanks in advance for your input. I use lua for the formatter plugin configuration, the following code is my current configuration.
require('formatter').setup({
filetype = {
javascript = {
-- prettier
function()
return {
exe = "prettier",
args = {"--stdin-filepath", vim.fn.fnameescape(vim.api.nvim_buf_get_name(0)), '--single-quote'},
stdin = true
}
end
},
typescriptreact = {
-- prettier
function()
return {
exe = "prettier",
args = {"--stdin-filepath", vim.fn.fnameescape(vim.api.nvim_buf_get_name(0)), '--single-quote'},
stdin = true
}
end
},
javascriptreact = {
-- prettier
function()
return {
exe = "prettier",
args = {"--stdin-filepath", vim.fn.fnameescape(vim.api.nvim_buf_get_name(0)), '--single-quote'},
stdin = true
}
end
},
html = {
-- prettier
function()
return {
exe = "prettier",
args = {"--stdin-filepath", vim.fn.fnameescape(vim.api.nvim_buf_get_name(0)), '--single-quote'},
stdin = true
}
end
},
css = {
-- prettier
function()
return {
exe = "prettier",
args = {"--stdin-filepath", vim.fn.fnameescape(vim.api.nvim_buf_get_name(0)), '--single-quote'},
stdin = true
}
end
},
json = {
-- prettier
function()
return {
exe = "prettier",
args = {"--stdin-filepath", vim.fn.fnameescape(vim.api.nvim_buf_get_name(0)), '--single-quote'},
stdin = true
}
end
},
php = {
-- prettier
function()
return {
exe = "prettier",
args = {"--stdin-filepath", vim.fn.fnameescape(vim.api.nvim_buf_get_name(0)), '--single-quote'},
stdin = true
}
end
},
vue = {
-- prettier
function()
return {
exe = "prettier",
args = {"--stdin-filepath", vim.fn.fnameescape(vim.api.nvim_buf_get_name(0)), '--single-quote'},
stdin = true
}
end
},
svelte = {
-- prettier
function()
return {
exe = "prettier",
args = {"--stdin-filepath", vim.fn.fnameescape(vim.api.nvim_buf_get_name(0)), '--single-quote'},
stdin = true
}
end
},
}
})
As you will notice the function inside the table is repeated many times, to refactor this code try the following:
function config()
return {
exe = "prettier",
args = {"--stdin-filepath", vim.fn.fnameescape(vim.api.nvim_buf_get_name(0)), '--single-quote'},
stdin = true
}
end
require('formatter').setup({
filetype = {
javascript = {
-- prettier
config()
},
}
})
However, that code gives me an error, I'm new to lua. I appreciate your help in refactoring this code.