Skip to content

Commit

Permalink
Merge pull request #68 from sigmaSd/sync_edit
Browse files Browse the repository at this point in the history
Add ':sync' command
  • Loading branch information
sigmaSd authored Sep 22, 2020
2 parents 00b61ee + ba02448 commit 3de796c
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 15 deletions.
2 changes: 2 additions & 0 deletions src/irust/cargo_cmds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ pub static IRUST_TARGET_DIR: Lazy<PathBuf> = Lazy::new(|| IRUST_DIR.join("target
pub static CARGO_TOML_FILE: Lazy<PathBuf> = Lazy::new(|| IRUST_DIR.join("Cargo.toml"));
pub static IRUST_SRC_DIR: Lazy<PathBuf> = Lazy::new(|| IRUST_DIR.join("src"));
pub static MAIN_FILE: Lazy<PathBuf> = Lazy::new(|| IRUST_SRC_DIR.join("main.rs"));
pub static MAIN_FILE_EXTERN: Lazy<PathBuf> = Lazy::new(|| IRUST_SRC_DIR.join("main_extern.rs"));
#[cfg(windows)]
pub static EXE_PATH: Lazy<PathBuf> = Lazy::new(|| IRUST_DIR.join("target/debug/irust.exe"));
#[cfg(not(windows))]
Expand Down Expand Up @@ -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(())
}

Expand Down
45 changes: 31 additions & 14 deletions src/irust/parser.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -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(),
Expand Down Expand Up @@ -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)
Expand All @@ -332,6 +337,19 @@ impl IRust {
}
}

fn sync(&mut self) -> Result<Printer, IRustError> {
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<Printer, IRustError> {
// exp: :edit vi
let editor: String = match self.buffer.to_string().split_whitespace().nth(1) {
Expand All @@ -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<Printer, IRustError> {
Expand Down
10 changes: 9 additions & 1 deletion src/irust/repl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 3de796c

Please sign in to comment.