strop-engine 0.41.0

strop editor engine: documents, grammar, services and sessions — no frontend UI dependencies
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
//! Source-to-view rendering and incremental projection updates.
use super::{Collection, CollectionRow, CollectionRowInfo, Document, Editor};
use strop_core::id::{Arena, DocumentId, DocumentKind};
use strop_core::Range;

/// The canonical rendering (0049 §6): a title row with counts, then ONE
/// CARD PER FILE — top border with path and badges, excerpt bodies with
/// a "⋮ N source lines omitted" gap between disjoint spans, and a bottom
/// border. Row roles are recorded in `collection.rows` for the renderer;
/// chrome rows are protected (write-back refuses them) and keep the
/// excerpt's `view_line` invariant: the row before the body.
pub(super) fn render(
    docs: &Arena<DocumentKind, Document>,
    cwd: &std::path::Path,
    collection: &mut Collection,
) -> String {
    let files = collection
        .excerpts
        .iter()
        .enumerate()
        .filter(|(index, excerpt)| {
            *index == 0 || collection.excerpts[index - 1].source != excerpt.source
        })
        .count();
    let unavailable = if collection.skipped == 0 {
        String::new()
    } else {
        format!(" · {} unavailable hit(s)", collection.skipped)
    };
    let mut text = format!(
        "collection: {} — {} match(es) · {} file(s){unavailable}\n",
        collection.title, collection.match_count, files
    );
    let mut rows = vec![CollectionRow::Title];
    let mut line = 1;
    let mut at = 0;
    while at < collection.excerpts.len() {
        let source = collection.excerpts[at].source;
        let mut card_end = at;
        while card_end + 1 < collection.excerpts.len()
            && collection.excerpts[card_end + 1].source == source
        {
            card_end += 1;
        }
        let doc = docs.get(source).unwrap();
        let path = doc.label(cwd);
        let lang = doc
            .syntax_path()
            .and_then(|path| path.extension())
            .and_then(|extension| extension.to_str())
            .and_then(strop_lsp::registry::language_for_extension_name);
        let count = card_end - at + 1;
        let lang_badge = lang.map(|l| format!("{l} · ")).unwrap_or_default();
        let context = collection.excerpts[at..=card_end]
            .iter()
            .map(|excerpt| excerpt.context)
            .max()
            .unwrap_or(0);
        text.push_str(&format!(
            "╭─ {path} ── {lang_badge}{count} excerpt(s) · context {context} (+/-)\n"
        ));
        rows.push(CollectionRow::CardTop(at));
        line += 1;
        for i in at..=card_end {
            if i > at {
                let prev = &collection.excerpts[i - 1];
                let here = &collection.excerpts[i];
                let gap_lines = doc
                    .buf
                    .line_of(here.start)
                    .saturating_sub(doc.buf.line_of(prev.end));
                text.push_str(&format!("⋮ {gap_lines} source lines omitted\n"));
                rows.push(CollectionRow::Gap);
                line += 1;
            }
            let start = collection.excerpts[i].start;
            let end = collection.excerpts[i].end;
            let body = doc.buf.text().byte_slice(start..end).to_string();
            let excerpt = &mut collection.excerpts[i];
            excerpt.view_line = line - 1; // the chrome row before the body
            excerpt.view_start = text.len();
            excerpt.view_lines = body.lines().count().max(1);
            text.push_str(&body);
            if !body.ends_with('\n') {
                text.push('\n');
            }
            excerpt.view_end = text.len();
            for _ in 0..excerpt.view_lines {
                rows.push(CollectionRow::Body);
            }
            line += excerpt.view_lines;
        }
        text.push_str("╰\n");
        rows.push(CollectionRow::CardBottom);
        line += 1;
        at = card_end + 1;
    }
    collection.rows = rows;
    text
}

impl Editor {
    /// Re-render the collection view from its sources and reset shadow +
    /// revision together (0049 §5: no stale text may be presented).
    pub(crate) fn collection_render_view(&mut self, id: DocumentId) {
        // Preserve the logical caret across the regeneration (0049 §5):
        // same row/column clamped into the new text.
        let caret = if self.current() == id {
            Some((
                self.buf().line_of(self.head()),
                self.buf().col_of(self.head()),
            ))
        } else {
            None
        };
        let text = {
            let entry = self.collections.get_mut(&id).unwrap();
            render(&self.docs, &self.cwd, entry)
        };
        let _ = self.doc_mut(id).buf.system_edit().replace_all(&text);
        if let Some((line, col)) = caret {
            let line = line.min(self.docs.get(id).unwrap().buf.len_lines().saturating_sub(1));
            let head = self.docs.get(id).unwrap().buf.clamp_boundary(
                self.docs
                    .get(id)
                    .unwrap()
                    .buf
                    .line_start(line)
                    .saturating_add(col),
            );
            if self.current() == id {
                self.set_head(head);
                self.clamp_cursor();
            }
        }
        let revision = self.docs.get(id).unwrap().buf.revision();
        self.collections.get_mut(&id).unwrap().revision = revision;
        // The view is a presentation: its dirty bit is never the story
        // (0049 §5 — the sources own unsaved state).
        self.docs.get_mut(id).unwrap().buf.dirty = false;
    }

