Skip to content

Commit

Permalink
Fix x behavior on empty lines
Browse files Browse the repository at this point in the history
  • Loading branch information
dpc committed Apr 18, 2024
1 parent 61b5ee6 commit 53e7e82
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
20 changes: 19 additions & 1 deletion helix-core/src/selection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ pub struct Range {
/// The previous visual offset (softwrapped lines and columns) from
/// the start of the line
pub old_visual_position: Option<(u32, u32)>,
/// True if range is intended to contain whole lines (e.g. after `line_extend_below`).
/// Useful to prevent first `line_extend_below` on an empty line selecting the next line.
pub line_mode: bool,
}

impl Range {
Expand All @@ -66,6 +69,7 @@ impl Range {
anchor,
head,
old_visual_position: None,
line_mode: false,
}
}

Expand Down Expand Up @@ -132,6 +136,7 @@ impl Range {
anchor: self.head,
head: self.anchor,
old_visual_position: self.old_visual_position,
line_mode: self.line_mode,
}
}

Expand Down Expand Up @@ -204,12 +209,14 @@ impl Range {
anchor: self.anchor.min(from),
head: self.head.max(to),
old_visual_position: None,
line_mode: self.line_mode,
}
} else {
Self {
anchor: self.anchor.max(to),
head: self.head.min(from),
old_visual_position: None,
line_mode: self.line_mode,
}
}
}
Expand All @@ -225,12 +232,14 @@ impl Range {
anchor: self.anchor.max(other.anchor),
head: self.head.min(other.head),
old_visual_position: None,
line_mode: self.line_mode || other.line_mode,
}
} else {
Range {
anchor: self.from().min(other.from()),
head: self.to().max(other.to()),
old_visual_position: None,
line_mode: self.line_mode || other.line_mode,
}
}
}
Expand Down Expand Up @@ -289,6 +298,7 @@ impl Range {
} else {
None
},
line_mode: self.line_mode,
}
}

Expand All @@ -312,6 +322,7 @@ impl Range {
anchor: self.anchor,
head: next_grapheme_boundary(slice, self.head),
old_visual_position: self.old_visual_position,
line_mode: self.line_mode,
}
} else {
*self
Expand Down Expand Up @@ -376,6 +387,11 @@ impl Range {
let second = graphemes.next();
first.is_some() && second.is_none()
}

pub fn with_line_mode(mut self, line_mode: bool) -> Self {
self.line_mode = line_mode;
self
}
}

impl From<(usize, usize)> for Range {
Expand All @@ -384,6 +400,7 @@ impl From<(usize, usize)> for Range {
anchor,
head,
old_visual_position: None,
line_mode: false,
}
}
}
Expand Down Expand Up @@ -506,7 +523,8 @@ impl Selection {
ranges: smallvec![Range {
anchor,
head,
old_visual_position: None
old_visual_position: None,
line_mode: false,
}],
primary_index: 0,
}
Expand Down
8 changes: 5 additions & 3 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2468,7 +2468,7 @@ fn extend_line_impl(cx: &mut Context, extend: Extend) {
);

// extend to previous/next line if current line is selected
let (anchor, head) = if range.from() == start && range.to() == end {
let (anchor, head) = if range.from() == start && range.to() == end && range.line_mode {
match extend {
Extend::Above => (end, text.line_to_char(start_line.saturating_sub(count))),
Extend::Below => (
Expand All @@ -2486,7 +2486,7 @@ fn extend_line_impl(cx: &mut Context, extend: Extend) {
}
};

Range::new(anchor, head)
Range::new(anchor, head).with_line_mode(true)
});

doc.set_selection(view.id, selection);
Expand Down Expand Up @@ -2557,7 +2557,9 @@ fn extend_to_line_bounds(cx: &mut Context) {
let start = text.line_to_char(start_line);
let end = text.line_to_char((end_line + 1).min(text.len_lines()));

Range::new(start, end).with_direction(range.direction())
Range::new(start, end)
.with_direction(range.direction())
.with_line_mode(true)
}),
);
}
Expand Down

0 comments on commit 53e7e82

Please sign in to comment.