aboutsummaryrefslogtreecommitdiff
path: root/helix-lsp
diff options
context:
space:
mode:
authorKirawi2022-01-03 02:26:17 +0000
committerGitHub2022-01-03 02:26:17 +0000
commitea095ca5fb5d0227ba778f12d128c978576ae55b (patch)
tree359095e3ad5c3b8dbe84bde9d6831546ad747230 /helix-lsp
parent4da050b4bb639755e30447518aa79f7511c8952c (diff)
Optimize lsp_pos_to_pos (#1423)
lines().count() is slow compared to len_lines()
Diffstat (limited to 'helix-lsp')
-rw-r--r--helix-lsp/src/lib.rs23
1 files changed, 5 insertions, 18 deletions
diff --git a/helix-lsp/src/lib.rs b/helix-lsp/src/lib.rs
index 1eb1c151..109546d0 100644
--- a/helix-lsp/src/lib.rs
+++ b/helix-lsp/src/lib.rs
@@ -66,39 +66,26 @@ pub mod util {
pos: lsp::Position,
offset_encoding: OffsetEncoding,
) -> Option<usize> {
- let max_line = doc.lines().count().saturating_sub(1);
let pos_line = pos.line as usize;
- let pos_line = if pos_line > max_line {
+ if pos_line > doc.len_lines() - 1 {
return None;
- } else {
- pos_line
- };
+ }
+
match offset_encoding {
OffsetEncoding::Utf8 => {
- let max_char = doc
- .line_to_char(max_line)
- .checked_add(doc.line(max_line).len_chars())?;
let line = doc.line_to_char(pos_line);
let pos = line.checked_add(pos.character as usize)?;
- if pos <= max_char {
+ if pos <= doc.len_chars() {
Some(pos)
} else {
None
}
}
OffsetEncoding::Utf16 => {
- let max_char = doc
- .line_to_char(max_line)
- .checked_add(doc.line(max_line).len_chars())?;
- let max_cu = doc.char_to_utf16_cu(max_char);
let line = doc.line_to_char(pos_line);
let line_start = doc.char_to_utf16_cu(line);
let pos = line_start.checked_add(pos.character as usize)?;
- if pos <= max_cu {
- Some(doc.utf16_cu_to_char(pos))
- } else {
- None
- }
+ doc.try_utf16_cu_to_char(pos).ok()
}
}
}