From e20f17d11278bc6bb937c1eb83cae0b29b90a4a1 Mon Sep 17 00:00:00 2001 From: Nbiba Bedis Date: Tue, 22 Sep 2020 18:09:09 +0100 Subject: [PATCH 1/6] use cmd /C for windows editor spawning --- src/irust/parser.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/irust/parser.rs b/src/irust/parser.rs index 6a3a6f58..bd486614 100644 --- a/src/irust/parser.rs +++ b/src/irust/parser.rs @@ -352,6 +352,16 @@ impl IRust { 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) + .spawn()? + .wait()?; + + #[cfg(not(windows))] std::process::Command::new(editor) .arg(&*MAIN_FILE) .spawn()? From fbc6c2c518a9de3f457cc1dcf6e53cd1183b6bf0 Mon Sep 17 00:00:00 2001 From: Nbiba Bedis Date: Tue, 22 Sep 2020 18:15:42 +0100 Subject: [PATCH 2/6] add sync --- src/irust/parser.rs | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/src/irust/parser.rs b/src/irust/parser.rs index bd486614..464de0eb 100644 --- a/src/irust/parser.rs +++ b/src/irust/parser.rs @@ -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(), @@ -332,6 +333,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) { @@ -367,16 +381,7 @@ impl IRust { .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 { From 5e542817c0ce4e73508ae9c59bf2b8061bda27a2 Mon Sep 17 00:00:00 2001 From: Nbiba Bedis Date: Tue, 22 Sep 2020 18:23:17 +0100 Subject: [PATCH 3/6] dont leave fingerprints with eval --- src/irust/repl.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/irust/repl.rs b/src/irust/repl.rs index 3bcc2576..cca5dca9 100644 --- a/src/irust/repl.rs +++ b/src/irust/repl.rs @@ -78,6 +78,9 @@ impl Repl { Ok(()) })?; + // Don't keep eval fingerprints + self.write()?; + Ok(eval_result) } From 4e12512074206e60f4bcb3e01cefc438c1bf26b5 Mon Sep 17 00:00:00 2001 From: Nbiba Bedis Date: Tue, 22 Sep 2020 18:55:40 +0100 Subject: [PATCH 4/6] use a differnt file than the original file for :edit and :sync --- src/irust/cargo_cmds.rs | 1 + src/irust/parser.rs | 9 ++++++--- src/irust/repl.rs | 5 +---- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/irust/cargo_cmds.rs b/src/irust/cargo_cmds.rs index 5ab5aa8f..f735d661 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))] diff --git a/src/irust/parser.rs b/src/irust/parser.rs index 464de0eb..a410a4c6 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}; @@ -366,18 +366,21 @@ impl IRust { let _ = cargo_fmt_file(&*MAIN_FILE); } + // copy contents to main_extern.rs which will be the one used by external editors + std::fs::copy(&*MAIN_FILE, &*MAIN_FILE_EXTERN)?; + // 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) + .arg(&*MAIN_FILE_EXTERN) .spawn()? .wait()?; #[cfg(not(windows))] std::process::Command::new(editor) - .arg(&*MAIN_FILE) + .arg(&*MAIN_FILE_EXTERN) .spawn()? .wait()?; diff --git a/src/irust/repl.rs b/src/irust/repl.rs index cca5dca9..aa628da6 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( @@ -78,9 +78,6 @@ impl Repl { Ok(()) })?; - // Don't keep eval fingerprints - self.write()?; - Ok(eval_result) } From e935c2626eaca87b53265c97da6e0dc049a9e6ce Mon Sep 17 00:00:00 2001 From: Nbiba Bedis Date: Tue, 22 Sep 2020 19:17:45 +0100 Subject: [PATCH 5/6] keep external editor in sync with the repl --- src/irust/parser.rs | 8 ++------ src/irust/repl.rs | 8 ++++++++ 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/irust/parser.rs b/src/irust/parser.rs index a410a4c6..0bfea92a 100644 --- a/src/irust/parser.rs +++ b/src/irust/parser.rs @@ -314,7 +314,8 @@ impl IRust { || buffer.starts_with(EXTERN) { self.repl.insert(self.buffer.to_string(), false); - + self.repl.write_to_extern()?; + let _ = cargo_fmt_file(&*MAIN_FILE_EXTERN); let printer = Printer::default(); Ok(printer) @@ -359,16 +360,11 @@ 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); } - // copy contents to main_extern.rs which will be the one used by external editors - std::fs::copy(&*MAIN_FILE, &*MAIN_FILE_EXTERN)?; - // some commands are not detected from path but still works with cmd /C #[cfg(windows)] std::process::Command::new("cmd") diff --git a/src/irust/repl.rs b/src/irust/repl.rs index aa628da6..8b2ea15d 100644 --- a/src/irust/repl.rs +++ b/src/irust/repl.rs @@ -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); From ba024484258a7fb7497a5741f2821c4676604a11 Mon Sep 17 00:00:00 2001 From: Nbiba Bedis Date: Tue, 22 Sep 2020 19:22:48 +0100 Subject: [PATCH 6/6] clean main_extern at start --- src/irust/cargo_cmds.rs | 1 + src/irust/parser.rs | 3 +++ 2 files changed, 4 insertions(+) diff --git a/src/irust/cargo_cmds.rs b/src/irust/cargo_cmds.rs index f735d661..278b089f 100644 --- a/src/irust/cargo_cmds.rs +++ b/src/irust/cargo_cmds.rs @@ -147,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 0bfea92a..02c1de19 100644 --- a/src/irust/parser.rs +++ b/src/irust/parser.rs @@ -314,8 +314,11 @@ impl IRust { || buffer.starts_with(EXTERN) { 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)