on master help sign in
Use requested direction for new textobject selection range
274f2eaMichael Davis
helix-core/src/movement.rs+5−3
@@ -389,6 +389,8 @@ fn reached_target(target: WordMotionTarget, prev_ch: char, next_ch: char) -> boo
389 }
389 }
390}
390}
391
391
392+/// Finds the range of the next or previous textobject in the syntax sub-tree of `node`.
393+/// Returns the range in the forwards direction.
392pub fn goto_treesitter_object(
394pub fn goto_treesitter_object(
393 slice: RopeSlice,
395 slice: RopeSlice,
394 range: Range,
396 range: Range,
@@ -419,8 +421,8 @@ pub fn goto_treesitter_object(
419 .filter(|n| n.start_byte() > byte_pos)
421 .filter(|n| n.start_byte() > byte_pos)
420 .min_by_key(|n| n.start_byte())?,
422 .min_by_key(|n| n.start_byte())?,
421 Direction::Backward => nodes
423 Direction::Backward => nodes
422− .filter(|n| n.start_byte() < byte_pos)
424+ .filter(|n| n.end_byte() < byte_pos)
423− .max_by_key(|n| n.start_byte())?,
425+ .max_by_key(|n| n.end_byte())?,
424 };
426 };
425
427
426 let len = slice.len_bytes();
428 let len = slice.len_bytes();
@@ -434,7 +436,7 @@ pub fn goto_treesitter_object(
434 let end_char = slice.byte_to_char(end_byte);
436 let end_char = slice.byte_to_char(end_byte);
435
437
436 // head of range should be at beginning
438 // head of range should be at beginning
437− Some(Range::new(end_char, start_char))
439+ Some(Range::new(start_char, end_char))
438 };
440 };
439 (0..count).fold(range, |range, _| get_range(range).unwrap_or(range))
441 (0..count).fold(range, |range, _| get_range(range).unwrap_or(range))
440}
442}
helix-core/src/selection.rs+11−1
@@ -122,7 +122,7 @@ impl Range {
122 }
122 }
123 }
123 }
124
124
125− // flips the direction of the selection
125+ /// Flips the direction of the selection
126 pub fn flip(&self) -> Self {
126 pub fn flip(&self) -> Self {
127 Self {
127 Self {
128 anchor: self.head,
128 anchor: self.head,
@@ -131,6 +131,16 @@ impl Range {
131 }
131 }
132 }
132 }
133
133
134+ /// Returns the selection if it goes in the direction of `direction`,
135+ /// flipping the selection otherwise.
136+ pub fn with_direction(self, direction: Direction) -> Self {
137+ if self.direction() == direction {
138+ self
139+ } else {
140+ self.flip()
141+ }
142+ }
143+
134 /// Check two ranges for overlap.
144 /// Check two ranges for overlap.
135 #[must_use]
145 #[must_use]
136 pub fn overlaps(&self, other: &Self) -> bool {
146 pub fn overlaps(&self, other: &Self) -> bool {
helix-term/src/commands.rs+1
@@ -4294,6 +4294,7 @@ fn goto_ts_object_impl(cx: &mut Context, object: &'static str, direction: Direct
4294 lang_config,
4294 lang_config,
4295 count,
4295 count,
4296 )
4296 )
4297+ .with_direction(direction)
4297 });
4298 });
4298
4299
4299 doc.set_selection(view.id, selection);
4300 doc.set_selection(view.id, selection);