From 7a1ff5e45f829df17dfdcbd4923ae9e83fc2bf3f Mon Sep 17 00:00:00 2001 From: Blaž Hrastnik Date: Fri, 19 Feb 2021 13:59:24 +0900 Subject: commands: Wire up toggle comments as ctrl-c --- helix-core/src/comment.rs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) (limited to 'helix-core') 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, ) -> (bool, Vec, 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 = 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 } } -- cgit v1.2.3-70-g09d2