From a0d2a9f151302fa1d2d78bb655db280e5cd458bb Mon Sep 17 00:00:00 2001 From: Akshay Date: Sun, 13 Jun 2021 11:30:49 +0530 Subject: add conversions to (line, col) for TextRange --- src/lib.rs | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 7385d66..9593d68 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -16,12 +16,6 @@ use std::{convert::From, str::FromStr}; #[global_allocator] static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT; -#[wasm_bindgen] -pub fn put_cst(source: &str) -> String { - let source_file = SourceFile::parse(source); - source_file.debug_dump() -} - // wrapper type to pass syntax elements to JS #[wasm_bindgen] #[derive(Debug, Clone)] @@ -133,14 +127,33 @@ impl From<(u32, u32)> for TextRange { } } +impl TextRange { + pub fn to_line_col(&self, source: &str) -> (u32, u32) { + let end = self.end() as usize; + let line = &source[..end].chars().filter(|&c| c == '\n').count() + 1; + let col = &source[..end].rfind('\n').map(|c| end - c).unwrap_or(end); + (line as u32, *col as u32) + } +} + #[wasm_bindgen] impl TextRange { pub fn start(&self) -> u32 { self.start } + pub fn end(&self) -> u32 { self.end } + + pub fn line(&self, source: &str) -> u32 { + self.to_line_col(source).0 + } + + pub fn col(&self, source: &str) -> u32 { + self.to_line_col(source).1 + } + pub fn to_string(&self) -> String { format!("{}..{}", self.start, self.end) } -- cgit v1.2.3