qem 0.6.3

High-performance cross-platform text engine for massive files.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
use super::*;

#[derive(Clone, Copy, Debug)]
struct PieceTableScanRange {
    range: (usize, usize),
    exact: bool,
}

fn next_piece_table_scan_line_range(
    bytes: &[u8],
    start0: usize,
    buffer_reaches_eof: bool,
) -> Option<PieceTableScanRange> {
    if start0 >= bytes.len() {
        return None;
    }

    let slice = &bytes[start0..];
    let end0 = if let Some(rel) = memchr::memchr2(b'\n', b'\r', slice) {
        let idx = start0 + rel;
        if bytes[idx] == b'\r' && idx + 1 < bytes.len() && bytes[idx + 1] == b'\n' {
            idx + 2
        } else {
            idx + 1
        }
    } else {
        bytes.len()
    };

    Some(PieceTableScanRange {
        range: (start0, end0.max(start0)),
        exact: end0 < bytes.len() || buffer_reaches_eof,
    })
}

fn count_text_units_in_bytes(bytes: &[u8]) -> usize {
    let mut units = 0usize;
    let mut i = 0usize;
    while i < bytes.len() {
        match bytes[i] {
            b'\r' => {
                units = units.saturating_add(1);
                i += 1;
                if i < bytes.len() && bytes[i] == b'\n' {
                    i += 1;
                }
            }
            b'\n' => {
                units = units.saturating_add(1);
                i += 1;
            }
            _ => {
                units = units.saturating_add(1);
                i += utf8_step(bytes, i, bytes.len());
            }
        }
    }
    units
}

fn safe_piece_table_offset_for_position(piece_table: &PieceTable, position: TextPosition) -> usize {
    Document::scanned_piece_table_offset_for_position(piece_table, position)
        .map(|(offset, _)| offset)
        .unwrap_or_else(|| piece_table.known_byte_len.min(piece_table.total_len()))
}

impl Document {
    pub(crate) fn piece_table_position_is_representable(
        piece_table: &PieceTable,
        position: TextPosition,
    ) -> bool {
        if piece_table.full_index() || piece_table.has_line(position.line0()) {
            return true;
        }

        let Some(scanned) = Self::scanned_piece_table_line_range(piece_table, position.line0())
        else {
            return false;
        };
        let bytes = piece_table.read_range(scanned.range.0, scanned.range.1);
        let mut end = bytes.len();
        while end > 0 {
            let b = bytes[end - 1];
            if b == b'\n' || b == b'\r' {
                end -= 1;
            } else {
                break;
            }
        }

        let line_len = if scanned.exact {
            count_text_columns_exact(&bytes[..end])
        } else {
            count_text_columns(&bytes[..end], MAX_LINE_SCAN_CHARS)
        };
        position.col0() <= line_len
    }

    pub(super) fn selection_requires_piece_table_promotion(
        &self,
        selection: TextSelection,
    ) -> bool {
        let Some(piece_table) = &self.piece_table else {
            return false;
        };
        if piece_table.full_index() {
            return false;
        }

        !Self::piece_table_position_is_representable(piece_table, selection.anchor())
            || !Self::piece_table_position_is_representable(piece_table, selection.head())
    }

    fn scanned_piece_table_line_range(
        piece_table: &PieceTable,
        line0: usize,
    ) -> Option<PieceTableScanRange> {
        if piece_table.full_index() || piece_table.has_line(line0) {
            return Some(PieceTableScanRange {
                range: piece_table.line_range(line0),
                exact: true,
            });
        }

        let scan_start = piece_table.known_byte_len.min(piece_table.total_len());
        let scan_end = scan_start
            .saturating_add(PARTIAL_PIECE_TABLE_SCAN_BYTES)
            .min(piece_table.total_len());
        if scan_start >= scan_end {
            return None;
        }

        let bytes = piece_table.read_range(scan_start, scan_end);
        let buffer_reaches_eof = scan_end == piece_table.total_len();
        let mut rel_start = 0usize;
        let mut skip_lines = line0.saturating_sub(piece_table.line_count());
        while skip_lines > 0 {
            let scanned = next_piece_table_scan_line_range(&bytes, rel_start, buffer_reaches_eof)?;
            if scanned.range.1 <= rel_start || !scanned.exact {
                return None;
            }
            rel_start = scanned.range.1;
            skip_lines -= 1;
        }

        let scanned = next_piece_table_scan_line_range(&bytes, rel_start, buffer_reaches_eof)?;
        Some(PieceTableScanRange {
            range: (
                scan_start.saturating_add(scanned.range.0),
                scan_start.saturating_add(scanned.range.1),
            ),
            exact: scanned.exact,
        })
    }

