Fix `try_restore_indent` on non-LF documents

On Windows for example the behavior of this function typically diverges
from the usual behavior on Unix. Instead of checking that the inserted
string starts with `'\n'` (untrue for for CRLF line endings) we need to
check that the first grapheme cluster in the string is a line ending.
(All line endings are single grapheme clusters.)
pull/11441/head
Michael Davis 2025-04-04 11:28:15 -04:00
parent 1023e8f964
commit 702a961517
No known key found for this signature in database
2 changed files with 29 additions and 3 deletions

View File

@ -512,3 +512,25 @@ async fn test_open_above_with_comments() -> anyhow::Result<()> {
Ok(())
}
#[tokio::test(flavor = "multi_thread")]
async fn try_restore_indent() -> anyhow::Result<()> {
// Assert that `helix_view::editor::try_restore_indent` handles line endings correctly
// endings.
test((
indoc! {"\
if true #[|{]#
}
"},
// `try_restore_indent` should remove the indentation when adding a blank line.
":lang rust<ret>o<esc>",
indoc! {"\
if true {
#[
|]#}
"},
))
.await?;
Ok(())
}

View File

@ -2262,16 +2262,20 @@ impl Editor {
fn try_restore_indent(doc: &mut Document, view: &mut View) {
use helix_core::{
chars::char_is_whitespace, line_ending::line_end_char_index, Operation, Transaction,
chars::char_is_whitespace,
line_ending::{line_end_char_index, str_is_line_ending},
unicode::segmentation::UnicodeSegmentation,
Operation, Transaction,
};
fn inserted_a_new_blank_line(changes: &[Operation], pos: usize, line_end_pos: usize) -> bool {
if let [Operation::Retain(move_pos), Operation::Insert(ref inserted_str), Operation::Retain(_)] =
changes
{
let mut graphemes = inserted_str.graphemes(true);
move_pos + inserted_str.len() == pos
&& inserted_str.starts_with('\n')
&& inserted_str.chars().skip(1).all(char_is_whitespace)
&& graphemes.next().is_some_and(str_is_line_ending)
&& graphemes.all(|g| g.chars().all(char_is_whitespace))
&& pos == line_end_pos // ensure no characters exists after current position
} else {
false