From 5d8cb19443c89367c4499f92ceeba5215269bee9 Mon Sep 17 00:00:00 2001 From: Shafkath Shuhan Date: Sun, 27 Mar 2022 22:27:36 -0400 Subject: [PATCH] set_line_ending: now replace line endings --- helix-term/src/commands/typed.rs | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/helix-term/src/commands/typed.rs b/helix-term/src/commands/typed.rs index 8b7f481b9c193..888dc3462240f 100644 --- a/helix-term/src/commands/typed.rs +++ b/helix-term/src/commands/typed.rs @@ -280,6 +280,7 @@ fn set_line_ending( args: &[Cow], _event: PromptEvent, ) -> anyhow::Result<()> { + use helix_core::line_ending; use LineEnding::*; // If no argument, report current line ending setting. @@ -321,8 +322,29 @@ fn set_line_ending( arg if arg.starts_with("nel") => Nel, _ => bail!("invalid line ending"), }; + let (view, doc) = current!(cx.editor); + doc.line_ending = line_ending; + + let mut pos = 0; + let transaction = Transaction::change( + doc.text(), + doc.text().lines().filter_map(|line| { + let current_pos = pos; + pos += line.len_chars(); + match line_ending::get_line_ending(&line) { + Some(ending) if ending != line_ending => { + let start = + current_pos + line_ending::rope_end_without_line_ending(&line); + let end = pos; + Some((start, end, Some(line_ending.as_str().into()))) + } + _ => None, + } + }), + ); + + doc.apply(&transaction, view.id); - doc_mut!(cx.editor).line_ending = line_ending; Ok(()) }