    fn scanned_piece_table_offset_for_position(
        piece_table: &PieceTable,
        position: TextPosition,
    ) -> Option<(usize, bool)> {
        let scanned = Self::scanned_piece_table_line_range(piece_table, position.line0())?;
        let bytes = piece_table.read_range(scanned.range.0, scanned.range.1);
        let offset = byte_offset_for_text_col_in_bytes(&bytes, (0, bytes.len()), position.col0());
        Some((
            scanned.range.0.saturating_add(offset),
            Self::piece_table_position_is_representable(piece_table, position),
        ))
    }

    /// Returns the line length in document text columns, excluding any trailing
    /// line ending.
    ///
    /// For UTF-8 text this counts Unicode scalar values rather than grapheme
    /// clusters or display cells.
    pub fn line_len_chars(&self, line0: usize) -> usize {
        if let Some(piece_table) = &self.piece_table {
            if piece_table.full_index() || piece_table.has_line(line0) {
                return piece_table.line_len_chars(line0);
            }
            if let Some(scanned) = Self::scanned_piece_table_line_range(piece_table, line0) {
                let bytes = piece_table.read_range(scanned.range.0, scanned.range.1);
                let mut end = bytes.len();
                while end > 0 {
                    let b = bytes[end - 1];
                    if b == b'\n' || b == b'\r' {
                        end -= 1;
                    } else {
                        break;
                    }
                }
                return if scanned.exact {
                    count_text_columns_exact(&bytes[..end])
                } else {
                    count_text_columns(&bytes[..end], MAX_LINE_SCAN_CHARS)
                };
            }
            return 0;
        }
        if let Some(rope) = &self.rope {
            return Self::rope_line_len_chars_without_newline(rope, line0);
        }

        let bytes = self.mmap_bytes();
        let file_len = self.file_len.min(bytes.len());
        if file_len == 0 {
            return 0;
        }

        let Some(start) = self.mmap_line_start_offset_exact(line0) else {
            return 0;
        };
        let mut end = super::search::next_line_start_exact(bytes, file_len, start).min(file_len);
        while end > start {
            let b = bytes[end - 1];
            if b == b'\n' || b == b'\r' {
                end -= 1;
            } else {
                break;
            }
        }
        count_text_columns_exact(&bytes[start..end])
    }

    /// Clamps a typed position into the currently known document bounds.
    pub fn clamp_position(&self, position: TextPosition) -> TextPosition {
        let line0 = if self.rope.is_some() {
            position
                .line0()
                .min(self.display_line_count().max(1).saturating_sub(1))
        } else if let Some(piece_table) = &self.piece_table {
            if piece_table.full_index()
                || piece_table.has_line(position.line0())
                || Self::scanned_piece_table_line_range(piece_table, position.line0()).is_some()
            {
                position.line0()
            } else {
                position
                    .line0()
                    .min(self.display_line_count().max(1).saturating_sub(1))
            }
        } else {
            let bytes = self.mmap_bytes();
            let file_len = self.file_len.min(bytes.len());
            if self
                .mmap_line_start_offset_exact(position.line0())
                .is_some()
            {
                position.line0()
            } else {
                self.mmap_position_for_byte_offset(file_len).line0()
            }
        };
        let col0 = position.col0().min(self.line_len_chars(line0));
        TextPosition::new(line0, col0)
    }

