prettier.nvim icon indicating copy to clipboard operation
prettier.nvim copied to clipboard

Error executing lua [string ":lua"]:1: attempt to call field 'formatting' (a nil value)

Open Clish254 opened this issue 1 year ago • 3 comments

I'm getting this error everytime I save a file after setting up the plugin.

Error detected while processing BufWritePost Autocommands for "<buffer=3>":
E5108: Error executing lua [string ":lua"]:1: attempt to call field 'formatting' (a nil value)
stack traceback:
        [string ":lua"]:1: in main chunk

My config

local null_ls = require("null-ls")

null_ls.setup({
  on_attach = function(client, bufnr)
    if client.server_capabilities.documentFormattingProvider then
      vim.cmd("nnoremap <silent><buffer> <Leader>f :lua vim.lsp.buf.formatting()<CR>")

      -- format on save
      vim.cmd("autocmd BufWritePost <buffer> lua vim.lsp.buf.formatting()")
    end

    if client.server_capabilities.documentRangeFormattingProvider then
      vim.cmd("xnoremap <silent><buffer> <Leader>f :lua vim.lsp.buf.range_formatting({})<CR>")
    end
  end,
})

local prettier = require("prettier")

prettier.setup({
  bin = 'prettier', -- or `'prettierd'` (v0.22+)
  filetypes = {
    "css",
    "graphql",
    "html",
    "javascript",
    "javascriptreact",
    "json",
    "less",
    "markdown",
    "scss",
    "typescript",
    "typescriptreact",
    "yaml",
  },
})

Clish254 avatar Oct 18 '22 07:10 Clish254

I found a solution in the null-ls format on save docs here Changed my format on save config from this:

local null_ls = require("null-ls")

null_ls.setup({
  on_attach = function(client, bufnr)
    if client.server_capabilities.documentFormattingProvider then
      vim.cmd("nnoremap <silent><buffer> <Leader>f :lua vim.lsp.buf.formatting()<CR>")

      -- format on save
      vim.cmd("autocmd BufWritePost <buffer> lua vim.lsp.buf.formatting()")
    end

    if client.server_capabilities.documentRangeFormattingProvider then
      vim.cmd("xnoremap <silent><buffer> <Leader>f :lua vim.lsp.buf.range_formatting({})<CR>")
    end
  end,
})

To this:

local augroup = vim.api.nvim_create_augroup("LspFormatting", {})
require("null-ls").setup({
    -- you can reuse a shared lspconfig on_attach callback here
    on_attach = function(client, bufnr)
        if client.supports_method("textDocument/formatting") then
            vim.api.nvim_clear_autocmds({ group = augroup, buffer = bufnr })
            vim.api.nvim_create_autocmd("BufWritePre", {
                group = augroup,
                buffer = bufnr,
                callback = function()
                    -- on 0.8, you should use vim.lsp.buf.format({ bufnr = bufnr }) instead
                    vim.lsp.buf.formatting_sync()
                end,
            })
        end
    end,
})

Clish254 avatar Oct 18 '22 08:10 Clish254

Yeah, I think this is because the latest Neovim removed some deprecated functions. Need to update the README.md with latest instructions.

MunifTanjim avatar Oct 18 '22 08:10 MunifTanjim

I can create a pr with the update

Clish254 avatar Oct 18 '22 08:10 Clish254