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 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<Range>,
_doc: &Rope,
ranges: &[Range],
commented: bool,
comment_changes: Vec<CommentChange>,
) -> (Vec<Change>, SmallVec<[Range; 1]>) {
@ -305,15 +302,17 @@ pub fn toggle_block_comments(
doc: &Rope,
ranges: &Vec<Range>,
tokens: &[BlockCommentToken],
) -> Vec<Change> {
) -> (Vec<Change>, 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<Range> {
@ -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, "");

View File

@ -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()))
},
),
);