aboutsummaryrefslogtreecommitdiff
path: root/helix-core
diff options
context:
space:
mode:
authorNathan Vegdahl2021-07-20 19:23:40 +0000
committerNathan Vegdahl2021-07-20 19:23:40 +0000
commitc9300ec35f1baeab92a5375814b91bb81c66de73 (patch)
tree08703716328b684b12ab24f53df94c5fefa70587 /helix-core
parent1c6b5581f01371a00dc7f6f6e1720ad8af61ec7a (diff)
Fix comment toggle command also sometimes toggling the next line.
Diffstat (limited to 'helix-core')
-rw-r--r--helix-core/src/comment.rs3
-rw-r--r--helix-core/src/selection.rs14
2 files changed, 15 insertions, 2 deletions
diff --git a/helix-core/src/comment.rs b/helix-core/src/comment.rs
index 5d564055..4fcece57 100644
--- a/helix-core/src/comment.rs
+++ b/helix-core/src/comment.rs
@@ -46,8 +46,7 @@ pub fn toggle_line_comments(doc: &Rope, selection: &Selection, token: Option<&st
let comment = Tendril::from(format!("{} ", token));
for selection in selection {
- let start = text.char_to_line(selection.from());
- let end = text.char_to_line(selection.to());
+ let (start, end) = selection.line_range(text);
let lines = start..end + 1;
let (commented, skipped, min) = find_line_comment(&token, text, lines.clone());
diff --git a/helix-core/src/selection.rs b/helix-core/src/selection.rs
index c08f504d..074b6199 100644
--- a/helix-core/src/selection.rs
+++ b/helix-core/src/selection.rs
@@ -73,6 +73,20 @@ impl Range {
std::cmp::max(self.anchor, self.head)
}
+ /// The (inclusive) range of lines that the range overlaps.
+ #[inline]
+ #[must_use]
+ pub fn line_range(&self, text: RopeSlice) -> (usize, usize) {
+ let from = self.from();
+ let to = if self.is_empty() {
+ self.to()
+ } else {
+ prev_grapheme_boundary(text, self.to()).max(from)
+ };
+
+ (text.char_to_line(from), text.char_to_line(to))
+ }
+
/// `true` when head and anchor are at the same position.
#[inline]
pub fn is_empty(&self) -> bool {