on master help sign in
Replace line endings using `set_line_ending` command (#1871)
dd5a7c6Kirawi
book/src/generated/typable-cmd.md+1−1
@@ -16,7 +16,7 @@
16| `:new`, `:n` | Create a new scratch buffer. |
16| `:new`, `:n` | Create a new scratch buffer. |
17| `:format`, `:fmt` | Format the file using the LSP formatter. |
17| `:format`, `:fmt` | Format the file using the LSP formatter. |
18| `:indent-style` | Set the indentation style for editing. ('t' for tabs or 1-8 for number of spaces.) |
18| `: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. |
20| `:earlier`, `:ear` | Jump back to an earlier point in edit history. Accepts a number of steps or a time span. |
20| `:earlier`, `:ear` | Jump back to an earlier point in edit history. Accepts a number of steps or a time span. |
21| `:later`, `:lat` | Jump to a later point in edit history. Accepts a number of steps or a time span. |
21| `:later`, `:lat` | Jump to a later point in edit history. Accepts a number of steps or a time span. |
22| `:write-quit`, `:wq`, `:x` | Write changes to disk and close the current view. Accepts an optional path (:wq some/path.txt) |
22| `: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(
352 arg if arg.starts_with("nel") => Nel,
352 arg if arg.starts_with("nel") => Nel,
353 _ => bail!("invalid line ending"),
353 _ => bail!("invalid line ending"),
354 };
354 };
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);
355
374
356− doc_mut!(cx.editor).line_ending = line_ending;
357 Ok(())
375 Ok(())
358}
376}
359
377
@@ -1193,6 +1211,9 @@ pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
1193 TypableCommand {
1211 TypableCommand {
1194 name: "line-ending",
1212 name: "line-ending",
1195 aliases: &[],
1213 aliases: &[],
1214+ #[cfg(not(feature = "unicode-lines"))]
1215+ doc: "Set the document's default line ending. Options: crlf, lf.",
1216+ #[cfg(feature = "unicode-lines")]
1196 doc: "Set the document's default line ending. Options: crlf, lf, cr, ff, nel.",
1217 doc: "Set the document's default line ending. Options: crlf, lf, cr, ff, nel.",
1197 fun: set_line_ending,
1218 fun: set_line_ending,
1198 completer: None,
1219 completer: None,