see if text ranges work instead

This commit is contained in:
dylwil3 2025-11-17 17:30:24 -06:00
parent 9b5021f22e
commit d3b663c932
6 changed files with 51 additions and 21 deletions

View File

@ -99,7 +99,6 @@ use ruff_formatter::{SourceCode, SourceCodeSlice};
use ruff_python_ast::AnyNodeRef; use ruff_python_ast::AnyNodeRef;
use ruff_python_trivia::{CommentLinePosition, CommentRanges, SuppressionKind}; use ruff_python_trivia::{CommentLinePosition, CommentRanges, SuppressionKind};
use ruff_text_size::{Ranged, TextRange}; use ruff_text_size::{Ranged, TextRange};
use rustc_hash::FxHashSet;
pub(crate) use visitor::collect_comments; pub(crate) use visitor::collect_comments;
use crate::comments::debug::{DebugComment, DebugComments}; use crate::comments::debug::{DebugComment, DebugComments};
@ -512,21 +511,51 @@ pub(crate) fn has_skip_comment(trailing_comments: &[SourceComment], source: &str
}) })
} }
pub(crate) struct SuppressedNodes<'a>(FxHashSet<NodeRefEqualityKey<'a>>); pub(crate) struct SuppressedNodeRanges(Vec<TextRange>);
impl<'a> SuppressedNodes<'a> { impl<'a> SuppressedNodeRanges {
pub(crate) fn from_comments(comments: &Comments<'a>, source: &'a str) -> Self { pub(crate) fn from_comments(comments: &Comments<'a>, source: &'a str) -> Self {
let map = &comments.clone().data.comments; let map = &comments.clone().data.comments;
Self(
map.keys() let mut ranges = map
.copied() .keys()
.filter(|key| has_skip_comment(map.trailing(key), source)) .copied()
.collect(), .filter_map(|key| suppressed_range(key.node(), map.trailing(&key), source))
) .collect::<Vec<_>>();
ranges.sort_by_key(ruff_text_size::Ranged::start);
Self(ranges)
} }
pub(crate) fn contains(&self, node: AnyNodeRef<'_>) -> bool { pub(crate) fn contains(&self, node: AnyNodeRef<'_>) -> bool {
self.0.contains(&NodeRefEqualityKey::from_ref(node)) let target = node.range();
self.0
.binary_search_by(|range| {
if range.eq(&target) {
std::cmp::Ordering::Equal
} else if range.start() < target.start() {
std::cmp::Ordering::Less
} else {
std::cmp::Ordering::Greater
}
})
.is_ok()
}
}
fn suppressed_range<'a>(
node: AnyNodeRef<'a>,
trailing_comments: &[SourceComment],
source: &'a str,
) -> Option<TextRange> {
if has_skip_comment(trailing_comments, source) {
let end = node.end();
let start = node.start();
Some(TextRange::new(start, end))
} else {
None
} }
} }

View File