    /// A source change strictly inside one excerpt (0049 §5): splice the
    /// excerpt's view span with the new source body instead of
    /// re-rendering the whole view. The caller gates on a clean view
    /// (no unsynced user edit) — the spans assume shadow == view.
    pub(crate) fn collection_splice_excerpt(&mut self, id: DocumentId, index: usize) {
        let (source, view_start, view_end, view_lines) = {
            let entry = &self.collections[&id];
            let excerpt = &entry.excerpts[index];
            (
                excerpt.source,
                excerpt.view_start,
                excerpt.view_end,
                excerpt.view_lines,
            )
        };
        let Some(source_doc) = self.docs.get(source) else {
            return;
        };
        let excerpt_span = {
            let entry = &self.collections[&id];
            let excerpt = &entry.excerpts[index];
            (excerpt.start, excerpt.end)
        };
        let mut body = source_doc
            .buf
            .text()
            .byte_slice(excerpt_span.0..excerpt_span.1)
            .to_string();
        if !body.ends_with('\n') {
            body.push('\n');
        }
        let new_lines = body.lines().count().max(1);
        // Splice the collection buffer at the excerpt's view span; the
        // view is clean, so the spans index it directly.
        {
            let doc = self.docs.get_mut(id).unwrap();
            let _ = doc
                .buf
                .system_edit()
                .replace(Range::charwise(view_start, view_end), &body);
        }
        // The splice's own journal entry feeds the view analysis and is
        // then consumed: the write-back diff must never see it.
        {
            let doc = self.docs.get_mut(id).unwrap();
            self.analysis.edits(id, doc.buf.changes());
            if let Err(error) = self.completion.edits(
                &self.tape,
                id,
                doc.buf.revision(),
                doc.buf.text(),
                doc.buf.changes(),
            ) {
                self.message = error;
            }
            doc.buf.clear_changes();
        }
        let byte_delta = body.len() as isize - (view_end - view_start) as isize;
        let line_delta = new_lines as isize - view_lines as isize;
        let entry = self.collections.get_mut(&id).unwrap();
        let first_row = entry.excerpts[index].view_line + 1;
        entry.rows.splice(
            first_row..first_row + view_lines,
            std::iter::repeat_n(CollectionRow::Body, new_lines),
        );
        let mut seen = false;
        for excerpt in &mut entry.excerpts {
            if seen {
                excerpt.view_start = (excerpt.view_start as isize + byte_delta) as usize;
                excerpt.view_end = (excerpt.view_end as isize + byte_delta) as usize;
                excerpt.view_line = (excerpt.view_line as isize + line_delta) as usize;
            } else if excerpt.view_start == view_start {
                seen = true;
                excerpt.view_lines = new_lines;
                excerpt.view_end = view_start + body.len();
            }
        }
        let revision = self.docs.get(id).unwrap().buf.revision();
        self.collections.get_mut(&id).unwrap().revision = revision;
    }
}

impl Editor {
    /// Per-row source facts for the renderer (0049 §6): the row's role,
    /// and for body rows the source document + this row's source byte
    /// span + the query hits inside it (source bytes) + whether the
    /// caret sits in this card.
    pub fn collection_row_info(
        &self,
        doc: strop_core::id::DocumentId,
        line: usize,
    ) -> Option<CollectionRowInfo> {
        let collection = self.collections.get(&doc)?;
        let kind = *collection.rows.get(line)?;
        let caret_line = if doc == self.current() {
            self.buf().line_of(self.head())
        } else {
            usize::MAX
        };
        let mut info = CollectionRowInfo {
            kind,
            source: None,
            source_matches: Vec::new(),
            card_active: false,
            source_dirty: false,
            source_readonly: false,
        };
        let index = collection
            .excerpts
            .partition_point(|excerpt| excerpt.view_line <= line);
        if let Some(excerpt) = index
            .checked_sub(1)
            .and_then(|index| collection.excerpts.get(index))
        {
            if let Some(source) = self.docs.get(excerpt.source) {
                info.source_dirty = source.buf.dirty;
                info.source_readonly = source.buf.readonly;
            }
            let active_index = collection
                .excerpts
                .partition_point(|excerpt| excerpt.view_line <= caret_line);
            info.card_active = caret_line != usize::MAX
                && active_index
                    .checked_sub(1)
                    .and_then(|index| collection.excerpts.get(index))
                    .is_some_and(|active| active.source == excerpt.source);
            if kind == CollectionRow::Body
                && line > excerpt.view_line
                && line <= excerpt.view_line + excerpt.view_lines
            {
                let source = self.docs.get(excerpt.source)?;
                let source_line = source.buf.line_of(excerpt.start) + line - excerpt.view_line - 1;
                let start = source.buf.line_start(source_line);
                let end = source.buf.line_end(source_line);
                info.source = Some((excerpt.source, start, end));
                let first = excerpt
                    .matches
                    .partition_point(|(offset, length)| offset.saturating_add(*length) <= start);
                info.source_matches = excerpt.matches[first..]
                    .iter()
                    .copied()
                    .take_while(|(offset, _)| *offset < end)
                    .map(|(offset, length)| {
                        (
                            offset.max(start),
                            offset.saturating_add(length).min(end) - offset.max(start),
                        )
                    })
                    .collect();
            }
        }
        Some(info)
    }

