-
Notifications
You must be signed in to change notification settings - Fork 927
Option to preserve match alignment #894
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
Comments
I would still expect match val {
&TOMLValue::Integer(_) => ArrayType::Integer,
&TOMLValue::Float(_) => ArrayType::Float,
&TOMLValue::Boolean(_) => ArrayType::Boolean,
&TOMLValue::DateTime(_) => ArrayType::DateTime,
&TOMLValue::Array(_) => ArrayType::Array,
&TOMLValue::String(_,_) => ArrayType::String,
&TOMLValue::InlineTable(_) => ArrayType::InlineTable,
&TOMLValue::Table => panic!("Cannot have a table in an array"),
} (Aside: is rustfmt doing anything as advanced as turning The really fun question would be how it handles a situation like the following (imagine we just added match *val {
TomlValue::Integer(_) => ArrayType::Integer,
TomlValue::Float(_) => ArrayType::Float,
TomlValue::Boolean(_) => ArrayType::Boolean,
TomlValue::DateTime(_) => ArrayType::DateTime,
TomlValue::Array(_) => ArrayType::Array,
TomlValue::String(_,_) => ArrayType::String,
TomlValue::InlineTable(_) => ArrayType::InlineTable,
TomlValue::Table => panic!("Cannot have a table in an array"),
} |
re aside: no. I'm keen to do some refactoring-ish changes, I'm not sure where to draw the line though. |
Absolutely, I just copy pasted the problematic code portion from a PR I made that ran rustfmt on someone else's repo. I didn't pay much attention to where the alignment began. It would indeed make a lot of sense to align as far left as possible.
I think, for starters, it would be totally reasonable to let the burden be on the programmer. If he wants it aligned, he can do so manually with the guarantee that rustfmt will respect the alignment. (assuming config is set to "Preserve") Later a "smarter" algorithm could be introduced to detect / guess if there was intent to align and format accordingly. It would be a nice addition but definitely not mandatory for this configuration option. |
I only line up match arms sometimes (not even very consistently), so this isn't that important to me. And alining it to the left makes sense. I'm always torn between lining things up or not and if I line things up do I line it up all the way to the left for to the closest tab stop.
Gah, and I worried so much over that. Is it a language standard? Man, I really don't want to have to make a breaking change to change the casing on a struct. |
In general, preserving alignments like these (not just in match arms) would be super useful for rustfmt. |
Is there anything new on this? I use tabular in vim which can handle all situations described above but rustfmt doesn't preserve alignment unfortunately. |
No update, sorry. This is a 'some day' feature tbh. |
Any update on this? I like aligning those match right hand sides. |
As far as I can tell it’s not possible to even apply This is a minimal example of what I need: fn f(b: bool) -> u8 {
#[rustfmt::skip]
match b {
true => 1,
false => 0,
}
} Is there an alternative way to specify the attribute? Does rustfmt support any kind of markup via comments, like clang-format? Right now I’m doing: fn f(b: bool) -> u8 {
#[rustfmt::skip]
let r = match b {
true => 1,
false => 0,
};
r
} but this is coding around the tool instead of the tool helping me. (Could use |
@dato worksforme (with rust nightly 2018) #![feature(stmt_expr_attributes)]
fn main() {
fn f(b: bool) -> u8 {
#[rustfmt::skip]
match b {
true => 1,
false => 0,
}
}
} |
Well, that’s assuming I’m on nightly… (I’m not; I prefer to stay in stable). |
@nrc, i'm interested in implementing an config option or attribute to do this sort of alignment. Would you think a PR for that would be well received? |
@estk that would depend on how invasive the patch is. If it can be done without too much change, then it would be good to have, if it requires a lot of new stuff, then I'd rather not. |
Any progress on this? |
Any updates on this? |
As well as match arms, here are some other examples whre I would like to be able to preserve regular two-dimensional formatting:
In neither case does it seem like |
Solution suggestion 1 - heuristicsIs it OK for rustfmt to have heuristics? ISTM that this could be detected fairly reliably by a combination of:
The effect of the heuristic would be preserve whitespace and prevent wrapping in the whole of the affected lines. Solution suggestion 2 - more specific skip attribute
|
Any updates on this? |
Any updates on this? :) |
Any updates on this? Is there a way to format a single line rather than the whole file (like what clang-format does). |
Canary: is this still the right place to ask about support for vertically aligned match arms? |
Any updates? It looks like multiple people have attempted to implement this over the past six years, but the PRs get very little attention and are ultimately closed. |
Adding some information on this thread (since it has not been mentioned): clang-format supports something similar: |
And to add a bit more alignment perfection to the original example match val {
&TOMLValue::Integer (_ ) => ArrayType::Integer ,
&TOMLValue::Float (_ ) => ArrayType::Float ,
&TOMLValue::Boolean (_ ) => ArrayType::Boolean ,
&TOMLValue::DateTime (_ ) => ArrayType::DateTime ,
&TOMLValue::Array (_ ) => ArrayType::Array ,
&TOMLValue::String (_,_) => ArrayType::String ,
&TOMLValue::InlineTable(_ ) => ArrayType::InlineTable ,
&TOMLValue::Table => panic!("Cannot have a table in an array"),
} |
TL;DR: this issue may be blocked, awaiting clarification from current maintainers about what sort of rewrite is required (if any). That's IIUC, and I hope that I am wrong! My goal here is only to summarize the issue's status based on my read of the comments. I'm not familiar enough with the code to have an opinion about whether or not it should be blocked. Question 1: Is this issue blocked by the need for a rewrite?A contributor attempted to resolve this issue with a PR in 2020, which was closed by maintainer
That seemingly contradicts (supercedes?) maintainer
Perhaps the current maintainers have a different view about whether this issue is blocked by a rewrite? Question 2: If yes, what rewrite?If a rewrite is required, it's not clear to me what sort of rewrite Perhaps the rewrite is already tracked in a separate issue, which folks here can follow and discuss? Question 3: Once unblocked, is the option
|
Some people like to align match arms for readability
And currently it gets reformatted like this
It would be nice if rustfmt offered an option like
match_align_arms
that can have multiple values:Always
: rustfmt will try to align all (non-block) match armsPreserve
: rustfmt will preserve alignment if it is already aligned manually. For matches that are not already aligned, current behavior will be used.Never
: current behaviorI think
Preserve
should be the default behavior./cc @joelself
The text was updated successfully, but these errors were encountered: