Unify comment terminology with that of rome_formatter (#2979)
This commit is contained in:
@@ -37,7 +37,7 @@ impl Format<ASTFormatContext<'_>> for LeadingComments<'_> {
|
||||
fn fmt(&self, f: &mut Formatter<ASTFormatContext<'_>>) -> FormatResult<()> {
|
||||
for comment in self.comments {
|
||||
if matches!(comment.relationship, Relationship::Leading) {
|
||||
if let TriviaKind::StandaloneComment(range) = comment.kind {
|
||||
if let TriviaKind::OwnLineComment(range) = comment.kind {
|
||||
write!(f, [hard_line_break()])?;
|
||||
write!(f, [literal(range)])?;
|
||||
}
|
||||
@@ -61,7 +61,7 @@ impl Format<ASTFormatContext<'_>> for TrailingComments<'_> {
|
||||
fn fmt(&self, f: &mut Formatter<ASTFormatContext<'_>>) -> FormatResult<()> {
|
||||
for comment in self.comments {
|
||||
if matches!(comment.relationship, Relationship::Trailing) {
|
||||
if let TriviaKind::StandaloneComment(range) = comment.kind {
|
||||
if let TriviaKind::OwnLineComment(range) = comment.kind {
|
||||
write!(f, [hard_line_break()])?;
|
||||
write!(f, [literal(range)])?;
|
||||
}
|
||||
|
||||
@@ -27,11 +27,11 @@ fn format_starred(
|
||||
) -> FormatResult<()> {
|
||||
write!(f, [text("*"), value.format()])?;
|
||||
|
||||
// Apply any inline comments.
|
||||
// Format any end-of-line 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 {
|
||||
if let TriviaKind::EndOfLineComment(range) = trivia.kind {
|
||||
Some(range)
|
||||
} else {
|
||||
None
|
||||
@@ -56,11 +56,11 @@ fn format_name(
|
||||
) -> FormatResult<()> {
|
||||
write!(f, [literal(Range::from_located(expr))])?;
|
||||
|
||||
// Apply any inline comments.
|
||||
// Format any end-of-line 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 {
|
||||
if let TriviaKind::EndOfLineComment(range) = trivia.kind {
|
||||
Some(range)
|
||||
} else {
|
||||
None
|
||||
@@ -310,11 +310,11 @@ fn format_call(
|
||||
write!(f, [text("(")])?;
|
||||
write!(f, [text(")")])?;
|
||||
|
||||
// Apply any inline comments.
|
||||
// Format any end-of-line 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 {
|
||||
if let TriviaKind::EndOfLineComment(range) = trivia.kind {
|
||||
Some(range)
|
||||
} else {
|
||||
None
|
||||
@@ -331,11 +331,11 @@ fn format_call(
|
||||
} else {
|
||||
write!(f, [text("(")])?;
|
||||
|
||||
// Apply any inline comments.
|
||||
// Format any end-of-line 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 {
|
||||
if let TriviaKind::EndOfLineComment(range) = trivia.kind {
|
||||
Some(range)
|
||||
} else {
|
||||
None
|
||||
@@ -400,7 +400,7 @@ fn format_call(
|
||||
// Apply any dangling trailing comments.
|
||||
for trivia in &expr.trivia {
|
||||
if matches!(trivia.relationship, Relationship::Dangling) {
|
||||
if let TriviaKind::StandaloneComment(range) = trivia.kind {
|
||||
if let TriviaKind::OwnLineComment(range) = trivia.kind {
|
||||
write!(f, [expand_parent()])?;
|
||||
write!(f, [hard_line_break()])?;
|
||||
write!(f, [literal(range)])?;
|
||||
@@ -593,11 +593,11 @@ fn format_compare(
|
||||
write!(f, [group(&format_args![comparators[i].format()])])?;
|
||||
}
|
||||
|
||||
// Apply any inline comments.
|
||||
// Format any end-of-line 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 {
|
||||
if let TriviaKind::EndOfLineComment(range) = trivia.kind {
|
||||
Some(range)
|
||||
} else {
|
||||
None
|
||||
@@ -709,11 +709,11 @@ fn format_attribute(
|
||||
write!(f, [text(".")])?;
|
||||
write!(f, [dynamic_text(attr, TextSize::default())])?;
|
||||
|
||||
// Apply any inline comments.
|
||||
// Format any end-of-line 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 {
|
||||
if let TriviaKind::EndOfLineComment(range) = trivia.kind {
|
||||
Some(range)
|
||||
} else {
|
||||
None
|
||||
@@ -749,11 +749,11 @@ fn format_bool_op(
|
||||
}
|
||||
}
|
||||
|
||||
// Apply any inline comments.
|
||||
// Format any end-of-line 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 {
|
||||
if let TriviaKind::EndOfLineComment(range) = trivia.kind {
|
||||
Some(range)
|
||||
} else {
|
||||
None
|
||||
@@ -791,11 +791,11 @@ fn format_bin_op(
|
||||
}
|
||||
write!(f, [group(&format_args![right.format()])])?;
|
||||
|
||||
// Apply any inline comments.
|
||||
// Format any end-of-line 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 {
|
||||
if let TriviaKind::EndOfLineComment(range) = trivia.kind {
|
||||
Some(range)
|
||||
} else {
|
||||
None
|
||||
@@ -886,7 +886,7 @@ impl Format<ASTFormatContext<'_>> for FormatExpr<'_> {
|
||||
// Any leading comments come on the line before.
|
||||
for trivia in &self.item.trivia {
|
||||
if matches!(trivia.relationship, Relationship::Leading) {
|
||||
if let TriviaKind::StandaloneComment(range) = trivia.kind {
|
||||
if let TriviaKind::OwnLineComment(range) = trivia.kind {
|
||||
write!(f, [expand_parent()])?;
|
||||
write!(f, [literal(range)])?;
|
||||
write!(f, [hard_line_break()])?;
|
||||
@@ -958,7 +958,7 @@ impl Format<ASTFormatContext<'_>> for FormatExpr<'_> {
|
||||
// Any trailing comments come on the lines after.
|
||||
for trivia in &self.item.trivia {
|
||||
if matches!(trivia.relationship, Relationship::Trailing) {
|
||||
if let TriviaKind::StandaloneComment(range) = trivia.kind {
|
||||
if let TriviaKind::OwnLineComment(range) = trivia.kind {
|
||||
write!(f, [expand_parent()])?;
|
||||
write!(f, [literal(range)])?;
|
||||
write!(f, [hard_line_break()])?;
|
||||
|
||||
@@ -20,11 +20,11 @@ fn format_pass(f: &mut Formatter<ASTFormatContext<'_>>, stmt: &Stmt) -> FormatRe
|
||||
// Write the statement body.
|
||||
write!(f, [text("pass")])?;
|
||||
|
||||
// Apply any inline comments.
|
||||
// Format any end-of-line comments.
|
||||
let mut first = true;
|
||||
for range in stmt.trivia.iter().filter_map(|trivia| {
|
||||
if matches!(trivia.relationship, Relationship::Trailing) {
|
||||
if let TriviaKind::InlineComment(range) = trivia.kind {
|
||||
if let TriviaKind::EndOfLineComment(range) = trivia.kind {
|
||||
Some(range)
|
||||
} else {
|
||||
None
|
||||
@@ -206,11 +206,11 @@ fn format_func_def(
|
||||
|
||||
write!(f, [text(":")])?;
|
||||
|
||||
// Apply any inline comments.
|
||||
// Format any end-of-line comments.
|
||||
let mut first = true;
|
||||
for range in stmt.trivia.iter().filter_map(|trivia| {
|
||||
if matches!(trivia.relationship, Relationship::Trailing) {
|
||||
if let TriviaKind::InlineComment(range) = trivia.kind {
|
||||
if let TriviaKind::EndOfLineComment(range) = trivia.kind {
|
||||
Some(range)
|
||||
} else {
|
||||
None
|
||||
@@ -255,11 +255,11 @@ fn format_assign(
|
||||
)?;
|
||||
}
|
||||
|
||||
// Apply any inline comments.
|
||||
// Format any end-of-line comments.
|
||||
let mut first = true;
|
||||
for range in stmt.trivia.iter().filter_map(|trivia| {
|
||||
if matches!(trivia.relationship, Relationship::Trailing) {
|
||||
if let TriviaKind::InlineComment(range) = trivia.kind {
|
||||
if let TriviaKind::EndOfLineComment(range) = trivia.kind {
|
||||
Some(range)
|
||||
} else {
|
||||
None
|
||||
@@ -543,11 +543,11 @@ fn format_import_from(
|
||||
)?;
|
||||
}
|
||||
|
||||
// Apply any inline comments.
|
||||
// Format any end-of-line comments.
|
||||
let mut first = true;
|
||||
for range in stmt.trivia.iter().filter_map(|trivia| {
|
||||
if matches!(trivia.relationship, Relationship::Trailing) {
|
||||
if let TriviaKind::InlineComment(range) = trivia.kind {
|
||||
if let TriviaKind::EndOfLineComment(range) = trivia.kind {
|
||||
Some(range)
|
||||
} else {
|
||||
None
|
||||
@@ -592,11 +592,11 @@ fn format_expr(
|
||||
)?;
|
||||
}
|
||||
|
||||
// Apply any inline comments.
|
||||
// Format any end-of-line comments.
|
||||
let mut first = true;
|
||||
for range in stmt.trivia.iter().filter_map(|trivia| {
|
||||
if matches!(trivia.relationship, Relationship::Trailing) {
|
||||
if let TriviaKind::InlineComment(range) = trivia.kind {
|
||||
if let TriviaKind::EndOfLineComment(range) = trivia.kind {
|
||||
Some(range)
|
||||
} else {
|
||||
None
|
||||
@@ -665,7 +665,7 @@ impl Format<ASTFormatContext<'_>> for FormatStmt<'_> {
|
||||
TriviaKind::EmptyLine => {
|
||||
write!(f, [empty_line()])?;
|
||||
}
|
||||
TriviaKind::StandaloneComment(range) => {
|
||||
TriviaKind::OwnLineComment(range) => {
|
||||
write!(f, [literal(range), hard_line_break()])?;
|
||||
}
|
||||
_ => {}
|
||||
@@ -808,7 +808,7 @@ impl Format<ASTFormatContext<'_>> for FormatStmt<'_> {
|
||||
TriviaKind::EmptyLine => {
|
||||
write!(f, [empty_line()])?;
|
||||
}
|
||||
TriviaKind::StandaloneComment(range) => {
|
||||
TriviaKind::OwnLineComment(range) => {
|
||||
write!(f, [literal(range), hard_line_break()])?;
|
||||
}
|
||||
_ => {}
|
||||
|
||||
@@ -28,8 +28,8 @@ impl Node<'_> {
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum TriviaTokenKind {
|
||||
StandaloneComment,
|
||||
InlineComment,
|
||||
OwnLineComment,
|
||||
EndOfLineComment,
|
||||
MagicTrailingComma,
|
||||
EmptyLine,
|
||||
Parentheses,
|
||||
@@ -44,8 +44,27 @@ pub struct TriviaToken {
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum TriviaKind {
|
||||
StandaloneComment(Range),
|
||||
InlineComment(Range),
|
||||
/// A Comment that is separated by at least one line break from the preceding token.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```ignore
|
||||
/// a = 1
|
||||
/// # This is an own-line comment.
|
||||
/// b = 2
|
||||
/// ```
|
||||
OwnLineComment(Range),
|
||||
/// A comment that is on the same line as the preceding token.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ## End of line
|
||||
///
|
||||
/// ```ignore
|
||||
/// a = 1 # This is an end-of-line comment.
|
||||
/// b = 2
|
||||
/// ```
|
||||
EndOfLineComment(Range),
|
||||
MagicTrailingComma,
|
||||
EmptyLine,
|
||||
Parentheses,
|
||||
@@ -85,12 +104,12 @@ impl Trivia {
|
||||
kind: TriviaKind::EmptyLine,
|
||||
relationship,
|
||||
},
|
||||
TriviaTokenKind::StandaloneComment => Self {
|
||||
kind: TriviaKind::StandaloneComment(Range::new(token.start, token.end)),
|
||||
TriviaTokenKind::OwnLineComment => Self {
|
||||
kind: TriviaKind::OwnLineComment(Range::new(token.start, token.end)),
|
||||
relationship,
|
||||
},
|
||||
TriviaTokenKind::InlineComment => Self {
|
||||
kind: TriviaKind::InlineComment(Range::new(token.start, token.end)),
|
||||
TriviaTokenKind::EndOfLineComment => Self {
|
||||
kind: TriviaKind::EndOfLineComment(Range::new(token.start, token.end)),
|
||||
relationship,
|
||||
},
|
||||
TriviaTokenKind::Parentheses => Self {
|
||||
@@ -125,9 +144,9 @@ pub fn extract_trivia_tokens(lxr: &[LexResult]) -> Vec<TriviaToken> {
|
||||
start: *start,
|
||||
end: *end,
|
||||
kind: if prev_non_newline_tok.map_or(true, |(prev, ..)| prev.row() < start.row()) {
|
||||
TriviaTokenKind::StandaloneComment
|
||||
TriviaTokenKind::OwnLineComment
|
||||
} else {
|
||||
TriviaTokenKind::InlineComment
|
||||
TriviaTokenKind::EndOfLineComment
|
||||
},
|
||||
});
|
||||
}
|
||||
@@ -774,7 +793,7 @@ pub fn decorate_trivia(tokens: Vec<TriviaToken>, python_ast: &[Stmt]) -> TriviaI
|
||||
for (index, token) in tokens.into_iter().enumerate() {
|
||||
let (preceding_node, following_node, enclosing_node, enclosed_node) = &stack[index];
|
||||
match token.kind {
|
||||
TriviaTokenKind::EmptyLine | TriviaTokenKind::StandaloneComment => {
|
||||
TriviaTokenKind::EmptyLine | TriviaTokenKind::OwnLineComment => {
|
||||
if let Some(following_node) = following_node {
|
||||
// Always a leading comment.
|
||||
add_comment(
|
||||
@@ -799,7 +818,7 @@ pub fn decorate_trivia(tokens: Vec<TriviaToken>, python_ast: &[Stmt]) -> TriviaI
|
||||
unreachable!("Attach token to the ast: {:?}", token);
|
||||
}
|
||||
}
|
||||
TriviaTokenKind::InlineComment => {
|
||||
TriviaTokenKind::EndOfLineComment => {
|
||||
if let Some(preceding_node) = preceding_node {
|
||||
// There is content before this comment on the same line, but
|
||||
// none after it, so prefer a trailing comment of the previous node.
|
||||
|
||||
Reference in New Issue
Block a user