Skip to content

Commit

Permalink
Workaround panic of autocompletion of import with unicode
Browse files Browse the repository at this point in the history
(As seen in the crash reporter)

Eg:
```
thread 'LanguageServer' panicked at tools/lsp/language/completion.rs:731:17:
byte index 4 is not a char boundary; it is inside '🍒' (bytes 2..6) of `./🍒🍓🍇"`
```

This can happen because offset might not be on a char boundary because
of the utf-16 / utf-8 mismatch.
  • Loading branch information
ogoffart authored Jan 2, 2025
1 parent 7b6afcb commit 61b4dcf
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
7 changes: 4 additions & 3 deletions tools/lsp/language.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2012,7 +2012,7 @@ export component TestWindow inherits Window {
"
\t
\t \t
"
.into(),
);
Expand Down Expand Up @@ -2112,6 +2112,7 @@ export component MainWindow inherits Window {
);
}

#[cfg(any(feature = "preview-external", feature = "preview-engine"))]
#[test]
fn test_show_preview_code_lens() {
// Empty slint document:
Expand All @@ -2120,8 +2121,8 @@ export component MainWindow inherits Window {
component Internal { }
export component Test {
}
}
"#
.into(),
);
Expand Down
5 changes: 3 additions & 2 deletions tools/lsp/language/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -724,11 +724,12 @@ fn complete_path_in_string(
text: &str,
offset: TextSize,
) -> Option<Vec<CompletionItem>> {
if u32::from(offset) as usize > text.len() || offset == 0.into() {
let offset = u32::from(offset) as usize;
if offset > text.len() || offset == 0 || !text.is_char_boundary(offset) {
return None;
}
let mut text = text.strip_prefix('\"')?;
text = &text[..(u32::from(offset) - 1) as usize];
text = &text[..(offset - 1)];
let base = i_slint_compiler::typeloader::base_directory(base);
let path = if let Some(last_slash) = text.rfind('/') {
base.join(Path::new(&text[..last_slash]))
Expand Down

0 comments on commit 61b4dcf

Please sign in to comment.