    /// Returns the ordered pair of two clamped positions.
    ///
    /// This is useful for frontend code that keeps anchor/head selection state
    /// and needs a stable document-ordered range before applying edits.
    pub fn ordered_positions(
        &self,
        first: TextPosition,
        second: TextPosition,
    ) -> (TextPosition, TextPosition) {
        let first = self.clamp_position(first);
        let second = self.clamp_position(second);
        if first <= second {
            (first, second)
        } else {
            (second, first)
        }
    }

    /// Clamps a selection into the currently known document bounds.
    pub fn clamp_selection(&self, selection: TextSelection) -> TextSelection {
        TextSelection::new(
            self.clamp_position(selection.anchor()),
            self.clamp_position(selection.head()),
        )
    }

    /// Returns the typed document position for a full-text character index.
    ///
    /// For UTF-8 text, this maps indices to Unicode scalar-value columns
    /// instead of grapheme clusters or display cells.
    pub fn position_for_char_index(&self, char_index: usize) -> TextPosition {
        let (line0, col0) = self.cursor_position_for_char_index(char_index);
        TextPosition::new(line0, col0)
    }

    /// Returns the full-text character index for a typed document position.
    ///
    /// This uses the same text-unit semantics as [`Document::try_replace`]:
    /// line-local columns count Unicode scalar values, and line breaks count as
    /// one text unit even when they are stored as CRLF.
    pub fn char_index_for_position(&self, position: TextPosition) -> usize {
        let position = self.clamp_position(position);
        if let Some(rope) = &self.rope {
            return Self::line_col_to_char_index(rope, position.line0(), position.col0());
        }
        if let Some(piece_table) = &self.piece_table {
            let offset = safe_piece_table_offset_for_position(piece_table, position);
            return count_text_units_in_bytes(&piece_table.read_range(0, offset));
        }
        let bytes = self.mmap_bytes();
        let file_len = self.file_len.min(bytes.len());
        let offset = self.mmap_byte_offset_for_position(position).min(file_len);
        count_text_units_in_bytes(&bytes[..offset])
    }

    /// Returns the number of edit text units between two typed positions.
    ///
    /// This follows the same semantics as [`Document::try_replace`]: line-local
    /// columns count Unicode scalar values, and line breaks count as one text
    /// unit even when they are stored as CRLF.
    pub fn text_units_between(&self, start: TextPosition, end: TextPosition) -> usize {
        let (start, end) = self.ordered_positions(start, end);
        if start == end {
            return 0;
        }

        if let Some(rope) = &self.rope {
            let start_idx = Self::line_col_to_char_index(rope, start.line0(), start.col0());
            let end_idx = Self::line_col_to_char_index(rope, end.line0(), end.col0());
            return end_idx.saturating_sub(start_idx);
        }
        if let Some(piece_table) = &self.piece_table {
            let start_offset = safe_piece_table_offset_for_position(piece_table, start);
            let end_offset = safe_piece_table_offset_for_position(piece_table, end);
            return count_text_units_in_bytes(
                &piece_table.read_range(start_offset.min(end_offset), end_offset.max(start_offset)),
            );
        }

        let bytes = self.mmap_bytes();
        let file_len = self.file_len.min(bytes.len());
        let start_offset = self.mmap_byte_offset_for_position(start).min(file_len);
        let end_offset = self.mmap_byte_offset_for_position(end).min(file_len);
        count_text_units_in_bytes(
            &bytes[start_offset.min(end_offset)..end_offset.max(start_offset)],
        )
    }

    /// Builds a typed edit range between two positions.
    ///
    /// Frontends that keep anchor/head selection state can use this helper to
    /// convert it directly into a [`TextRange`] for [`Document::try_replace`].
    pub fn text_range_between(&self, start: TextPosition, end: TextPosition) -> TextRange {
        let (start, end) = self.ordered_positions(start, end);
        TextRange::new(start, self.text_units_between(start, end))
    }

