Fix comment toggle command also sometimes toggling the next line.

pull/376/head
Nathan Vegdahl 3 years ago
parent 1c6b5581f0
commit c9300ec35f

@ -46,8 +46,7 @@ pub fn toggle_line_comments(doc: &Rope, selection: &Selection, token: Option<&st
let comment = Tendril::from(format!("{} ", token)); let comment = Tendril::from(format!("{} ", token));
for selection in selection { for selection in selection {
let start = text.char_to_line(selection.from()); let (start, end) = selection.line_range(text);
let end = text.char_to_line(selection.to());
let lines = start..end + 1; let lines = start..end + 1;
let (commented, skipped, min) = find_line_comment(&token, text, lines.clone()); let (commented, skipped, min) = find_line_comment(&token, text, lines.clone());

@ -73,6 +73,20 @@ impl Range {
std::cmp::max(self.anchor, self.head) 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. /// `true` when head and anchor are at the same position.
#[inline] #[inline]
pub fn is_empty(&self) -> bool { pub fn is_empty(&self) -> bool {

Loading…
Cancel
Save