diff --git a/Cargo.lock b/Cargo.lock index cc184d8..6adc372 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -379,7 +379,7 @@ checksum = "d8adc4bb1803a324070e64a98ae98f38934d91957a99cfb3a43dcbc01bc56439" [[package]] name = "live-server-lsp" -version = "0.3.3" +version = "0.3.4" dependencies = [ "rusty-live-server", "serde", diff --git a/Cargo.toml b/Cargo.toml index 8a9b9bf..701d52c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "live-server-lsp" -version = "0.3.3" +version = "0.3.4" edition = "2021" [dependencies] diff --git a/src/lsp.rs b/src/lsp.rs index 31c29c0..e17de09 100644 --- a/src/lsp.rs +++ b/src/lsp.rs @@ -413,17 +413,20 @@ pub async fn lsp() { } pub fn get_byte_index_from_position(s: &str, position: Position) -> usize { - if s.len() == 0 { + if s.is_empty() { return 0; } + let line_start = index_of_first_char_in_line(s, position.line).unwrap_or(s.len()); let char_index = line_start + position.character as usize; - if char_index >= s.len() { - s.char_indices().nth(s.len() - 1).unwrap().0 + let char_count = s.chars().count(); + + if char_index >= char_count { + s.char_indices().last().map(|(i, _)| i).unwrap_or(0) } else { - s.char_indices().nth(char_index).unwrap().0 + s.char_indices().nth(char_index).map(|(i, _)| i).unwrap_or(s.len()) } }