Skip to content

Commit

Permalink
Use line_suffix for end-of-line comments (#2975)
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh authored Feb 16, 2023
1 parent 66a162f commit 6088a36
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 23 deletions.
85 changes: 73 additions & 12 deletions crates/ruff_python_formatter/src/format/expr.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#![allow(unused_variables, clippy::too_many_arguments)]

use rustpython_parser::ast::Constant;

use ruff_formatter::prelude::*;
use ruff_formatter::{format_args, write};
use ruff_text_size::TextSize;
use rustpython_parser::ast::Constant;

use crate::builders::literal;
use crate::context::ASTFormatContext;
Expand Down Expand Up @@ -40,9 +41,9 @@ fn format_starred(
}
}) {
if std::mem::take(&mut first) {
write!(f, [text(" ")])?;
write!(f, [line_suffix(&text(" "))])?;
}
write!(f, [literal(range)])?;
write!(f, [line_suffix(&literal(range))])?;
}

Ok(())
Expand All @@ -69,9 +70,9 @@ fn format_name(
}
}) {
if std::mem::take(&mut first) {
write!(f, [text(" ")])?;
write!(f, [line_suffix(&text(" "))])?;
}
write!(f, [literal(range)])?;
write!(f, [line_suffix(&literal(range))])?;
}

Ok(())
Expand Down Expand Up @@ -283,7 +284,9 @@ fn format_set(
write!(f, [text(",")])?;
write!(f, [soft_line_break_or_space()])?;
} else {
write!(f, [if_group_breaks(&text(","))])?;
if magic_trailing_comma {
write!(f, [if_group_breaks(&text(","))])?;
}
}
}
Ok(())
Expand All @@ -306,9 +309,47 @@ fn format_call(
if args.is_empty() && keywords.is_empty() {
write!(f, [text("(")])?;
write!(f, [text(")")])?;

// Apply any inline comments.
let mut first = true;
for range in expr.trivia.iter().filter_map(|trivia| {
if matches!(trivia.relationship, Relationship::Trailing) {
if let TriviaKind::InlineComment(range) = trivia.kind {
Some(range)
} else {
None
}
} else {
None
}
}) {
if std::mem::take(&mut first) {
write!(f, [line_suffix(&text(" "))])?;
}
write!(f, [line_suffix(&literal(range))])?;
}
} else {
write!(f, [text("(")])?;

// Apply any inline comments.
let mut first = true;
for range in expr.trivia.iter().filter_map(|trivia| {
if matches!(trivia.relationship, Relationship::Trailing) {
if let TriviaKind::InlineComment(range) = trivia.kind {
Some(range)
} else {
None
}
} else {
None
}
}) {
if std::mem::take(&mut first) {
write!(f, [line_suffix(&text(" "))])?;
}
write!(f, [line_suffix(&literal(range))])?;
}

let magic_trailing_comma = expr
.trivia
.iter()
Expand Down Expand Up @@ -551,6 +592,26 @@ fn format_compare(
write!(f, [space()])?;
write!(f, [group(&format_args![comparators[i].format()])])?;
}

// Apply any inline comments.
let mut first = true;
for range in expr.trivia.iter().filter_map(|trivia| {
if matches!(trivia.relationship, Relationship::Trailing) {
if let TriviaKind::InlineComment(range) = trivia.kind {
Some(range)
} else {
None
}
} else {
None
}
}) {
if std::mem::take(&mut first) {
write!(f, [line_suffix(&text(" "))])?;
}
write!(f, [line_suffix(&literal(range))])?;
}

Ok(())
}

Expand Down Expand Up @@ -662,9 +723,9 @@ fn format_attribute(
}
}) {
if std::mem::take(&mut first) {
write!(f, [text(" ")])?;
write!(f, [line_suffix(&text(" "))])?;
}
write!(f, [literal(range)])?;
write!(f, [line_suffix(&literal(range))])?;
}

