-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Change default formatter for any language #2942
Conversation
Another bug I found using The expected usage of |
You probably need to close stdin when you're done. |
Yeah that was it, thanks |
Thanks for the style guides btw. |
I found another bug. Basically, when using Stdio as the formatter type, when saving, the document is formatted and saved, but the editor shows that the file is still unsaved |
I don't know anything about async, ideas on how to make this work?
line 458 of document.rs. |
Now, the actual problem is that different async blocks generate different types that implement
|
Thanks for all the help and the explanation. I guess the only problem now is the fact that write_all only writes to viewed buffers instead of all buffers, because we need the view for reloading the document if we are using the file formatter. Maybe we could remove the file formatter type entirely? It depends on whether there are other known formatters that only work like that. |
Since I haven't found any formatter that doesn't support the |
This PR should include changes to documentation? |
Probably we should mention it in the languages configuration section |
Can we access the current file name for the formatter config? This is needed for formatter = { command = "eslint", args = ["--fix", "$HELIX_FILE"] } Is there something like this? |
Helix sends the content of the file through stdin to the formatter. I think eslint has a --stdin flag that should work |
You could add it here: https://docs.helix-editor.com/guides/adding_languages.html |
The problem with /usr/local/bin/eslintfmt
This is close, except if the |
Let's tackle the write_all problem in a separate PR, the fix there should be to just pick any view_id that already has a selection for that document. I had to do that somewhere else in commands.rs if I remember correctly |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great work on this!
I think I forgot to mention it, but when we removed the file formatter in favour of just using stdio formatting, the problem with write_all was also solved. That is because with the file formatter we needed the view_id to reload the window, but now that we just use transactions the problem doesn't exist |
Brilliant! Could anyone provide an example for prettier to format markdown files? |
I don't know the cli flags for prettier, assuming that the default behaviour is that it takes from stdin and prints to stdout, so for example you would call it like
In your languages.toml |
Cheers! I tested it with
Do I need any escape charcters for the command? In Neovim it was |
Then it's |
It'll return an error: helix/helix-view/src/document.rs Lines 443 to 451 in 0ee2061
|
@PiergiorgioZagaria , thank you for this! It's great to be able to use One question is related to the EDIT: Just realized there is more local |
.await | ||
.map_err(|_| FormatterError::WaitForOutputFailed)?; | ||
|
||
if !output.stderr.is_empty() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Stderr doesn't always mean there's an error. There are programs that use it to print diagnostic messages so that e.g. you can still pipe stdout to a file and see informational messages in the output. The only indication of error we can really use reliably is the exit code. What we should probably do is move this whole if expression inside the following one that checks the exit code. The enum variants can probably change the enum to get rid of Stderr
and add an Option<String>
to NonZeroExitStatus
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, I'll open a new PR to fix it.
Btw the function shell_impl
in helix-term/src/commands.rs
, which is used when calling the shell with ! Or | doesn't implement these checks, should we add them there too?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see, in that case, yeah, those need to be fixed too. The presence of output on stderr does not necessarily mean there was an error.
return Err(FormatterError::NonZeroExitStatus); | ||
} | ||
|
||
let str = String::from_utf8(output.stdout) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can use from_reader
here instead. I think it is more efficient. Or str::from_utf8
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
std::str::from_utf8
works, but str::from_utf8
doesn't. Btw for further fixes to this part you can comment on #3349
I case anybody stumbles in here searching for how to configure black, this is the configuration that does the trick. [[language]]
name = "python"
auto-format = true
formatter = { command = "black", args = ["-", "-q"] } |
* Change default formatter for any language * Fix clippy error * Close stdin for Stdio formatters * Better indentation and pattern matching * Return Result<Option<...>> for fn format instead of Option * Remove unwrap for stdin * Handle FormatterErrors instead of Result<Option<...>> * Use Transaction instead of LspFormatting * Use Transaction directly in Document::format * Perform stdin type formatting asynchronously * Rename formatter.type values to kebab-case * Debug format for displaying io::ErrorKind (msrv fix) * Solve conflict? * Use only stdio type formatters * Remove FormatterType enum * Remove old comment * Check if the formatter exited correctly * Add formatter configuration to the book * Avoid allocations when writing to stdin and formatting errors * Remove unused import Co-authored-by: Gokul Soumya <[email protected]>
I have never worked on a big project in rust, so I don't know if the
Result
/Option
handling is ok.This PR aims to allow the user to choose which formatter to use instead of always relying on lsp. Discussion about this in issue #2362.
Currently the only problem is that write_all_impl saves only the viewed buffers, this is because the formatter needs to reload or apply the transaction to the document. How do we get around this?