mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-29 05:15:04 +00:00
fix: handle character boundaries for wide chars in extend_selection
This commit is contained in:
parent
6b8b8ff4c5
commit
bf356e0eec
1 changed files with 21 additions and 1 deletions
|
@ -210,7 +210,13 @@ fn extend_single_word_in_comment_or_string(
|
||||||
let start_idx = before.rfind(non_word_char)? as u32;
|
let start_idx = before.rfind(non_word_char)? as u32;
|
||||||
let end_idx = after.find(non_word_char).unwrap_or(after.len()) as u32;
|
let end_idx = after.find(non_word_char).unwrap_or(after.len()) as u32;
|
||||||
|
|
||||||
let from: TextSize = (start_idx + 1).into();
|
// FIXME: use `ceil_char_boundary` from `std::str` when it gets stable
|
||||||
|
// https://github.com/rust-lang/rust/issues/93743
|
||||||
|
fn ceil_char_boundary(text: &str, index: u32) -> u32 {
|
||||||
|
(index..).find(|&index| text.is_char_boundary(index as usize)).unwrap_or(text.len() as u32)
|
||||||
|
}
|
||||||
|
|
||||||
|
let from: TextSize = ceil_char_boundary(text, start_idx + 1).into();
|
||||||
let to: TextSize = (cursor_position + end_idx).into();
|
let to: TextSize = (cursor_position + end_idx).into();
|
||||||
|
|
||||||
let range = TextRange::new(from, to);
|
let range = TextRange::new(from, to);
|
||||||
|
@ -662,4 +668,18 @@ fn main() { let (
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn extend_selection_inside_str_with_wide_char() {
|
||||||
|
// should not panic
|
||||||
|
do_check(
|
||||||
|
r#"fn main() { let x = "═$0═══════"; }"#,
|
||||||
|
&[
|
||||||
|
r#""════════""#,
|
||||||
|
r#"let x = "════════";"#,
|
||||||
|
r#"{ let x = "════════"; }"#,
|
||||||
|
r#"fn main() { let x = "════════"; }"#,
|
||||||
|
],
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue