Correctly associate own-line comments in bodies (#4671)

This commit is contained in:
Micha Reiser 2023-06-01 08:12:53 +02:00 committed by GitHub
parent 46c3b3af94
commit be31d71849
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 1747 additions and 144 deletions

View file

@ -1,11 +1,10 @@
use crate::comments::map::MultiMap;
use crate::comments::node_key::NodeRefEqualityKey;
use crate::comments::placement::place_comment;
use crate::comments::{CommentTextPosition, CommentsMap, SourceComment};
use ruff_formatter::{SourceCode, SourceCodeSlice};
use ruff_python_ast::node::AnyNodeRef;
use ruff_python_ast::prelude::*;
use ruff_python_ast::source_code::CommentRanges;
use ruff_python_ast::source_code::{CommentRanges, Locator};
use std::cell::Cell;
// The interface is designed to only export the members relevant for iterating nodes in
// pre-order.
@ -13,7 +12,6 @@ use std::cell::Cell;
use ruff_python_ast::visitor::preorder::*;
use ruff_python_ast::whitespace::is_python_whitespace;
use ruff_text_size::TextRange;
use std::cmp::Ordering;
use std::iter::Peekable;
/// Visitor extracting the comments from an AST.
@ -68,12 +66,15 @@ impl<'a> CommentsVisitor<'a> {
enclosing: enclosing_node,
preceding: self.preceding_node,
following: Some(node),
parent: self.parents.iter().rev().nth(1).copied(),
text_position: text_position(*comment_range, self.source_code),
slice: self.source_code.slice(*comment_range),
};
self.builder
.add_comment(place_comment(comment, self.source_code));
self.builder.add_comment(place_comment(
comment,
&Locator::new(self.source_code.as_str()),
));
self.comment_ranges.next();
}
@ -121,13 +122,16 @@ impl<'a> CommentsVisitor<'a> {
let comment = DecoratedComment {
enclosing: node,
preceding: self.preceding_node,
parent: self.parents.last().copied(),
following: None,
text_position: text_position(*comment_range, self.source_code),
slice: self.source_code.slice(*comment_range),
};
self.builder
.add_comment(place_comment(comment, self.source_code));
self.builder.add_comment(place_comment(
comment,
&Locator::new(self.source_code.as_str()),
));
self.comment_ranges.next();
}
@ -135,7 +139,7 @@ impl<'a> CommentsVisitor<'a> {
self.preceding_node = Some(node);
}
fn finish(mut self) -> CommentsMap<'a> {
fn finish(self) -> CommentsMap<'a> {
self.builder.finish()
}
}
@ -264,6 +268,7 @@ pub(super) struct DecoratedComment<'a> {
enclosing: AnyNodeRef<'a>,
preceding: Option<AnyNodeRef<'a>>,
following: Option<AnyNodeRef<'a>>,
parent: Option<AnyNodeRef<'a>>,
text_position: CommentTextPosition,
slice: SourceCodeSlice,
}
@ -289,6 +294,11 @@ impl<'a> DecoratedComment<'a> {
self.enclosing
}
/// Returns the parent of the enclosing node, if any
pub(super) fn enclosing_parent(&self) -> Option<AnyNodeRef<'a>> {
self.parent
}
/// Returns the slice into the source code.
pub(super) fn slice(&self) -> &SourceCodeSlice {
&self.slice