2021-03-18 08:45:57 +04:00
|
|
|
use crate::{
|
|
|
|
find_first_non_whitespace_char2, Change, Rope, RopeSlice, Selection, Tendril, Transaction,
|
|
|
|
};
|
2021-02-18 13:35:39 +04:00
|
|
|
use core::ops::Range;
|
|
|
|
use std::borrow::Cow;
|
|
|
|
|
|
|
|
fn find_line_comment(
|
|
|
|
token: &str,
|
|
|
|
text: RopeSlice,
|
|
|
|
lines: Range<usize>,
|
|
|
|
) -> (bool, Vec<usize>, usize) {
|
|
|
|
let mut commented = true;
|
|
|
|
let mut skipped = Vec::new();
|
|
|
|
let mut min = usize::MAX; // minimum col for find_first_non_whitespace_char
|
|
|
|
for line in lines {
|
|
|
|
let line_slice = text.line(line);
|
|
|
|
if let Some(pos) = find_first_non_whitespace_char2(line_slice) {
|
|
|
|
let len = line_slice.len_chars();
|
|
|
|
|
|
|
|
if pos < min {
|
|
|
|
min = pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
// line can be shorter than pos + token len
|
|
|
|
let fragment = Cow::from(line_slice.slice(pos..std::cmp::min(pos + token.len(), len)));
|
|
|
|
|
|
|
|
if fragment != token {
|
|
|
|
// as soon as one of the non-blank lines doesn't have a comment, the whole block is
|
|
|
|
// considered uncommented.
|
|
|
|
commented = false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// blank line
|
|
|
|
skipped.push(line);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
(commented, skipped, min)
|
|
|
|
}
|
|
|
|
|
2021-03-24 09:52:13 +04:00
|
|
|
#[must_use]
|
2021-03-18 08:45:57 +04:00
|
|
|
pub fn toggle_line_comments(doc: &Rope, selection: &Selection) -> Transaction {
|
|
|
|
let text = doc.slice(..);
|
2021-02-18 13:35:39 +04:00
|
|
|
let mut changes: Vec<Change> = Vec::new();
|
|
|
|
|
|
|
|
let token = "//";
|
2021-02-19 08:59:24 +04:00
|
|
|
let comment = Tendril::from(format!("{} ", token));
|
2021-02-18 13:35:39 +04:00
|
|
|
|
2021-03-19 06:14:13 +04:00
|
|
|
for selection in selection {
|
2021-02-18 13:35:39 +04:00
|
|
|
let start = text.char_to_line(selection.from());
|
|
|
|
let end = text.char_to_line(selection.to());
|
|
|
|
let lines = start..end + 1;
|
|
|
|
let (commented, skipped, min) = find_line_comment(token, text, lines.clone());
|
|
|
|
|
|
|
|
changes.reserve(end - start - skipped.len());
|
|
|
|
|
|
|
|
for line in lines {
|
|
|
|
if skipped.contains(&line) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
let pos = text.line_to_char(line) + min;
|
|
|
|
|
|
|
|
if !commented {
|
|
|
|
// comment line
|
2021-02-19 08:59:24 +04:00
|
|
|
changes.push((pos, pos, Some(comment.clone())))
|
2021-02-18 13:35:39 +04:00
|
|
|
} else {
|
|
|
|
// uncomment line
|
|
|
|
let margin = 1; // TODO: margin is hardcoded 1 but could easily be 0
|
|
|
|
changes.push((pos, pos + token.len() + margin, None))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-03-18 08:45:57 +04:00
|
|
|
Transaction::change(doc, changes.into_iter())
|
2021-02-18 13:35:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_find_line_comment() {
|
2021-03-18 08:45:57 +04:00
|
|
|
use crate::State;
|
2021-02-18 13:35:39 +04:00
|
|
|
|
|
|
|
// four lines, two space indented, except for line 1 which is blank.
|
|
|
|
let doc = Rope::from(" 1\n\n 2\n 3");
|
|
|
|
|
|
|
|
let mut state = State::new(doc);
|
|
|
|
// select whole document
|
|
|
|
state.selection = Selection::single(0, state.doc.len_chars() - 1);
|
|
|
|
|
|
|
|
let text = state.doc.slice(..);
|
|
|
|
|
|
|
|
let res = find_line_comment("//", text, 0..3);
|
|
|
|
// (commented = true, skipped = [line 1], min = col 2)
|
|
|
|
assert_eq!(res, (false, vec![1], 2));
|
|
|
|
|
|
|
|
// comment
|
2021-03-18 08:45:57 +04:00
|
|
|
let transaction = toggle_line_comments(&state.doc, &state.selection);
|
2021-02-18 13:35:39 +04:00
|
|
|
transaction.apply(&mut state);
|
|
|
|
|
|
|
|
assert_eq!(state.doc, " // 1\n\n // 2\n // 3");
|
|
|
|
|
|
|
|
// uncomment
|
2021-03-18 08:45:57 +04:00
|
|
|
let transaction = toggle_line_comments(&state.doc, &state.selection);
|
2021-02-18 13:35:39 +04:00
|
|
|
transaction.apply(&mut state);
|
|
|
|
assert_eq!(state.doc, " 1\n\n 2\n 3");
|
2021-02-19 08:59:24 +04:00
|
|
|
|
|
|
|
// TODO: account for no margin after comment
|
|
|
|
// TODO: account for uncommenting with uneven comment indentation
|
2021-02-18 13:35:39 +04:00
|
|
|
}
|
|
|
|
}
|