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 Dec 5, 2022
1 parent 5781aa0 commit 9ff48dd
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
21 changes: 20 additions & 1 deletion helix-core/src/selection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ pub struct Range {
/// The head of the range, moved when extending.
pub head: usize,
pub horiz: Option<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 @@ -62,6 +65,7 @@ impl Range {
anchor,
head,
horiz: None,
line_mode: false,
}
}

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

Expand Down Expand Up @@ -186,6 +191,7 @@ impl Range {
anchor,
head,
horiz: None,
line_mode: false,
}
}

Expand All @@ -199,12 +205,14 @@ impl Range {
anchor: self.anchor.min(from),
head: self.head.max(to),
horiz: None,
line_mode: self.line_mode,
}
} else {
Self {
anchor: self.anchor.max(to),
head: self.head.min(from),
horiz: None,
line_mode: self.line_mode,
}
}
}
Expand All @@ -220,12 +228,14 @@ impl Range {
anchor: self.anchor.max(other.anchor),
head: self.head.min(other.head),
horiz: None,
line_mode: self.line_mode || other.line_mode,
}
} else {
Range {
anchor: self.from().min(other.from()),
head: self.to().max(other.to()),
horiz: None,
line_mode: self.line_mode || other.line_mode,
}
}
}
Expand Down Expand Up @@ -284,6 +294,7 @@ impl Range {
} else {
None
},
line_mode: self.line_mode,
}
}

Expand All @@ -307,6 +318,7 @@ impl Range {
anchor: self.anchor,
head: next_grapheme_boundary(slice, self.head),
horiz: self.horiz,
line_mode: self.line_mode,
}
} else {
*self
Expand Down Expand Up @@ -371,6 +383,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 @@ -379,6 +396,7 @@ impl From<(usize, usize)> for Range {
anchor,
head,
horiz: None,
line_mode: false,
}
}
}
Expand Down Expand Up @@ -482,7 +500,8 @@ impl Selection {
ranges: smallvec![Range {
anchor,
head,
horiz: None
horiz: 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 @@ -2059,7 +2059,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 @@ -2074,7 +2074,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 All @@ -2092,7 +2092,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 9ff48dd

Please sign in to comment.