Skip to content
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

Format changed code? #72

Open
davidhewitt opened this issue Dec 28, 2019 · 9 comments · Fixed by #74 · May be fixed by #78
Open

Format changed code? #72

davidhewitt opened this issue Dec 28, 2019 · 9 comments · Fixed by #74 · May be fixed by #78
Labels
Bikeshed Things we would love to design, that might require more time and or more minds ! enhancement New feature or request

Comments

@davidhewitt
Copy link

Do you think it would be possible to add a flag/mode to format the changed code? This would be really helpful alongside clippy warnings to keep PRs high quality.

(A lot of projects would pick up a lot of changes that would be irrelevant to the PR if I run cargo fmt, so formatting just my diff would be great.)

@o0Ignition0o
Copy link
Owner

Hey @davidhewitt :)

I think it would be a great idea, it has raised concerns in the fmt project rust-lang/rustfmt#1324 , and it seems like it would be a bit hard to do.

Other trade offs would include running fmt only on the files that are part of a diff, or maybe try to work on a way to get the shortest part of the AST and apply rust fmt on it.

I wonder if that would be possible, but it would definitely be a great thing to try to figure out!

I would gladly try to work on the ast idea if someone could mentor me, I’m not really sure who I could refer to.
Maybe that would be something @nrc could mentor me on. I’ll try to send the team a message :)

@o0Ignition0o o0Ignition0o added Bikeshed Things we would love to design, that might require more time and or more minds ! enhancement New feature or request labels Dec 28, 2019
@nrc
Copy link

nrc commented Jan 6, 2020

Maybe that would be something @nrc could mentor me on. I’ll try to send the team a message :)

Sorry, I am not working on Rustfmt anymore. If you comment on an issue on their repo, there should be somebody who can mentor you. Good luck!

@o0Ignition0o
Copy link
Owner

Hey and thanks for your reply, I’ll send a message over :)

@o0Ignition0o
Copy link
Owner

There might be a way to use an unstable feature to do it:

If you want to restrict reformatting to specific sets of lines, you can use the --file-lines option. 
Its argument is a JSON array of objects with file and range properties, where file is a file name, and range is an array representing a range of lines like [7,13].
Ranges are 1-based and inclusive of both end points.

rustfmt --file-lines '[
    {"file":"src/lib.rs","range":[7,13]},
    {"file":"src/lib.rs","range":[21,29]},
    {"file":"src/foo.rs","range":[10,11]},
    {"file":"src/foo.rs","range":[15,15]}]'

Let's try to play around with that :D

@o0Ignition0o
Copy link
Owner

It starts looking pretty good:

[VCS] - Getting diff with target master
[RustFmt] - checking format for directory /home/ignition/projects/oss/cargo-scout/cargo-scout
[RustFmt] - checking format for directory /home/ignition/projects/oss/cargo-scout/cargo-scout-lib
[Scout] - checking for intersections
Diff in /home/ignition/projects/oss/cargo-scout/cargo-scout-lib/src/lib.rs at line 7: 
-            pub use error::Error;
+pub use error::Error;

Cargo scout found 1 warnings
Error: NotClean

For now it only checks for formatting, but we can start working on a --fix command as soon as this gets merged.

@davidhewitt
Copy link
Author

Awesome, many thanks for implementing this!

@o0Ignition0o
Copy link
Owner

Whoops, I'm going to reopen this ^^'

For now cargo-scout fmt can find the lints. We need to add a --fix option so we can apply the suggestions!

@o0Ignition0o o0Ignition0o reopened this Jan 12, 2020
@o0Ignition0o o0Ignition0o linked a pull request Jan 13, 2020 that will close this issue
@o0Ignition0o
Copy link
Owner

might be able to revive this one soon : https://twitter.com/yaahc_/status/1250842702242910208?s=20 :D

@cecton
Copy link

cecton commented Sep 17, 2020

Hey @o0Ignition0o do you have any update on that issue? I'm still very interested

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bikeshed Things we would love to design, that might require more time and or more minds ! enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants