Skip to content

Commit

Permalink
no more unwraps here
Browse files Browse the repository at this point in the history
  • Loading branch information
frederik-uni committed Jan 26, 2025
1 parent 711cf77 commit 95dc86e
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "live-server-lsp"
version = "0.3.3"
version = "0.3.4"
edition = "2021"

[dependencies]
Expand Down
11 changes: 7 additions & 4 deletions src/lsp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}

Expand Down

0 comments on commit 95dc86e

Please sign in to comment.