Cheap cloneable LineIndex (#3896)

This commit is contained in:
Micha Reiser 2023-04-11 09:33:40 +02:00 committed by GitHub
parent 9209e57c5a
commit 76c47a9a43
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 465 additions and 335 deletions

View file

@ -3,6 +3,7 @@ use rustpython_parser::ast::Location;
use ruff_python_ast::newlines::StrExt;
use ruff_python_ast::source_code::Locator;
use ruff_python_ast::types::Range;
use ruff_text_size::TextRange;
/// Return `true` if the given string is a radix literal (e.g., `0b101`).
pub fn is_radix_literal(content: &str) -> bool {
@ -55,7 +56,7 @@ pub fn expand_indented_block(
let mut nesting = 0;
let mut colon = None;
for (start, tok, _end) in rustpython_parser::lexer::lex_located(
&contents[start_index..end_index],
&contents[TextRange::new(start_index, end_index)],
rustpython_parser::Mode::Module,
location,
)
@ -80,7 +81,7 @@ pub fn expand_indented_block(
// From here, we have two options: simple statement or compound statement.
let indent = rustpython_parser::lexer::lex_located(
&contents[colon_index..end_index],
&contents[TextRange::new(colon_index, end_index)],
rustpython_parser::Mode::Module,
colon_location,
)
@ -97,7 +98,7 @@ pub fn expand_indented_block(
// Compound statement: from the colon to the end of the block.
let mut offset = 0;
for (index, line) in contents[end_index..]
for (index, line) in contents[usize::from(end_index)..]
.universal_newlines()
.skip(1)
.enumerate()

View file

@ -80,10 +80,7 @@ impl Format<ASTFormatContext<'_>> for Literal {
f.write_element(FormatElement::StaticTextSlice {
text,
range: TextRange::new(
start_index.try_into().unwrap(),
end_index.try_into().unwrap(),
),
range: TextRange::new(start_index, end_index),
})
}
}

View file

@ -3,7 +3,7 @@ use rustpython_parser::ast::Location;
use ruff_formatter::prelude::*;
use ruff_formatter::{write, Format};
use ruff_python_ast::types::Range;
use ruff_text_size::TextSize;
use ruff_text_size::{TextRange, TextSize};
use crate::context::ASTFormatContext;
use crate::format::builders::literal;
@ -20,9 +20,10 @@ impl Format<ASTFormatContext<'_>> for FloatAtom {
let start_index = locator.offset(self.range.location);
let end_index = locator.offset(self.range.end_location);
if let Some(dot_index) = contents[start_index..end_index].find('.') {
let integer = &contents[start_index..start_index + dot_index];
let fractional = &contents[start_index + dot_index + 1..end_index];
let content = &contents[TextRange::new(start_index, end_index)];
if let Some(dot_index) = content.find('.') {
let integer = &content[..dot_index];
let fractional = &content[dot_index + 1..];
if integer.is_empty() {
write!(f, [text("0")])?;
@ -80,11 +81,10 @@ impl Format<ASTFormatContext<'_>> for FloatLiteral {
let start_index = locator.offset(self.range.location);
let end_index = locator.offset(self.range.end_location);
let content = &contents[TextRange::new(start_index, end_index)];
// Scientific notation
if let Some(exponent_index) = contents[start_index..end_index]
.find('e')
.or_else(|| contents[start_index..end_index].find('E'))
{
if let Some(exponent_index) = content.find('e').or_else(|| content.find('E')) {
// Write the base.
write!(
f,
@ -100,7 +100,7 @@ impl Format<ASTFormatContext<'_>> for FloatLiteral {
write!(f, [text("e")])?;
// Write the exponent, omitting the sign if it's positive.
let plus = contents[start_index + exponent_index + 1..end_index].starts_with('+');
let plus = content[exponent_index + 1..].starts_with('+');
write!(
f,
[literal(Range::new(
@ -137,10 +137,11 @@ impl Format<ASTFormatContext<'_>> for IntLiteral {
let end_index = locator.offset(self.range.end_location);
for prefix in ["0b", "0B", "0o", "0O", "0x", "0X"] {
if contents[start_index..end_index].starts_with(prefix) {
let content = &contents[TextRange::new(start_index, end_index)];
if content.starts_with(prefix) {
// In each case, the prefix must be lowercase, while the suffix must be uppercase.
let prefix = &contents[start_index..start_index + prefix.len()];
let suffix = &contents[start_index + prefix.len()..end_index];
let prefix = &content[..prefix.len()];
let suffix = &content[prefix.len()..];
if prefix.bytes().any(|b| b.is_ascii_uppercase())
|| suffix.bytes().any(|b| b.is_ascii_lowercase())
@ -185,9 +186,11 @@ impl Format<ASTFormatContext<'_>> for ComplexLiteral {
let start_index = locator.offset(self.range.location);
let end_index = locator.offset(self.range.end_location);
if contents[start_index..end_index].ends_with('j') {
let content = &contents[TextRange::new(start_index, end_index)];
if content.ends_with('j') {
write!(f, [literal(self.range)])?;
} else if contents[start_index..end_index].ends_with('J') {
} else if content.ends_with('J') {
write!(
f,
[literal(Range::new(

View file

@ -4,7 +4,7 @@ use ruff_formatter::prelude::*;
use ruff_formatter::{write, Format};
use ruff_python_ast::str::{leading_quote, trailing_quote};
use ruff_python_ast::types::Range;
use ruff_text_size::TextSize;
use ruff_text_size::{TextRange, TextSize};
use crate::context::ASTFormatContext;
use crate::cst::Expr;
@ -22,7 +22,7 @@ impl Format<ASTFormatContext<'_>> for StringLiteralPart {
let end_index = locator.offset(self.range.end_location);
// Extract leading and trailing quotes.
let contents = &contents[start_index..end_index];
let contents = &contents[TextRange::new(start_index, end_index)];
let leading_quote = leading_quote(contents).unwrap();
let trailing_quote = trailing_quote(contents).unwrap();
let body = &contents[leading_quote.len()..contents.len() - trailing_quote.len()];