-
Notifications
You must be signed in to change notification settings - Fork 889
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
Ability to format inline functions with dependencies through stdin #1290
Comments
SO, the problem here is that rustfmt uses the Rust parser and that expects to start at the module level, so anything you pass it must be an item that can appear in a module. Passing a method doesn't work because they must be inside an impl. IDEs have a similar problem when you try to reformat a selection, so it is something I very much want to solve. There are a few solutions:
On top of any of these you'd still need to solve the 'starting indent' problem, but that at least should be very easy. |
I would’ve guessed this to be a complicated issue, and this is why I have deliberately delayed filing it until rustfmt achieved some maturity (-: I suspect the easiest thing I can do here as a user is to write a shell script that places anything I pass by stdin that begins with About the two other issues, should I file separate issues for these?
Despite these issues, I want to use this opportunity to say how much I appreciate the work you and others have put into rustfmt. It makes it an enjoyable experience to work with Rust in editors that rely heavily on integrating with the surrounding system. |
It's probably worth filing an issue for the error on stdin stuff. The whitespace thing is tied very closely to knowing the context for parsing so can just be dealt with here. Thanks, I'm glad its helpful! |
Triage: they both seems to be fixed on the current master. |
Both issues I described above still reproduces on master at the time of writing this:
|
@andreastt Are you using |
I used
If I try to install rustfmt-nightly through
Using rustfmt built from 5b1a84c:
|
@andreastt 5b1a84c is pretty ancient. To get rustfmt-nightly running, see the last tip here https://github.com/rust-lang-nursery/rustfmt#tips |
It appears that my upstream remote was https://github.com/nrc/rustfmt.git (and not https://github.com/rust-lang-nursery/rustfmt.git) which explains why 5b1a84c was at the HEAD of master after I pulled. However, using the nightly rustc, I’m not able to start rustfmt:
|
@andreastt you want the last tip, here - https://github.com/rust-lang-nursery/rustfmt#tips |
@nrc By modifying the
|
Glad you got it working. I would actually expect to still see these issues, at least the way you are testing. If you format the whole file (including the impl), but just select the |
Let me explain my use case a bit better. The reason I want rustfmt to (1) preserve the default whitespace when formatting a selection of indented code and (2) be able to format selections that by themselves are invalid without an encasing Quite often you have a long implementation where you made a change to a single function. In this case you don’t want to enforce a code style on the rest of the file you didn’t change, but you want the changed code to be conforming to the rustfmt coding style. |
That is pretty much the use-case for the 'file-lines' feature existing and is how we do it in the RLS (although not using stdin/out directly). The one thing we don't really aim to do is 'learn' the indentation from the surrounding file - we assume you've indented the surrounding file properly (so we'll be indented the right amount for the selection, without reformatting the rest of the file). |
(Please excuse my lack of proper internals parlance, and I apologise that I am probably mixing a few issues here.)
I often only format selections of a file by passing these to rustfmt by stdin and replacing the selection with the stdout that comes back. This works great for top-level functions that are not part of an implentation, but not so good for functions that have dependencies such as
self
. In this case, rustfmt trips up because&self
is missing a type definition.Given this code:
Selecting lines 2 through 7 (the
to_json
function) and passing it through rustfmt yields the following error:If I’m formatting one such function in the middle of a long
impl
block, I often find myself adding aimpl F { … }
to wrap the function so that I will not have to format potentially several hundred lines of code.What is also problematic is that selecting the inner function deletes the selection because the stdout is nothing. It would be less annoying if stdout was equal to stdin in so the selection could be preserved whenever there is an error.
In other cases where the function is syntactically correct and without dependencies, rustfmt disregards the indentation of the lines because it thinks it is a top-level function. When rustfmt runs in stdin mode, it should preserve this whitespace:
Selecting lines 2 through 4 in the above example will result in this:
Whereas I would expect this to happen:
Let me know if I should break this up into multiple issues. Because I’m unfamiliar with the rustfmt terminology, I understand some of this might be replicated in other issues or be available as config options.
The text was updated successfully, but these errors were encountered: