diff --git a/helix-core/src/comment.rs b/helix-core/src/comment.rs index 6cf7fd873..36747c869 100644 --- a/helix-core/src/comment.rs +++ b/helix-core/src/comment.rs @@ -80,7 +80,7 @@ fn find_line_comment( } // for a given range and syntax, determine if there are additional tokens to consider -pub type GetCommentTokens<'a> = +pub type GetInjectedTokens<'a> = Box (Option>, Option>) + 'a>; #[must_use] @@ -306,15 +306,14 @@ pub fn toggle_block_comments( ranges: &Vec, tokens: &[BlockCommentToken], ) -> Vec { - todo!() - // let text = doc.slice(..); - // let (commented, comment_changes) = find_block_comments(tokens, text, selection); - // let (mut transaction, ranges) = - // create_block_comment_transaction(doc, selection, commented, comment_changes); + let text = doc.slice(..); + let (commented, comment_changes) = find_block_comments(tokens, text, ranges); + let (changes, _ranges) = + create_block_comment_transaction(doc, ranges, commented, comment_changes); // if !commented { - // transaction = transaction.with_selection(Selection::new(ranges, selection.primary_index())); + // changes = changes.with_selection(Selection::new(ranges, selection.primary_index())); // } - // transaction + changes } pub fn split_lines_of_range(text: RopeSlice, range: &Range) -> Vec { @@ -445,26 +444,30 @@ mod test { ) ); - // // comment - // let transaction = toggle_block_comments(&doc, &range, &[BlockCommentToken::default()]); - // transaction.apply(&mut doc); + // comment + let changes = + toggle_block_comments(&doc, &vec![range], &[BlockCommentToken::default()]); + let transaction = Transaction::change(&doc, changes.into_iter()); + transaction.apply(&mut doc); - // assert_eq!(doc, "/* 1\n2\n3 */"); + assert_eq!(doc, "/* 1\n2\n3 */"); - // // uncomment - // let selection = Selection::single(0, doc.len_chars()); - // let transaction = - // toggle_block_comments(&doc, &selection, &[BlockCommentToken::default()]); - // transaction.apply(&mut doc); - // assert_eq!(doc, "1\n2\n3"); + // uncomment + let range = Range::new(0, doc.len_chars()); + let changes = + toggle_block_comments(&doc, &vec![range], &[BlockCommentToken::default()]); + let transaction = Transaction::change(&doc, changes.into_iter()); + transaction.apply(&mut doc); + assert_eq!(doc, "1\n2\n3"); - // // don't panic when there is just a space in comment - // doc = Rope::from("/* */"); - // let selection = Selection::single(0, doc.len_chars()); - // let transaction = - // toggle_block_comments(&doc, &selection, &[BlockCommentToken::default()]); - // transaction.apply(&mut doc); - // assert_eq!(doc, ""); + // don't panic when there is just a space in comment + doc = Rope::from("/* */"); + let range = Range::new(0, doc.len_chars()); + let changes = + toggle_block_comments(&doc, &vec![range], &[BlockCommentToken::default()]); + let transaction = Transaction::change(&doc, changes.into_iter()); + transaction.apply(&mut doc); + assert_eq!(doc, ""); } /// Test, if `get_comment_tokens` works, even if the content of the file includes chars, whose diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 12860b897..6f16f3fce 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -5101,7 +5101,7 @@ pub type CommentTransactionFn<'a> = Box< Option<&[BlockCommentToken]>, &Rope, &Selection, - comment::GetCommentTokens<'a>, + comment::GetInjectedTokens<'a>, ) -> Transaction + 'a, >; @@ -5115,7 +5115,7 @@ fn toggle_comments_impl<'a>( let rope = doc.text(); let selection = doc.selection(view.id); - // The default comment tokens to fallback to if no comment tokens are found for the injection layer. + // The comment tokens to fallback to if no comment tokens are found for the injection layer. let doc_line_token: Option<&str> = doc .language_config() .and_then(|lc| lc.comment_tokens.as_ref()) @@ -5194,8 +5194,8 @@ fn toggle_comments(cx: &mut Context) { let line_token = injected_line_tokens .as_ref() - .and_then(|lt| lt.first()) - .map(|lt| lt.as_str()) + .and_then(|token| token.first()) + .map(|token| token.as_str()) .or(doc_line_token); let block_tokens = injected_block_tokens.as_deref().or(doc_block_tokens); @@ -5271,7 +5271,7 @@ fn toggle_line_comments(cx: &mut Context) { let line_token = injected_line_tokens .as_ref() - .and_then(|token| token.first()) + .and_then(|tokens| tokens.first()) .map(|token| token.as_str()) .or(doc_line_token); @@ -5299,12 +5299,12 @@ fn toggle_block_comments(cx: &mut Context) { toggle_comments_impl( cx, Box::new( - |doc_line_token, doc_block_tokens, rope, selection, mut get_comment_tokens| { + |doc_line_token, doc_block_tokens, rope, selection, mut get_injected_tokens| { Transaction::change( rope, selection.iter().flat_map(|range| { let (injected_line_tokens, injected_block_tokens) = - get_comment_tokens(range.from(), range.to()); + get_injected_tokens(range.from(), range.to()); let line_token = injected_line_tokens .as_ref()