| 1 | use crate::{movement::Direction, syntax::TreeCursor, Range, RopeSlice, Selection, Syntax}; |
| 2 | |
| 3 | pub fn expand_selection(syntax: &Syntax, text: RopeSlice, selection: Selection) -> Selection { |
| 4 | let cursor = &mut syntax.walk(); |
| 5 | |
| 6 | selection.transform(|range| { |
| 7 | let from = text.char_to_byte(range.from()) as u32; |
| 8 | let to = text.char_to_byte(range.to()) as u32; |
| 9 | |
| 10 | let byte_range = from..to; |
| 11 | cursor.reset_to_byte_range(from, to); |
| 12 | |
| 13 | while cursor.node().byte_range() == byte_range { |
| 14 | if !cursor.goto_parent() { |
| 15 | break; |
| 16 | } |
| 17 | } |
| 18 | |
| 19 | let node = cursor.node(); |
| 20 | let from = text.byte_to_char(node.start_byte() as usize); |
| 21 | let to = text.byte_to_char(node.end_byte() as usize); |
| 22 | |
| 23 | Range::new(to, from).with_direction(range.direction()) |
| 24 | }) |
| 25 | } |
| 26 | |
| 27 | pub fn shrink_selection(syntax: &Syntax, text: RopeSlice, selection: Selection) -> Selection { |
| 28 | select_node_impl( |
| 29 | syntax, |
| 30 | text, |
| 31 | selection, |
| 32 | |cursor| { |
| 33 | cursor.goto_first_child(); |
| 34 | }, |
| 35 | None, |
| 36 | ) |
| 37 | } |
| 38 | |
| 39 | pub fn select_next_sibling(syntax: &Syntax, text: RopeSlice, selection: Selection) -> Selection { |
| 40 | select_node_impl( |
| 41 | syntax, |
| 42 | text, |
| 43 | selection, |
| 44 | |cursor| { |
| 45 | while !cursor.goto_next_sibling() { |
| 46 | if !cursor.goto_parent() { |
| 47 | break; |
| 48 | } |
| 49 | } |
| 50 | }, |
| 51 | Some(Direction::Forward), |
| 52 | ) |
| 53 | } |
| 54 | |
| 55 | pub fn select_all_siblings(syntax: &Syntax, text: RopeSlice, selection: Selection) -> Selection { |
| 56 | let mut cursor = syntax.walk(); |
| 57 | selection.transform_iter(move |range| { |
| 58 | let (from, to) = range.into_byte_range(text); |
| 59 | cursor.reset_to_byte_range(from as u32, to as u32); |
| 60 | |
| 61 | if !cursor.goto_parent_with(|parent| parent.child_count() > 1) { |
| 62 | return vec![range].into_iter(); |
| 63 | } |
| 64 | |
| 65 | select_children(&mut cursor, text, range).into_iter() |
| 66 | }) |
| 67 | } |
| 68 | |
| 69 | pub fn select_all_children(syntax: &Syntax, text: RopeSlice, selection: Selection) -> Selection { |
| 70 | let mut cursor = syntax.walk(); |
| 71 | selection.transform_iter(move |range| { |
| 72 | let (from, to) = range.into_byte_range(text); |
| 73 | cursor.reset_to_byte_range(from as u32, to as u32); |
| 74 | select_children(&mut cursor, text, range).into_iter() |
| 75 | }) |
| 76 | } |
| 77 | |
| 78 | fn select_children(cursor: &mut TreeCursor, text: RopeSlice, range: Range) -> Vec<Range> { |
| 79 | let children = cursor |
| 80 | .children() |
| 81 | .filter(|child| child.is_named()) |
| 82 | .map(|child| Range::from_node(child, text, range.direction())) |
| 83 | .collect::<Vec<_>>(); |
| 84 | |
| 85 | if !children.is_empty() { |
| 86 | children |
| 87 | } else { |
| 88 | vec![range] |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | pub fn select_prev_sibling(syntax: &Syntax, text: RopeSlice, selection: Selection) -> Selection { |
| 93 | select_node_impl( |
| 94 | syntax, |
| 95 | text, |
| 96 | selection, |
| 97 | |cursor| { |
| 98 | while !cursor.goto_previous_sibling() { |
| 99 | if !cursor.goto_parent() { |
| 100 | break; |
| 101 | } |
| 102 | } |
| 103 | }, |
| 104 | Some(Direction::Backward), |
| 105 | ) |
| 106 | } |
| 107 | |
| 108 | fn select_node_impl<F>( |
| 109 | syntax: &Syntax, |
| 110 | text: RopeSlice, |
| 111 | selection: Selection, |
| 112 | motion: F, |
| 113 | direction: Option<Direction>, |
| 114 | ) -> Selection |
| 115 | where |
| 116 | F: Fn(&mut TreeCursor), |
| 117 | { |
| 118 | let cursor = &mut syntax.walk(); |
| 119 | |
| 120 | selection.transform(|range| { |
| 121 | let from = text.char_to_byte(range.from()) as u32; |
| 122 | let to = text.char_to_byte(range.to()) as u32; |
| 123 | |
| 124 | cursor.reset_to_byte_range(from, to); |
| 125 | |
| 126 | motion(cursor); |
| 127 | |
| 128 | let node = cursor.node(); |
| 129 | let from = text.byte_to_char(node.start_byte() as usize); |
| 130 | let to = text.byte_to_char(node.end_byte() as usize); |
| 131 | |
| 132 | Range::new(from, to).with_direction(direction.unwrap_or_else(|| range.direction())) |
| 133 | }) |
| 134 | } |