Replace row/column based Location with byte-offsets.

This commit is contained in:
Micha Reiser 2023-05-06 15:54:14 +09:00 committed by Jeong YunWon
parent 7b8844bd3e
commit 58c35ab458
131 changed files with 12120 additions and 23198 deletions

View file

@ -352,7 +352,7 @@ class FoldImplVisitor(TypeInfoEmitVisitor):
depth,
)
self.emit(
"Ok(Located { custom: folder.map_user(node.custom)?, location: node.location, end_location: node.end_location, node: f(folder, node.node)? })",
"Ok(Located { custom: folder.map_user(node.custom)?, range: node.range, node: f(folder, node.node)? })",
depth + 1,
)
self.emit("}", depth)
@ -718,7 +718,7 @@ def write_ast_def(mod, typeinfo, f):
#![allow(clippy::derive_partial_eq_without_eq)]
pub use crate::constant::*;
pub use crate::Location;
pub use ruff_text_size::{TextSize, TextRange};
type Ident = String;
\n
@ -730,26 +730,54 @@ def write_ast_def(mod, typeinfo, f):
textwrap.dedent(
"""
pub struct Located<T, U = ()> {
pub location: Location,
pub end_location: Option<Location>,
pub range: TextRange,
pub custom: U,
pub node: T,
}
impl<T> Located<T> {
pub fn new(location: Location, end_location: Location, node: T) -> Self {
Self { location, end_location: Some(end_location), custom: (), node }
pub fn new(start: TextSize, end: TextSize, node: T) -> Self {
Self { range: TextRange::new(start, end), custom: (), node }
}
pub const fn start(&self) -> Location {
self.location
/// Creates a new node that spans the position specified by `range`.
pub fn with_range(node: T, range: TextRange) -> Self {
Self {
range,
custom: (),
node,
}
}
/// Returns the absolute start position of the node from the beginning of the document.
#[inline]
pub const fn start(&self) -> TextSize {
self.range.start()
}
/// Returns the node
#[inline]
pub fn node(&self) -> &T {
&self.node
}
/// Consumes self and returns the node.
#[inline]
pub fn into_node(self) -> T {
self.node
}
/// Returns the `range` of the node. The range offsets are absolute to the start of the document.
#[inline]
pub const fn range(&self) -> TextRange {
self.range
}
/// Returns the absolute position at which the node ends in the source document.
#[inline]
pub const fn end(&self) -> TextSize {
self.range.end()
}
/// Returns the node's [`end_location`](Located::end_location) or [`location`](Located::start) if
/// [`end_location`](Located::end_location) is `None`.
pub fn end(&self) -> Location {
self.end_location.unwrap_or(self.location)
}
}
impl<T, U> std::ops::Deref for Located<T, U> {