cairo
cairo copied to clipboard
feat: allow formatter to format stdin
Feature Request
Describe the Feature Request
Currently the formatter only takes file paths as input. Would be nice if it can take stdin as input when the argument is -
. This is useful in editors, which usually interface with formatters via stdin/stdout.
Describe Preferred Solution
Read from stdin when the supplied argument is -
.
Describe Alternatives
N/A
Related Code
N/A
Additional Context
N/A
If the feature request is approved, would you be willing to submit a PR? (Help can be provided if you need assistance submitting a PR)
- [x] Yes
- [ ] No
I have a branch that works quite well. It's interfacing with Helix perfectly (and REALLY fast compared to the old cairo-format
from cairo-lang
).
It needs some cleanup but I can polish it up and submit the PR once this issue is approved.
Sounds legit, would love to get a PR. And also tips on how to integrate with Helix :)
Sure will submit the PR soon. For Helix integration, I simply added this to my languages.toml
(located at ~/.config/helix/languages.toml
):
[[language]]
name = "cairo"
formatter = { command = "formatter_cli" , args = ["-"] }
It really is :zap: blazing :zap: fast :zap: lol.
This works because Helix simply sends the entire buffer to the formatter command via stdin.
Can it integrate with langserver?
Oh Helix has native support for LSP and it supports formatting through the LSP as long as the server supports it.
Though somehow I haven't successfully used the Cairo LSP for really anything at all, but I'll investigate on that front a bit and submit a separate issue.
I have a branch ready now, but I'm wondering how we should deal with the formatter CI issue. Should I just add an .cairofmtignore
to crates/parser/test_data/cairo_files/
to just exclude everything there?
Btw the formatter code was pretty tightly coupled with the file system, so I had to do some refactoring here and there or else I would have a lot of code duplications. I ended up almost rewriting the whole cli.rs
file... I hope you don't mind.
Update on the LSP issue: I'm trying the language server again with the current main
head and it seems to work for me this time. Hover and going to definition work just fine. Not sure what I got wrong last time so I guess we're good.
However, formatting through the LSP doesn't seem to work. From Helix logs I can clearly see the response for the textDocument/formatting
request, but the editor doesn't seem to do anything about it. Helix works fine with formatting through LSP for other languages like rust-analyzer
, so I doubt there's something wrong with the Cairo LSP implementation. Will dig deeper and submit the issue.
Note that making formatting work through LSP doesn't supersede this issue.
For the follow-up on the LSP formatting issue: #1141
Do you get semantic higlighting?
@spapinistarkware Helix seems to rely solely on treesitter for syntax highlighting so I can't tell. I haven't tried using the Cairo LSP with neovim tho.