Skip to content

Commit

Permalink
set_line_ending: now replace line endings
Browse files Browse the repository at this point in the history
  • Loading branch information
kirawi committed Mar 28, 2022
1 parent 7b3a3d5 commit 5d8cb19
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion helix-term/src/commands/typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ fn set_line_ending(
args: &[Cow<str>],
_event: PromptEvent,
) -> anyhow::Result<()> {
use helix_core::line_ending;
use LineEnding::*;

// If no argument, report current line ending setting.
Expand Down Expand Up @@ -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(())
}

Expand Down

0 comments on commit 5d8cb19

Please sign in to comment.