Skip to content

Commit b17ca0d

Browse files
committed
store suppressed nodes ahead of time
1 parent bcae0b0 commit b17ca0d

File tree

6 files changed

+37
-8
lines changed

6 files changed

+37
-8
lines changed

crates/ruff_python_formatter/src/comments/mod.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ use ruff_formatter::{SourceCode, SourceCodeSlice};
9999
use ruff_python_ast::AnyNodeRef;
100100
use ruff_python_trivia::{CommentLinePosition, CommentRanges, SuppressionKind};
101101
use ruff_text_size::{Ranged, TextRange};
102+
use rustc_hash::FxHashSet;
102103
pub(crate) use visitor::collect_comments;
103104

104105
use crate::comments::debug::{DebugComment, DebugComments};
@@ -511,6 +512,23 @@ pub(crate) fn has_skip_comment(trailing_comments: &[SourceComment], source: &str
511512
})
512513
}
513514

515+
pub(crate) struct SuppressedNodes<'a>(FxHashSet<NodeRefEqualityKey<'a>>);
516+
517+
impl<'a> SuppressedNodes<'a> {
518+
pub(crate) fn from_comments(comments: &Comments<'a>, source: &'a str) -> Self {
519+
let map = &comments.clone().data.comments;
520+
Self(FxHashSet::from_iter(
521+
map.keys()
522+
.copied()
523+
.filter(|key| has_skip_comment(map.trailing(key), source)),
524+
))
525+
}
526+
527+
pub(crate) fn contains(&self, node: AnyNodeRef<'_>) -> bool {
528+
self.0.contains(&NodeRefEqualityKey::from_ref(node))
529+
}
530+
}
531+
514532
#[cfg(test)]
515533
mod tests {
516534
use insta::assert_debug_snapshot;

crates/ruff_python_formatter/src/context.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@ use ruff_python_ast::str::Quote;
77
use ruff_python_parser::Tokens;
88

99
use crate::PyFormatOptions;
10-
use crate::comments::{Comments, has_skip_comment};
10+
use crate::comments::{Comments, SuppressedNodes};
1111
use crate::other::interpolated_string::InterpolatedStringContext;
1212

1313
pub struct PyFormatContext<'a> {
1414
options: PyFormatOptions,
1515
contents: &'a str,
1616
comments: Comments<'a>,
17+
suppressed_nodes: SuppressedNodes<'a>,
1718
tokens: &'a Tokens,
1819
node_level: NodeLevel,
1920
indent_level: IndentLevel,
@@ -35,12 +36,14 @@ impl<'a> PyFormatContext<'a> {
3536
options: PyFormatOptions,
3637
contents: &'a str,
3738
comments: Comments<'a>,
39+
suppressed_nodes: SuppressedNodes<'a>,
3840
tokens: &'a Tokens,
3941
) -> Self {
4042
Self {
4143
options,
4244
contents,
4345
comments,
46+
suppressed_nodes,
4447
tokens,
4548
node_level: NodeLevel::TopLevel(TopLevelStatementPosition::Other),
4649
indent_level: IndentLevel::new(0),
@@ -116,7 +119,7 @@ impl<'a> PyFormatContext<'a> {
116119

117120
/// Returns `true` if node is suppressed via skip comment.
118121
pub(crate) fn is_suppressed(&self, node: AnyNodeRef<'_>) -> bool {
119-
has_skip_comment(self.comments().trailing(node), self.source())
122+
self.suppressed_nodes.contains(node)
120123
}
121124
}
122125

crates/ruff_python_formatter/src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use ruff_python_trivia::CommentRanges;
1414
use ruff_text_size::{Ranged, TextRange};
1515

1616
use crate::comments::{
17-
Comments, SourceComment, has_skip_comment, leading_comments, trailing_comments,
17+
Comments, SourceComment, SuppressedNodes, has_skip_comment, leading_comments, trailing_comments,
1818
};
1919
pub use crate::context::PyFormatContext;
2020
pub use crate::db::Db;
@@ -174,9 +174,10 @@ where
174174
{
175175
let source_code = SourceCode::new(source);
176176
let comments = Comments::from_ast(parsed.syntax(), source_code, comment_ranges);
177+
let suppressed_nodes = SuppressedNodes::from_comments(&comments, source);
177178

178179
let formatted = format!(
179-
PyFormatContext::new(options, source, comments, parsed.tokens()),
180+
PyFormatContext::new(options, source, comments, suppressed_nodes, parsed.tokens()),
180181
[parsed.syntax().format()]
181182
)?;
182183
formatted

crates/ruff_python_formatter/src/range.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use ruff_python_trivia::{
1212
};
1313
use ruff_text_size::{Ranged, TextLen, TextRange, TextSize};
1414

15-
use crate::comments::Comments;
15+
use crate::comments::{Comments, SuppressedNodes};
1616
use crate::context::{IndentLevel, NodeLevel};
1717
use crate::prelude::*;
1818
use crate::statement::suite::DocstringStmt;
@@ -77,11 +77,13 @@ pub fn format_range(
7777
let source_code = SourceCode::new(source);
7878
let comment_ranges = CommentRanges::from(parsed.tokens());
7979
let comments = Comments::from_ast(parsed.syntax(), source_code, &comment_ranges);
80+
let suppressed_nodes = SuppressedNodes::from_comments(&comments, source);
8081

8182
let mut context = PyFormatContext::new(
8283
options.with_source_map_generation(SourceMapGeneration::Enabled),
8384
source,
8485
comments,
86+
suppressed_nodes,
8587
parsed.tokens(),
8688
);
8789

crates/ruff_python_formatter/src/statement/suite.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -945,7 +945,7 @@ mod tests {
945945
use ruff_python_trivia::CommentRanges;
946946

947947
use crate::PyFormatOptions;
948-
use crate::comments::Comments;
948+
use crate::comments::{Comments, SuppressedNodes};
949949
use crate::prelude::*;
950950
use crate::statement::suite::SuiteKind;
951951

@@ -973,11 +973,14 @@ def trailing_func():
973973

974974
let parsed = parse_module(source).unwrap();
975975
let comment_ranges = CommentRanges::from(parsed.tokens());
976+
let comments = Comments::from_ranges(&comment_ranges);
977+
let suppressed_nodes = SuppressedNodes::from_comments(&comments, source);
976978

977979
let context = PyFormatContext::new(
978980
PyFormatOptions::default(),
979981
source,
980-
Comments::from_ranges(&comment_ranges),
982+
comments,
983+
suppressed_nodes,
981984
parsed.tokens(),
982985
);
983986

crates/ruff_python_formatter/src/string/docstring.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use {
2020
};
2121

2222
use super::NormalizedString;
23+
use crate::comments::SuppressedNodes;
2324
use crate::preview::is_no_chaperone_for_escaped_quote_in_triple_quoted_docstring_enabled;
2425
use crate::string::StringQuotes;
2526
use crate::{DocstringCodeLineWidth, FormatModuleError, prelude::*};
@@ -1585,8 +1586,9 @@ fn docstring_format_source(
15851586
let comment_ranges = CommentRanges::from(parsed.tokens());
15861587
let source_code = ruff_formatter::SourceCode::new(source);
15871588
let comments = crate::Comments::from_ast(parsed.syntax(), source_code, &comment_ranges);
1589+
let suppressed_nodes = SuppressedNodes::from_comments(&comments, source_code.as_str());
15881590

1589-
let ctx = PyFormatContext::new(options, source, comments, parsed.tokens())
1591+
let ctx = PyFormatContext::new(options, source, comments, suppressed_nodes, parsed.tokens())
15901592
.in_docstring(docstring_quote_style);
15911593
let formatted = crate::format!(ctx, [parsed.syntax().format()])?;
15921594
formatted

0 commit comments

Comments
 (0)