Replace row/column based Location with byte-offsets. (#3931)

This commit is contained in:
Micha Reiser 2023-04-26 20:11:02 +02:00 committed by GitHub
parent ee91598835
commit cab65b25da
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
418 changed files with 6203 additions and 7040 deletions

View file

@ -1,11 +1,11 @@
use anyhow::Result;
use ruff_text_size::{TextLen, TextRange};
use rustpython_parser as parser;
use rustpython_parser::ast::{Expr, Location};
use rustpython_parser::ast::Expr;
use crate::relocate::relocate_expr;
use crate::source_code::Locator;
use crate::str;
use crate::types::Range;
#[derive(is_macro::Is, Copy, Clone)]
pub enum AnnotationKind {
@ -24,10 +24,11 @@ pub enum AnnotationKind {
/// Parse a type annotation from a string.
pub fn parse_type_annotation(
value: &str,
range: Range,
range: TextRange,
locator: &Locator,
) -> Result<(Expr, AnnotationKind)> {
let expression = locator.slice(range);
let expression = &locator.contents()[range];
if str::raw_contents(expression).map_or(false, |body| body == value) {
// The annotation is considered "simple" if and only if the raw representation (e.g.,
// `List[int]` within "List[int]") exactly matches the parsed representation. This
@ -37,10 +38,7 @@ pub fn parse_type_annotation(
let expr = parser::parse_expression_located(
value,
"<filename>",
Location::new(
range.location.row(),
range.location.column() + leading_quote.len(),
),
range.start() + leading_quote.text_len(),
)?;
Ok((expr, AnnotationKind::Simple))
} else {