on master help sign in
Fix comment toggle command also sometimes toggling the next line.
c9300ecNathan Vegdahl
helix-core/src/comment.rs+1−2
@@ -46,8 +46,7 @@ pub fn toggle_line_comments(doc: &Rope, selection: &Selection, token: Option<&st
46 let comment = Tendril::from(format!("{} ", token));
46 let comment = Tendril::from(format!("{} ", token));
47
47
48 for selection in selection {
48 for selection in selection {
49− let start = text.char_to_line(selection.from());
49+ let (start, end) = selection.line_range(text);
50− let end = text.char_to_line(selection.to());
51 let lines = start..end + 1;
50 let lines = start..end + 1;
52 let (commented, skipped, min) = find_line_comment(&token, text, lines.clone());
51 let (commented, skipped, min) = find_line_comment(&token, text, lines.clone());
53
52
helix-core/src/selection.rs+14
@@ -73,6 +73,20 @@ impl Range {
73 std::cmp::max(self.anchor, self.head)
73 std::cmp::max(self.anchor, self.head)
74 }
74 }
75
75
76+ /// The (inclusive) range of lines that the range overlaps.
77+ #[inline]
78+ #[must_use]
79+ pub fn line_range(&self, text: RopeSlice) -> (usize, usize) {
80+ let from = self.from();
81+ let to = if self.is_empty() {
82+ self.to()
83+ } else {
84+ prev_grapheme_boundary(text, self.to()).max(from)
85+ };
86+
87+ (text.char_to_line(from), text.char_to_line(to))
88+ }
89+
76 /// `true` when head and anchor are at the same position.
90 /// `true` when head and anchor are at the same position.
77 #[inline]
91 #[inline]
78 pub fn is_empty(&self) -> bool {
92 pub fn is_empty(&self) -> bool {