    /// Builds a typed edit range from an anchor/head selection.
    pub fn text_range_for_selection(&self, selection: TextSelection) -> TextRange {
        self.text_range_between(selection.anchor(), selection.head())
    }

    /// Returns whether the requested position is currently editable.
    ///
    /// This lets frontends distinguish between positions that are already
    /// editable, positions that would trigger a backend promotion, and positions
    /// that would fail with [`DocumentError::EditUnsupported`].
    pub fn edit_capability_at(&self, position: TextPosition) -> EditCapability {
        let position = self.clamp_position(position);
        let backing = self.backing();

        if self.rope.is_some() {
            return EditCapability::Editable { backing };
        }

        if let Some(piece_table) = &self.piece_table {
            if piece_table.full_index() || piece_table.has_line(position.line0()) {
                return EditCapability::Editable { backing };
            }

            return if self.can_materialize_rope(piece_table.total_len()) {
                EditCapability::RequiresPromotion {
                    from: DocumentBacking::PieceTable,
                    to: DocumentBacking::Rope,
                }
            } else {
                EditCapability::Unsupported {
                backing: DocumentBacking::PieceTable,
                reason: "document is too large to widen partial piece-table editing beyond the indexed prefix",
            }
            };
        }

        let use_piece_table = self.storage.is_some() && self.file_len >= PIECE_TABLE_MIN_BYTES;
        if use_piece_table
            && self
                .piece_table_line_lengths_for_edit(position.line0())
                .is_some()
        {
            return EditCapability::RequiresPromotion {
                from: DocumentBacking::Mmap,
                to: DocumentBacking::PieceTable,
            };
        }

        if self.can_materialize_rope(self.file_len) {
            return EditCapability::RequiresPromotion {
                from: backing,
                to: DocumentBacking::Rope,
            };
        }

        EditCapability::Unsupported {
            backing,
            reason:
                "document is too large to materialize into a rope; editing this region is disabled",
        }
    }

    /// Returns the editability for a typed edit range.
    pub fn edit_capability_for_range(&self, range: TextRange) -> EditCapability {
        self.edit_capability_at(range.start())
    }

    /// Returns the editability for an anchor/head selection.
    pub fn edit_capability_for_selection(&self, selection: TextSelection) -> EditCapability {
        if self.selection_requires_piece_table_promotion(selection) {
            let total_len = self
                .piece_table
                .as_ref()
                .map(|piece_table| piece_table.total_len())
                .unwrap_or(self.file_len);
            return if self.can_materialize_rope(total_len) {
                EditCapability::RequiresPromotion {
                    from: DocumentBacking::PieceTable,
                    to: DocumentBacking::Rope,
                }
            } else {
                EditCapability::Unsupported {
                    backing: DocumentBacking::PieceTable,
                    reason:
                        "document is too large to widen partial piece-table editing beyond the indexed prefix",
                }
            };
        }
        let range = self.text_range_for_selection(selection);
        self.edit_capability_for_range(range)
    }

    pub(crate) fn cursor_position_for_char_index(&self, char_index: usize) -> (usize, usize) {
        if let Some(rope) = &self.rope {
            let char_index = char_index.min(rope.len_chars());
            let line0 = rope.char_to_line(char_index);
            let line_start = rope.line_to_char(line0);
            let line_len = Self::rope_line_len_chars_without_newline(rope, line0);
            let col0 = char_index.saturating_sub(line_start).min(line_len);
            return (line0, col0);
        }

        if let Some(piece_table) = &self.piece_table {
            return piece_table.position_for_char_index(char_index);
        }

        let mut state = CursorScanState::new(char_index);
        scan_cursor_position_bytes(self.mmap_bytes(), &mut state);
        state.position()
    }

    pub(super) fn line_col_to_char_index(rope: &Rope, line0: usize, col0: usize) -> usize {
        let line0 = line0.min(rope.len_lines().saturating_sub(1));
        let line_start = rope.line_to_char(line0);
        let line_len = Self::rope_line_len_chars_without_newline(rope, line0);
        line_start + col0.min(line_len)
    }
}