on master help sign in
Add command to set the document's default line ending.
a18d50bNathan Vegdahl
helix-term/src/commands.rs+46
@@ -1160,6 +1160,45 @@ mod cmd {
1160 }
1160 }
1161 }
1161 }
1162
1162
1163+ /// Sets or reports the current document's line ending setting.
1164+ fn set_line_ending(editor: &mut Editor, args: &[&str], event: PromptEvent) {
1165+ use LineEnding::*;
1166+
1167+ // If no argument, report current line ending setting.
1168+ if args.is_empty() {
1169+ let line_ending = current!(editor).1.line_ending;
1170+ editor.set_status(match line_ending {
1171+ Crlf => "crlf".into(),
1172+ LF => "line feed".into(),
1173+ FF => "form feed".into(),
1174+ CR => "carriage return".into(),
1175+ Nel => "next line".into(),
1176+
1177+ // These should never be a document's default line ending.
1178+ VT | LS | PS => "error".into(),
1179+ });
1180+ return;
1181+ }
1182+
1183+ // Attempt to parse argument as a line ending.
1184+ let line_ending = match args.get(0) {
1185+ // We check for CR first because it shares a common prefix with CRLF.
1186+ Some(arg) if "cr".starts_with(&arg.to_lowercase()) => Some(CR),
1187+ Some(arg) if "crlf".starts_with(&arg.to_lowercase()) => Some(Crlf),
1188+ Some(arg) if "lf".starts_with(&arg.to_lowercase()) => Some(LF),
1189+ Some(arg) if "ff".starts_with(&arg.to_lowercase()) => Some(FF),
1190+ Some(arg) if "nel".starts_with(&arg.to_lowercase()) => Some(Nel),
1191+ _ => None,
1192+ };
1193+
1194+ if let Some(le) = line_ending {
1195+ doc_mut!(editor).line_ending = le;
1196+ } else {
1197+ // Invalid argument.
1198+ editor.set_error(format!("invalid line ending '{}'", args[0],));
1199+ }
1200+ }
1201+
1163 fn earlier(editor: &mut Editor, args: &[&str], event: PromptEvent) {
1202 fn earlier(editor: &mut Editor, args: &[&str], event: PromptEvent) {
1164 let uk = match args.join(" ").parse::<helix_core::history::UndoKind>() {
1203 let uk = match args.join(" ").parse::<helix_core::history::UndoKind>() {
1165 Ok(uk) => uk,
1204 Ok(uk) => uk,
@@ -1392,6 +1431,13 @@ mod cmd {
1392 fun: set_indent_style,
1431 fun: set_indent_style,
1393 completer: None,
1432 completer: None,
1394 },
1433 },
1434+ TypableCommand {
1435+ name: "line-ending",
1436+ alias: None,
1437+ doc: "Set the document's default line ending. Options: crlf, lf, cr, ff, nel.",
1438+ fun: set_line_ending,
1439+ completer: None,
1440+ },
1395 TypableCommand {
1441 TypableCommand {
1396 name: "earlier",
1442 name: "earlier",
1397 alias: Some("ear"),
1443 alias: Some("ear"),