-
Notifications
You must be signed in to change notification settings - Fork 898
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
Implement empty lines surrounding blocks #5969
base: master
Are you sure you want to change the base?
Implement empty lines surrounding blocks #5969
Conversation
37f581f
to
8b6edcf
Compare
9b7251c
to
3bace87
Compare
Hey @ytmimi, any chance I could bug you to take a quick glance here and tell me if I'm on the right track? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I haven't done a thorough review of the code, but I've posed a few questions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All of the test cases here only show one level of nesting. What happens when there are multiple levels of nesting, e.g:
fn main() {
if true {
if true {
if true {
}
}
}
}
@@ -12,6 +12,7 @@ use crate::utils::semicolon_for_stmt; | |||
|
|||
pub(crate) struct Stmt<'a> { | |||
inner: &'a ast::Stmt, | |||
is_first: bool, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need to add is_first to get this to work?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In my current implementation, yes. I'm not sure if there's a better way to do this, though. Open to suggestions.
@@ -1194,6 +1194,63 @@ pub(crate) fn is_simple_block( | |||
&& attrs.map_or(true, |a| a.is_empty()) | |||
} | |||
|
|||
pub(crate) fn contains_curly_block(expr: &ast::Expr) -> bool { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know if contains_curly_block
is the right name for this function. Are you trying to add space around anything with curly braces or just all statements?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm open to suggestions for names.
Here's the "problem" that I've run into: I've been reviewing code that will have 50 lines without a single line between them. If one looks at any popular open-source repo (including rustfmt), one can see many more empty lines, breaking code up into logical chunks like paragraphs in prose. So the question is, how can a formatter that doesn't have logic context of the code add some of this spacing? Naturally, new scopes (demarcated by curly braces) provide an opportunity to add spacing.
So maybe the name should be something like has_new_scope
? Or something along those lines...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rustfmt also has the blank_lines_lower_bound
and blank_lines_upper_bound
configuration options, which seem to operate pretty similar to this option. What happens when using your new option and these other configurations?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It behaves as one would expect if the blank_lines_(upper|lower)_bound
config takes precedence over surround_blocks_with_empty_lines
. I would expect that the bounds take precedence as they affect a larger subset of the codebase. However, maybe that expectation is incorrect, but I couldn't imagine someone setting the upper bound to 0
and using surround_blocks_with_empty_lines
, where the former overrides the latter. As for setting a lower-bound greater than 0
, I also wouldn't expect its usage together with surround_blocks_with_empty_lines
since it actually adds more spacing.
12a1240
to
73ac971
Compare
73ac971
to
9aff401
Compare
9aff401
to
50c0787
Compare
50c0787
to
fad8b62
Compare
fad8b62
to
4ef160b
Compare
4ef160b
to
a4388bc
Compare
a4388bc
to
cb20f1d
Compare
cb20f1d
to
fd7b6e0
Compare
Here's my first shot at solving #5963. Please see the description in that issue to review this PR. I would rather the reviewer have an idea what I'm trying to do before taking a look at my documentation (otherwise, I might miss something in the docs).
I think the code is pretty straight forward, but I might not being following the idioms in this library.
I'm also pretty sure that there must be edge cases that I missed in my testing, please advise if you can think of any.
If the initial review here makes sense, I can go ahead and fill in the documentation and continue from there.
Thanks in advance!