vize_patina 0.42.0

Patina - The quality checker for Vize code linting
Documentation
use super::{errors::compact_error, CorsaTypeAwareSession, TypeProbe};
use corsa::{api::TypeProbeOptions, runtime::block_on};
use vize_carton::{String, ToCompactString};

impl CorsaTypeAwareSession {
    pub(in crate::linter) fn probe_type_at_offset(
        &self,
        generated_source: &str,
        generated_offset: u32,
        load_property_types: bool,
        load_signatures: bool,
    ) -> Result<Option<TypeProbe>, String> {
        let utf16_offset = byte_offset_to_utf16_offset(generated_source, generated_offset);
        block_on(self.session.probe_type_at_position(
            self.virtual_file_wire.as_str(),
            utf16_offset,
            TypeProbeOptions {
                load_property_types,
                load_signatures,
            },
        ))
        .map_err(|error| {
            compact_error(
                "Failed to query checker type probe",
                error.to_compact_string().as_str(),
            )
        })
    }
}

pub(super) fn byte_offset_to_utf16_offset(source: &str, byte_offset: u32) -> u32 {
    let mut clamped = usize::min(byte_offset as usize, source.len());
    while clamped > 0 && !source.is_char_boundary(clamped) {
        clamped -= 1;
    }
    source[..clamped].encode_utf16().count() as u32
}