Skip to content

Mismatch behavior between stylua cli and stylua --lsp with .editorconfig #1122

@pbnj

Description

@pbnj

I am experiencing some mismatching behavior when executing stylua cli versus when stylua is running as an LSP (both with default configurations -- no .stylua.toml and stock lspconfig).

For example:

Given this excerpt/snippet:

vim.api.nvim_create_autocmd("LspAttach", {
  group = vim.api.nvim_create_augroup("lsp-attach", { clear = true }),
  callback = function(event)
    local client = assert(vim.lsp.get_client_by_id(event.data.client_id))
    if client:supports_method(vim.lsp.protocol.Methods.textDocument_completion)
    then
      vim.lsp.completion.enable(
        true,
        client.id,
        event.buf,
        { autotrigger = false }
      ) -- :help lsp-completion
    end
  end,
})

When formatting this through the LSP, everything is fine (no changes).

However, when formatting via stylua CLI, it gets changed to:

vim.api.nvim_create_autocmd("LspAttach", {
  group = vim.api.nvim_create_augroup("lsp-attach", { clear = true }),
  callback = function(event)
    local client = assert(vim.lsp.get_client_by_id(event.data.client_id))
-   if client:supports_method(vim.lsp.protocol.Methods.textDocument_completion)
+   if
+     client:supports_method(vim.lsp.protocol.Methods.textDocument_completion)
    then
      vim.lsp.completion.enable(
        true,
        client.id,
        event.buf,
        { autotrigger = false }
      ) -- :help lsp-completion
    end
  end,
})

Note the output of stylua -c test.lua:

$ stylua --check test.lua 
Diff in tmp/test.lua:
2   2    |   group = vim.api.nvim_create_augroup("lsp-attach", { clear = true }),
3   3    |   callback = function(event)
4   4    |     local client = assert(vim.lsp.get_client_by_id(event.data.client_id))
5        |-    if client:supports_method(vim.lsp.protocol.Methods.textDocument_completion)
    5    |+    if
    6    |+      client:supports_method(vim.lsp.protocol.Methods.textDocument_completion)
6   7    |     then
7   8    |       vim.lsp.completion.enable(
8   9    |         true,

I believe this wrapping happens due to my .editorconfig:

root = true

[*]
charset = utf-8
end_of_line = lf
indent_size = 2
indent_style = space
insert_final_newline = true
max_line_length = 80
trim_trailing_whitespace = true

Assuming that is true, it is unclear why formatting through LSP doesn't respect my .editorconfig.

Additionally, when formatting stylua CLI's output through the LSP, it changes again:

vim.api.nvim_create_autocmd("LspAttach", {
  group = vim.api.nvim_create_augroup("lsp-attach", { clear = true }),
  callback = function(event)
    local client = assert(vim.lsp.get_client_by_id(event.data.client_id))
    if
-     client:supports_method(vim.lsp.protocol.Methods.textDocument_completion)
+       client:supports_method(vim.lsp.protocol.Methods.textDocument_completion)
    then
      vim.lsp.completion.enable(
        true,
        client.id,
        event.buf,
        { autotrigger = false }
      ) -- :help lsp-completion
    end
  end,
})

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions