tui-treelistview 0.2.2

Interactive tree list widget for Ratatui
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
use std::hash::Hash;

use rustc_hash::{FxBuildHasher, FxHashMap, FxHashSet};
use smallvec::SmallVec;

use crate::context::{TreeExpansionState, TreeMatchState};
use crate::model::{
    TreeChildren, TreeFilter, TreeFilterConfig, TreeModel, TreeQuery, TreeRevision,
    TreeRootVisibility, TreeSort,
};
use crate::traversal::TreePostorder;

pub struct OccurrencePath<Id> {
    root_parent: Option<Id>,
    ids: SmallVec<[Id; 16]>,
}

impl<Id> OccurrencePath<Id> {
    pub fn len(&self) -> usize {
        self.ids.len()
    }
}

/// A node in the flat visible tree projection.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct ProjectedNode<Id> {
    id: Id,
    parent: Option<Id>,
    parent_index: Option<usize>,
    level: usize,
    is_last_sibling: bool,
    visible_child_count: usize,
    expansion: TreeExpansionState,
    match_state: TreeMatchState,
}

impl<Id: Copy> ProjectedNode<Id> {
    #[must_use]
    pub const fn id(self) -> Id {
        self.id
    }

    #[must_use]
    pub const fn parent(self) -> Option<Id> {
        self.parent
    }

    /// Возвращает индекс родительского вхождения в проекции строк.
    ///
    /// В отличие от [`Self::parent`], различает повторные вхождения одной вершины
    /// модели в проекции DAG.
    #[must_use]
    pub const fn parent_index(self) -> Option<usize> {
        self.parent_index
    }

    #[must_use]
    pub const fn level(self) -> usize {
        self.level
    }

    #[must_use]
    pub const fn is_last_sibling(self) -> bool {
        self.is_last_sibling
    }

    #[must_use]
    pub const fn visible_child_count(self) -> usize {
        self.visible_child_count
    }

    #[must_use]
    pub const fn expansion(self) -> TreeExpansionState {
        self.expansion
    }

