-
Notifications
You must be signed in to change notification settings - Fork 898
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
Give rustfmt a hint about stdin input #5266
base: master
Are you sure you want to change the base?
Conversation
bb4519e
to
2a3ed2c
Compare
Looks like the "stable" tests are failing because the ignore list can't be set. I'll add the
|
d33bbcc
to
22c23da
Compare
Looking at (i686-pc-windows-gnu, nightly) logs (but all windows logs should be similar) I added some Heres' an example of one of the logs
Edit: If you compare the >>> expect = """
... fn main() {
... println!("hello world!");
... }
... """
>>> stdout = """
... fn main() {
... println!("hello world!");
... }
... """
>>> expect == stdout
True |
Haven't looked at this in detail but this type of thing often hints at line endings |
Ahh I didn't even consider that as a possible issue. I'll investigate that! |
cd23c85
to
bcf190d
Compare
@calebcartwright you were right. It turns out it was a line ending issue. For the tests that were giving me trouble I decided to pass them the No rush on the review! |
|
||
// return early if there are issues with the file hint specified | ||
if let Some(file_hint) = &options.stdin_file_hint { | ||
if !file_hint.exists() { |
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.
Is there a reason that the file must exist on the file system?
For our use case #5792 (files in git history) there could be egde cases where the file is not present on disk.
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.
To be honest, it's been a very long time since I've looked at this PR, and I don't remember why. I definitely didn't have your use case in mind when I initially wrote this PR.
It's hard to say when I'll revisit this, but when I do, I'll double check if there are any issues with providing non-existent files as the file hint.
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.
Another use case would be editing usaved files.
When formatting files via stdin rustfmt didn't have a way to ignore stdin input. Now, when passing input to rustfmt via stdin one can also provide the `--stdin-file-hint` option to inform rustfmt that the input is actually from the hinted at file. rustfmt now uses this hint to determine if it can ignore formatting stdin. Note: This option is intended for text editor plugins that call rustfmt by passing input via stdin (e.g. rust-analyzer).
@@ -90,6 +90,22 @@ pub enum OperationError { | |||
/// supported with standard input. | |||
#[error("Emit mode {0} not supported with standard output.")] | |||
StdinBadEmit(EmitMode), | |||
/// Using `--std-file-hint` incorrectly |
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.
/// Using `--std-file-hint` incorrectly | |
/// Using `--stdin-file-hint` incorrectly |
@@ -494,6 +523,13 @@ fn determine_operation(matches: &Matches) -> Result<Operation, OperationError> { | |||
return Ok(Operation::Stdin { input: buffer }); | |||
} | |||
|
|||
// User's can only pass `--stdin-file-hint` when formating files via stdin. |
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.
// User's can only pass `--stdin-file-hint` when formating files via stdin. | |
// Users can only pass `--stdin-file-hint` when formating files via stdin. |
} | ||
|
||
if let Some(ext) = file_hint.extension() { | ||
if ext != "rs" { |
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.
Does rustfmt reject files with other extensions when passed a path? We should probably match that behavior, someone could use a different extension for various reasons.
When formatting text via stdin rustfmt didn't have a way to ignore stdin input.
Now, when passing input to rustfmt via stdin one can also provide the
--stdin-file-hint
option to inform rustfmt that the input is actually from the hinted at file. rustfmt now uses this hint to determine if it can ignore formatting stdin and immediately echo the input back to the user.Note: This option can only be passed when formatting files via stdin, and is intended for text editor plugins that call rustfmt by passing input via stdin (e.g. rust-analyzer).