diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/lib.rs | 25 |
1 files changed, 19 insertions, 6 deletions
@@ -16,12 +16,6 @@ use std::{convert::From, str::FromStr}; | |||
16 | #[global_allocator] | 16 | #[global_allocator] |
17 | static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT; | 17 | static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT; |
18 | 18 | ||
19 | #[wasm_bindgen] | ||
20 | pub 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 | ||
130 | impl 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] |
137 | impl TextRange { | 140 | impl 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 | } |