    #[must_use]
    pub const fn match_state(self) -> TreeMatchState {
        self.match_state
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
struct ProjectionStamp {
    model: TreeRevision,
    filter: PolicyStamp,
    sort: PolicyStamp,
    expansion: TreeRevision,
    filter_config: TreeFilterConfig,
    root_visibility: TreeRootVisibility,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
struct PolicyStamp {
    revision: TreeRevision,
    generation: TreeRevision,
}

impl PolicyStamp {
    const fn new(revision: TreeRevision, generation: TreeRevision) -> Self {
        Self {
            revision,
            generation,
        }
    }
}

/// A cached flat projection shared by navigation and rendering.
pub struct TreeProjection<Id> {
    nodes: Vec<ProjectedNode<Id>>,
    index: FxHashMap<Id, usize>,
    filter_memo: FxHashMap<Id, bool>,
    direct_matches: FxHashSet<Id>,
    stamp: Option<ProjectionStamp>,
}

impl<Id: Copy + Eq + Hash> TreeProjection<Id> {
    pub(crate) fn with_capacity(capacity: usize) -> Self {
        Self {
            nodes: Vec::with_capacity(capacity),
            index: FxHashMap::with_capacity_and_hasher(capacity, FxBuildHasher),
            filter_memo: FxHashMap::with_capacity_and_hasher(capacity, FxBuildHasher),
            direct_matches: FxHashSet::with_capacity_and_hasher(capacity, FxBuildHasher),
            stamp: None,
        }
    }

    /// Returns rows in display order.
    #[must_use]
    pub fn nodes(&self) -> &[ProjectedNode<Id>] {
        &self.nodes
    }

    /// Returns the number of visible rows.
    #[must_use]
    pub const fn len(&self) -> usize {
        self.nodes.len()
    }

    /// Returns `true` when the projection is empty.
    #[must_use]
    pub const fn is_empty(&self) -> bool {
        self.nodes.is_empty()
    }

    /// Возвращает индекс первого видимого вхождения узла.
    #[must_use]
    pub fn index_of(&self, id: Id) -> Option<usize> {
        self.index.get(&id).copied()
    }

    /// Возвращает первое видимое вхождение узла по идентификатору.
    #[must_use]
    pub fn get_by_id(&self, id: Id) -> Option<ProjectedNode<Id>> {
        self.index_of(id)
            .and_then(|index| self.nodes.get(index))
            .copied()
    }

    pub(crate) fn is_current<T, F, S>(
        &self,
        model: &T,
        query: &TreeQuery<F, S>,
        expansion: TreeRevision,
    ) -> bool
    where
        T: TreeModel<Id = Id>,
    {
        self.stamp == Some(Self::stamp(model, query, expansion))
    }

    pub(crate) fn rebuild<T, F, S, E>(
        &mut self,
        model: &T,
        query: &TreeQuery<F, S>,
        expansion_revision: TreeRevision,
        is_expanded: E,
    ) where
        T: TreeModel<Id = Id>,
        F: TreeFilter<T>,
        S: TreeSort<T>,
        E: Fn(Option<Id>, Id) -> bool,
    {
        self.nodes.clear();
        self.index.clear();
        self.reserve(model.size_hint());

        let filtering = matches!(query.filter_config(), TreeFilterConfig::Enabled { .. });
        if filtering {
            self.compute_filter_matches(model, query.filter());
        } else {
            self.filter_memo.clear();
            self.direct_matches.clear();
        }

        let mut roots: SmallVec<[Id; 8]> = model.roots().collect();
        Self::sort_ids(model, query.sort(), &mut roots);
        let mut stack = Vec::with_capacity(model.size_hint().min(1024).max(roots.len()));

        match query.root_visibility() {
            TreeRootVisibility::Visible => {
                Self::push_children(&mut stack, &roots, None, None, 0);
            }
            TreeRootVisibility::Hidden => {
                for root in roots.iter().rev().copied() {
                    let mut children =
                        self.visible_children(query, model.children(root).loaded_slice());
                    Self::sort_ids(model, query.sort(), &mut children);
                    Self::push_children(&mut stack, &children, Some(root), None, 0);
                }
            }
        }

        while let Some(frame) = stack.pop() {
            if filtering && !self.filter_memo.get(&frame.id).copied().unwrap_or(false) {
                continue;
            }

            let children_state = model.children(frame.id);
            let mut visible_children = match children_state {
                TreeChildren::Loaded(children) => self.visible_children(query, children),
                TreeChildren::Leaf | TreeChildren::Unloaded | TreeChildren::Loading => {
                    SmallVec::new()
                }
            };
            Self::sort_ids(model, query.sort(), &mut visible_children);

            let expansion = match children_state {
                TreeChildren::Leaf => TreeExpansionState::Leaf,
                TreeChildren::Unloaded => TreeExpansionState::Unloaded,
                TreeChildren::Loading => TreeExpansionState::Loading,
                TreeChildren::Loaded(_) if visible_children.is_empty() => TreeExpansionState::Leaf,
                TreeChildren::Loaded(_) => match query.filter_config() {
                    TreeFilterConfig::Enabled { auto_expand: true } => {
                        TreeExpansionState::ForcedByFilter
                    }
                    TreeFilterConfig::Disabled
                    | TreeFilterConfig::Enabled { auto_expand: false } => {
                        if is_expanded(frame.parent, frame.id) {
                            TreeExpansionState::Expanded
                        } else {
                            TreeExpansionState::Collapsed
                        }
                    }
                },
            };
            let match_state = if !filtering {
                TreeMatchState::Unfiltered
            } else if self.direct_matches.contains(&frame.id) {
                TreeMatchState::Direct
            } else {
                TreeMatchState::Ancestor
            };

            let index = self.nodes.len();
            self.nodes.push(ProjectedNode {
                id: frame.id,
                parent: frame.parent,
                parent_index: frame.parent_index,
                level: frame.level,
                is_last_sibling: frame.is_last_sibling,
                visible_child_count: visible_children.len(),
                expansion,
                match_state,
            });
            self.index.entry(frame.id).or_insert(index);

            if expansion.is_expanded() {
                Self::push_children(
                    &mut stack,
                    &visible_children,
                    Some(frame.id),
                    Some(index),
                    frame.level.saturating_add(1),
                );
            }
        }

        self.stamp = Some(Self::stamp(model, query, expansion_revision));
    }

    fn stamp<T, F, S>(
        model: &T,
        query: &TreeQuery<F, S>,
        expansion: TreeRevision,
    ) -> ProjectionStamp
    where
        T: TreeModel<Id = Id>,
    {
        ProjectionStamp {
            model: model.revision(),
            filter: PolicyStamp::new(query.filter_revision(), query.filter_generation()),
            sort: PolicyStamp::new(query.sort_revision(), query.sort_generation()),
            expansion,
            filter_config: query.filter_config(),
            root_visibility: query.root_visibility(),
        }
    }

    fn reserve(&mut self, hint: usize) {
        if hint == 0 {
            return;
        }
        reserve_to(&mut self.nodes, hint);
        reserve_map_to(&mut self.index, hint);
        reserve_map_to(&mut self.filter_memo, hint);
        let extra = hint.saturating_sub(self.direct_matches.len());
        self.direct_matches.reserve(extra);
    }

    fn visible_children<F, S>(
        &self,
        query: &TreeQuery<F, S>,
        children: &[Id],
    ) -> SmallVec<[Id; 8]> {
        match query.filter_config() {
            TreeFilterConfig::Disabled => children.iter().copied().collect(),
            TreeFilterConfig::Enabled { .. } => children
                .iter()
                .copied()
                .filter(|child| self.filter_memo.get(child).copied().unwrap_or(false))
                .collect(),
        }
    }

    fn compute_filter_matches<T, F>(&mut self, model: &T, filter: &F)
    where
        T: TreeModel<Id = Id>,
        F: TreeFilter<T>,
    {
        self.filter_memo.clear();
        self.direct_matches.clear();
        for node in TreePostorder::forest(model) {
            let direct = filter.is_match(model, node.id);
            if direct {
                self.direct_matches.insert(node.id);
            }
            let descendant = node
                .children
                .iter()
                .any(|child| self.filter_memo.get(child).copied().unwrap_or(false));
            self.filter_memo.insert(node.id, direct || descendant);
        }
    }

    fn sort_ids<T, S>(model: &T, sort: &S, ids: &mut [Id])
    where
        T: TreeModel<Id = Id>,
        S: TreeSort<T>,
    {
        if sort.is_enabled() {
            ids.sort_by(|left, right| sort.compare(model, *left, *right));
        }
    }

    pub(crate) fn occurrence_path(&self, index: usize) -> Option<OccurrencePath<Id>> {
        let mut ids = SmallVec::new();
        let mut cursor = Some(index);
        let mut root_parent = None;
        while let Some(index) = cursor {
            let node = self.nodes.get(index)?;
            ids.push(node.id);
            cursor = node.parent_index;
            if cursor.is_none() {
                root_parent = node.parent;
            }
        }
        ids.reverse();
        Some(OccurrencePath { root_parent, ids })
    }

    pub(crate) fn index_of_path(&self, path: &OccurrencePath<Id>) -> Option<usize> {
        self.index_of_path_prefix(path, path.len())
    }

    pub(crate) fn index_of_path_prefix(
        &self,
        path: &OccurrencePath<Id>,
        end: usize,
    ) -> Option<usize> {
        let ids = path.ids.get(..end)?;
        let (&id, _) = ids.split_last()?;
        let first = self.index_of(id)?;
        if self.path_matches(first, path.root_parent, ids) {
            return Some(first);
        }
        self.nodes[first + 1..]
            .iter()
            .enumerate()
            .filter(|(_, node)| node.id == id)
            .find_map(|(offset, _)| {
                let index = first + 1 + offset;
                self.path_matches(index, path.root_parent, ids)
                    .then_some(index)
            })
    }

    fn path_matches(&self, index: usize, root_parent: Option<Id>, ids: &[Id]) -> bool {
        let mut cursor = Some(index);
        let mut actual_root_parent = None;
        for &expected_id in ids.iter().rev() {
            let Some(node) = cursor.and_then(|index| self.nodes.get(index)) else {
                return false;
            };
            if node.id != expected_id {
                return false;
            }
            cursor = node.parent_index;
            actual_root_parent = node.parent;
        }
        cursor.is_none() && actual_root_parent == root_parent
    }

    fn push_children(
        stack: &mut Vec<ProjectionFrame<Id>>,
        children: &[Id],
        parent: Option<Id>,
        parent_index: Option<usize>,
        level: usize,
    ) {
        let last = children.len().saturating_sub(1);
        stack.extend(
            children
                .iter()
                .copied()
                .enumerate()
                .rev()
                .map(|(index, id)| ProjectionFrame {
                    id,
                    parent,
                    parent_index,
                    level,
                    is_last_sibling: index == last,
                }),
        );
    }
}

struct ProjectionFrame<Id> {
    id: Id,
    parent: Option<Id>,
    parent_index: Option<usize>,
    level: usize,
    is_last_sibling: bool,
}

fn reserve_to<T>(values: &mut Vec<T>, capacity: usize) {
    values.reserve(capacity.saturating_sub(values.len()));
}

fn reserve_map_to<K: Eq + Hash, V>(values: &mut FxHashMap<K, V>, capacity: usize) {
    values.reserve(capacity.saturating_sub(values.len()));
}