format-on-save.nvim icon indicating copy to clipboard operation
format-on-save.nvim copied to clipboard

Getting error when installing

Open jordan-bravo opened this issue 11 months ago • 3 comments

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,
}

jordan-bravo avatar Sep 08 '23 15:09 jordan-bravo