LuaLS / lua-language-server

A language server that offers Lua language support - programmed in Lua
https://luals.github.io
MIT License
3.34k stars 319 forks source link

Formatting is not working #2191

Open MarcelRobitaille opened 1 year ago

MarcelRobitaille commented 1 year ago

How are you using the lua-language-server?

NeoVim

Which OS are you using?

Linux

What is the issue affecting?

Formatting

Expected Behaviour

The document gets formatted.

Actual Behaviour

I get [LSP] Format request failed, no matching language servers. logged. Formatters for other lsps are working. :LspInfo shows lua_ls is running.

Reproduction steps

  1. I installed lua-language-server 3.6.22 via nix
  2. Here is my lspconfig:
    require'lspconfig'.lua_ls.setup{
    settings = {
    Lua = {
      runtime = {
        -- Tell the language server which version of Lua you're using (most likely LuaJIT in the case of Neovim)
        version = 'LuaJIT',
      },
      diagnostics = {
        -- Get the language server to recognize the `vim` global
        globals = {'vim'},
      },
      workspace = {
        -- Make the server aware of Neovim runtime files
        library = vim.api.nvim_get_runtime_file("", true),
      },
      -- Do not send telemetry data containing a randomized but unique identifier
      telemetry = {
        enable = false,
      },
      format = {
        enable = true,
        defaultConfig = {
          indent_style = "tab",
        },
      },
    },
    },
    }

Additional Notes

:LspInfo:

 Press q or <Esc> to close this window. Press <Tab> to view server doc.

 Language client log: /home/marcel/.local/state/nvim/lsp.log
 Detected filetype:   lua

 0 client(s) attached to this buffer: 

 Other clients that match the filetype: lua

 Config: lua_ls
    filetypes:         lua
    root directory:    /home/marcel/.dotfiles
    cmd:               /nix/var/nix/profiles/default/bin/lua-language-server
    cmd is executable: true
    autostart:         true
    custom handlers:   

 Configured servers list: phpactor, tsserver, lua_ls, ccls, elmls, rust_analyzer, pyright, vuels, texlab

Log File

[START][2023-02-22 23:00:05] LSP logging initiated
[INFO][2023-02-22 23:01:41] .../lua/vim/lsp.lua:1392    "exit_handler"  {}
[START][2023-02-22 23:01:42] LSP logging initiated
[START][2023-02-22 23:03:42] LSP logging initiated
[START][2023-02-22 23:04:00] LSP logging initiated
[START][2023-02-22 23:05:11] LSP logging initiated
[START][2023-02-22 23:06:26] LSP logging initiated
alewis001 commented 1 year ago

Hi, I think I'm seeing this same issue.

lspconfig (via default NVChad setup):

require("lspconfig").lua_ls.setup {
  on_attach = M.on_attach,
  capabilities = M.capabilities,

  settings = {
    Lua = {
      diagnostics = {
        globals = { "vim" },
      },
      workspace = {
        library = {
          [vim.fn.expand "$VIMRUNTIME/lua"] = true,
          [vim.fn.expand "$VIMRUNTIME/lua/vim/lsp"] = true,
          [vim.fn.stdpath "data" .. "/lazy/extensions/nvchad_types"] = true,
          [vim.fn.stdpath "data" .. "/lazy/lazy.nvim/lua/lazy"] = true,
        },
        maxPreload = 100000,
        preloadFileSize = 10000,
      },
    },
  },
}

According to the Mason output for the plugin, it's installed and the Lua.format.enable property is using the default of true.

LspInfo:

 Press q or <Esc> to close this window. Press <Tab> to view server doc.

 Language client log: /Users/alewis/.local/state/nvim/lsp.log
 Detected filetype:   lua

 1 client(s) attached to this buffer: 

 Client: lua_ls (id: 1, bufnr: [1])
    filetypes:       lua
    autostart:       true
    root directory:  /Users/alewis/Library/CloudStorage/Sync/shared/.config/nvim/lua/
    cmd:             /Users/alewis/.local/share/nvim/mason/bin/lua-language-server

 Configured servers list: lua_ls, html, cssls, tsserver, vuels
CppCXY commented 1 year ago

I'm sorry, but I don't know. Neither I nor sumneko are Neovim users. However, I can provide some suggestions. The format of lua_ls is dynamically registered and it requires client support for this capability. On the other hand, check if any other plugins are blocking the formatting ability of lua_ls. You can try setting up a fresh Neovim instance to see if there are any issues.

alewis001 commented 1 year ago

No problem. I'll try to carve out some time to strip my config back to see whether there is another plugin/lsp that might be causing the issue and I'll report back here.

costa-simulatedreality commented 10 months ago

i'm also facing the same problem on my macos (intel) monterey, using neovim (I made sure to install the lua-language-server using Mason, and configured the lua_ls LSP on_attach), but the formatter does not seem to be responding

ripjackie commented 10 months ago

I've gotten it to work on NvChad. Setup: Neovim Nightly, NvChad 2.0

NvChad specifically disables LSP Server formatting on it's default implementation of on_attach:

file: plugins/configs/lspconfig.lua

M.on_attach = function(client, bufnr)
  client.server_capabilities.documentFormattingProvider = false
  client.server_capabilities.documentRangeFormattingProvider = false

  utils.load_mappings("lspconfig", { buffer = bufnr })

  if client.server_capabilities.signatureHelpProvider then
    require("nvchad.signature").setup(client)
  end

  if not utils.load_config().ui.lsp_semantic_tokens and client.supports_method "textDocument/semanticTokens" then
    client.server_capabilities.semanticTokensProvider = nil
  end
end

These Two Lines:

client.server_capabilities.documentFormattingProvider = false
client.server_capabilities.documentRangeFormattingProvider = false

When you modify them after the fact, enables lua_ls formatting:

...
lspconfig.lua_ls.setup {
    on_attach = function(client, bufnr)
        config.on_attach(client, bufnr)
        client.server_capabilities.documentFormattingProvider = true
        client.server_capabilities.documentRangeFormattingProvider = true
    end,
    capabilities = config.capabilities,
    filetypes = { "lua" },
    settings = {
        Lua = {
            format = {
                defaultConfig = {
                    indent_style = "space",
                    indent_size = "4",
                },
            },
            ...