Skip to content

Commit

Permalink
O(n) translation to utf16 offsets
Browse files Browse the repository at this point in the history
  • Loading branch information
kritzcreek committed Aug 4, 2024
1 parent 103c343 commit 3b6ebb4
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 16 deletions.
20 changes: 20 additions & 0 deletions crates/frontend/src/highlight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,23 @@ pub fn highlight(root: &Root, occurrences: &OccurrenceMap) -> Vec<Highlight> {
highlights.sort_by_key(|hl| hl.range.start());
highlights
}

pub fn translate_to_utf16(
input: &str,
highlights: Vec<Highlight>,
) -> Vec<(usize, usize, HighlightKind)> {
let mut prev_tkn_end = 0;
let mut utf16_offset = 0;
highlights
.into_iter()
.map(|h| {
let tkn_start = h.range.start().into();
let tkn_end = h.range.end().into();
utf16_offset += str::encode_utf16(&input[prev_tkn_end..tkn_start]).count();
let utf16_start = utf16_offset;
utf16_offset += str::encode_utf16(&input[tkn_start..tkn_end]).count();
prev_tkn_end = tkn_end;
(utf16_start, utf16_offset, h.kind)
})
.collect()
}
27 changes: 11 additions & 16 deletions crates/wasm-lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,15 @@ pub struct Highlight {
}

impl Highlight {
pub fn new(input: &str, highlight: highlight::Highlight) -> Self {
pub fn new(start: usize, end: usize, kind: highlight::HighlightKind) -> Self {
Highlight {
kind: format!("{:?}", highlight.kind),
start: utf8_offset_to_utf16_offset(input, highlight.range.start().into()),
end: utf8_offset_to_utf16_offset(input, highlight.range.end().into()),
kind: format!("{:?}", kind),
start,
end,
}
}
}

// TODO: This is quadratic, do it in linear time
fn utf8_offset_to_utf16_offset(s: &str, byte_offset: usize) -> usize {
s.char_indices()
.take_while(|(i, _)| *i < byte_offset)
.map(|(_, c)| c.len_utf16())
.sum()
}

#[wasm_bindgen(getter_with_clone)]
pub struct CompileResult {
pub wasm: Vec<u8>,
Expand All @@ -49,10 +41,13 @@ pub struct CompileResult {
pub fn compile(input: &str) -> CompileResult {
console_error_panic_hook::set_once();
let check_result = frontend::run_frontend(input);
let highlights = highlight::highlight(&check_result.parse, &check_result.occurrences)
.into_iter()
.map(|h| Highlight::new(input, h))
.collect();
let highlights = highlight::translate_to_utf16(
input,
highlight::highlight(&check_result.parse, &check_result.occurrences),
)
.into_iter()
.map(|(start, end, kind)| Highlight::new(start, end, kind))
.collect();
let (name_map, result) = match check_result.ir {
Some(ir) if check_result.errors.is_empty() => {
let (wasm, names) = codegen(ir, check_result.names);
Expand Down

0 comments on commit 3b6ebb4

Please sign in to comment.