-
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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ use rustc_span::Span; | |
|
||
use crate::comment::recover_comment_removed; | ||
use crate::config::Version; | ||
use crate::expr::{format_expr, is_simple_block, ExprType}; | ||
use crate::expr::{contains_curly_block, format_expr, is_simple_block, ExprType}; | ||
use crate::rewrite::{Rewrite, RewriteContext}; | ||
use crate::shape::Shape; | ||
use crate::source_map::LineRangeUtils; | ||
|
@@ -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 commentThe 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 commentThe 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. |
||
is_last: bool, | ||
} | ||
|
||
|
@@ -41,15 +42,24 @@ impl<'a> Stmt<'a> { | |
if is_simple_block(context, block, attrs) { | ||
let inner = &block.stmts[0]; | ||
// Simple blocks only contain one expr and no stmts | ||
let is_first = true; | ||
let is_last = true; | ||
Some(Stmt { inner, is_last }) | ||
Some(Stmt { | ||
inner, | ||
is_first, | ||
is_last, | ||
}) | ||
} else { | ||
None | ||
} | ||
} | ||
|
||
pub(crate) fn from_ast_node(inner: &'a ast::Stmt, is_last: bool) -> Self { | ||
Stmt { inner, is_last } | ||
pub(crate) fn from_ast_node(inner: &'a ast::Stmt, is_first: bool, is_last: bool) -> Self { | ||
Stmt { | ||
inner, | ||
is_first, | ||
is_last, | ||
} | ||
} | ||
|
||
pub(crate) fn from_ast_nodes<I>(iter: I) -> Vec<Self> | ||
|
@@ -58,9 +68,19 @@ impl<'a> Stmt<'a> { | |
{ | ||
let mut result = vec![]; | ||
let mut iter = iter.peekable(); | ||
while iter.peek().is_some() { | ||
|
||
if let Some(inner) = iter.next() { | ||
result.push(Stmt { | ||
inner: iter.next().unwrap(), | ||
inner, | ||
is_first: true, | ||
is_last: iter.peek().is_none(), | ||
}) | ||
} | ||
|
||
while let Some(inner) = iter.next() { | ||
result.push(Stmt { | ||
inner, | ||
is_first: false, | ||
is_last: iter.peek().is_none(), | ||
}) | ||
} | ||
|
@@ -71,6 +91,14 @@ impl<'a> Stmt<'a> { | |
matches!(self.inner.kind, ast::StmtKind::Empty) | ||
} | ||
|
||
pub(crate) fn is_first(&self) -> bool { | ||
self.is_first | ||
} | ||
|
||
pub(crate) fn is_last(&self) -> bool { | ||
self.is_last | ||
} | ||
|
||
fn is_last_expr(&self) -> bool { | ||
if !self.is_last { | ||
return false; | ||
|
@@ -86,6 +114,20 @@ impl<'a> Stmt<'a> { | |
_ => false, | ||
} | ||
} | ||
|
||
pub(crate) fn is_block_with_curly_braces(&self) -> bool { | ||
match self.as_ast_node().kind { | ||
ast::StmtKind::Let(ref local) => match local.kind { | ||
ast::LocalKind::Decl => false, | ||
ast::LocalKind::Init(ref expr) => contains_curly_block(expr), | ||
ast::LocalKind::InitElse(..) => true, | ||
}, | ||
ast::StmtKind::Expr(ref expr) | ast::StmtKind::Semi(ref expr) => { | ||
contains_curly_block(expr) | ||
} | ||
_ => false, | ||
} | ||
} | ||
} | ||
|
||
impl<'a> Rewrite for Stmt<'a> { | ||
|
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...