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