aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAkshay <[email protected]>2021-06-13 07:00:49 +0100
committerAkshay <[email protected]>2021-06-13 07:00:49 +0100
commita0d2a9f151302fa1d2d78bb655db280e5cd458bb (patch)
tree3e8a3915b37f188b4ebfed8e8c111f90f3e34cbb /src
parent422872e00e86eea9d45783d12645717f1cfd0344 (diff)
add conversions to (line, col) for TextRange
Diffstat (limited to 'src')
-rw-r--r--src/lib.rs25
1 files 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};
16#[global_allocator] 16#[global_allocator]
17static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT; 17static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
18 18
19#[wasm_bindgen]
20pub fn put_cst(source: &str) -> String {
21 let source_file = SourceFile::parse(source);
22 source_file.debug_dump()
23}
24
25// wrapper type to pass syntax elements to JS 19// wrapper type to pass syntax elements to JS
26#[wasm_bindgen] 20#[wasm_bindgen]
27#[derive(Debug, Clone)] 21#[derive(Debug, Clone)]
@@ -133,14 +127,33 @@ impl From<(u32, u32)> for TextRange {
133 } 127 }
134} 128}
135 129
130impl TextRange {
131 pub fn to_line_col(&self, source: &str) -> (u32, u32) {
132 let end = self.end() as usize;
133 let line = &source[..end].chars().filter(|&c| c == '\n').count() + 1;
134 let col = &source[..end].rfind('\n').map(|c| end - c).unwrap_or(end);
135 (line as u32, *col as u32)
136 }
137}
138
136#[wasm_bindgen] 139#[wasm_bindgen]
137impl TextRange { 140impl TextRange {
138 pub fn start(&self) -> u32 { 141 pub fn start(&self) -> u32 {
139 self.start 142 self.start
140 } 143 }
144
141 pub fn end(&self) -> u32 { 145 pub fn end(&self) -> u32 {
142 self.end 146 self.end
143 } 147 }
148
149 pub fn line(&self, source: &str) -> u32 {
150 self.to_line_col(source).0
151 }
152
153 pub fn col(&self, source: &str) -> u32 {
154 self.to_line_col(source).1
155 }
156
144 pub fn to_string(&self) -> String { 157 pub fn to_string(&self) -> String {
145 format!("{}..{}", self.start, self.end) 158 format!("{}..{}", self.start, self.end)
146 } 159 }