stylua --lsp does not respect `.stylua.toml`
Do you have an example setup with the issue you are seeing? What is not getting picked up from .stylua.toml?
I'm not sure if this is the same issue since they didn't provide a description but I'm experiencing something similar. I've found that running stylua . in a terminal respects my .stylua.toml while formatting with vim.lsp.buf.format() (textDocument/formatting) does not.
The .stylua.toml for my neovim config looks like this:
indent_type = "Spaces"
indent_width = 4
so it's pretty easy to tell when it's being respected or not (tab indentation by default but I prefer to use spaces).
The directory of the lsp client is the same as where I ran the manual command in the terminal so that shouldn't be the issue (note formatting is disabled for lua_ls so I don't think it's an issue of conflicting clients. I also tested without lua_ls and the issue persisted.):
vim.lsp: Active Clients ~
- lua_ls (id: 1)
- Version: 3.16.4
- Root directory: ~\AppData\Local\nvim
- Command: { "lua-language-server" }
- Settings: {
Lua = {
codeLens = {
enable = true
},
diagnostics = {
globals = { "vim" }
},
format = {
enable = false
},
hint = {
enable = true,
semicolon = "Disable"
}
}
}
- Attached buffers: 1
- stylua (id: 2)
- Version: 2.3.1
- Root directory: ~\AppData\Local\nvim
- Command: { "stylua", "--lsp" }
- Settings: {}
- Attached buffers: 1
nvim-lspconfig doesn't do anything crazy in the configuration either:
return {
cmd = { 'stylua', '--lsp' },
filetypes = { 'lua' },
root_markers = { '.stylua.toml', 'stylua.toml', '.editorconfig' },
}
I've been using this for formatting:
vim.api.nvim_create_autocmd("LspAttach", {
callback = function(args)
local client = assert(vim.lsp.get_client_by_id(args.data.client_id))
vim.lsp.inlay_hint.enable(true, { bufnr = args.buf })
if
not client:supports_method("textDocument/willSaveWaitUntil")
and client:supports_method("textDocument/formatting")
then
vim.api.nvim_create_autocmd("BufWritePre", {
buffer = args.buf,
callback = function()
vim.lsp.buf.format({ bufnr = args.buf, id = client.id })
end,
})
end
end,
})
vim.api.nvim_create_autocmd("LspDetach", {
callback = function(args)
local client = assert(vim.lsp.get_client_by_id(args.data.client_id))
if
not client:supports_method("textDocument/willSaveWaitUntil")
and client:supports_method("textDocument/formatting")
then
vim.api.nvim_clear_autocmds({
event = "BufWritePre",
buffer = args.buf,
})
end
end,
})
Lastly I've found that placing the .stylua.toml file in my root directory (C:\.stylua.toml since I'm on windows) works and spaces are used for indentation. I tried placing it in my user directory ~\.stylua.toml and it didn't work. I'm not sure why this is happening since --search-parent-directories isn't passed and it looks like it should be running in ~\AppData\Local\nvim.
Again I'm not sure if this is the same issue but it is similar.
I also encountered this problem with StyLua in Sublime Text, where the.stylua.toml file does not load correctly in--lsp mode