on master help sign in
Replace line endings using `set_line_ending` command (#1871)

* set_line_ending: now replace line endings * use ending.len_chars() directly * account for unicode-lines feaure in line-ending doc

commitdd5a7c6191314b22347c323e8cda8aff225e21ba authorKirawi <67773714+kirawi@users.noreply.github.com> dateSat 23 Apr 2022, 04:01 (5 years ago) parent5c25705
2 files changed+23−2
book/src/generated/typable-cmd.md+1−1 helix-term/src/commands/typed.rs+22−1
book/src/generated/typable-cmd.md+1−1
@@ -16,7 +16,7 @@
1616| `:new`, `:n` | Create a new scratch buffer. |
1717| `:format`, `:fmt` | Format the file using the LSP formatter. |
1818| `:indent-style` | Set the indentation style for editing. ('t' for tabs or 1-8 for number of spaces.) |
19−| `:line-ending` | Set the document's default line ending. Options: crlf, lf, cr, ff, nel. |
19+| `:line-ending` | Set the document's default line ending. Options: crlf, lf. |
2020| `:earlier`, `:ear` | Jump back to an earlier point in edit history. Accepts a number of steps or a time span. |
2121| `:later`, `:lat` | Jump to a later point in edit history. Accepts a number of steps or a time span. |
2222| `:write-quit`, `:wq`, `:x` | Write changes to disk and close the current view. Accepts an optional path (:wq some/path.txt) |
helix-term/src/commands/typed.rs+22−1
@@ -352,8 +352,26 @@ fn set_line_ending(
352352 arg if arg.starts_with("nel") => Nel,
353353 _ => bail!("invalid line ending"),
354354 };
355+ let (view, doc) = current!(cx.editor);
356+ doc.line_ending = line_ending;
357+
358+ let mut pos = 0;
359+ let transaction = Transaction::change(
360+ doc.text(),
361+ doc.text().lines().filter_map(|line| {
362+ pos += line.len_chars();
363+ match helix_core::line_ending::get_line_ending(&line) {
364+ Some(ending) if ending != line_ending => {
365+ let start = pos - ending.len_chars();
366+ let end = pos;
367+ Some((start, end, Some(line_ending.as_str().into())))
368+ }
369+ _ => None,
370+ }
371+ }),
372+ );
373+ doc.apply(&transaction, view.id);
355374
356− doc_mut!(cx.editor).line_ending = line_ending;
357375 Ok(())
358376}
359377
@@ -1193,6 +1211,9 @@ pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
11931211 TypableCommand {
11941212 name: "line-ending",
11951213 aliases: &[],
1214+ #[cfg(not(feature = "unicode-lines"))]
1215+ doc: "Set the document's default line ending. Options: crlf, lf.",
1216+ #[cfg(feature = "unicode-lines")]
11961217 doc: "Set the document's default line ending. Options: crlf, lf, cr, ff, nel.",
11971218 fun: set_line_ending,
11981219 completer: None,