Skip to main content

lexical_ui/code/
flat_structure_utils.rs

1//! `FlatStructureUtils.ts`: the lines of a code block, which are the runs
2//! of [`CodeHighlightNode`](super::CodeHighlightNode)s and tabs between its
3//! line breaks.
4
5use super::code_highlight_node::{create_code_highlight_node, is_code_highlight_node};
6use crate::LexicalError;
7use crate::caret::{CaretDirection, get_sibling_caret};
8use crate::error::invariant;
9use crate::lexical_selection::{PointType, SelectionRef, Which};
10use crate::lexical_utils::get_text_direction;
11use crate::node::NodeRef;
12use crate::nodes::element::Direction;
13use crate::nodes::line_break::{LineBreakNode, create_line_break_node};
14use crate::nodes::tab::{TabNode, create_tab_node};
15use crate::selection_insert_nodes::{RawTextVisitor, tokenize_raw_text};
16
17const SPACE: u16 = b' ' as u16;
18
19fn is_tab_node(node: &NodeRef) -> bool {
20    node.get_latest().is::<TabNode>()
21}
22
23fn is_line_break_node(node: &NodeRef) -> bool {
24    node.get_latest().is::<LineBreakNode>()
25}
26
27fn units(node: &NodeRef) -> Vec<u16> {
28    node.get_text_content().encode_utf16().collect()
29}
30
31/// `$getLastMatchingCodeNode(anchor, direction)`: the farthest node from
32/// `anchor` in `direction` across a run of code highlight and tab nodes,
33/// starting from `anchor` itself; `anchor` when it isn't one.
34fn get_last_matching_code_node(anchor: &NodeRef, direction: CaretDirection) -> NodeRef {
35    let mut matching_node = anchor.clone();
36    let mut caret = Some(get_sibling_caret(anchor, direction));
37    while let Some(c) =
38        caret.filter(|c| is_code_highlight_node(c.origin()) || is_tab_node(c.origin()))
39    {
40        matching_node = c.origin().clone();
41        caret = c.get_adjacent_caret();
42    }
43    matching_node
44}
45
46/// `$getFirstCodeNodeOfLine(anchor)`.
47pub fn get_first_code_node_of_line(anchor: &NodeRef) -> NodeRef {
48    get_last_matching_code_node(anchor, CaretDirection::Previous)
49}
50
51/// `$getLastCodeNodeOfLine(anchor)`.
52pub fn get_last_code_node_of_line(anchor: &NodeRef) -> NodeRef {
53    get_last_matching_code_node(anchor, CaretDirection::Next)
54}
55
56/// `$getCodeLineDirection(anchor)`: the direction of the first strongly
57/// directional character of the line's code highlight nodes, else the code
58/// block's own direction, else `None`.
59pub fn get_code_line_direction(anchor: &NodeRef) -> Option<Direction> {
60    let start = get_first_code_node_of_line(anchor);
61    let end = get_last_code_node_of_line(anchor);
62    let mut node = Some(start.clone());
63    while let Some(n) = node {
64        if is_code_highlight_node(&n)
65            && let Some(direction) = get_text_direction(&n.get_text_content())
66        {
67            return Some(direction);
68        }
69        if n == end {
70            break;
71        }
72        node = n.get_next_sibling();
73    }
74    start
75        .get_parent()
76        .filter(NodeRef::is_element)
77        .and_then(|parent| parent.element_data().get_direction())
78}
79
80/// A point `$getStartOfCodeInLine` returns: `{node, offset}`.
81#[derive(Clone, Debug, PartialEq, Eq)]
82pub struct CodeLinePoint {
83    pub node: NodeRef,
84    pub offset: usize,
85}
86
87/// `$getStartOfCodeInLine(anchor, offset)`: where the code of the line
88/// starts, past its leading spaces and tabs (the first non-blank character
89/// before the anchor when there is one), the line break before the line
90/// (offset 1), or `None` at the start of the block. Offsets are UTF-16 code
91/// units.
92pub fn get_start_of_code_in_line(
93    anchor: &NodeRef,
94    offset: usize,
95) -> Result<Option<CodeLinePoint>, LexicalError> {
96    let mut last = None;
97    let mut last_non_blank = None;
98    let mut node = anchor.clone();
99    let mut node_offset = offset;
100    let mut node_text_content = units(anchor);
101    loop {
102        if node_offset == 0 {
103            let Some(prev_sibling) = node.get_previous_sibling() else {
104                break;
105            };
106            invariant!(
107                is_code_highlight_node(&prev_sibling)
108                    || is_tab_node(&prev_sibling)
109                    || is_line_break_node(&prev_sibling),
110                "Expected a valid Code Node: CodeHighlightNode, TabNode, LineBreakNode"
111            );
112            node = prev_sibling;
113            if is_line_break_node(&node) {
114                last = Some(CodeLinePoint { node, offset: 1 });
115                break;
116            }
117            node_offset = node.get_text_content_size().saturating_sub(1);
118            node_text_content = units(&node);
119        } else {
120            node_offset -= 1;
121        }
122        let character = node_text_content.get(node_offset);
123        if is_code_highlight_node(&node) && character != Some(&SPACE) {
124            last_non_blank = Some(CodeLinePoint {
125                node: node.clone(),
126                offset: node_offset,
127            });
128        }
129    }
130    // lastNonBlank !== null: anchor in the middle of code; move to line beginning
131    if last_non_blank.is_some() {
132        return Ok(last_non_blank);
133    }
134    // Spaces, tabs or nothing ahead of anchor. `None` is JS's `null`, and
135    // `Some(None)` the `undefined` of reading past a text's end.
136    let mut code_character_at_anchor_offset: Option<Option<u16>> = None;
137    if offset < anchor.get_text_content_size() {
138        if is_code_highlight_node(anchor) {
139            code_character_at_anchor_offset = Some(units(anchor).get(offset).copied());
140        }
141    } else if let Some(next_sibling) = anchor.get_next_sibling().filter(is_code_highlight_node) {
142        code_character_at_anchor_offset = Some(units(&next_sibling).first().copied());
143    }
144    if code_character_at_anchor_offset.is_some()
145        && code_character_at_anchor_offset != Some(Some(SPACE))
146    {
147        // Borderline whitespace and code, move to line beginning
148        Ok(last)
149    } else {
150        Ok(find_next_non_blank_in_line(anchor, offset).or(last))
151    }
152}
153
154/// `findNextNonBlankInLine(anchor, offset)`.
155fn find_next_non_blank_in_line(anchor: &NodeRef, offset: usize) -> Option<CodeLinePoint> {
156    let mut node = anchor.clone();
157    let mut node_offset = offset;
158    let mut node_text_content = units(anchor);
159    let mut node_text_content_size = anchor.get_text_content_size();
160    loop {
161        if !is_code_highlight_node(&node) || node_offset == node_text_content_size {
162            node = node.get_next_sibling()?;
163            if is_line_break_node(&node) {
164                return None;
165            }
166            if is_code_highlight_node(&node) {
167                node_offset = 0;
168                node_text_content = units(&node);
169                node_text_content_size = node.get_text_content_size();
170            }
171        }
172        if is_code_highlight_node(&node) {
173            if node_text_content.get(node_offset) != Some(&SPACE) {
174                return Some(CodeLinePoint {
175                    node,
176                    offset: node_offset,
177                });
178            }
179            node_offset += 1;
180        }
181    }
182}
183
184/// `$getEndOfCodeInLine(anchor)`: the last node of the line.
185pub fn get_end_of_code_in_line(anchor: &NodeRef) -> Result<NodeRef, LexicalError> {
186    let last_node = get_last_code_node_of_line(anchor);
187    invariant!(
188        !is_line_break_node(&last_node),
189        "Unexpected lineBreakNode in getEndOfCodeInLine"
190    );
191    Ok(last_node)
192}
193
194struct Plainify(Vec<NodeRef>);
195
196impl RawTextVisitor for Plainify {
197    fn linebreak(&mut self) {
198        self.0.push(create_line_break_node());
199    }
200    fn tab(&mut self) {
201        self.0.push(create_tab_node());
202    }
203    fn text(&mut self, text: &str) {
204        self.0.push(create_code_highlight_node(text, None));
205    }
206}
207
208/// `$plainifyCodeContent(text)`: `text` as code highlight nodes with no
209/// highlight type, line breaks and tabs (`tokenizeRawText`).
210pub fn plainify_code_content(text: &str) -> Vec<NodeRef> {
211    let mut out = Plainify(Vec::new());
212    tokenize_raw_text(text, &mut out);
213    out.0
214}
215
216/// `$outdentLeadingSpaces(node, tabSize, selection)`: removes up to
217/// `tab_size` leading spaces from the code highlight node that starts a
218/// line, and moves the range selection's points in it back by as many
219/// (not before 0). Returns whether it removed any. JS takes any number and
220/// does nothing unless it is a positive integer; here it is a `usize`, and
221/// 0 does nothing.
222pub fn outdent_leading_spaces(
223    node: &NodeRef,
224    tab_size: usize,
225    selection: &SelectionRef,
226) -> Result<bool, LexicalError> {
227    if tab_size == 0 {
228        return Ok(false);
229    }
230    let text = node.get_text_content();
231    let leading = text.chars().take_while(|&c| c == ' ').count();
232    if leading == 0 {
233        return Ok(false);
234    }
235    let strip_count = tab_size.min(leading);
236    let line_key = node.get_key().to_owned();
237    let old_offset = |which| {
238        selection
239            .point(which)
240            .filter(|p| p.key == line_key && p.point_type == PointType::Text)
241            .map(|p| p.offset)
242    };
243    let old_anchor_offset = old_offset(Which::Anchor);
244    let old_focus_offset = old_offset(Which::Focus);
245    node.splice_text(0, strip_count as i64, "", false)?;
246    for (which, old) in [
247        (Which::Anchor, old_anchor_offset),
248        (Which::Focus, old_focus_offset),
249    ] {
250        if let Some(old) = old {
251            selection.point_set(
252                which,
253                &line_key,
254                old.saturating_sub(strip_count),
255                PointType::Text,
256            )?;
257        }
258    }
259    Ok(true)
260}
261
262#[cfg(test)]
263mod tests {
264    use super::*;
265    use crate::code::{code_nodes, create_code_node};
266    use crate::lexical_selection::{create_range_selection, set_selection};
267    use crate::{CreateEditorArgs, UpdateOptions, create_editor, get_root, reset_random_key};
268
269    /// What JS's `getTextDirection` returns, recorded from Node 24.
270    #[test]
271    fn get_text_direction_matches_js() {
272        let cases = [
273            ("", None),
274            ("123 ", None),
275            ("abc", Some(Direction::Ltr)),
276            (" שלום a", Some(Direction::Rtl)),
277            ("1 a שלום", Some(Direction::Ltr)),
278            ("😀", Some(Direction::Ltr)),
279            ("\u{FEFC}", Some(Direction::Rtl)),
280            ("\u{FEFD}", Some(Direction::Ltr)),
281            ("— ٣", Some(Direction::Rtl)),
282        ];
283        for (text, want) in cases {
284            assert_eq!(get_text_direction(text), want, "{text:?}");
285        }
286    }
287
288    /// A node's key, `$getStartOfCodeInLine` at each of its offsets,
289    /// `$getEndOfCodeInLine` and `$getCodeLineDirection`.
290    type LineCase = (
291        &'static str,
292        Vec<Option<(String, usize)>>,
293        &'static str,
294        Option<Direction>,
295    );
296
297    /// The line functions on one code block, against what JS's return
298    /// (recorded from Node 24 with the same keys): a tab, `"  ab"`, `" "`,
299    /// `"cd"`, a line break, `"   "`, a line break, `"x  y"` and `""`.
300    #[test]
301    fn line_functions_match_js() {
302        reset_random_key();
303        let editor = create_editor(CreateEditorArgs {
304            nodes: code_nodes(),
305            ..CreateEditorArgs::default()
306        });
307        editor
308            .update(
309                || {
310                    let code = create_code_node(Some("js"), None);
311                    let h = |text| create_code_highlight_node(text, None);
312                    code.append(&[
313                        create_tab_node(),
314                        h("  ab"),
315                        h(" "),
316                        h("cd"),
317                        create_line_break_node(),
318                        h("   "),
319                        create_line_break_node(),
320                        h("x  y"),
321                        h(""),
322                    ])?;
323                    get_root().append(std::slice::from_ref(&code))?;
324                    let all = code.get_children();
325                    let keys: Vec<_> = all.iter().map(|n| n.get_key().to_owned()).collect();
326                    assert_eq!(keys, ["2", "3", "4", "5", "6", "7", "8", "9", "10"]);
327
328                    let point = |p: Option<CodeLinePoint>| {
329                        p.map(|p| (p.node.get_key().to_owned(), p.offset))
330                    };
331                    let some = |k: &str, o| Some((k.to_owned(), o));
332                    // Key, then `$getStartOfCodeInLine` at each offset, `$getEndOfCodeInLine`
333                    // and `$getCodeLineDirection`.
334                    let want: [LineCase; 7] = [
335                        (
336                            "2",
337                            vec![some("3", 2), some("3", 2)],
338                            "5",
339                            Some(Direction::Ltr),
340                        ),
341                        (
342                            "3",
343                            vec![some("3", 2), some("3", 2), None, some("3", 2), some("3", 2)],
344                            "5",
345                            Some(Direction::Ltr),
346                        ),
347                        (
348                            "4",
349                            vec![some("3", 2), some("3", 2)],
350                            "5",
351                            Some(Direction::Ltr),
352                        ),
353                        (
354                            "5",
355                            vec![some("3", 2), some("3", 2), some("3", 2)],
356                            "5",
357                            Some(Direction::Ltr),
358                        ),
359                        ("7", vec![some("6", 1); 4], "7", None),
360                        (
361                            "9",
362                            vec![
363                                some("8", 1),
364                                some("9", 0),
365                                some("9", 0),
366                                some("9", 0),
367                                some("9", 0),
368                            ],
369                            "10",
370                            Some(Direction::Ltr),
371                        ),
372                        ("10", vec![some("9", 0)], "10", Some(Direction::Ltr)),
373                    ];
374                    for (key, starts, end, direction) in want {
375                        let node = all.iter().find(|n| n.get_key() == key).expect("a key");
376                        let got: Vec<_> = (0..starts.len())
377                            .map(|o| get_start_of_code_in_line(node, o).map(point))
378                            .collect::<Result<_, _>>()?;
379                        assert_eq!(got, starts, "start of {key}");
380                        assert_eq!(
381                            get_end_of_code_in_line(node)?.get_key(),
382                            end,
383                            "end of {key}"
384                        );
385                        assert_eq!(
386                            get_code_line_direction(node),
387                            direction,
388                            "direction of {key}"
389                        );
390                    }
391
392                    let plain: Vec<_> = plainify_code_content("a\tb\r\nc\n\n")
393                        .iter()
394                        .map(|n| (n.get_type(), n.get_text_content()))
395                        .collect();
396                    let want: Vec<_> = [
397                        ("code-highlight", "a"),
398                        ("tab", "\t"),
399                        ("code-highlight", "b"),
400                        ("linebreak", "\n"),
401                        ("code-highlight", "c"),
402                        ("linebreak", "\n"),
403                        ("linebreak", "\n"),
404                    ]
405                    .into_iter()
406                    .map(|(t, s)| (t, s.to_owned()))
407                    .collect();
408                    assert_eq!(plain, want);
409
410                    let spaces = &all[5];
411                    let selection = create_range_selection();
412                    selection.point_set(Which::Anchor, spaces.get_key(), 3, PointType::Text)?;
413                    selection.point_set(Which::Focus, spaces.get_key(), 1, PointType::Text)?;
414                    set_selection(Some(selection.clone()));
415                    let offsets = || {
416                        (
417                            selection.point(Which::Anchor).unwrap().offset,
418                            selection.point(Which::Focus).unwrap().offset,
419                        )
420                    };
421                    assert!(outdent_leading_spaces(spaces, 2, &selection)?);
422                    assert_eq!(
423                        (spaces.get_text_content(), offsets()),
424                        (" ".to_owned(), (1, 0))
425                    );
426                    assert!(!outdent_leading_spaces(spaces, 0, &selection)?);
427                    assert!(!outdent_leading_spaces(&all[7], 4, &selection)?);
428                    assert!(outdent_leading_spaces(spaces, 4, &selection)?);
429                    assert_eq!(
430                        (spaces.get_text_content(), offsets()),
431                        (String::new(), (0, 0))
432                    );
433                    Ok(())
434                },
435                UpdateOptions {
436                    discrete: true,
437                    ..UpdateOptions::default()
438                },
439            )
440            .unwrap();
441    }
442}