mhartington/formatter.nvim

Modules Not Found

Opened this issue · 2 comments

Formatter: builtin

Configuration(s) (post all the variants you tried):

return {
  "mhartington/formatter.nvim",
  opts = {
    filetype = {
      lua = {
        require("formatter.filetypes.lua").stylua,
      },

      ["*"] = {
        require("formatter.filetypes.any").remove_trailing_whitespace
      }
    }
  },
}

Expected behavior
Install and load properly

Actual behaviour
Error:

Error detected while processing /Users/pandoks/.config/nvim/init.lua:
Failed to load `plugins.formatter`
/Users/pandoks/.config/nvim/lua/plugins/formatter.lua:6: module 'formatter.filetypes.lua' not found:
^Ino field package.preload['formatter.filetypes.lua']
cache_loader: module formatter.filetypes.lua not found
cache_loader_lib: module formatter.filetypes.lua not found
^Ino file './formatter/filetypes/lua.lua'
^Ino file '/opt/homebrew/share/luajit-2.1.0-beta3/formatter/filetypes/lua.lua'
^Ino file '/usr/local/share/lua/5.1/formatter/filetypes/lua.lua'
^Ino file '/usr/local/share/lua/5.1/formatter/filetypes/lua/init.lua'
^Ino file '/opt/homebrew/share/lua/5.1/formatter/filetypes/lua.lua'
^Ino file '/opt/homebrew/share/lua/5.1/formatter/filetypes/lua/init.lua'
^Ino file './formatter/filetypes/lua.so'
^Ino file '/usr/local/lib/lua/5.1/formatter/filetypes/lua.so'
^Ino file '/opt/homebrew/lib/lua/5.1/formatter/filetypes/lua.so'
^Ino file '/usr/local/lib/lua/5.1/loadall.so'
^Ino file './formatter.so'
^Ino file '/usr/local/lib/lua/5.1/formatter.so'
^Ino file '/opt/homebrew/lib/lua/5.1/formatter.so'
^Ino file '/usr/local/lib/lua/5.1/loadall.so'
# stacktrace:
  - lua/plugins/formatter.lua:6
  - lua/config/lazy.lua:14
  - lua/config/init.lua:3
  - init.lua:1

This also happens to when I remove the lua in the file types. There is no 'formatter.filetypes.any' module. I am using lazy package manager.

Fixed.

I needed to set it up using config. Don't know why it doesn't work though since lazy automatically calls require("formatter").setup(opts) if you only have the opts param. I even tried passing opts in through config like this: config = function(_, opts).

This is what worked for me:

return {
  "mhartington/formatter.nvim",
  keys = {
    {"<leader>F", "<cmd>Format<cr>", mode = "n", desc = "Format the file"},
  },
  config = function()
    local opts = {
    filetype = {
      lua = {
	require("formatter.filetypes.lua").stylua,
      },
      ["*"] = {				 
        require("formatter.filetypes.any").remove_trailing_whitespace,
        },
      },
    }
    require("formatter").setup(opts)
  end,
}

I've found a workaround if you still want to use opts. You need to define it as an anonymous function. However, in my case, I've required the formatter.filetypes.lua module before using the stylua function in the table. The declared function should return the table.

return {
  'mhartington/formatter.nvim',
  opts = function()
    local stylua = require('formatter.filetypes.lua').stylua
    return {
      filetypes = {
        lua = { stylua() },
      },
    }
  end
}