From 0a882107edd4ad5c7793d25863bb07d4650716f0 Mon Sep 17 00:00:00 2001 From: Nikita Revenco <154856872+NikitaRevenco@users.noreply.github.com> Date: Sat, 1 Feb 2025 21:14:35 +0000 Subject: [PATCH] fix: restore selections when created comment --- helix-core/src/comment.rs | 29 +++++++++++++++-------------- helix-term/src/commands.rs | 38 ++++++++++++++++++++++++-------------- 2 files changed, 39 insertions(+), 28 deletions(-) diff --git a/helix-core/src/comment.rs b/helix-core/src/comment.rs index 36747c869..5a4ae87c1 100644 --- a/helix-core/src/comment.rs +++ b/helix-core/src/comment.rs @@ -3,10 +3,7 @@ use smallvec::SmallVec; -use crate::{ - syntax::{BlockCommentToken, InjectionLanguageMarker}, - Change, Range, Rope, RopeSlice, Selection, Tendril, Transaction, -}; +use crate::{syntax::BlockCommentToken, Change, Range, Rope, RopeSlice, Tendril}; use helix_stdx::rope::RopeSliceExt; use std::borrow::Cow; @@ -230,8 +227,8 @@ pub fn find_block_comments( #[must_use] pub fn create_block_comment_transaction( - doc: &Rope, - ranges: &Vec, + _doc: &Rope, + ranges: &[Range], commented: bool, comment_changes: Vec, ) -> (Vec, SmallVec<[Range; 1]>) { @@ -305,15 +302,17 @@ pub fn toggle_block_comments( doc: &Rope, ranges: &Vec, tokens: &[BlockCommentToken], -) -> Vec { +) -> (Vec, bool) { 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 { - // changes = changes.with_selection(Selection::new(ranges, selection.primary_index())); - // } - changes + if commented { + // changes = changes.with_selection(Selection::new(ranges, selection.primary_index())); + (changes, false) + } else { + (changes, true) + } } pub fn split_lines_of_range(text: RopeSlice, range: &Range) -> Vec { @@ -361,6 +360,8 @@ mod test { // TODO: account for uncommenting with uneven comment indentation mod toggle_line_comment { + use crate::Transaction; + use super::*; #[test] @@ -446,7 +447,7 @@ mod test { // comment let changes = - toggle_block_comments(&doc, &vec![range], &[BlockCommentToken::default()]); + toggle_block_comments(&doc, &vec![range], &[BlockCommentToken::default()]).0; let transaction = Transaction::change(&doc, changes.into_iter()); transaction.apply(&mut doc); @@ -455,7 +456,7 @@ mod test { // uncomment let range = Range::new(0, doc.len_chars()); let changes = - toggle_block_comments(&doc, &vec![range], &[BlockCommentToken::default()]); + toggle_block_comments(&doc, &vec![range], &[BlockCommentToken::default()]).0; let transaction = Transaction::change(&doc, changes.into_iter()); transaction.apply(&mut doc); assert_eq!(doc, "1\n2\n3"); @@ -464,7 +465,7 @@ mod test { doc = Rope::from("/* */"); let range = Range::new(0, doc.len_chars()); let changes = - toggle_block_comments(&doc, &vec![range], &[BlockCommentToken::default()]); + toggle_block_comments(&doc, &vec![range], &[BlockCommentToken::default()]).0; let transaction = Transaction::change(&doc, changes.into_iter()); transaction.apply(&mut doc); assert_eq!(doc, ""); diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 6f16f3fce..e0293f8aa 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -10,7 +10,6 @@ use helix_stdx::{ rope::{self, RopeSliceExt}, }; use helix_vcs::{FileChange, Hunk}; -use libc::NF_IP6_PRI_CONNTRACK_DEFRAG; pub use lsp::*; use tui::{ text::{Span, Spans}, @@ -63,7 +62,6 @@ use crate::{ compositor::{self, Component, Compositor}, filter_picker_entry, job::Callback, - keymap::default, ui::{self, overlay::overlaid, Picker, PickerColumn, Popup, Prompt, PromptEvent}, }; @@ -5231,7 +5229,7 @@ fn toggle_comments(cx: &mut Context) { if block_commented { return comment::create_block_comment_transaction( rope, - &vec![*range], + &[*range], block_commented, comment_changes, ) @@ -5263,7 +5261,8 @@ fn toggle_line_comments(cx: &mut Context) { cx, Box::new( |doc_line_token, doc_block_tokens, rope, selection, mut get_comment_tokens| { - Transaction::change( + let mut selections = SmallVec::new(); + let transaction = Transaction::change( rope, selection.iter().flat_map(|range| { let (injected_line_tokens, injected_block_tokens) = @@ -5280,16 +5279,20 @@ fn toggle_line_comments(cx: &mut Context) { if line_token.is_none() && block_tokens.is_some() { let default_block_tokens = &[BlockCommentToken::default()]; let block_comment_tokens = block_tokens.unwrap_or(default_block_tokens); - comment::toggle_block_comments( - rope, - &comment::split_lines_of_range(rope.slice(..), range), - block_comment_tokens, - ) + let ranges = &comment::split_lines_of_range(rope.slice(..), range); + let (changes, should_select) = + comment::toggle_block_comments(rope, ranges, block_comment_tokens); + if should_select { + selections.extend(ranges.clone()); + }; + changes } else { comment::toggle_line_comments(rope, range, line_token) } }), - ) + ); + + transaction.with_selection(Selection::new(selections, selection.primary_index())) }, ), ); @@ -5300,7 +5303,8 @@ fn toggle_block_comments(cx: &mut Context) { cx, Box::new( |doc_line_token, doc_block_tokens, rope, selection, mut get_injected_tokens| { - Transaction::change( + let mut selections = SmallVec::new(); + let transaction = Transaction::change( rope, selection.iter().flat_map(|range| { let (injected_line_tokens, injected_block_tokens) = @@ -5319,14 +5323,20 @@ fn toggle_block_comments(cx: &mut Context) { } else { let default_block_tokens = &[BlockCommentToken::default()]; let block_comment_tokens = block_tokens.unwrap_or(default_block_tokens); - comment::toggle_block_comments( + let (changes, should_select) = comment::toggle_block_comments( rope, &vec![*range], block_comment_tokens, - ) + ); + if should_select { + selections.push(*range); + }; + changes } }), - ) + ); + + transaction.with_selection(Selection::new(selections, selection.primary_index())) }, ), );