diff options
| author | Laurenz <laurmaedje@gmail.com> | 2022-01-04 00:27:05 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-01-04 00:27:05 +0100 |
| commit | 4c81a5d43eabd959dbb500a8076f99f21bd037bd (patch) | |
| tree | 03349230f74786c7128876889c07a31a4932f108 /src/parse/scanner.rs | |
| parent | 52761a3baa901865b1fc42366017740cfa7eb566 (diff) | |
| parent | c994cfa7d814e3909682b19322867ed5c676c453 (diff) | |
Merge pull request #48 from typst/parser-incr
Incremental parsing
Diffstat (limited to 'src/parse/scanner.rs')
| -rw-r--r-- | src/parse/scanner.rs | 23 |
1 files changed, 19 insertions, 4 deletions
diff --git a/src/parse/scanner.rs b/src/parse/scanner.rs index c735be40..6db89132 100644 --- a/src/parse/scanner.rs +++ b/src/parse/scanner.rs @@ -162,11 +162,26 @@ impl<'s> Scanner<'s> { /// The column index of a given index in the source string. #[inline] pub fn column(&self, index: usize) -> usize { - self.src[.. index] - .chars() + self.column_offset(index, 0) + } + + /// The column index of a given index in the source string when an offset is + /// applied to the first line of the string. + #[inline] + pub fn column_offset(&self, index: usize, offset: usize) -> usize { + let mut apply_offset = false; + let res = self.src[.. index] + .char_indices() .rev() - .take_while(|&c| !is_newline(c)) - .count() + .take_while(|&(_, c)| !is_newline(c)) + .inspect(|&(i, _)| { + if i == 0 { + apply_offset = true + } + }) + .count(); + + if apply_offset { res + offset } else { res } } } |
