cairo icon indicating copy to clipboard operation
cairo copied to clipboard

feat: allow formatter to format stdin

Open xJonathanLEI opened this issue 2 years ago • 1 comments

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

xJonathanLEI avatar Nov 25 '22 10:11 xJonathanLEI

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.

xJonathanLEI avatar Nov 26 '22 07:11 xJonathanLEI

Sounds legit, would love to get a PR. And also tips on how to integrate with Helix :)

spapinistarkware avatar Nov 28 '22 14:11 spapinistarkware

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.

xJonathanLEI avatar Nov 28 '22 14:11 xJonathanLEI

Can it integrate with langserver?

spapinistarkware avatar Nov 28 '22 14:11 spapinistarkware

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.

xJonathanLEI avatar Nov 28 '22 14:11 xJonathanLEI

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?

xJonathanLEI avatar Nov 28 '22 18:11 xJonathanLEI

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.

xJonathanLEI avatar Nov 28 '22 18:11 xJonathanLEI

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.

xJonathanLEI avatar Nov 29 '22 08:11 xJonathanLEI

For the follow-up on the LSP formatting issue: #1141

xJonathanLEI avatar Nov 29 '22 09:11 xJonathanLEI

Do you get semantic higlighting?

spapinistarkware avatar Nov 29 '22 11:11 spapinistarkware

@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.

xJonathanLEI avatar Nov 29 '22 14:11 xJonathanLEI