Ok(())
Expand Down Expand Up @@ -702,9 +763,9 @@ fn format_bool_op(
}
}) {
if std::mem::take(&mut first) {
write!(f, [text(" ")])?;
write!(f, [line_suffix(&text(" "))])?;
}
write!(f, [literal(range)])?;
write!(f, [line_suffix(&literal(range))])?;
}

Ok(())
Expand Down Expand Up @@ -744,9 +805,9 @@ fn format_bin_op(
}
}) {
if std::mem::take(&mut first) {
write!(f, [text(" ")])?;
write!(f, [line_suffix(&text(" "))])?;
}
write!(f, [literal(range)])?;
write!(f, [line_suffix(&literal(range))])?;
}

Ok(())
Expand Down
20 changes: 10 additions & 10 deletions crates/ruff_python_formatter/src/format/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ fn format_pass(f: &mut Formatter<ASTFormatContext<'_>>, stmt: &Stmt) -> FormatRe
}
}) {
if std::mem::take(&mut first) {
write!(f, [text(" ")])?;
write!(f, [line_suffix(&text(" "))])?;
}
write!(f, [literal(range)])?;
write!(f, [line_suffix(&literal(range))])?;
}

Ok(())
Expand Down Expand Up @@ -220,9 +220,9 @@ fn format_func_def(
}
}) {
if std::mem::take(&mut first) {
write!(f, [text(" ")])?;
write!(f, [line_suffix(&text(" "))])?;
}
write!(f, [literal(range)])?;
write!(f, [line_suffix(&literal(range))])?;
}

write!(f, [block_indent(&format_args![block(body)])])
Expand Down Expand Up @@ -269,9 +269,9 @@ fn format_assign(
}
}) {
if std::mem::take(&mut first) {
write!(f, [text(" ")])?;
write!(f, [line_suffix(&text(" "))])?;
}
write!(f, [literal(range)])?;
write!(f, [line_suffix(&literal(range))])?;
}

Ok(())
Expand Down Expand Up @@ -557,9 +557,9 @@ fn format_import_from(
}
}) {
if std::mem::take(&mut first) {
write!(f, [text(" ")])?;
write!(f, [line_suffix(&text(" "))])?;
}
write!(f, [literal(range)])?;
write!(f, [line_suffix(&literal(range))])?;
}

Ok(())
Expand Down Expand Up @@ -606,9 +606,9 @@ fn format_expr(
}
}) {
if std::mem::take(&mut first) {
write!(f, [text(" ")])?;
write!(f, [line_suffix(&text(" "))])?;
}
write!(f, [literal(range)])?;
write!(f, [line_suffix(&literal(range))])?;
}

Ok(())
Expand Down
4 changes: 3 additions & 1 deletion crates/ruff_python_formatter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ mod tests {
#[test_case(Path::new("simple_cases/class_methods_new_line.py"); "class_methods_new_line")]
#[test_case(Path::new("simple_cases/beginning_backslash.py"); "beginning_backslash")]
#[test_case(Path::new("simple_cases/import_spacing.py"); "import_spacing")]
#[test_case(Path::new("simple_cases/power_op_spacing.py"); "power_op_spacing")]
fn passing(path: &Path) -> Result<()> {
let snapshot = format!("{}", path.display());
let content = std::fs::read_to_string(test_resource_path(
Expand Down Expand Up @@ -103,7 +104,8 @@ mod tests {
#[test_case(Path::new("simple_cases/expression.py"); "expression")]
#[test_case(Path::new("simple_cases/function.py"); "function")]
#[test_case(Path::new("simple_cases/function2.py"); "function2")]
#[test_case(Path::new("simple_cases/power_op_spacing.py"); "power_op_spacing")]
#[test_case(Path::new("simple_cases/function_trailing_comma.py"); "function_trailing_comma")]
#[test_case(Path::new("simple_cases/composition.py"); "composition")]
fn failing(path: &Path) -> Result<()> {
let snapshot = format!("{}", path.display());
let content = std::fs::read_to_string(test_resource_path(
Expand Down

0 comments on commit 6088a36

Please sign in to comment.