formatter.lua 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. local status, formatter = pcall(require, "formatter")
  2. if not status then
  3. vim.notify("没有找到 formatter")
  4. return
  5. end
  6. formatter.setup({
  7. filetype = {
  8. lua = {
  9. function()
  10. return {
  11. exe = "stylua",
  12. args = {
  13. -- "--config-path "
  14. -- .. os.getenv("XDG_CONFIG_HOME")
  15. -- .. "/stylua/stylua.toml",
  16. "-",
  17. },
  18. stdin = true,
  19. }
  20. end,
  21. },
  22. rust = {
  23. -- Rustfmt
  24. function()
  25. return {
  26. exe = "rustfmt",
  27. args = { "--emit=stdout" },
  28. stdin = true,
  29. }
  30. end,
  31. },
  32. javascript = {
  33. -- prettier
  34. function()
  35. return {
  36. exe = "prettier",
  37. args = { "--stdin-filepath", vim.fn.fnameescape(vim.api.nvim_buf_get_name(0)), "--single-quote" },
  38. stdin = true,
  39. }
  40. end,
  41. },
  42. },
  43. })
  44. -- format on save
  45. vim.api.nvim_exec(
  46. [[
  47. augroup FormatAutogroup
  48. autocmd!
  49. autocmd BufWritePost *.js,*.rs,*.lua FormatWrite
  50. augroup END
  51. ]],
  52. true
  53. )