Memoize text width (#6552)

This commit is contained in:
Micha Reiser 2023-09-06 09:10:13 +02:00 committed by GitHub
parent fa6bff0078
commit 5f59101811
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 213 additions and 184 deletions

View file

@ -1,7 +1,5 @@
use std::borrow::Cow;
use unicode_width::UnicodeWidthChar;
use ruff_formatter::{format_args, write, FormatError, FormatOptions, SourceCode};
use ruff_python_ast::node::{AnyNodeRef, AstNode};
use ruff_python_trivia::{lines_after, lines_after_ignoring_trivia, lines_before};
@ -377,16 +375,12 @@ impl Format<PyFormatContext<'_>> for FormatTrailingEndOfLineComment<'_> {
0
} else {
// Start with 2 because of the two leading spaces.
let mut width = 2;
// SAFETY: The formatted file is <= 4GB, and each comment should as well.
#[allow(clippy::cast_possible_truncation)]
for c in normalized_comment.chars() {
width += match c {
'\t' => f.options().tab_width().value(),
c => c.width().unwrap_or(0) as u32,
}
}
let width = 2u32.saturating_add(
TextWidth::from_text(&normalized_comment, f.options().tab_width())
.width()
.expect("Expected comment not to contain any newlines")
.value(),
);
width
};
@ -430,11 +424,9 @@ pub(crate) struct FormatNormalizedComment<'a> {
impl Format<PyFormatContext<'_>> for FormatNormalizedComment<'_> {
fn fmt(&self, f: &mut Formatter<PyFormatContext>) -> FormatResult<()> {
match self.comment {
Cow::Borrowed(borrowed) => source_text_slice(
TextRange::at(self.range.start(), borrowed.text_len()),
ContainsNewlines::No,
)
.fmt(f),
Cow::Borrowed(borrowed) => {
source_text_slice(TextRange::at(self.range.start(), borrowed.text_len())).fmt(f)
}
Cow::Owned(ref owned) => {
write!(