-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Preserve trailing semicolon for Notebooks #8590
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,7 @@ use ruff_text_size::{Ranged, TextRange}; | |
use crate::comments::{ | ||
leading_comments, trailing_comments, Comments, LeadingDanglingTrailingComments, | ||
}; | ||
use crate::context::{NodeLevel, WithNodeLevel}; | ||
use crate::context::{NodeLevel, TopLevelStatementPosition, WithNodeLevel}; | ||
use crate::expression::string::StringLayout; | ||
use crate::prelude::*; | ||
use crate::statement::stmt_expr::FormatStmtExpr; | ||
|
@@ -49,8 +49,19 @@ impl Default for FormatSuite { | |
|
||
impl FormatRule<Suite, PyFormatContext<'_>> for FormatSuite { | ||
fn fmt(&self, statements: &Suite, f: &mut PyFormatter) -> FormatResult<()> { | ||
let mut iter = statements.iter(); | ||
let Some(first) = iter.next() else { | ||
return Ok(()); | ||
}; | ||
|
||
let node_level = match self.kind { | ||
SuiteKind::TopLevel => NodeLevel::TopLevel, | ||
SuiteKind::TopLevel => NodeLevel::TopLevel( | ||
iter.clone() | ||
.next() | ||
.map_or(TopLevelStatementPosition::Last, |_| { | ||
TopLevelStatementPosition::Other | ||
}), | ||
), | ||
Comment on lines
+58
to
+64
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. I think this can be simplified to SuiteKind::TopLevel => NodeLevel::TopLevel(if statements.len() < 2 {
TopLevelStatementPosition::Last
} else {
TopLevelStatementPosition::Other
}), 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. Yes, that can be done. |
||
SuiteKind::Function | SuiteKind::Class | SuiteKind::Other => { | ||
NodeLevel::CompoundStatement | ||
} | ||
|
@@ -62,11 +73,6 @@ impl FormatRule<Suite, PyFormatContext<'_>> for FormatSuite { | |
|
||
let f = &mut WithNodeLevel::new(node_level, f); | ||
|
||
let mut iter = statements.iter(); | ||
let Some(first) = iter.next() else { | ||
return Ok(()); | ||
}; | ||
|
||
// Format the first statement in the body, which often has special formatting rules. | ||
let first = match self.kind { | ||
SuiteKind::Other => { | ||
|
@@ -165,6 +171,11 @@ impl FormatRule<Suite, PyFormatContext<'_>> for FormatSuite { | |
let mut preceding_comments = comments.leading_dangling_trailing(preceding); | ||
|
||
while let Some(following) = iter.next() { | ||
if self.kind == SuiteKind::TopLevel && iter.clone().next().is_none() { | ||
f.context_mut() | ||
.set_node_level(NodeLevel::TopLevel(TopLevelStatementPosition::Last)); | ||
} | ||
|
||
let following_comments = comments.leading_dangling_trailing(following); | ||
|
||
let needs_empty_lines = if is_class_or_function_definition(following) { | ||
|
@@ -351,7 +362,7 @@ impl FormatRule<Suite, PyFormatContext<'_>> for FormatSuite { | |
.map_or(preceding.end(), |comment| comment.slice().end()); | ||
|
||
match node_level { | ||
NodeLevel::TopLevel => match lines_after(end, source) { | ||
NodeLevel::TopLevel(_) => match lines_after(end, source) { | ||
0 | 1 => hard_line_break().fmt(f)?, | ||
2 => empty_line().fmt(f)?, | ||
_ => match source_type { | ||
|
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.
We could avoid cloning the iterator by making it peekable but that involves updating the function signature wherever this iterator is being passed. This is because it expects
std::slice::Iter
.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.
This clone is effectively free (we just clone the slice reference), so this is fine. The peakable things with the function signatures is annoying though, i had also already hit that