elentok / format-on-save.nvim

Automatically formats files when saving using either LSP or shell utilities like prettierd or shfmt
MIT License
159 stars 5 forks source link

Getting error when installing #16

Open jordan-bravo opened 1 year ago

jordan-bravo commented 1 year ago

I followed the README to install format-on-save.nvim and Neovim is throwing an error when it starts up:

Error detected while processing /home/jordan/.config/nvim/init.lua:                                                 
Failed to load `user.plugins.format-on-save`                                                                        
vim/loader.lua:0: .../jordan/.config/nvim/lua/user/plugins/format-on-save.lua:104: '}' expected (to close '{' at lin
e 9) near 'run_with_sh'                                                                                             
# stacktrace:                                                                                                       
  - vim/loader.lua:0                                                                                                
  - lua/user/lazy.lua:23                                                                                            
  - init.lua:5      

Here is the contents of the file ~/.config/nvim/lua/user/plugins/format-on-save.lua, which I copied from the README. Am I doing something incorrectly? My plugin manager is lazy.nvim.

-- ~/.config/nvim/lua/user/plugins/format-on-save.lua

return {
  "elentok/format-on-save.nvim",
  config = function()
    local format_on_save = require("format-on-save")
    local formatters = require("format-on-save.formatters")

    format_on_save.setup({
      exclude_path_patterns = {
        "/node_modules/",
        ".local/share/nvim/lazy",
      },
      formatter_by_ft = {
        css = formatters.lsp,
        html = formatters.lsp,
        java = formatters.lsp,
        javascript = formatters.lsp,
        json = formatters.lsp,
        lua = formatters.lsp,
        markdown = formatters.prettierd,
        openscad = formatters.lsp,
        python = formatters.black,
        rust = formatters.lsp,
        scad = formatters.lsp,
        scss = formatters.lsp,
        sh = formatters.shfmt,
        terraform = formatters.lsp,
        typescript = formatters.prettierd,
        typescriptreact = formatters.prettierd,
        yaml = formatters.lsp,

        -- Add your own shell formatters:
        myfiletype = formatters.shell({ cmd = { "myformatter", "%" } }),

        -- Add lazy formatter that will only run when formatting:
        my_custom_formatter = function()
          if vim.api.nvim_buf_get_name(0):match("/README.md$") then
            return formatters.prettierd
          else
            return formatters.lsp()
          end
        end,

        -- Add custom formatter
        filetype1 = formatters.remove_trailing_whitespace,
        filetype2 = formatters.custom({ format = function(lines)
          return vim.tbl_map(function(line)
            return line:gsub("true", "false")
          end, lines)
        end}),

        -- Concatenate formatters
        python = {
          formatters.remove_trailing_whitespace,
          formatters.shell({ cmd = "tidy-imports" }),
          formatters.black,
          formatters.ruff,
        },

        -- Use a tempfile instead of stdin
        go = {
          formatters.shell({
            cmd = { "goimports-reviser", "-rm-unused", "-set-alias", "-format", "%" },
            tempfile = function()
              return vim.fn.expand("%") .. '.formatter-temp'
            end
          }),
          formatters.shell({ cmd = { "gofmt" } }),
        },

        -- Add conditional formatter that only runs if a certain file exists
        -- in one of the parent directories.
        javascript = {
          formatters.if_file_exists({
            pattern = ".eslintrc.*",
            formatter = formatters.eslint_d_fix
          }),
          formatters.if_file_exists({
            pattern = { ".prettierrc", ".prettierrc.*", "prettier.config.*" },
            formatter = formatters.prettierd,
          }),
          -- By default it stops at the git repo root (or "/" if git repo not found)
          -- but it can be customized with the `stop_path` option:
          formatters.if_file_exists({
            pattern = ".prettierrc",
            formatter = formatters.prettierd,
            stop_path = function()
              return "/my/custom/stop/path"
            end
          }),
        },
      },

      -- Optional: fallback formatter to use when no formatters match the current filetype
      fallback_formatter = {
        formatters.remove_trailing_whitespace,
        formatters.remove_trailing_newlines,
        formatters.prettierd,
      }

      -- By default, all shell commands are prefixed with "sh -c" (see PR #3)
      -- To prevent that set `run_with_sh` to `false`.
      run_with_sh = false,
    })
  end,
}
faergeek commented 1 year ago

I guess the problem is in a missing comma after fallback_formatter = { ... }. But you don't need to copy everything in here to your config. It just demonstrates various setups, which are possible.

elentok commented 1 year ago

Thanks @faergeek

Hi @jordan-bravo, yeah the config in the doc was just an example of what can be done, perhaps I should clarify that (not sure how). are you still having an issue or has this been solved?

jordan-bravo commented 1 year ago

I couldn't get it to work so I ended up using lsp-format.nvim as an alternative.

Could you please give an example of a minimum working config for format-on-save.nvim? That would be very helpful, thank you in advance.