Skip to content

Commit

Permalink
Merge pull request #118 from michaelb/dev
Browse files Browse the repository at this point in the history
fix REPL interpreters not closing + isolate different repl from different  neovim instances
  • Loading branch information
michaelb authored Nov 17, 2021
2 parents ee257b7 + 14e583e commit 545aa90
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 23 deletions.
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "sniprun"
version = "1.0.4"
version = "1.0.5"
authors = ["michaelb <[email protected]>"]
edition = "2018"

Expand Down
6 changes: 5 additions & 1 deletion lua/sniprun.lua
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ M.config_values = {
SniprunFloatingWinOk = {fg="#66eeff",ctermfg="Cyan"},
SniprunVirtualTextErr = {bg="#881515",fg="#000000",ctermbg="DarkRed",cterfg="Black"},
SniprunFloatingWinErr = {fg="#881515",ctermfg="DarkRed"},
}
},

neovim_pid=0

}

Expand Down Expand Up @@ -78,6 +80,8 @@ function M.setup(opts)
M.setup_autocommands()
M.setup_display()

M.config_values.neovim_pid = vim.fn.getpid()

M.config_up = 1
end

Expand Down
11 changes: 8 additions & 3 deletions ressources/init_repl.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/bin/bash
working_dir="$1/fifo_repl"
if [ -z "$working_dir" ]; then
if test -e "$working_dir" ; then
echo "process already present" >> $working_dir/log
exit 1
fi

Expand All @@ -13,18 +14,20 @@ err=err_file

repl="$2"


rm -rf $working_dir/
mkdir -p $working_dir
echo "setting up things" > $working_dir/log

echo $repl " process started at $(date +"%F %T")." >> $working_dir/log
mkfifo $working_dir/$pipe
touch $working_dir/$out
sleep 36000 > $working_dir/$pipe &

echo "/bin/cat " $working_dir/$pipe " | " $repl > $working_dir/real_launcher.sh
chmod +x $working_dir/real_launcher.sh

bash $working_dir/real_launcher.sh > $working_dir/$out 2> $working_dir/$err
echo $repl " process started at $(date +"%F %T")." >> $working_dir/log
bash $working_dir/real_launcher.sh > $working_dir/$out 2> $working_dir/$err &

echo "done_logged" >> $working_dir/log

Expand All @@ -36,6 +39,8 @@ while pkill -0 nvim ;do
sleep 1
done

rm -rf $working_dir

pkill -P $$

echo $repl " and other backoung process terminated at $(date +"%F %T")." >> $working_dir/log
Expand Down
2 changes: 1 addition & 1 deletion src/interpreters/Julia_jupyter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ impl Interpreter for Julia_jupyter {

let kp = pwd.clone() + "/kernel_sniprun.json";
Box::new(Julia_jupyter {
cache_dir: pwd + "/" + &data.nvim_pid.to_string(),
data,
support_level: level,
code: String::new(),
kernel_file: kp,
main_file_path: mfp,
launcher_path: lp,
plugin_root: pgr,
cache_dir: pwd,
})
}

Expand Down
15 changes: 12 additions & 3 deletions src/interpreters/Python3_fifo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ impl Python3_fifo {
}
}

fn get_nvim_pid(data: &DataHolder) -> String {
data.nvim_pid.to_string()
}

