mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-27 04:19:43 +00:00
Reuse locator in formatter comments (#7227)
**Summary** The comment visitor used to rebuild the locator for every comment. Instead, we now keep the locator on the builder. Follow-up to #6813. **Test Plan** No formatting changes.
This commit is contained in:
parent
6661be2c30
commit
45f9fca228
2 changed files with 16 additions and 10 deletions
|
@ -104,6 +104,7 @@ use ruff_python_ast::node::AnyNodeRef;
|
||||||
use ruff_python_ast::visitor::preorder::{PreorderVisitor, TraversalSignal};
|
use ruff_python_ast::visitor::preorder::{PreorderVisitor, TraversalSignal};
|
||||||
use ruff_python_index::CommentRanges;
|
use ruff_python_index::CommentRanges;
|
||||||
use ruff_python_trivia::PythonWhitespace;
|
use ruff_python_trivia::PythonWhitespace;
|
||||||
|
use ruff_source_file::Locator;
|
||||||
|
|
||||||
use crate::comments::debug::{DebugComment, DebugComments};
|
use crate::comments::debug::{DebugComment, DebugComments};
|
||||||
use crate::comments::map::{LeadingDanglingTrailing, MultiMap};
|
use crate::comments::map::{LeadingDanglingTrailing, MultiMap};
|
||||||
|
@ -325,7 +326,7 @@ impl<'a> Comments<'a> {
|
||||||
let map = if comment_ranges.is_empty() {
|
let map = if comment_ranges.is_empty() {
|
||||||
CommentsMap::new()
|
CommentsMap::new()
|
||||||
} else {
|
} else {
|
||||||
let mut builder = CommentsMapBuilder::default();
|
let mut builder = CommentsMapBuilder::new(Locator::new(source_code.as_str()));
|
||||||
CommentsVisitor::new(source_code, comment_ranges, &mut builder).visit(root);
|
CommentsVisitor::new(source_code, comment_ranges, &mut builder).visit(root);
|
||||||
builder.finish()
|
builder.finish()
|
||||||
};
|
};
|
||||||
|
|
|
@ -91,8 +91,7 @@ impl<'ast> PreorderVisitor<'ast> for CommentsVisitor<'ast, '_> {
|
||||||
slice: self.source_code.slice(*comment_range),
|
slice: self.source_code.slice(*comment_range),
|
||||||
};
|
};
|
||||||
|
|
||||||
self.builder
|
self.builder.push_comment(comment);
|
||||||
.push_comment(comment, &Locator::new(self.source_code.as_str()));
|
|
||||||
self.comment_ranges.next();
|
self.comment_ranges.next();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -132,8 +131,7 @@ impl<'ast> PreorderVisitor<'ast> for CommentsVisitor<'ast, '_> {
|
||||||
slice: self.source_code.slice(*comment_range),
|
slice: self.source_code.slice(*comment_range),
|
||||||
};
|
};
|
||||||
|
|
||||||
self.builder
|
self.builder.push_comment(comment);
|
||||||
.push_comment(comment, &Locator::new(self.source_code.as_str()));
|
|
||||||
|
|
||||||
self.comment_ranges.next();
|
self.comment_ranges.next();
|
||||||
}
|
}
|
||||||
|
@ -518,7 +516,7 @@ impl<'a> CommentPlacement<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(super) trait PushComment<'a> {
|
pub(super) trait PushComment<'a> {
|
||||||
fn push_comment(&mut self, placement: DecoratedComment<'a>, locator: &Locator);
|
fn push_comment(&mut self, placement: DecoratedComment<'a>);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A storage for the [`CommentsVisitor`] that just pushes the decorated comments to a [`Vec`] for
|
/// A storage for the [`CommentsVisitor`] that just pushes the decorated comments to a [`Vec`] for
|
||||||
|
@ -529,21 +527,21 @@ struct CommentsVecBuilder<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> PushComment<'a> for CommentsVecBuilder<'a> {
|
impl<'a> PushComment<'a> for CommentsVecBuilder<'a> {
|
||||||
fn push_comment(&mut self, placement: DecoratedComment<'a>, _: &Locator) {
|
fn push_comment(&mut self, placement: DecoratedComment<'a>) {
|
||||||
self.comments.push(placement);
|
self.comments.push(placement);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A storage for the [`CommentsVisitor`] that fixes the placement and stores the comments in a
|
/// A storage for the [`CommentsVisitor`] that fixes the placement and stores the comments in a
|
||||||
/// [`CommentsMap`].
|
/// [`CommentsMap`].
|
||||||
#[derive(Clone, Debug, Default)]
|
|
||||||
pub(super) struct CommentsMapBuilder<'a> {
|
pub(super) struct CommentsMapBuilder<'a> {
|
||||||
comments: CommentsMap<'a>,
|
comments: CommentsMap<'a>,
|
||||||
|
locator: Locator<'a>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> PushComment<'a> for CommentsMapBuilder<'a> {
|
impl<'a> PushComment<'a> for CommentsMapBuilder<'a> {
|
||||||
fn push_comment(&mut self, comment: DecoratedComment<'a>, locator: &Locator) {
|
fn push_comment(&mut self, placement: DecoratedComment<'a>) {
|
||||||
let placement = place_comment(comment, locator);
|
let placement = place_comment(placement, &self.locator);
|
||||||
match placement {
|
match placement {
|
||||||
CommentPlacement::Leading { node, comment } => {
|
CommentPlacement::Leading { node, comment } => {
|
||||||
self.push_leading_comment(node, comment);
|
self.push_leading_comment(node, comment);
|
||||||
|
@ -605,6 +603,13 @@ impl<'a> PushComment<'a> for CommentsMapBuilder<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> CommentsMapBuilder<'a> {
|
impl<'a> CommentsMapBuilder<'a> {
|
||||||
|
pub(crate) fn new(locator: Locator<'a>) -> Self {
|
||||||
|
Self {
|
||||||
|
comments: CommentsMap::default(),
|
||||||
|
locator,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub(crate) fn finish(self) -> CommentsMap<'a> {
|
pub(crate) fn finish(self) -> CommentsMap<'a> {
|
||||||
self.comments
|
self.comments
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue