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
use promkit_core::{
ContentPosition, CreatedGraphemes, Widget, WidgetLayout, grapheme::StyledGraphemes,
};
mod document;
pub use document::Document;
pub mod config;
pub use config::Config;
pub mod path;
pub mod treez;
pub use treez::Row;
/// Represents the state of a tree structure within the application.
#[derive(Clone)]
pub struct State {
pub document: Document,
pub config: Config,
}
impl Widget for State {
fn create_graphemes(&self) -> CreatedGraphemes {
let symbol = |row: &Row| -> &str {
if row.has_children && !row.collapsed {
&self.config.unfolded_symbol
} else {
&self.config.folded_symbol
}
};
let rows = self.document.visible_rows();
let active_row = self.document.visible_position();
let mut lines = rows
.iter()
.enumerate()
.map(|(offset, row)| {
if offset == active_row {
StyledGraphemes::from_str(
format!(
"{}{}{}",
symbol(row),
" ".repeat(row.depth * self.config.indent),
row.id,
),
self.config.active_item_style,
)
} else {
StyledGraphemes::from_str(
format!(
"{}{}{}",
" ".repeat(StyledGraphemes::from(symbol(row)).widths()),
" ".repeat(row.depth * self.config.indent),
row.id,
),
self.config.inactive_item_style,
)
}
})
.collect::<Vec<_>>();
if self.config.show_line_numbers {
lines = super::with_line_numbers(
lines,
self.document.visible_line_numbers(),
self.document.line_count(),
);
}
CreatedGraphemes {
graphemes: StyledGraphemes::from_lines(lines),
layout: WidgetLayout {
max_height: self.config.lines,
..Default::default()
},
cursor: (!rows.is_empty()).then_some(ContentPosition {
row: active_row,
column: 0,
}),
}
}
}
impl State {
/// Interprets a tree content position as a semantic operation target.
///
/// Wrapped visual rows are normalized to their logical content row by the
/// core renderer before this method resolves the underlying document row.
pub fn hit_at(&self, position: ContentPosition) -> Option<TreeHit> {
self.document
.row_index_at_visible_position(position.row)
.map(|row_index| TreeHit::Toggle { row_index })
}
}
/// Semantic targets exposed by the tree widget.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum TreeHit {
Toggle { row_index: usize },
}
#[cfg(test)]
mod tests {
use super::*;
mod state {
use super::*;
mod hit_at {
use super::*;
#[test]
fn resolves_visible_rows() {
let state = State {
document: Document::new(vec![
Row {
id: "root".into(),
path: vec!["root".into()],
depth: 0,
has_children: true,
collapsed: false,
},
Row {
id: "child".into(),
path: vec!["root".into(), "child".into()],
depth: 1,
has_children: false,
collapsed: false,
},
]),
config: Config::default(),
};
assert_eq!(
state.hit_at(ContentPosition { row: 1, column: 20 }),
Some(TreeHit::Toggle { row_index: 1 })
);
assert_eq!(state.hit_at(ContentPosition { row: 2, column: 0 }), None);
}
}
mod create_graphemes {
use super::*;
#[test]
fn preserves_expanded_line_numbers_after_toggle() {
let mut state = State {
document: Document::new(vec![
Row {
id: "root".into(),
path: vec!["root".into()],
depth: 0,
has_children: true,
collapsed: false,
},
Row {
id: "child".into(),
path: vec!["root".into(), "child".into()],
depth: 1,
has_children: false,
collapsed: false,
},
Row {
id: "sibling".into(),
path: vec!["sibling".into()],
depth: 0,
has_children: false,
collapsed: false,
},
]),
config: Config {
show_line_numbers: true,
..Default::default()
},
};
assert_eq!(state.document.visible_line_numbers(), vec![1, 2, 3]);
state.document.toggle();
assert_eq!(state.document.visible_line_numbers(), vec![1, 3]);
let rendered = state.create_graphemes().graphemes.to_string();
assert!(rendered.starts_with("1 "));
assert!(rendered.contains("\n3 "));
assert!(!rendered.contains("\n2 "));
}
}
}
}