StyLua icon indicating copy to clipboard operation
StyLua copied to clipboard

FR: Make available as LSP

Open chrisgrieser opened this issue 11 months ago • 6 comments

Many formatters/linters such as biome or ruff are also available as LSP, even though they aren't "real" language servers.

Offering an LSP, even if it is just for the formatting command, has its advantages, as it allows for easier integration into editors. With editors like nvim, for example, integrating stylua requires an extra plugin, while an LSP could be used out of the box.

chrisgrieser avatar Dec 09 '24 09:12 chrisgrieser

Interesting idea. I can see the value in this, although building out a fully fledged language server that conforms to the protocol might be a bit overkill for the tool itself. Maybe it should be a wrapper binary tool, not sure yet.

JohnnyMorganz avatar Dec 14 '24 17:12 JohnnyMorganz

Would using efm-langserver be an idea? Maybe just provide an example config on how to set stylua up with it.

Implementing a full LSP wrapper for stylua seems a waste of energy when there already are tools that are explicitly made to wrap things like formatters to be available via LSP.

(Note I haven't used it, as I'm happy with how conform.nvim uses stylua, but the readme looks like efm should be easy enough too.)

Another consideration would be to have a common crate to provide an LSP wrapper, but I'm not sure how much that would actually be used.

titaniumtraveler avatar Jan 06 '25 06:01 titaniumtraveler

As it is already pointed out biome is also written in rust and has a language client available as a subcommand of its CLI. The LS implementation can be checked out here for the inspiration or perhaps it is possible to reuse some of it.

Personally I think if the language client can be a part of the stylua binary itself - that would provide the best user experience and performance end to end.

Another options is to have a typescript/javascript language server as part of the existing npm package. It can shell out to the CLI similar to the VS Code plugin, perhaps some of the code can be reused. Though, the user will have to make sure that stylua is installed through the npm specifically.

antonk52 avatar Jan 08 '25 22:01 antonk52

I've just published to npm a prototype that works good enough for me. It is quite similar code wise to the current vscode plugin. Hopefully this helps.

https://github.com/antonk52/lua-3p-language-servers

antonk52 avatar Jan 11 '25 18:01 antonk52

@antonk52 awesome! I just gave it a try, and so far it seems to be working as expected.

~~You should open a PR to have it added to nvim-lspconfig.~~ Just found you already did: https://github.com/neovim/nvim-lspconfig/pull/3553

I also opened a PR to have the package added to mason.nvim: https://github.com/mason-org/mason-registry/pull/8532

chrisgrieser avatar Jan 11 '25 19:01 chrisgrieser

Any thoughts on integrating lua-3p-language-servers into stylua? With tools like biome or ruff, the LSP can simply be called by passing an extra argument like cli --lsp. Shipping them together streamlines things, since right now, it's still nonetheless required to installed two things to make one thing work.

chrisgrieser avatar Jan 14 '25 12:01 chrisgrieser

PS: https://github.com/LuaLS/lua-language-server support format, check (type annotation), and more features.

Freed-Wu avatar Jul 08 '25 12:07 Freed-Wu