From 01be964765cbe2cdca52c0e84affac64cf43c4b8 Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Tue, 29 Sep 2026 17:01:03 +0200 Subject: [PATCH] feat: map LSP UTF-16 positions --- src/lsp.rs | 156 +++++++++++++++++++++++++++++++++++++++++++++++++---- tasks.md | 2 +- 2 files changed, 148 insertions(+), 10 deletions(-) diff --git a/src/lsp.rs b/src/lsp.rs index 13c1ed5..bd01111 100644 --- a/src/lsp.rs +++ b/src/lsp.rs @@ -1,20 +1,106 @@ //! Language Server Protocol state and adapter implementation. -use std::collections::HashMap; +use std::{collections::HashMap, ops::Range as ByteRange}; use tokio::sync::RwLock; use tower_lsp_server::{ Client, LanguageServer, - jsonrpc::Result, + jsonrpc::Result as JsonResult, ls_types::{ DidChangeTextDocumentParams, DidCloseTextDocumentParams, DidOpenTextDocumentParams, - InitializeParams, InitializeResult, InitializedParams, ServerCapabilities, ServerInfo, - TextDocumentSyncCapability, TextDocumentSyncKind, Uri, + InitializeParams, InitializeResult, InitializedParams, Position, Range, ServerCapabilities, + ServerInfo, TextDocumentSyncCapability, TextDocumentSyncKind, Uri, }, }; use crate::{Config, ConfigLoader}; +/// Failure to convert a canonical UTF-8 byte offset into an LSP position. +#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)] +pub enum PositionError { + /// The requested offset lies beyond the indexed snapshot. + #[error("byte offset {offset} is outside a {source_len}-byte document")] + OutOfBounds { + /// Requested UTF-8 byte offset. + offset: usize, + /// Indexed snapshot length in bytes. + source_len: usize, + }, + /// The requested offset splits a UTF-8 scalar value. + #[error("byte offset {offset} is not a UTF-8 character boundary")] + InvalidCharacterBoundary { + /// Invalid UTF-8 byte offset. + offset: usize, + }, + /// The document is too large for the protocol's unsigned coordinates. + #[error("document position exceeds the LSP coordinate limit")] + CoordinateOverflow, +} + +/// Indexed conversion from UTF-8 byte offsets to zero-based LSP UTF-16 positions. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct LineIndex<'source> { + source: &'source str, + line_starts: Vec, +} + +impl<'source> LineIndex<'source> { + /// Index every logical line in one immutable source snapshot. + pub fn new(source: &'source str) -> Self { + let mut line_starts = vec![0]; + line_starts.extend( + source + .match_indices('\n') + .map(|(offset, _)| offset.saturating_add(1)), + ); + Self { + source, + line_starts, + } + } + + /// Convert a canonical byte offset, including EOF, into an LSP position. + pub fn position(&self, offset: usize) -> Result { + if offset > self.source.len() { + return Err(PositionError::OutOfBounds { + offset, + source_len: self.source.len(), + }); + } + if !self.source.is_char_boundary(offset) { + return Err(PositionError::InvalidCharacterBoundary { offset }); + } + + let line_index = self.line_starts.partition_point(|start| *start <= offset) - 1; + let line_start = self.line_starts[line_index]; + let line_end = self + .line_starts + .get(line_index + 1) + .map_or(self.source.len(), |next_start| next_start - 1); + let content_end = line_end + .checked_sub(1) + .filter(|candidate| self.source.as_bytes().get(*candidate) == Some(&b'\r')) + .unwrap_or(line_end); + let effective_offset = offset.min(content_end); + let utf16_units = self.source[line_start..effective_offset] + .encode_utf16() + .count(); + + Ok(Position { + line: u32::try_from(line_index).map_err(|_| PositionError::CoordinateOverflow)?, + character: u32::try_from(utf16_units).map_err(|_| PositionError::CoordinateOverflow)?, + }) + } + + /// Convert a UTF-8 byte range into an LSP UTF-16 range. + pub fn range(&self, bytes: ByteRange) -> Result { + Ok(Range { + start: self.position(bytes.start)?, + end: self.position(bytes.end)?, + }) + } +} + #[derive(Debug, Clone, PartialEq, Eq)] struct OpenDocument { version: i32, @@ -86,7 +172,7 @@ impl LspBackend { } impl LanguageServer for LspBackend { - async fn initialize(&self, _: InitializeParams) -> Result { + async fn initialize(&self, _: InitializeParams) -> JsonResult { Ok(InitializeResult { server_info: Some(ServerInfo { name: "john-slop-lsp".to_owned(), @@ -104,7 +190,7 @@ impl LanguageServer for LspBackend { async fn initialized(&self, _: InitializedParams) {} - async fn shutdown(&self) -> Result<()> { + async fn shutdown(&self) -> JsonResult<()> { Ok(()) } @@ -128,11 +214,11 @@ mod tests { use std::str::FromStr; use tower_lsp_server::ls_types::{ - DidChangeTextDocumentParams, DidOpenTextDocumentParams, TextDocumentContentChangeEvent, - TextDocumentItem, Uri, VersionedTextDocumentIdentifier, + DidChangeTextDocumentParams, DidOpenTextDocumentParams, Position, + TextDocumentContentChangeEvent, TextDocumentItem, Uri, VersionedTextDocumentIdentifier, }; - use super::DocumentStore; + use super::{DocumentStore, LineIndex, PositionError}; fn uri() -> Uri { Uri::from_str("file:///tmp/example.txt").unwrap() @@ -201,4 +287,56 @@ mod tests { assert!(store.close(&uri())); assert!(!store.close(&uri())); } + + #[test] + fn maps_ascii_crlf_and_every_eof_shape() { + let index = LineIndex::new("ab\r\ncd\n"); + + assert_eq!(index.position(0).unwrap(), Position::new(0, 0)); + assert_eq!(index.position(2).unwrap(), Position::new(0, 2)); + assert_eq!(index.position(3).unwrap(), Position::new(0, 2)); + assert_eq!(index.position(4).unwrap(), Position::new(1, 0)); + assert_eq!(index.position(6).unwrap(), Position::new(1, 2)); + assert_eq!(index.position(7).unwrap(), Position::new(2, 0)); + assert_eq!(LineIndex::new("").position(0).unwrap(), Position::new(0, 0)); + assert_eq!( + LineIndex::new("last").position(4).unwrap(), + Position::new(0, 4) + ); + } + + #[test] + fn maps_emoji_astral_and_combining_text_to_utf16_units() { + let source = "a😀e\u{301}\n𐐷z"; + let index = LineIndex::new(source); + + assert_eq!(index.position(1).unwrap(), Position::new(0, 1)); + assert_eq!(index.position(5).unwrap(), Position::new(0, 3)); + assert_eq!(index.position(6).unwrap(), Position::new(0, 4)); + assert_eq!(index.position(8).unwrap(), Position::new(0, 5)); + assert_eq!(index.position(9).unwrap(), Position::new(1, 0)); + assert_eq!(index.position(13).unwrap(), Position::new(1, 2)); + assert_eq!(index.position(source.len()).unwrap(), Position::new(1, 3)); + assert_eq!( + index.range(1..8).unwrap(), + tower_lsp_server::ls_types::Range::new(Position::new(0, 1), Position::new(0, 5)) + ); + } + + #[test] + fn rejects_offsets_outside_utf8_boundaries() { + let index = LineIndex::new("😀"); + + assert_eq!( + index.position(1), + Err(PositionError::InvalidCharacterBoundary { offset: 1 }) + ); + assert_eq!( + index.position(5), + Err(PositionError::OutOfBounds { + offset: 5, + source_len: 4, + }) + ); + } } diff --git a/tasks.md b/tasks.md index 6bcc49b..df8e6c3 100644 --- a/tasks.md +++ b/tasks.md @@ -193,7 +193,7 @@ first formatter milestone tag. - [x] Implement stdio initialization/shutdown and incremental or full document synchronization with URI, version, language ID, and current text tracked per open document. -- [ ] Build and thoroughly test byte-offset to LSP UTF-16 line/character mapping, +- [x] Build and thoroughly test byte-offset to LSP UTF-16 line/character mapping, including CRLF, emoji, combining marks, astral characters, and EOF positions. - [ ] Publish diagnostics only for configured ASCII-replacement findings in the initial release. Include stable codes and source `john-slop`.