aboutsummaryrefslogtreecommitdiff
path: root/helix-core
diff options
context:
space:
mode:
authorBlaž Hrastnik2021-02-19 04:59:24 +0000
committerBlaž Hrastnik2021-02-19 04:59:24 +0000
commit7a1ff5e45f829df17dfdcbd4923ae9e83fc2bf3f (patch)
treed550fd8913676f7be7165a7f0f5491be8b1037e5 /helix-core
parent4ab5631d6521ffae6b31e9b9c72dd31a49e793ce (diff)
commands: Wire up toggle comments as ctrl-c
Diffstat (limited to 'helix-core')
-rw-r--r--helix-core/src/comment.rs11
1 files changed, 7 insertions, 4 deletions
diff --git a/helix-core/src/comment.rs b/helix-core/src/comment.rs
index 87becfa3..9109e2d6 100644
--- a/helix-core/src/comment.rs
+++ b/helix-core/src/comment.rs
@@ -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 std::borrow::Cow;
@@ -7,7 +7,6 @@ fn find_line_comment(
text: RopeSlice,
lines: Range<usize>,
) -> (bool, Vec<usize>, usize) {
- // selection ranges map char_to_line from() .. to()
let mut commented = true;
let mut skipped = Vec::new();
let mut min = usize::MAX; // minimum col for find_first_non_whitespace_char
@@ -36,11 +35,12 @@ fn find_line_comment(
(commented, skipped, min)
}
-fn toggle_line_comments(state: &State) -> Transaction {
+pub fn toggle_line_comments(state: &State) -> Transaction {
let text = state.doc.slice(..);
let mut changes: Vec<Change> = Vec::new();
let token = "//";
+ let comment = Tendril::from(format!("{} ", token));
for selection in state.selection.ranges() {
let start = text.char_to_line(selection.from());
@@ -59,7 +59,7 @@ fn toggle_line_comments(state: &State) -> Transaction {
if !commented {
// comment line
- changes.push((pos, pos, Some(format!("{} ", token).into())))
+ changes.push((pos, pos, Some(comment.clone())))
} else {
// uncomment line
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);
transaction.apply(&mut state);
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
}
}