lukas-reineke / lsp-format.nvim

A wrapper around Neovims native LSP formatting.
559 stars 27 forks source link

[Question] Unable to format python with black formatter #54

Closed vihu closed 1 year ago

vihu commented 1 year ago

Summary

I'm unable to format python files with black formatter using efm language server.

Configuration

lsp-config.lua

local lsp_format_status_ok, lsp_format = pcall(require, "lsp-format")
if not lsp_format_status_ok then
    return
end

lsp_format.setup()
local capabilities = cmp_nvim_lsp.update_capabilities(vim.lsp.protocol.make_client_capabilities())

lspconfig.efm.setup {
    on_attach = on_attach,
    init_options = { documentFormatting = true },
    capabilities = capabilities,
    filetypes = { "python" },
    settings = {
        languages = {
            python = {
                formatCommand = "black --quiet -",
                formatStdin = true,
            }
        }
    }
}
$ which black
/home/rahul/.pyenv/shims/black
$ which efm-langserver
/home/rahul/go/bin/efm-langserver

language servers connected to python:

 Client: pyright (id: 1, pid: 26009, bufnr: [1])
    filetypes:       python
    autostart:       true
    root directory:  Running in single file mode.
    cmd:             pyright-langserver --stdio

 Client: efm (id: 2, pid: 26010, bufnr: [1])
    filetypes:       python
    autostart:       true
    root directory:  Running in single file mode.
    cmd:             efm-langserver

My suspicion is that somehow pyright takes priority and since it does not have formatting capabilities that could be a problem? Any advice would be helpful, thanks!

lukas-reineke commented 1 year ago

My suspicion is that somehow pyright takes priority and since it does not have formatting capabilities that could be a problem?

No, lsp-format iterates over all clients.

The config you posted looks fine. Try to set logLevel = 5 in the efm settings and see if you get any logs that are helpful. You get the log path with :lua print(vim.lsp.get_log_path())

vihu commented 1 year ago
[ERROR][2022-09-05 19:26:20] .../vim/lsp/rpc.lua:420    "rpc"   "efm-langserver"        "stderr"        "2022/09/05 19:26:20 jsonrpc2 handler: sending response 2: jsonrpc2: connection is closed\n2022/09/05 19:26:20 efm-langserver: connections closed\n"
[START][2022-09-05 19:26:22] LSP logging initiated
[ERROR][2022-09-05 19:26:22] .../vim/lsp/rpc.lua:420    "rpc"   "efm-langserver"        "stderr"        "2022/09/05 19:26:22 efm-langserver: reading on stdin, writing on stdout\n"
[ERROR][2022-09-05 19:26:22] .../vim/lsp/rpc.lua:420    "rpc"   "efm-langserver"        "stderr"        '2022/09/05 19:26:22 jsonrpc2 handler: notification "workspace/didChangeConfiguration" handling error: json: cannot unmarshal object into Go struct field Config.settings.languages of type []langserver.Language\n'
[WARN][2022-09-05 19:26:22] ...-format/init.lua:124     '"textDocument/formatting" is not supported for pyright, not attaching lsp-format'
[WARN][2022-09-05 19:26:22] ...lsp/handlers.lua:110     "The language server pyright triggers a registerCapability handler despite dynamicRegistration set to false. Report upstream, this warning is harmless"
[WARN][2022-09-05 19:26:22] ...lsp/handlers.lua:457     "stubPath typings is not a valid directory."

Those jsonrpc2 errors seem to be the culprit here? I wonder if I need to install efm-langserver differently? I basically did this: go install github.com/mattn/efm-langserver@latest and added the binary to my $PATH variable.

lukas-reineke commented 1 year ago

Ah sorry, I missed it as well. You are missing one level of braces. Because you can have more than one formatter per filetype.

python = {
    {
        formatCommand = "black --quiet -",
        formatStdin = true,
    },
}
vihu commented 1 year ago

Ah sorry, I missed it as well. You are missing one level of braces. Because you can have more than one formatter per filetype.

python = {
    {
        formatCommand = "black --quiet -",
        formatStdin = true,
    },
}

Oh yes, that worked perfectly! Much thanks and my bad for missing those extra curlies.