on master help sign in
Determine whether to use a margin of 0 or 1 when uncommenting (#476)

* Implement `margin` calculation for uncommenting * Move `margin` calculation to `find_line_comment` * Fix comment bug with multiple selections on a line * Fix `find_line_comment` test for new return type * Generate a single vec of lines for comment toggle `toggle_line_comments` collects the lines covered by all selections into a `Vec`, skipping duplicates. `find_line_comment` now returns the lines to operate on, instead of returning the lines to skip. * Fix test for `find_line_comment` * Reserve length of `to_change` instead of `lines` The length of `lines` includes blank lines which will be skipped, and as such do not need space for a change reserved for them. `to_change` includes only the lines which will be changed. * Use `token.chars().count()` for token char length * Create `changes` with capacity instead of reserving * Remove unnecessary clones in `test_find_line_comment` * Add test case for 0 margin comments * Add comments explaining `find_line_comment`

commit112ae5cffe8cc645623cd2cb04cbee2e69c37a09 authorOmnikar <omkar.subramaniam@icloud.com> dateSun 25 Jul 2021, 22:00 (5 years ago) parente07e42d
1 file changed+57−32
helix-core/src/comment.rs+57−32
helix-core/src/comment.rs+57−32
@@ -1,17 +1,27 @@
11use crate::{
22 find_first_non_whitespace_char, Change, Rope, RopeSlice, Selection, Tendril, Transaction,
33};
4−use core::ops::Range;
54use std::borrow::Cow;
65
6+/// Given text, a comment token, and a set of line indices, returns the following:
7+/// - Whether the given lines should be considered commented
8+/// - If any of the lines are uncommented, all lines are considered as such.
9+/// - The lines to change for toggling comments
10+/// - This is all provided lines excluding blanks lines.
11+/// - The column of the comment tokens
12+/// - Column of existing tokens, if the lines are commented; column to place tokens at otherwise.
13+/// - The margin to the right of the comment tokens
14+/// - Defaults to `1`. If any existing comment token is not followed by a space, changes to `0`.
715fn find_line_comment(
816 token: &str,
917 text: RopeSlice,
10− lines: Range<usize>,
11−) -> (bool, Vec<usize>, usize) {
18+ lines: impl IntoIterator<Item = usize>,
19+) -> (bool, Vec<usize>, usize, usize) {
1220 let mut commented = true;
13− let mut skipped = Vec::new();
21+ let mut to_change = Vec::new();
1422 let mut min = usize::MAX; // minimum col for find_first_non_whitespace_char
23+ let mut margin = 1;
24+ let token_len = token.chars().count();
1525 for line in lines {
1626 let line_slice = text.line(line);
1727 if let Some(pos) = find_first_non_whitespace_char(line_slice) {
@@ -29,47 +39,53 @@ fn find_line_comment(
2939 // considered uncommented.
3040 commented = false;
3141 }
32− } else {
33− // blank line
34− skipped.push(line);
42+
43+ // determine margin of 0 or 1 for uncommenting; if any comment token is not followed by a space,
44+ // a margin of 0 is used for all lines.
45+ if matches!(line_slice.get_char(pos + token_len), Some(c) if c != ' ') {
46+ margin = 0;
47+ }
48+
49+ // blank lines don't get pushed.
50+ to_change.push(line);
3551 }
3652 }
37− (commented, skipped, min)
53+ (commented, to_change, min, margin)
3854}
3955
4056#[must_use]
4157pub fn toggle_line_comments(doc: &Rope, selection: &Selection, token: Option<&str>) -> Transaction {
4258 let text = doc.slice(..);
43− let mut changes: Vec<Change> = Vec::new();
4459
4560 let token = token.unwrap_or("//");
4661 let comment = Tendril::from(format!("{} ", token));
4762
63+ let mut lines: Vec<usize> = Vec::new();
64+
65+ let mut min_next_line = 0;
4866 for selection in selection {
49− let start = text.char_to_line(selection.from());
50− let end = text.char_to_line(selection.to());
51− let lines = start..end + 1;
52− let (commented, skipped, min) = find_line_comment(&token, text, lines.clone());
67+ let start = text.char_to_line(selection.from()).max(min_next_line);
68+ let end = text.char_to_line(selection.to()) + 1;
69+ lines.extend(start..end);
70+ min_next_line = end + 1;
71+ }
5372
54− changes.reserve((end - start).saturating_sub(skipped.len()));
73+ let (commented, to_change, min, margin) = find_line_comment(&token, text, lines);
5574
56− for line in lines {
57− if skipped.contains(&line) {
58− continue;
59− }
75+ let mut changes: Vec<Change> = Vec::with_capacity(to_change.len());
6076
61− let pos = text.line_to_char(line) + min;
77+ for line in to_change {
78+ let pos = text.line_to_char(line) + min;
6279
63− if !commented {
64− // comment line
65− changes.push((pos, pos, Some(comment.clone())))
66− } else {
67− // uncomment line
68− let margin = 1; // TODO: margin is hardcoded 1 but could easily be 0
69− changes.push((pos, pos + token.len() + margin, None))
70− }
80+ if !commented {
81+ // comment line
82+ changes.push((pos, pos, Some(comment.clone())));
83+ } else {
84+ // uncomment line
85+ changes.push((pos, pos + token.len() + margin, None));
7186 }
7287 }
88+
7389 Transaction::change(doc, changes.into_iter())
7490}
7591
@@ -91,23 +107,32 @@ mod test {
91107 let text = state.doc.slice(..);
92108
93109 let res = find_line_comment("//", text, 0..3);
94− // (commented = true, skipped = [line 1], min = col 2)
95− assert_eq!(res, (false, vec![1], 2));
110+ // (commented = true, to_change = [line 0, line 2], min = col 2, margin = 1)
111+ assert_eq!(res, (false, vec![0, 2], 2, 1));
96112
97113 // comment
98114 let transaction = toggle_line_comments(&state.doc, &state.selection, None);
99115 transaction.apply(&mut state.doc);
100− state.selection = state.selection.clone().map(transaction.changes());
116+ state.selection = state.selection.map(transaction.changes());
101117
102118 assert_eq!(state.doc, " // 1\n\n // 2\n // 3");
103119
104120 // uncomment
105121 let transaction = toggle_line_comments(&state.doc, &state.selection, None);
106122 transaction.apply(&mut state.doc);
107− state.selection = state.selection.clone().map(transaction.changes());
123+ state.selection = state.selection.map(transaction.changes());
124+ assert_eq!(state.doc, " 1\n\n 2\n 3");
125+
126+ // 0 margin comments
127+ state.doc = Rope::from(" //1\n\n //2\n //3");
128+ // reset the selection.
129+ state.selection = Selection::single(0, state.doc.len_chars() - 1);
130+
131+ let transaction = toggle_line_comments(&state.doc, &state.selection, None);
132+ transaction.apply(&mut state.doc);
133+ state.selection = state.selection.map(transaction.changes());
108134 assert_eq!(state.doc, " 1\n\n 2\n 3");
109135
110− // TODO: account for no margin after comment
111136 // TODO: account for uncommenting with uneven comment indentation
112137 }
113138}