aboutsummaryrefslogtreecommitdiff
path: root/helix-lsp/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'helix-lsp/src/lib.rs')
-rw-r--r--helix-lsp/src/lib.rs22
1 files changed, 17 insertions, 5 deletions
diff --git a/helix-lsp/src/lib.rs b/helix-lsp/src/lib.rs
index bcaff10a..72456b37 100644
--- a/helix-lsp/src/lib.rs
+++ b/helix-lsp/src/lib.rs
@@ -20,7 +20,6 @@ use std::{
},
};
-use serde::{Deserialize, Serialize};
use thiserror::Error;
use tokio_stream::wrappers::UnboundedReceiverStream;
@@ -45,13 +44,14 @@ pub enum Error {
Other(#[from] anyhow::Error),
}
-#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
+#[derive(Clone, Copy, Debug, Default)]
pub enum OffsetEncoding {
/// UTF-8 code units aka bytes
- #[serde(rename = "utf-8")]
Utf8,
+ /// UTF-32 code units aka chars
+ Utf32,
/// UTF-16 code units
- #[serde(rename = "utf-16")]
+ #[default]
Utf16,
}
@@ -168,6 +168,11 @@ pub mod util {
let line_end = line_end_char_index(&doc.slice(..), pos_line);
doc.char_to_utf16_cu(line_start)..doc.char_to_utf16_cu(line_end)
}
+ OffsetEncoding::Utf32 => {
+ let line_start = doc.line_to_char(pos_line);
+ let line_end = line_end_char_index(&doc.slice(..), pos_line);
+ line_start..line_end
+ }
};
// The LSP spec demands that the offset is capped to the end of the line
@@ -177,10 +182,10 @@ pub mod util {
.unwrap_or(line.end)
.min(line.end);
- // TODO prefer UTF32/char indices to avoid this step
match offset_encoding {
OffsetEncoding::Utf8 => doc.try_byte_to_char(pos).ok(),
OffsetEncoding::Utf16 => doc.try_utf16_cu_to_char(pos).ok(),
+ OffsetEncoding::Utf32 => Some(pos),
}
}
@@ -207,6 +212,13 @@ pub mod util {
lsp::Position::new(line as u32, col as u32)
}
+ OffsetEncoding::Utf32 => {
+ let line = doc.char_to_line(pos);
+ let line_start = doc.line_to_char(line);
+ let col = pos - line_start;
+
+ lsp::Position::new(line as u32, col as u32)
+ }
}
}