@@ -1,4 +1,4 @@
1−use crate::{find_first_non_whitespace_char2, Change, RopeSlice, State, Transaction};
1+use crate::{find_first_non_whitespace_char2, Change, RopeSlice, State, Tendril, Transaction};
2use core::ops::Range;
2use core::ops::Range;
3use std::borrow::Cow;
3use std::borrow::Cow;
@@ -7,7 +7,6 @@ fn find_line_comment(
7 text: RopeSlice,
7 text: RopeSlice,
8 lines: Range<usize>,
8 lines: Range<usize>,
9) -> (bool, Vec<usize>, usize) {
9) -> (bool, Vec<usize>, usize) {
10− // selection ranges map char_to_line from() .. to()
11 let mut commented = true;
10 let mut commented = true;
12 let mut skipped = Vec::new();
11 let mut skipped = Vec::new();
13 let mut min = usize::MAX; // minimum col for find_first_non_whitespace_char
12 let mut min = usize::MAX; // minimum col for find_first_non_whitespace_char
@@ -36,11 +35,12 @@ fn find_line_comment(
36 (commented, skipped, min)
35 (commented, skipped, min)
39−fn toggle_line_comments(state: &State) -> Transaction {
38+pub fn toggle_line_comments(state: &State) -> Transaction {
40 let text = state.doc.slice(..);
39 let text = state.doc.slice(..);
41 let mut changes: Vec<Change> = Vec::new();
40 let mut changes: Vec<Change> = Vec::new();
43 let token = "//";
42 let token = "//";
43+ let comment = Tendril::from(format!("{} ", token));
45 for selection in state.selection.ranges() {
45 for selection in state.selection.ranges() {
46 let start = text.char_to_line(selection.from());
46 let start = text.char_to_line(selection.from());
@@ -59,7 +59,7 @@ fn toggle_line_comments(state: &State) -> Transaction {
60 if !commented {
60 if !commented {
61 // comment line
61 // comment line
62− changes.push((pos, pos, Some(format!("{} ", token).into())))
62+ changes.push((pos, pos, Some(comment.clone())))
64 // uncomment line
64 // uncomment line
65 let margin = 1; // TODO: margin is hardcoded 1 but could easily be 0
65 let margin = 1; // TODO: margin is hardcoded 1 but could easily be 0
@@ -101,5 +101,8 @@ mod test {
101 let transaction = toggle_line_comments(&state);
101 let transaction = toggle_line_comments(&state);
102 transaction.apply(&mut state);
102 transaction.apply(&mut state);
103 assert_eq!(state.doc, " 1\n\n 2\n 3");
103 assert_eq!(state.doc, " 1\n\n 2\n 3");
105+ // TODO: account for no margin after comment
106+ // TODO: account for uncommenting with uneven comment indentation