lutra-text gets Buffer on a ropey rope: byte/char/UTF-16/line counts, byte offset <-> (line, column) in UTF-16 code units or chars, absolute UTF-16 offsets, atomic Edit batches in LSP TextEdit[] semantics, and \n / \r\n / \r detection with a keep-the-file's-ending save policy. All conversions return TextError instead of panicking. ropey 1.6.1 (MIT) without unicode_lines, so only the LSP/CodeMirror line breaks split lines. proptest moves into workspace deps. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
151 lines
5 KiB
Rust
151 lines
5 KiB
Rust
//! Errors returned by position conversions and edits.
|
|
|
|
use std::fmt;
|
|
use std::ops::Range;
|
|
|
|
/// Why a position conversion or an edit was rejected.
|
|
///
|
|
/// Every conversion and edit on [`crate::Buffer`] returns this instead of
|
|
/// panicking, so callers can turn bad offsets from untrusted input (LSP
|
|
/// messages, frontend edits, command params) into an error message.
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
pub enum TextError {
|
|
/// A byte offset lies past the end of the text.
|
|
OffsetOutOfRange {
|
|
/// The rejected byte offset.
|
|
offset: usize,
|
|
/// The text length in bytes.
|
|
len: usize,
|
|
},
|
|
/// A byte offset lies inside a multi-byte UTF-8 character.
|
|
NotCharBoundary {
|
|
/// The rejected byte offset.
|
|
offset: usize,
|
|
},
|
|
/// A byte offset lies between the `\r` and the `\n` of a CRLF line
|
|
/// break, where no line and column exist.
|
|
InsideLineBreak {
|
|
/// The rejected byte offset.
|
|
offset: usize,
|
|
},
|
|
/// A UTF-16 offset lies past the end of the text.
|
|
Utf16OutOfRange {
|
|
/// The rejected offset in UTF-16 code units.
|
|
offset: usize,
|
|
/// The text length in UTF-16 code units.
|
|
len: usize,
|
|
},
|
|
/// A UTF-16 offset lies between the two halves of a surrogate pair.
|
|
Utf16InsideChar {
|
|
/// The rejected offset in UTF-16 code units.
|
|
offset: usize,
|
|
},
|
|
/// A line index is not smaller than the line count.
|
|
LineOutOfRange {
|
|
/// The rejected zero-based line index.
|
|
line: usize,
|
|
/// The number of lines in the text.
|
|
line_count: usize,
|
|
},
|
|
/// A column lies past the end of its line (line break excluded).
|
|
ColumnOutOfRange {
|
|
/// The zero-based line index.
|
|
line: usize,
|
|
/// The rejected column.
|
|
column: usize,
|
|
/// The line length in the column's unit, line break excluded.
|
|
line_len: usize,
|
|
},
|
|
/// A UTF-16 column lies between the two halves of a surrogate pair.
|
|
ColumnInsideChar {
|
|
/// The zero-based line index.
|
|
line: usize,
|
|
/// The rejected column in UTF-16 code units.
|
|
column: usize,
|
|
},
|
|
/// A range whose start lies after its end.
|
|
InvalidRange {
|
|
/// The range start in bytes.
|
|
start: usize,
|
|
/// The range end in bytes.
|
|
end: usize,
|
|
},
|
|
/// Two edits of one batch touch overlapping text.
|
|
OverlappingEdits {
|
|
/// The byte range of the edit that starts first.
|
|
first: Range<usize>,
|
|
/// The byte range of the edit that overlaps it.
|
|
second: Range<usize>,
|
|
},
|
|
/// The rope rejected an operation that validation had accepted; a bug in
|
|
/// this crate rather than bad input.
|
|
Internal {
|
|
/// The rope's own error message.
|
|
detail: String,
|
|
},
|
|
}
|
|
|
|
impl fmt::Display for TextError {
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
match self {
|
|
Self::OffsetOutOfRange { offset, len } => {
|
|
write!(
|
|
f,
|
|
"byte offset {offset} is past the end of the text ({len} bytes)"
|
|
)
|
|
}
|
|
Self::NotCharBoundary { offset } => {
|
|
write!(f, "byte offset {offset} is inside a UTF-8 character")
|
|
}
|
|
Self::InsideLineBreak { offset } => {
|
|
write!(f, "byte offset {offset} is inside a CRLF line break")
|
|
}
|
|
Self::Utf16OutOfRange { offset, len } => write!(
|
|
f,
|
|
"UTF-16 offset {offset} is past the end of the text ({len} code units)"
|
|
),
|
|
Self::Utf16InsideChar { offset } => {
|
|
write!(f, "UTF-16 offset {offset} is inside a surrogate pair")
|
|
}
|
|
Self::LineOutOfRange { line, line_count } => {
|
|
write!(
|
|
f,
|
|
"line {line} does not exist (the text has {line_count} lines)"
|
|
)
|
|
}
|
|
Self::ColumnOutOfRange {
|
|
line,
|
|
column,
|
|
line_len,
|
|
} => write!(
|
|
f,
|
|
"column {column} is past the end of line {line} (length {line_len})"
|
|
),
|
|
Self::ColumnInsideChar { line, column } => {
|
|
write!(
|
|
f,
|
|
"column {column} of line {line} is inside a surrogate pair"
|
|
)
|
|
}
|
|
Self::InvalidRange { start, end } => {
|
|
write!(f, "range start {start} is after its end {end}")
|
|
}
|
|
Self::OverlappingEdits { first, second } => write!(
|
|
f,
|
|
"edits {}..{} and {}..{} overlap",
|
|
first.start, first.end, second.start, second.end
|
|
),
|
|
Self::Internal { detail } => write!(f, "internal text buffer error: {detail}"),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl std::error::Error for TextError {}
|
|
|
|
impl From<ropey::Error> for TextError {
|
|
fn from(error: ropey::Error) -> Self {
|
|
Self::Internal {
|
|
detail: error.to_string(),
|
|
}
|
|
}
|
|
}
|