-
Notifications
You must be signed in to change notification settings - Fork 5
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
Try to break long lines at assignment first before breaking within RHS #183
Comments
That's tough because you definitely want this to break within the RHS first foo <- this %>% that() %>% this_other_long_thing() %>% and_this_one_too_as_well() I agree the current situation isn't great but I'm not sure what the best situation is yet without diving in a bit more. The line break right after the |
One way of expressing my preference is that I prefer line-wrapping strategies that minimize line breaks, with exceptions. In my original example, I'd much prefer breaking around an entire expression over introducing several breaks. Once internal breaks are required, then I like consistent breaks. Personally, I'd be okay with your over-long pipe chain example becoming this foo <-
this %>% that() %>% this_other_long_thing() %>% and_this_one_too_as_well() if you're only 6 characters over the line width constraint. Very often the second line will still not be under, in which case you'd move to an "internal break" on the RHS foo <- this_is_too_long %>%
that() %>%
this_other_long_thing() %>%
and_this_one_too_as_well() |
I feel like we never want to break in subset operators whose arguments is a simple literal or symbol? |
This is also the conclusion that I came to, making the ideal result in this case private$event_callback_counts[[domain]] <- private$event_callback_counts[[domain]]
- 1 |
I agree with @lionel-, but I still find it much more readable to have the entire RHS on the same line # very easy to miss that -1 is part of the RHS
private$event_callback_counts[[domain]] <- private$event_callback_counts[[domain]]
- 1
# My preference (possible with persistent breaks)
private$event_callback_counts[[domain]] <-
private$event_callback_counts[[domain]] - 1 I just ran into another example that feels related # Original
stopifnot(
"Running shortcuts by name requires using an R file for your shortcuts" = tolower(fs::path_ext(path)) == "r"
)
# air-formatted
stopifnot(
"Running shortcuts by name requires using an R file for your shortcuts" = tolower(
fs::path_ext(path)
) ==
"r"
)
# My preference (reformatted to above even with persistent breaks)
stopifnot(
"Running shortcuts by name requires using an R file for your shortcuts" =
tolower(fs::path_ext(path)) == "r"
)
# air-compatible formatting
stopifnot(
"Running shortcuts by name requires using an R file for your shortcuts" = {
tolower(fs::path_ext(path)) == "r"
}
) Somewhere else in the issues I read @lionel- talk about symmetry, which made this whole thing click for me. There's a symmetry in breaking once at a higher level of the AST than introducing several breaks among lower branches of the tree, which I feel results in much easier to read code. |
Not entirely sure what the best solution is yet, but I thought about this a little today and compared to biome/ruff. I think if we were to do this it would break down into two problems that need to be tackled. It seems like quite a big task though. For this particular syntax type, can we ever move the RHS to its own line?i.e. the RHS on its own line looks nice for this binary expression private$event_callback_counts[[domain]] <-
private$event_callback_counts[[domain]] - 1 But I would never want it to move onto the next line for this function call private$event_callback_counts[[domain]] <- my_special_function(with, really_really_really, long_arguments) That should always prefer expanding to private$event_callback_counts[[domain]] <- my_special_function(
with,
really_really_really,
long_arguments
) Similarly for pipes, I still think I prefer foo <- this %>% that() %>% this_other_long_thing() %>% and_this_one_too_as_well() turning into foo <- this %>%
that() %>%
this_other_long_thing() %>%
and_this_one_too_as_well() rather than foo <-
this %>% that() %>% this_other_long_thing() %>% and_this_one_too_as_well() In particular, the last form here only buys you 4 more characters before you'd have to break again, but now a persistent new line is in play, so as you keep typing to add more to your pipe chain Air would never remove that persistent new line on its own...that would be annoying. Ruff does sort of have a similar construct. It adds parentheses in some cases like this one That's kind of like what we are talking about here, but R would not need parens due to how the parser works. Ruff has a For this particular syntax type, what format is best?Say we decide binary expressions can move to their own line. We need the formatter to decide between (in order of least to most expanded) # option 1 - no breaks (past line length)
private$event_callback_counts[[domain]] <- private$event_callback_counts[[domain]] - 1
# option 2 - on own line AND ensure the RHS doesn't have any breaks in it
private$event_callback_counts[[domain]] <-
private$event_callback_counts[[domain]] - 1
# option 3 - fully broken out form
private$event_callback_counts[[domain]] <- private$event_callback_counts[[
domain
]] -
1 This typically ends up being some kind of job for You can see here how its an optimized form of the 3 options listed above Unfortunately their |
A big worry of mine is dealing with this combined with the persistent line break behavior after the # If this
private$event_callback_counts[[domain]] <- my_special_function(with, really_really_really, long_arguments)
# turned into this
private$event_callback_counts[[domain]] <-
my_special_function(with, really_really_really, long_arguments)
# but then you typed more
private$event_callback_counts[[domain]] <-
my_special_function(with, really_really_really, long_arguments, long_arguments, long_arguments)
# then it would respect the persistent line break and break as
private$event_callback_counts[[domain]] <-
my_special_function(
with,
really_really_really,
long_arguments,
long_arguments,
long_arguments
)
# rather than what you'd really want, which is
private$event_callback_counts[[domain]] <- my_special_function(
with,
really_really_really,
long_arguments,
long_arguments,
long_arguments
) You'd have to manually remove the line break before the |
Thanks for writing and sharing your thoughts!
Never is a strong word but it matches how much I would want That said, I'm certainly willing to trade some of my style preferences for easy and consistent formatting! And I think it's very defensible to have a persistent line break formula require intervention, so I understand the desire to avoid adding line break rules that have the formatter insert characters that are reserved for humans. In other words, I could very much live with a solution that lets me add a persistent line break after stopifnot(
"Running shortcuts by name requires using an R file for your shortcuts" =
tolower(fs::path_ext(path)) == "r"
) |
Actually, I suppose I can live with this: stopifnot(
"Running shortcuts by name requires using an R file for your shortcuts" = {
tolower(fs::path_ext(path)) == "r"
}
) Joking aside, I do think it's a good and clean idea to stick with "Air won't add persistent line breaks on its own". Thanks for entertaining my suggestion! |
This is not unreasonable, provided we don't come across any weirdness while implementing it, or think of any scenario where it might lock us in to some behavior. It would be the exact same reasoning as the persistent line break after The I also think we will probably make |
In chromote, we had this line
which, when wrapped to 80 characters with air 1.0.0 becomes
but I'd much rather this break at the
<-
(without me having to introduce that break manually)The text was updated successfully, but these errors were encountered: