Skip to content

Commit

Permalink
fix: surprising behaviour when changing line above a comment (helix-e…
Browse files Browse the repository at this point in the history
…ditor#12575)

Co-authored-by: Nikita Revenco <154856872+NikitaRevenco@users.noreply.github.com>
  • Loading branch information
2 people authored and rmburg committed Jan 20, 2025
1 parent 6c09bd0 commit 0cadc7b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 12 deletions.
34 changes: 22 additions & 12 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2792,7 +2792,7 @@ fn delete_selection_impl(cx: &mut Context, op: Operation, yank: YankAction) {
}
Operation::Change => {
if only_whole_lines {
open_above(cx);
open(cx, Open::Above, CommentContinuation::Disabled);
} else {
enter_insert_mode(cx);
}
Expand Down Expand Up @@ -3471,9 +3471,16 @@ pub enum Open {
Above,
}

fn open(cx: &mut Context, open: Open) {
#[derive(PartialEq)]
pub enum CommentContinuation {
Enabled,
Disabled,
}

fn open(cx: &mut Context, open: Open, comment_continuation: CommentContinuation) {
let count = cx.count();
enter_insert_mode(cx);
let config = cx.editor.config();
let (view, doc) = current!(cx.editor);

let text = doc.text().slice(..);
Expand All @@ -3483,6 +3490,14 @@ fn open(cx: &mut Context, open: Open) {

let mut ranges = SmallVec::with_capacity(selection.len());

let continue_comment_tokens =
if comment_continuation == CommentContinuation::Enabled && config.continue_comments {
doc.language_config()
.and_then(|config| config.comment_tokens.as_ref())
} else {
None
};

let mut transaction = Transaction::change_by_selection(contents, selection, |range| {
// the line number, where the cursor is currently
let curr_line_num = text.char_to_line(match open {
Expand All @@ -3498,13 +3513,8 @@ fn open(cx: &mut Context, open: Open) {

let above_next_new_line_num = next_new_line_num.saturating_sub(1);

let continue_comment_token = if doc.config.load().continue_comments {
doc.language_config()
.and_then(|config| config.comment_tokens.as_ref())
.and_then(|tokens| comment::get_comment_token(text, tokens, curr_line_num))
} else {
None
};
let continue_comment_token = continue_comment_tokens
.and_then(|tokens| comment::get_comment_token(text, tokens, curr_line_num));

// Index to insert newlines after, as well as the char width
// to use to compensate for those inserted newlines.
Expand All @@ -3523,7 +3533,7 @@ fn open(cx: &mut Context, open: Open) {
_ => indent::indent_for_newline(
doc.language_config(),
doc.syntax(),
&doc.config.load().indent_heuristic,
&config.indent_heuristic,
&doc.indent_style,
doc.tab_width(),
text,
Expand Down Expand Up @@ -3586,12 +3596,12 @@ fn open(cx: &mut Context, open: Open) {

// o inserts a new line after each line with a selection
fn open_below(cx: &mut Context) {
open(cx, Open::Below)
open(cx, Open::Below, CommentContinuation::Enabled)
}

// O inserts a new line before each line with a selection
fn open_above(cx: &mut Context) {
open(cx, Open::Above)
open(cx, Open::Above, CommentContinuation::Enabled)
}

fn normal_mode(cx: &mut Context) {
Expand Down
19 changes: 19 additions & 0 deletions helix-term/tests/test/commands/insert.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
use super::*;

#[tokio::test(flavor = "multi_thread")]
async fn change_line_above_comment() -> anyhow::Result<()> {
// <https://github.com/helix-editor/helix/issues/12570>
test((
indoc! {"\
#[fn main() {}
|]#// a comment
"},
":lang rust<ret>c",
indoc! {"\
#[
|]#// a comment
"},
))
.await?;

Ok(())
}

#[tokio::test(flavor = "multi_thread")]
async fn insert_newline_many_selections() -> anyhow::Result<()> {
test((
Expand Down

0 comments on commit 0cadc7b

Please sign in to comment.