    /// A collection view row's role (0049 §6) for the renderer —
    /// structure as data, never text parsing.
    pub fn collection_row_kind(
        &self,
        doc: strop_core::id::DocumentId,
        line: usize,
    ) -> Option<CollectionRow> {
        self.collections.get(&doc)?.rows.get(line).copied()
    }

    /// The SOURCE line number for a collection view row (0049 §6):
    /// Some(Some(n)) for body rows, Some(None) for chrome (title,
    /// headers — the gutter stays blank there), None for ordinary
    /// buffers.
    pub fn collection_source_lineno(
        &self,
        doc: strop_core::id::DocumentId,
        line: usize,
    ) -> Option<Option<usize>> {
        let collection = self.collections.get(&doc)?;
        let index = collection
            .excerpts
            .partition_point(|excerpt| excerpt.view_line < line);
        if let Some(excerpt) = index
            .checked_sub(1)
            .and_then(|index| collection.excerpts.get(index))
        {
            if line <= excerpt.view_line + excerpt.view_lines {
                let source = self.docs.get(excerpt.source)?;
                return Some(Some(
                    source.buf.line_of(excerpt.start) + line - excerpt.view_line,
                ));
            }
        }
        Some(None) // title row
    }
}

impl Editor {
    pub fn collection_unsaved(&self, document: DocumentId) -> bool {
        self.collections.get(&document).is_some_and(|collection| {
            collection.excerpts.iter().any(|excerpt| {
                self.docs
                    .get(excerpt.source)
                    .is_some_and(|source| source.buf.dirty)
            })
        })
    }
}

impl Editor {
    /// Map a real buffer byte to its authoritative source; chrome has no source byte.
    pub fn source_position(
        &self,
        document: DocumentId,
        byte: usize,
    ) -> Option<(DocumentId, usize)> {
        let view = self.docs.get(document)?;
        let Some(collection) = self.collections.get(&document) else {
            return Some((
                document,
                view.buf.clamp_boundary(byte.min(view.buf.len_bytes())),
            ));
        };
        let after = collection
            .excerpts
            .partition_point(|excerpt| excerpt.view_start <= byte);
        let excerpt = collection.excerpts.get(after.checked_sub(1)?)?;
        if byte >= excerpt.view_end {
            return None;
        }
        let source = self.docs.get(excerpt.source)?;
        let offset = (excerpt.start + byte - excerpt.view_start).min(excerpt.end);
        Some((
            excerpt.source,
            source
                .buf
                .clamp_boundary(offset.min(source.buf.len_bytes())),
        ))
    }

    /// Strict insertion coordinates for source edits. Unlike navigation this
    /// never clamps an invalid byte, and admits an excerpt's source EOF only
    /// when the same verified write-back predicate owns that insertion.
    pub(crate) fn source_edit_position(
        &self,
        document: DocumentId,
        byte: usize,
    ) -> Option<(DocumentId, usize)> {
        let view = self.docs.get(document)?;
        let character = view.buf.text().try_byte_to_char(byte).ok()?;
        if view.buf.text().char_to_byte(character) != byte {
            return None;
        }
        let Some(collection) = self.collections.get(&document) else {
            return Some((document, byte));
        };
        if collection.revision != view.buf.revision() {
            return None;
        }
        let after = collection
            .excerpts
            .partition_point(|excerpt| excerpt.view_start <= byte);
        let excerpt = collection.excerpts.get(after.checked_sub(1)?)?;
        let body_end = excerpt.view_start + excerpt.end - excerpt.start;
        if !strop_core::projectguard::span_owns_edit(
            byte,
            byte,
            excerpt.view_start,
            body_end,
            excerpt.view_end,
        ) {
            return None;
        }
        let source = self.docs.get(excerpt.source)?;
        let offset = excerpt.start + byte - excerpt.view_start;
        let character = source.buf.text().try_byte_to_char(offset).ok()?;
        (source.buf.text().char_to_byte(character) == offset).then_some((excerpt.source, offset))
    }
}