fix: restore selections when created comment

pull/12759/head
Nikita Revenco 2025-02-01 21:14:35 +00:00 committed by Nik Revenco
parent de7884c7dd
commit 0a882107ed
2 changed files with 39 additions and 28 deletions

View File

@ -3,10 +3,7 @@
use smallvec::SmallVec; use smallvec::SmallVec;
use crate::{ use crate::{syntax::BlockCommentToken, Change, Range, Rope, RopeSlice, Tendril};
syntax::{BlockCommentToken, InjectionLanguageMarker},
Change, Range, Rope, RopeSlice, Selection, Tendril, Transaction,
};
use helix_stdx::rope::RopeSliceExt; use helix_stdx::rope::RopeSliceExt;
use std::borrow::Cow; use std::borrow::Cow;
@ -230,8 +227,8 @@ pub fn find_block_comments(
#[must_use] #[must_use]
pub fn create_block_comment_transaction( pub fn create_block_comment_transaction(
doc: &Rope, _doc: &Rope,
ranges: &Vec<Range>, ranges: &[Range],
commented: bool, commented: bool,
comment_changes: Vec<CommentChange>, comment_changes: Vec<CommentChange>,
) -> (Vec<Change>, SmallVec<[Range; 1]>) { ) -> (Vec<Change>, SmallVec<[Range; 1]>) {
@ -305,15 +302,17 @@ pub fn toggle_block_comments(
doc: &Rope, doc: &Rope,
ranges: &Vec<Range>, ranges: &Vec<Range>,
tokens: &[BlockCommentToken], tokens: &[BlockCommentToken],
) -> Vec<Change> { ) -> (Vec<Change>, bool) {
let text = doc.slice(..); let text = doc.slice(..);
let (commented, comment_changes) = find_block_comments(tokens, text, ranges); let (commented, comment_changes) = find_block_comments(tokens, text, ranges);
let (changes, _ranges) = let (changes, _ranges) =
create_block_comment_transaction(doc, ranges, commented, comment_changes); create_block_comment_transaction(doc, ranges, commented, comment_changes);
// if !commented { if commented {
// changes = changes.with_selection(Selection::new(ranges, selection.primary_index())); // changes = changes.with_selection(Selection::new(ranges, selection.primary_index()));
// } (changes, false)
changes } else {
(changes, true)
}
} }
pub fn split_lines_of_range(text: RopeSlice, range: &Range) -> Vec<Range> { pub fn split_lines_of_range(text: RopeSlice, range: &Range) -> Vec<Range> {
@ -361,6 +360,8 @@ mod test {
// TODO: account for uncommenting with uneven comment indentation // TODO: account for uncommenting with uneven comment indentation
mod toggle_line_comment { mod toggle_line_comment {
use crate::Transaction;
use super::*; use super::*;
#[test] #[test]
@ -446,7 +447,7 @@ mod test {
// comment // comment
let changes = 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()); let transaction = Transaction::change(&doc, changes.into_iter());
transaction.apply(&mut doc); transaction.apply(&mut doc);
@ -455,7 +456,7 @@ mod test {
// uncomment // uncomment
let range = Range::new(0, doc.len_chars()); let range = Range::new(0, doc.len_chars());
let changes = 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()); let transaction = Transaction::change(&doc, changes.into_iter());
transaction.apply(&mut doc); transaction.apply(&mut doc);
assert_eq!(doc, "1\n2\n3"); assert_eq!(doc, "1\n2\n3");
@ -464,7 +465,7 @@ mod test {
doc = Rope::from("/* */"); doc = Rope::from("/* */");
let range = Range::new(0, doc.len_chars()); let range = Range::new(0, doc.len_chars());
let changes = 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()); let transaction = Transaction::change(&doc, changes.into_iter());
transaction.apply(&mut doc); transaction.apply(&mut doc);
assert_eq!(doc, ""); assert_eq!(doc, "");

View File

@ -10,7 +10,6 @@ use helix_stdx::{
rope::{self, RopeSliceExt}, rope::{self, RopeSliceExt},
}; };
use helix_vcs::{FileChange, Hunk}; use helix_vcs::{FileChange, Hunk};
use libc::NF_IP6_PRI_CONNTRACK_DEFRAG;
pub use lsp::*; pub use lsp::*;
use tui::{ use tui::{
text::{Span, Spans}, text::{Span, Spans},
@ -63,7 +62,6 @@ use crate::{
compositor::{self, Component, Compositor}, compositor::{self, Component, Compositor},
filter_picker_entry, filter_picker_entry,
job::Callback, job::Callback,
keymap::default,
ui::{self, overlay::overlaid, Picker, PickerColumn, Popup, Prompt, PromptEvent}, ui::{self, overlay::overlaid, Picker, PickerColumn, Popup, Prompt, PromptEvent},
}; };
@ -5231,7 +5229,7 @@ fn toggle_comments(cx: &mut Context) {
if block_commented { if block_commented {
return comment::create_block_comment_transaction( return comment::create_block_comment_transaction(
rope, rope,
&vec![*range], &[*range],
block_commented, block_commented,
comment_changes, comment_changes,
) )
@ -5263,7 +5261,8 @@ fn toggle_line_comments(cx: &mut Context) {
cx, cx,
Box::new( Box::new(
|doc_line_token, doc_block_tokens, rope, selection, mut get_comment_tokens| { |doc_line_token, doc_block_tokens, rope, selection, mut get_comment_tokens| {
Transaction::change( let mut selections = SmallVec::new();
let transaction = Transaction::change(
rope, rope,
selection.iter().flat_map(|range| { selection.iter().flat_map(|range| {
let (injected_line_tokens, injected_block_tokens) = 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() { if line_token.is_none() && block_tokens.is_some() {
let default_block_tokens = &[BlockCommentToken::default()]; let default_block_tokens = &[BlockCommentToken::default()];
let block_comment_tokens = block_tokens.unwrap_or(default_block_tokens); let block_comment_tokens = block_tokens.unwrap_or(default_block_tokens);
comment::toggle_block_comments( let ranges = &comment::split_lines_of_range(rope.slice(..), range);
rope, let (changes, should_select) =
&comment::split_lines_of_range(rope.slice(..), range), comment::toggle_block_comments(rope, ranges, block_comment_tokens);
block_comment_tokens, if should_select {
) selections.extend(ranges.clone());
};
changes
} else { } else {
comment::toggle_line_comments(rope, range, line_token) 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, cx,
Box::new( Box::new(
|doc_line_token, doc_block_tokens, rope, selection, mut get_injected_tokens| { |doc_line_token, doc_block_tokens, rope, selection, mut get_injected_tokens| {
Transaction::change( let mut selections = SmallVec::new();
let transaction = Transaction::change(
rope, rope,
selection.iter().flat_map(|range| { selection.iter().flat_map(|range| {
let (injected_line_tokens, injected_block_tokens) = let (injected_line_tokens, injected_block_tokens) =
@ -5319,14 +5323,20 @@ fn toggle_block_comments(cx: &mut Context) {
} else { } else {
let default_block_tokens = &[BlockCommentToken::default()]; let default_block_tokens = &[BlockCommentToken::default()];
let block_comment_tokens = block_tokens.unwrap_or(default_block_tokens); let block_comment_tokens = block_tokens.unwrap_or(default_block_tokens);
comment::toggle_block_comments( let (changes, should_select) = comment::toggle_block_comments(
rope, rope,
&vec![*range], &vec![*range],
block_comment_tokens, block_comment_tokens,
) );
if should_select {
selections.push(*range);
};
changes
} }
}), }),
) );
transaction.with_selection(Selection::new(selections, selection.primary_index()))
}, },
), ),
); );