on master help sign in
commands: Add more write commands
b500a2aWojciech Kępka
helix-term/src/commands.rs+102−8
@@ -954,17 +954,22 @@ mod cmd {
954 };
954 };
955 }
955 }
956
956
957− fn write(editor: &mut Editor, args: &[&str], event: PromptEvent) {
957+ fn _write<P: AsRef<Path>>(
958− let (view, doc) = editor.current();
958+ view: &View,
959+ doc: &mut Document,
960+ path: Option<P>,
961+ ) -> Result<(), anyhow::Error> {
962+ use anyhow::anyhow;
963+
959− if let Some(path) = args.get(0) {
964+ if let Some(path) = path {
960− if let Err(err) = doc.set_path(Path::new(path)) {
965+ if let Err(err) = doc.set_path(path.as_ref()) {
961− editor.set_error(format!("invalid filepath: {}", err));
966+ return Err(anyhow!("invalid filepath: {}", err));
962− return;
963 };
967 };
964 }
968 }
965 if doc.path().is_none() {
969 if doc.path().is_none() {
970+ return Err(anyhow!(
966− editor.set_error("cannot write a buffer without a filename".to_string());
971+ "cannot write a buffer without a filename".to_string()
967− return;
972+ ));
968 }
973 }
969 let autofmt = doc
974 let autofmt = doc
970 .language_config()
975 .language_config()
@@ -974,6 +979,14 @@ mod cmd {
974 doc.format(view.id); // TODO: merge into save
979 doc.format(view.id); // TODO: merge into save
975 }
980 }
976 tokio::spawn(doc.save());
981 tokio::spawn(doc.save());
982+ Ok(())
983+ }
984+
985+ fn write(editor: &mut Editor, args: &[&str], event: PromptEvent) {
986+ let (view, doc) = editor.current();
987+ if let Err(e) = _write(view, doc, args.first()) {
988+ editor.set_error(e.to_string());
989+ };
977 }
990 }
978
991
979 fn new_file(editor: &mut Editor, args: &[&str], event: PromptEvent) {
992 fn new_file(editor: &mut Editor, args: &[&str], event: PromptEvent) {
@@ -1010,6 +1023,52 @@ mod cmd {
1010 doc.later(view.id, uk)
1023 doc.later(view.id, uk)
1011 }
1024 }
1012
1025
1026+ fn write_quit(editor: &mut Editor, args: &[&str], event: PromptEvent) {
1027+ write(editor, args, event.clone());
1028+ quit(editor, &[], event);
1029+ }
1030+
1031+ fn force_write_quit(editor: &mut Editor, args: &[&str], event: PromptEvent) {
1032+ write(editor, args, event.clone());
1033+ force_quit(editor, &[], event);
1034+ }
1035+
1036+ fn _write_all(editor: &mut Editor, args: &[&str], event: PromptEvent, quit: bool, force: bool) {
1037+ let ids = editor
1038+ .tree
1039+ .views()
1040+ .map(|(view, _)| (view.id, view.doc))
1041+ .collect::<Vec<_>>();
1042+
1043+ for (view_id, doc_id) in ids {
1044+ if let Some(doc) = editor.documents.get_mut(doc_id) {
1045+ let view = editor.tree.get(view_id);
1046+ if let Err(e) = _write(view, doc, None::<&str>) {
1047+ editor.set_error(e.to_string());
1048+ } else if quit {
1049+ editor.close(view_id, false);
1050+ continue;
1051+ }
1052+
1053+ if force {
1054+ editor.close(view_id, false);
1055+ }
1056+ }
1057+ }
1058+ }
1059+
1060+ fn write_all(editor: &mut Editor, args: &[&str], event: PromptEvent) {
1061+ _write_all(editor, args, event, false, false)
1062+ }
1063+
1064+ fn write_all_quit(editor: &mut Editor, args: &[&str], event: PromptEvent) {
1065+ _write_all(editor, args, event, true, false)
1066+ }
1067+
1068+ fn force_write_all_quit(editor: &mut Editor, args: &[&str], event: PromptEvent) {
1069+ _write_all(editor, args, event, true, true)
1070+ }
1071+
1013 pub const COMMAND_LIST: &[Command] = &[
1072 pub const COMMAND_LIST: &[Command] = &[
1014 Command {
1073 Command {
1015 name: "quit",
1074 name: "quit",
@@ -1067,6 +1126,41 @@ mod cmd {
1067 fun: later,
1126 fun: later,
1068 completer: None,
1127 completer: None,
1069 },
1128 },
1129+ Command {
1130+ name: "write-quit",
1131+ alias: Some("wq"),
1132+ doc: "Writes changes to disk and closes the current view. Accepts an optional path (:wq some/path.txt)",
1133+ fun: write_quit,
1134+ completer: Some(completers::filename),
1135+ },
1136+ Command {
1137+ name: "write-quit!",
1138+ alias: Some("wq!"),
1139+ doc: "Writes changes to disk and closes the current view forcefully. Accepts an optional path (:wq! some/path.txt)",
1140+ fun: force_write_quit,
1141+ completer: Some(completers::filename),
1142+ },
1143+ Command {
1144+ name: "write-all",
1145+ alias: Some("wa"),
1146+ doc: "Writes changes from all views to disk.",
1147+ fun: write_all,
1148+ completer: None,
1149+ },
1150+ Command {
1151+ name: "write-all-quit",
1152+ alias: Some("waq"),
1153+ doc: "Writes changes from all views to disk and close all views.",
1154+ fun: write_all_quit,
1155+ completer: None,
1156+ },
1157+ Command {
1158+ name: "write-all-quit!",
1159+ alias: Some("waq!"),
1160+ doc: "Writes changes from all views to disk and close all views forcefully (ignoring errors).",
1161+ fun: force_write_all_quit,
1162+ completer: None,
1163+ },
1070 ];
1164 ];
1071
1165
1072 pub static COMMANDS: Lazy<HashMap<&'static str, &'static Command>> = Lazy::new(|| {
1166 pub static COMMANDS: Lazy<HashMap<&'static str, &'static Command>> = Lazy::new(|| {
helix-term/src/ui/prompt.rs+1−1
@@ -18,7 +18,7 @@ pub struct Prompt {
18 pub doc_fn: Box<dyn Fn(&str) -> Option<&'static str>>,
18 pub doc_fn: Box<dyn Fn(&str) -> Option<&'static str>>,
19}
19}
20
20
21−#[derive(PartialEq)]
21+#[derive(Clone, PartialEq)]
22pub enum PromptEvent {
22pub enum PromptEvent {
23 /// The prompt input has been updated.
23 /// The prompt input has been updated.
24 Update,
24 Update,