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
impl NativePositionMapper
Sourcepub fn new(mapping: SourceMappingResult) -> Self
pub fn new(mapping: SourceMappingResult) -> Self
Sourcepub fn is_empty(&self) -> bool
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.
Sourcepub fn original_to_expanded(&self, pos: u32) -> u32
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
- Binary search to find the segment containing or after
pos - If inside a segment, compute offset within segment and translate
- If after all segments, extrapolate from the last segment
- Otherwise, return position unchanged (gap or before first segment)
Sourcepub fn expanded_to_original(&self, pos: u32) -> Option<u32>
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.
Sourcepub fn generated_by(&self, pos: u32) -> Option<String>
pub fn generated_by(&self, pos: u32) -> Option<String>
Sourcepub fn map_span_to_original(
&self,
start: u32,
length: u32,
) -> Option<SpanResult>
pub fn map_span_to_original( &self, start: u32, length: u32, ) -> Option<SpanResult>
Sourcepub fn map_span_to_expanded(&self, start: u32, length: u32) -> SpanResult
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 sourcelength- Length of the span in bytes
§Returns
A SpanResult with the mapped span in expanded source.
Trait Implementations§
Source§impl FromNapiMutRef for NativePositionMapper
impl FromNapiMutRef for NativePositionMapper
Source§unsafe fn from_napi_mut_ref(
env: napi_env,
napi_val: napi_value,
) -> Result<&'static mut Self>
unsafe fn from_napi_mut_ref( env: napi_env, napi_val: napi_value, ) -> Result<&'static mut Self>
Source§impl FromNapiRef for NativePositionMapper
impl FromNapiRef for NativePositionMapper
Source§unsafe fn from_napi_ref(
env: napi_env,
napi_val: napi_value,
) -> Result<&'static Self>
unsafe fn from_napi_ref( env: napi_env, napi_val: napi_value, ) -> Result<&'static Self>
Source§impl JavaScriptClassExt for NativePositionMapper
impl JavaScriptClassExt for NativePositionMapper
fn into_instance<'scope>( self, env: &'scope Env, ) -> Result<ClassInstance<'scope, Self>>
fn into_reference(self, env: Env) -> Result<Reference<Self>>
fn instance_of<'env, V: JsValue<'env>>(env: &Env, value: &V) -> Result<bool>
Source§impl ToNapiValue for NativePositionMapper
impl ToNapiValue for NativePositionMapper
Source§unsafe fn to_napi_value(
env: napi_env,
val: NativePositionMapper,
) -> Result<napi_value>
unsafe fn to_napi_value( env: napi_env, val: NativePositionMapper, ) -> Result<napi_value>
fn into_unknown(self, env: &Env) -> Result<Unknown<'_>, Error>
Source§impl TypeName for &NativePositionMapper
impl TypeName for &NativePositionMapper
Source§impl TypeName for &mut NativePositionMapper
impl TypeName for &mut NativePositionMapper
Source§impl TypeName for NativePositionMapper
impl TypeName for NativePositionMapper
Source§impl ValidateNapiValue for &NativePositionMapper
impl ValidateNapiValue for &NativePositionMapper
Source§unsafe fn validate(env: napi_env, napi_val: napi_value) -> Result<napi_value>
unsafe fn validate(env: napi_env, napi_val: napi_value) -> Result<napi_value>
Source§impl ValidateNapiValue for &mut NativePositionMapper
impl ValidateNapiValue for &mut NativePositionMapper
Source§unsafe fn validate(env: napi_env, napi_val: napi_value) -> Result<napi_value>
unsafe fn validate(env: napi_env, napi_val: napi_value) -> Result<napi_value>
Auto Trait Implementations§
impl Freeze for NativePositionMapper
impl RefUnwindSafe for NativePositionMapper
impl Send for NativePositionMapper
impl Sync for NativePositionMapper
impl Unpin for NativePositionMapper
impl UnwindSafe for NativePositionMapper
Blanket Implementations§
§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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