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

Scoped formatting #2713

Closed
jamesmunns opened this issue May 16, 2018 · 3 comments
Closed

Scoped formatting #2713

jamesmunns opened this issue May 16, 2018 · 3 comments

Comments

@jamesmunns
Copy link
Member

Hey all,

It would be nice to have an option that would allow me to only format a subset of a project. Typically this comes into play when making changes to a larger project which may either: 1. never have had formatting applied, or 2. was formatted using a different version of rustfmt with different rules, or 3. has components that do not format deterministically.

In these cases, a small change + a format can end up touching nearly every file (or large parts of a single file) in the project, which is not great for maintainers, as it makes it difficult to review changes.

Ideally, I would like to do something like this:

# check out the `master` branch of a project
git checkout <SOME PROJECT>

# make some changes
nano src/main.rs

# This part could be optional (if diffing against the working directory)
git checkout -b some-patch
git add .
git commit -m "This is a helpful patch!"

# Now the magic
cargo fmt --only-changes-since-git-revision="master"

This would apply formatting changes ONLY to some subset of the code, probably one of the following:

  • Only the changed lines
  • Only the nearest block containing changed lines (e.g. function, closure, struct definition)

I am not familiar with the inner workings of rustfmt, and I saw the --file-lines option, however I couldn't find any docs regarding that, and I wasn't sure if it was possible (within rustfmt) to figure out how far a block stretches when a line number is specified.

If this sounds feasible, I would be interested in trying to work on this, if there is a mentor I can ask some spinning-up questions to.

Let me know what you think!

@jamesmunns
Copy link
Member Author

Whoops, after searching a little more, I believe this is a duplicate of #1324.

Please feel free to close this issue, and let me know if you would like me to add the body of the issue as a comment elsewhere.

@jamesmunns
Copy link
Member Author

I'd be especially interested in hearing if anyone has a good way to convert "lines touched" to "AST nodes touched", maybe back to "lines to format".

@nrc nrc changed the title Feature Request: Scoped formatting Scoped formatting Jun 22, 2018
@topecongiro
Copy link
Contributor

Closing in favor of #1324.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants