NativePositionMapper

Struct NativePositionMapper 

Source
pub struct NativePositionMapper { /* private fields */ }
Expand description

Bidirectional position mapper for translating between original and expanded source positions.

This mapper enables IDE features like error reporting, go-to-definition, and hover to work correctly with macro-expanded code by translating positions between the original source (what the user wrote) and the expanded source (what the compiler sees).

§Performance

Position lookups use binary search for O(log n) complexity, where n is the number of mapping segments. This is critical for responsive IDE interactions.

§Example

const mapper = new PositionMapper(sourceMapping);

// Convert original position to expanded
const expandedPos = mapper.original_to_expanded(42);

// Convert expanded position back to original (if not in generated code)
const originalPos = mapper.expanded_to_original(100);

// Check if a position is in macro-generated code
if (mapper.is_in_generated(pos)) {
    const macro = mapper.generated_by(pos); // e.g., "Debug"
}

Implementations§

Source§

impl NativePositionMapper

Source

pub fn new(mapping: SourceMappingResult) -> Self

Creates a new position mapper from source mapping data.

§Arguments
  • mapping - The source mapping result from macro expansion
§Returns

A new NativePositionMapper ready for position translation.

Source

pub fn is_empty(&self) -> bool

Checks if this mapper has no mapping data.

An empty mapper indicates no transformations occurred, so position translation is an identity operation.

§Returns

true if there are no segments and no generated regions.

Source

pub fn original_to_expanded(&self, pos: u32) -> u32

Converts a position in the original source to the corresponding position in expanded source.

Uses binary search for O(log n) lookup performance.

§Arguments
  • pos - Byte offset in the original source
§Returns

The corresponding byte offset in the expanded source. If the position falls in a gap between segments, returns the position unchanged. If after the last segment, extrapolates based on the delta.

§Algorithm
  1. Binary search to find the segment containing or after pos
  2. If inside a segment, compute offset within segment and translate
  3. If after all segments, extrapolate from the last segment
  4. Otherwise, return position unchanged (gap or before first segment)
Source

pub fn expanded_to_original(&self, pos: u32) -> Option<u32>

Converts a position in the expanded source back to the original source position.

Returns None if the position is inside macro-generated code that has no corresponding location in the original source.

§Arguments
  • pos - Byte offset in the expanded source
§Returns

Some(original_pos) if the position maps to original code, None if the position is in macro-generated code.

Source

pub fn generated_by(&self, pos: u32) -> Option<String>

Returns the name of the macro that generated code at the given position.

§Arguments
  • pos - Byte offset in the expanded source
§Returns

Some(macro_name) if the position is inside generated code (e.g., “Debug”), None if the position is in original (non-generated) code.

Source

pub fn map_span_to_original( &self, start: u32, length: u32, ) -> Option<SpanResult>

Maps a span (start + length) from expanded source to original source.

§Arguments
  • start - Start byte offset in expanded source
  • length - Length of the span in bytes
§Returns

Some(SpanResult) with the mapped span in original source, None if either endpoint is in generated code.

Source

pub fn map_span_to_expanded(&self, start: u32, length: u32) -> SpanResult

Maps a span (start + length) from original source to expanded source.

This always succeeds since every original position has an expanded equivalent.

§Arguments
  • start - Start byte offset in original source
  • length - Length of the span in bytes
§Returns

A SpanResult with the mapped span in expanded source.

Source

pub fn is_in_generated(&self, pos: u32) -> bool

Checks if a position is inside macro-generated code.

§Arguments
  • pos - Byte offset in the expanded source
§Returns

true if the position is inside a generated region, false otherwise.

Trait Implementations§

Source§

impl FromNapiMutRef for NativePositionMapper

Source§

unsafe fn from_napi_mut_ref( env: napi_env, napi_val: napi_value, ) -> Result<&'static mut Self>

Safety Read more
Source§

impl FromNapiRef for NativePositionMapper

Source§

unsafe fn from_napi_ref( env: napi_env, napi_val: napi_value, ) -> Result<&'static Self>

Safety Read more
Source§

impl JavaScriptClassExt for NativePositionMapper

Source§

fn into_instance<'scope>( self, env: &'scope Env, ) -> Result<ClassInstance<'scope, Self>>

Source§

fn into_reference(self, env: Env) -> Result<Reference<Self>>

Source§

fn instance_of<'env, V: JsValue<'env>>(env: &Env, value: &V) -> Result<bool>

Source§

impl ObjectFinalize for NativePositionMapper

Source§

fn finalize(self, env: Env) -> Result<(), Error>

Source§

impl ToNapiValue for NativePositionMapper

Source§

impl TypeName for &NativePositionMapper

Source§

impl TypeName for &mut NativePositionMapper

Source§

impl TypeName for NativePositionMapper

Source§

impl ValidateNapiValue for &NativePositionMapper

Source§

unsafe fn validate(env: napi_env, napi_val: napi_value) -> Result<napi_value>

Safety Read more
Source§

impl ValidateNapiValue for &mut NativePositionMapper

Source§

unsafe fn validate(env: napi_env, napi_val: napi_value) -> Result<napi_value>

Safety Read more

Auto Trait Implementations§

Blanket Implementations§

§

impl<T> Any for T
where T: 'static + ?Sized,

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<T> Borrow<T> for T
where T: ?Sized,

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

impl<T> BorrowMut<T> for T
where T: ?Sized,

§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
§

impl<T, U> Into<U> for T
where U: From<T>,

§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> JsValuesTupleIntoVec for T
where T: ToNapiValue,

§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> Send for T
where T: ?Sized,

Source§

impl<T> Sync for T
where T: ?Sized,