diff --git a/src/irust/cargo_cmds.rs b/src/irust/cargo_cmds.rs index 5ab5aa8f..278b089f 100644 --- a/src/irust/cargo_cmds.rs +++ b/src/irust/cargo_cmds.rs @@ -45,6 +45,7 @@ pub static IRUST_TARGET_DIR: Lazy = Lazy::new(|| IRUST_DIR.join("target pub static CARGO_TOML_FILE: Lazy = Lazy::new(|| IRUST_DIR.join("Cargo.toml")); pub static IRUST_SRC_DIR: Lazy = Lazy::new(|| IRUST_DIR.join("src")); pub static MAIN_FILE: Lazy = Lazy::new(|| IRUST_SRC_DIR.join("main.rs")); +pub static MAIN_FILE_EXTERN: Lazy = Lazy::new(|| IRUST_SRC_DIR.join("main_extern.rs")); #[cfg(windows)] pub static EXE_PATH: Lazy = Lazy::new(|| IRUST_DIR.join("target/debug/irust.exe")); #[cfg(not(windows))] @@ -146,6 +147,7 @@ fn clean_main_file() -> io::Result<()> { const MAIN_SRC: &str = "fn main() {\n\n}"; let mut main = fs::File::create(&*MAIN_FILE)?; write!(main, "{}", MAIN_SRC)?; + std::fs::copy(&*MAIN_FILE, &*MAIN_FILE_EXTERN)?; Ok(()) } diff --git a/src/irust/parser.rs b/src/irust/parser.rs index 6a3a6f58..02c1de19 100644 --- a/src/irust/parser.rs +++ b/src/irust/parser.rs @@ -1,5 +1,5 @@ use super::cargo_cmds::ToolChain; -use super::cargo_cmds::{cargo_fmt, cargo_fmt_file, cargo_run, MAIN_FILE}; +use super::cargo_cmds::{cargo_fmt, cargo_fmt_file, cargo_run, MAIN_FILE, MAIN_FILE_EXTERN}; use super::highlight::highlight; use crate::irust::format::{format_err, format_eval_output, output_is_err}; use crate::irust::printer::{Printer, PrinterItem, PrinterItemType}; @@ -16,6 +16,7 @@ impl IRust { ":show" => self.show(), ":pop" => self.pop(), ":irust" => self.irust(), + ":sync" => self.sync(), cmd if cmd.starts_with("::") => self.run_cmd(), cmd if cmd.starts_with(":edit") => self.extern_edit(), cmd if cmd.starts_with(":add") => self.add_dep(), @@ -314,6 +315,10 @@ impl IRust { { self.repl.insert(self.buffer.to_string(), false); + // save repl to main_extern.rs which can be used with external editors + self.repl.write_to_extern()?; + let _ = cargo_fmt_file(&*MAIN_FILE_EXTERN); + let printer = Printer::default(); Ok(printer) @@ -332,6 +337,19 @@ impl IRust { } } + fn sync(&mut self) -> Result { + match self.repl.update_from_main_file() { + Ok(_) => Ok(Printer::new(PrinterItem::new( + SUCCESS.to_string(), + PrinterItemType::Ok, + ))), + Err(e) => { + self.repl.reset(self.options.toolchain); + Err(e) + } + } + } + fn extern_edit(&mut self) -> Result { // exp: :edit vi let editor: String = match self.buffer.to_string().split_whitespace().nth(1) { @@ -345,28 +363,27 @@ impl IRust { )?; self.write_newline()?; - // write current repl (to ensure eval leftover is cleaned) - self.repl.write()?; // beautify code if self.repl.body.len() > 2 { let _ = cargo_fmt_file(&*MAIN_FILE); } + // some commands are not detected from path but still works with cmd /C + #[cfg(windows)] + std::process::Command::new("cmd") + .arg("/C") + .arg(editor) + .arg(&*MAIN_FILE_EXTERN) + .spawn()? + .wait()?; + + #[cfg(not(windows))] std::process::Command::new(editor) - .arg(&*MAIN_FILE) + .arg(&*MAIN_FILE_EXTERN) .spawn()? .wait()?; - match self.repl.update_from_main_file() { - Ok(_) => Ok(Printer::new(PrinterItem::new( - SUCCESS.to_string(), - PrinterItemType::Ok, - ))), - Err(e) => { - self.repl.reset(self.options.toolchain); - Err(e) - } - } + self.sync() } fn irust(&mut self) -> Result { diff --git a/src/irust/repl.rs b/src/irust/repl.rs index 3bcc2576..8b2ea15d 100644 --- a/src/irust/repl.rs +++ b/src/irust/repl.rs @@ -17,7 +17,7 @@ impl Repl { } pub fn update_from_main_file(&mut self) -> Result<(), IRustError> { - let main_file = std::fs::read_to_string(&*MAIN_FILE)?; + let main_file = std::fs::read_to_string(&*MAIN_FILE_EXTERN)?; let lines_num = main_file.lines().count(); if lines_num < 2 { return Err(IRustError::Custom( @@ -131,6 +131,14 @@ impl Repl { Ok(()) } + // Used for external editors + pub fn write_to_extern(&self) -> io::Result<()> { + let mut main_file = std::fs::File::create(&*MAIN_FILE_EXTERN)?; + write!(main_file, "{}", self.body.join("\n"))?; + + Ok(()) + } + pub fn pop(&mut self) { if self.body.len() > 2 { self.body.remove(self.cursor - 1);