fn fetch_imports(&mut self) -> Result<(), SniprunError> {
if self.support_level < SupportLevel::Import {
return Ok(());
Expand Down Expand Up @@ -216,13 +220,13 @@ impl Interpreter for Python3_fifo {

let pgr = data.sniprun_root_dir.clone();
Box::new(Python3_fifo {
cache_dir: rwd + "/" + &Python3_fifo::get_nvim_pid(&data),
data,
support_level: level,
code: String::from(""),
imports: String::from(""),
main_file_path: mfp,
plugin_root: pgr,
cache_dir: rwd,
current_output_id: 0,
interpreter: String::new(),
venv: None,
Expand Down Expand Up @@ -386,7 +390,12 @@ impl ReplLikeInterpreter for Python3_fifo {
+ "\", file=sys.stderr)\n";

// remove empty lines interpreted as 'enter' by python
self.code = self.code.lines().filter(|l| !l.trim().is_empty()).collect::<Vec<&str>>().join("\n");
self.code = self
.code
.lines()
.filter(|l| !l.trim().is_empty())
.collect::<Vec<&str>>()
.join("\n");

let all_code = self.imports.clone() + "\n" + &self.code + "\n\n";
self.code = String::from("\nimport sys\n\n")
Expand All @@ -406,7 +415,7 @@ impl ReplLikeInterpreter for Python3_fifo {
let send_repl_cmd = self.data.sniprun_root_dir.clone() + "/ressources/launcher_repl.sh";
info!("running launcher {}", send_repl_cmd);
let res = Command::new(send_repl_cmd)
.arg(self.cache_dir.clone() + "/main.py")
.arg(self.main_file_path.clone())
.arg(self.cache_dir.clone() + "/fifo_repl/pipe_in")
.spawn();
info!("cmd status: {:?}", res);
Expand Down
13 changes: 8 additions & 5 deletions src/interpreters/Python3_jupyter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ impl Python3_jupyter {
info!("import founds : {:?}", self.imports);
Ok(())
}

fn get_nvim_pid(data: &DataHolder) -> String {
data.nvim_pid.to_string()
}

fn module_used(&self, line: &str, code: &str) -> bool {
info!(
"checking for python module usage: line {} in code {}",
Expand Down Expand Up @@ -123,6 +128,7 @@ impl Interpreter for Python3_jupyter {

let kp = pwd.clone() + "/kernel_sniprun.json";
Box::new(Python3_jupyter {
cache_dir: pwd + "/" + &Python3_jupyter::get_nvim_pid(&data),
data,
support_level: level,
code: String::new(),
Expand All @@ -131,7 +137,6 @@ impl Interpreter for Python3_jupyter {
main_file_path: mfp,
launcher_path: lp,
plugin_root: pgr,
cache_dir: pwd,
})
}

Expand Down Expand Up @@ -355,10 +360,8 @@ impl ReplLikeInterpreter for Python3_jupyter {
.lines()
.last()
.unwrap_or(
&String::from_utf8(
strip_ansi_escapes::strip(output.stderr).unwrap(),
)
.unwrap(),
&String::from_utf8(strip_ansi_escapes::strip(output.stderr).unwrap())
.unwrap(),
)
.to_owned(),
));
Expand Down
29 changes: 23 additions & 6 deletions src/interpreters/Sage_fifo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,17 +210,24 @@ impl Sage_fifo {
}
false
}
fn get_nvim_pid(data: &DataHolder) -> String {
data.nvim_pid.to_string()
}

fn fetch_config(&mut self) {
let default_interpreter = String::from("sage");
if let Some(used_interpreter) = Sage_fifo::get_interpreter_option(&self.get_data(), "interpreter") {
if let Some(used_interpreter) =
Sage_fifo::get_interpreter_option(&self.get_data(), "interpreter")
{
if let Some(interpreter_string) = used_interpreter.as_str() {
info!("Using custom interpreter: {}", interpreter_string);
self.interpreter = interpreter_string.to_string();
}
}
self.interpreter = default_interpreter;
if let Some(user_sage_config) = Sage_fifo::get_interpreter_option(&self.get_data(), "interpreter") {
if let Some(user_sage_config) =
Sage_fifo::get_interpreter_option(&self.get_data(), "interpreter")
{
if let Some(_user_sage_config_str) = user_sage_config.as_str() {
info!("Using user sage config");
self.user_sage_config = true;
Expand All @@ -244,13 +251,13 @@ impl Interpreter for Sage_fifo {

let pgr = data.sniprun_root_dir.clone();
Box::new(Sage_fifo {
cache_dir: rwd + "/" + &Sage_fifo::get_nvim_pid(&data),
data,
support_level: level,
code: String::from(""),
imports: String::from(""),
main_file_path: mfp,
plugin_root: pgr,
cache_dir: rwd,
current_output_id: 0,
interpreter: String::new(),
user_sage_config: false,
Expand Down Expand Up @@ -418,10 +425,20 @@ impl ReplLikeInterpreter for Sage_fifo {
+ "\", file=sys.stderr)\n";

// remove empty lines interpreted as 'enter' by the sage interpreter
self.code = self.code.lines().filter(|l| !l.trim().is_empty()).collect::<Vec<&str>>().join("\n");
self.code = self
.code
.lines()
.filter(|l| !l.trim().is_empty())
.collect::<Vec<&str>>()
.join("\n");

let all_code = self.imports.clone() + "\n" + &self.code;
self.code = String::from("\nimport sys\n\n") + &start_mark + &start_mark_err + &all_code + &end_mark + &end_mark_err;
self.code = String::from("\nimport sys\n\n")
+ &start_mark
+ &start_mark_err
+ &all_code
+ &end_mark
+ &end_mark_err;
Ok(())
}

Expand All @@ -433,7 +450,7 @@ impl ReplLikeInterpreter for Sage_fifo {
let send_repl_cmd = self.data.sniprun_root_dir.clone() + "/ressources/launcher_repl.sh";
info!("running launcher {}", send_repl_cmd);
let res = Command::new(send_repl_cmd)
.arg(self.cache_dir.clone() + "/main.sage")
.arg(self.main_file_path.clone())
.arg(self.cache_dir.clone() + "/fifo_repl/pipe_in")
.spawn();
info!("cmd status: {:?}", res);
Expand Down
13 changes: 13 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ pub struct DataHolder {
pub display_no_output: Vec<DisplayType>,

pub cli_args: Vec<String>,

pub nvim_pid: usize,
}

#[derive(Clone, Default, Debug)]
Expand Down Expand Up @@ -118,6 +120,7 @@ impl Default for DataHolder {
display_type: vec![DisplayType::Classic],
display_no_output: vec![DisplayType::Classic],
cli_args: vec![],
nvim_pid: 0,
}
}
}
Expand Down Expand Up @@ -360,6 +363,16 @@ impl EventHandler {
info!("[FILLDATA] got inline_messages setting");
}
}
{
if let Some(i) = self.index_from_name("neovim_pid", config) {
if let Some(pid) = config[i].1.as_u64() {
self.data.nvim_pid = pid as usize;
info!("[FILLDATA] got neovim_pid value setting: {}", pid);
} else {
info!("[FILLDATA] could get neovim_pid");
}
}
}

{
self.data.interpreter_options = Some(values[2].clone());
Expand Down

0 comments on commit 545aa90

Please sign in to comment.