@@ -352,7 +352,7 @@ def visitModule(self, mod, depth):
352352 depth ,
353353 )
354354 self .emit (
355- "Ok(Located { custom: folder.map_user(node.custom)?, location : node.location, end_location: node.end_location , node: f(folder, node.node)? })" ,
355+ "Ok(Located { custom: folder.map_user(node.custom)?, range : node.range , node: f(folder, node.node)? })" ,
356356 depth + 1 ,
357357 )
358358 self .emit ("}" , depth )
@@ -718,7 +718,7 @@ def write_ast_def(mod, typeinfo, f):
718718 #![allow(clippy::derive_partial_eq_without_eq)]
719719
720720 pub use crate::constant::*;
721- pub use crate::Location ;
721+ pub use ruff_text_size::{TextSize, TextRange} ;
722722
723723 type Ident = String;
724724 \n
@@ -730,26 +730,54 @@ def write_ast_def(mod, typeinfo, f):
730730 textwrap .dedent (
731731 """
732732 pub struct Located<T, U = ()> {
733- pub location: Location,
734- pub end_location: Option<Location>,
733+ pub range: TextRange,
735734 pub custom: U,
736735 pub node: T,
737736 }
738737
739738 impl<T> Located<T> {
740- pub fn new(location: Location, end_location: Location , node: T) -> Self {
741- Self { location, end_location: Some(end_location ), custom: (), node }
739+ pub fn new(start: TextSize, end: TextSize , node: T) -> Self {
740+ Self { range: TextRange::new(start, end ), custom: (), node }
742741 }
743742
744- pub const fn start(&self) -> Location {
745- self.location
743+ /// Creates a new node that spans the position specified by `range`.
744+ pub fn with_range(node: T, range: TextRange) -> Self {
745+ Self {
746+ range,
747+ custom: (),
748+ node,
749+ }
750+ }
751+
752+ /// Returns the absolute start position of the node from the beginning of the document.
753+ #[inline]
754+ pub const fn start(&self) -> TextSize {
755+ self.range.start()
756+ }
757+
758+ /// Returns the node
759+ #[inline]
760+ pub fn node(&self) -> &T {
761+ &self.node
762+ }
763+
764+ /// Consumes self and returns the node.
765+ #[inline]
766+ pub fn into_node(self) -> T {
767+ self.node
768+ }
769+
770+ /// Returns the `range` of the node. The range offsets are absolute to the start of the document.
771+ #[inline]
772+ pub const fn range(&self) -> TextRange {
773+ self.range
774+ }
775+
776+ /// Returns the absolute position at which the node ends in the source document.
777+ #[inline]
778+ pub const fn end(&self) -> TextSize {
779+ self.range.end()
746780 }
747-
748- /// Returns the node's [`end_location`](Located::end_location) or [`location`](Located::start) if
749- /// [`end_location`](Located::end_location) is `None`.
750- pub fn end(&self) -> Location {
751- self.end_location.unwrap_or(self.location)
752- }
753781 }
754782
755783 impl<T, U> std::ops::Deref for Located<T, U> {
0 commit comments