@ -7,14 +7,14 @@ use ruff_python_ast::str::Quote;
use ruff_python_parser::Tokens; use ruff_python_parser::Tokens;
use crate::PyFormatOptions; use crate::PyFormatOptions;
use crate::comments::{Comments, SuppressedNodes}; use crate::comments::{Comments, SuppressedNodeRanges};
use crate::other::interpolated_string::InterpolatedStringContext; use crate::other::interpolated_string::InterpolatedStringContext;
pub struct PyFormatContext<'a> { pub struct PyFormatContext<'a> {
options: PyFormatOptions, options: PyFormatOptions,
contents: &'a str, contents: &'a str,
comments: Comments<'a>, comments: Comments<'a>,
suppressed_nodes: SuppressedNodes<'a>, suppressed_nodes: SuppressedNodeRanges,
tokens: &'a Tokens, tokens: &'a Tokens,
node_level: NodeLevel, node_level: NodeLevel,
indent_level: IndentLevel, indent_level: IndentLevel,
@ -36,7 +36,7 @@ impl<'a> PyFormatContext<'a> {
options: PyFormatOptions, options: PyFormatOptions,
contents: &'a str, contents: &'a str,
comments: Comments<'a>, comments: Comments<'a>,
suppressed_nodes: SuppressedNodes<'a>, suppressed_nodes: SuppressedNodeRanges,
tokens: &'a Tokens, tokens: &'a Tokens,
) -> Self { ) -> Self {
Self { Self {

View File

@ -14,7 +14,8 @@ use ruff_python_trivia::CommentRanges;
use ruff_text_size::{Ranged, TextRange}; use ruff_text_size::{Ranged, TextRange};
use crate::comments::{ use crate::comments::{
Comments, SourceComment, SuppressedNodes, has_skip_comment, leading_comments, trailing_comments, Comments, SourceComment, SuppressedNodeRanges, has_skip_comment, leading_comments,
trailing_comments,
}; };
pub use crate::context::PyFormatContext; pub use crate::context::PyFormatContext;
pub use crate::db::Db; pub use crate::db::Db;
@ -174,7 +175,7 @@ where
{ {
let source_code = SourceCode::new(source); let source_code = SourceCode::new(source);
let comments = Comments::from_ast(parsed.syntax(), source_code, comment_ranges); let comments = Comments::from_ast(parsed.syntax(), source_code, comment_ranges);
let suppressed_nodes = SuppressedNodes::from_comments(&comments, source); let suppressed_nodes = SuppressedNodeRanges::from_comments(&comments, source);
let formatted = format!( let formatted = format!(
PyFormatContext::new(options, source, comments, suppressed_nodes, parsed.tokens()), PyFormatContext::new(options, source, comments, suppressed_nodes, parsed.tokens()),

View File

@ -12,7 +12,7 @@ use ruff_python_trivia::{
}; };
use ruff_text_size::{Ranged, TextLen, TextRange, TextSize}; use ruff_text_size::{Ranged, TextLen, TextRange, TextSize};
use crate::comments::{Comments, SuppressedNodes}; use crate::comments::{Comments, SuppressedNodeRanges};
use crate::context::{IndentLevel, NodeLevel}; use crate::context::{IndentLevel, NodeLevel};
use crate::prelude::*; use crate::prelude::*;
use crate::statement::suite::DocstringStmt; use crate::statement::suite::DocstringStmt;
@ -77,7 +77,7 @@ pub fn format_range(
let source_code = SourceCode::new(source); let source_code = SourceCode::new(source);
let comment_ranges = CommentRanges::from(parsed.tokens()); let comment_ranges = CommentRanges::from(parsed.tokens());
let comments = Comments::from_ast(parsed.syntax(), source_code, &comment_ranges); let comments = Comments::from_ast(parsed.syntax(), source_code, &comment_ranges);
let suppressed_nodes = SuppressedNodes::from_comments(&comments, source); let suppressed_nodes = SuppressedNodeRanges::from_comments(&comments, source);
let mut context = PyFormatContext::new( let mut context = PyFormatContext::new(
options.with_source_map_generation(SourceMapGeneration::Enabled), options.with_source_map_generation(SourceMapGeneration::Enabled),

View File

@ -945,7 +945,7 @@ mod tests {
use ruff_python_trivia::CommentRanges; use ruff_python_trivia::CommentRanges;
use crate::PyFormatOptions; use crate::PyFormatOptions;
use crate::comments::{Comments, SuppressedNodes}; use crate::comments::{Comments, SuppressedNodeRanges};
use crate::prelude::*; use crate::prelude::*;
use crate::statement::suite::SuiteKind; use crate::statement::suite::SuiteKind;
@ -974,7 +974,7 @@ def trailing_func():
let parsed = parse_module(source).unwrap(); let parsed = parse_module(source).unwrap();
let comment_ranges = CommentRanges::from(parsed.tokens()); let comment_ranges = CommentRanges::from(parsed.tokens());
let comments = Comments::from_ranges(&comment_ranges); let comments = Comments::from_ranges(&comment_ranges);
let suppressed_nodes = SuppressedNodes::from_comments(&comments, source); let suppressed_nodes = SuppressedNodeRanges::from_comments(&comments, source);
let context = PyFormatContext::new( let context = PyFormatContext::new(
PyFormatOptions::default(), PyFormatOptions::default(),

View File

@ -20,7 +20,7 @@ use {
}; };
use super::NormalizedString; use super::NormalizedString;
use crate::comments::SuppressedNodes; use crate::comments::SuppressedNodeRanges;
use crate::preview::is_no_chaperone_for_escaped_quote_in_triple_quoted_docstring_enabled; use crate::preview::is_no_chaperone_for_escaped_quote_in_triple_quoted_docstring_enabled;
use crate::string::StringQuotes; use crate::string::StringQuotes;
use crate::{DocstringCodeLineWidth, FormatModuleError, prelude::*}; use crate::{DocstringCodeLineWidth, FormatModuleError, prelude::*};
@ -1586,7 +1586,7 @@ fn docstring_format_source(
let comment_ranges = CommentRanges::from(parsed.tokens()); let comment_ranges = CommentRanges::from(parsed.tokens());
let source_code = ruff_formatter::SourceCode::new(source); let source_code = ruff_formatter::SourceCode::new(source);
let comments = crate::Comments::from_ast(parsed.syntax(), source_code, &comment_ranges); let comments = crate::Comments::from_ast(parsed.syntax(), source_code, &comment_ranges);
let suppressed_nodes = SuppressedNodes::from_comments(&comments, source_code.as_str()); let suppressed_nodes = SuppressedNodeRanges::from_comments(&comments, source_code.as_str());
let ctx = PyFormatContext::new(options, source, comments, suppressed_nodes, parsed.tokens()) let ctx = PyFormatContext::new(options, source, comments, suppressed_nodes, parsed.tokens())
.in_docstring(docstring_quote_style); .in_docstring(docstring_quote_style);