Skip to content

Commit

Permalink
Merge pull request #101 from michaelb/dev
Browse files Browse the repository at this point in the history
fix virtual text getting cleared
  • Loading branch information
michaelb authored Oct 6, 2021
2 parents c9ac59f + 951dd21 commit e1202a7
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 52 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: Rust

on:
push:
branches: [ master, dev]
branches: [ master]
pull_request:
branches: [ master, dev]
workflow_dispatch:
Expand Down Expand Up @@ -48,10 +48,10 @@ jobs:


- name: Unit tests
run: cargo test --features ignore_in_ci
run: cargo test --release --features ignore_in_ci

- name: Integration tests
run: cargo test --features ignore_in_ci --test integration
run: cargo test --release --features ignore_in_ci --test integration



Expand Down
8 changes: 4 additions & 4 deletions lua/sniprun.lua
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,10 @@ function M.setup_autocommands()
vim.cmd("augroup END")

vim.cmd("function! Sniprun_clear_vt_on_leave()\n lua require'sniprun.display'.clear_virtual_text()\n endfunction")
vim.cmd("augroup sniprun_clear_vt")
vim.cmd("autocmd!")
vim.cmd("autocmd BufWinLeave * call Sniprun_clear_vt_on_leave()")
vim.cmd("augroup END")
-- vim.cmd("augroup sniprun_clear_vt")
-- vim.cmd("autocmd!")
-- vim.cmd("autocmd BufWinLeave * call Sniprun_clear_vt_on_leave()")
-- vim.cmd("augroup END")

vim.cmd("function! Sniprun_close_term_on_leave()\n lua require'sniprun.display'.term_close()\n endfunction")
vim.cmd("augroup sniprun_close_term")
Expand Down
6 changes: 0 additions & 6 deletions ressources/display_notify.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,3 @@ require'sniprun'.setup({
})
EOF
```


!! As of writing, virtual text gets deleted when a notification from nvim-notify expires for an unknown reason.



32 changes: 19 additions & 13 deletions src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ use std::sync::{Arc, Mutex};
#[derive(Clone, Debug, Ord, PartialOrd, PartialEq, Eq)]
pub enum DisplayType {
Classic = 0,
NvimNotify,
VirtualTextOk,
VirtualTextErr,
Terminal,
LongTempFloatingWindow,
TempFloatingWindow,
Api,
NvimNotify,
}
use DisplayType::*;

Expand Down Expand Up @@ -101,14 +101,17 @@ pub fn send_api(
Ok(result) => {
let mut nvim_instance = nvim.lock().unwrap();
nvim_instance.command_async(&format!(
"lua require\"sniprun.display\".send_api(\"{}\", true)",
no_output_wrap(result, data, &DisplayType::Terminal),
))},
Err(result) => {let mut nvim_instance = nvim.lock().unwrap();
"lua require\"sniprun.display\".send_api(\"{}\", true)",
no_output_wrap(result, data, &DisplayType::Terminal),
))
}
Err(result) => {
let mut nvim_instance = nvim.lock().unwrap();
nvim_instance.command_async(&format!(
"lua require\"sniprun.display\".send_api(\"{}\", false)",
no_output_wrap(&result.to_string(), data, &DisplayType::Terminal),
))},
"lua require\"sniprun.display\".send_api(\"{}\", false)",
no_output_wrap(&result.to_string(), data, &DisplayType::Terminal),
))
}
};
info!("!done displyaing notify");
}
Expand All @@ -123,7 +126,11 @@ pub fn display_virtual_text(
return; //don't display unasked-for things
}

let namespace_id = nvim.lock().unwrap().create_namespace("sniprun").unwrap();
let namespace_id = nvim
.lock()
.unwrap()
.create_namespace("sniprun")
.unwrap();
info!("namespace_id = {:?}", namespace_id);

let last_line = data.range[1] - 1;
Expand All @@ -147,7 +154,7 @@ pub fn display_virtual_text(
.is_empty()
{
return;
}
}
nvim.lock().unwrap().command(&format!(
"lua require\"sniprun.display\".display_extmark({},{},\"{}\",\"{}\")",
namespace_id,
Expand All @@ -159,8 +166,7 @@ pub fn display_virtual_text(
)),
hl_ok
))

}
}
Err(message_err) => {
if shorten_err(&no_output_wrap(
&message_err.to_string(),
Expand Down Expand Up @@ -331,7 +337,7 @@ fn cleanup_and_escape(message: &str) -> String {
.trim_start_matches('\n')
.trim_end_matches('\n')
.to_string();

answer_str.replace("\n", "\\\n")
}

Expand Down
3 changes: 3 additions & 0 deletions src/interpreters/GFM_original.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ impl Interpreter for GFM_original {
String::from("GFM_original")
}

fn default_for_filetype() -> bool {
true
}
fn get_current_level(&self) -> SupportLevel {
self.support_level
}
Expand Down
72 changes: 47 additions & 25 deletions src/interpreters/Sage_fifo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl Sage_fifo {
err_path: String,
id: u32,
) -> Result<String, SniprunError> {
let end_mark = String::from("sniprun_finished_id=") + &id.to_string()+"\n";
let end_mark = String::from("sniprun_finished_id=") + &id.to_string() + "\n";
let start_mark = String::from("sniprun_started_id=") + &id.to_string();

info!(
Expand All @@ -46,8 +46,8 @@ impl Sage_fifo {
// info!("file : {:?}", contents);
if err_contents.contains(&end_mark) {
if let Some(index) = err_contents.rfind(&start_mark) {
let err_to_display = err_contents[index + start_mark.len()
..err_contents.len() - end_mark.len() - 1]
let err_to_display = err_contents
[index + start_mark.len()..err_contents.len() - end_mark.len() - 1]
.to_owned();
info!("err to display : {:?}", err_to_display);
if !err_to_display.trim().is_empty() {
Expand All @@ -71,13 +71,37 @@ impl Sage_fifo {
if out_contents.contains(&end_mark) {
info!("out found");
let index = out_contents.rfind(&start_mark).unwrap();
let out_contents_current = out_contents[index + start_mark.len()
..out_contents.len() - end_mark.len() - 1].to_string();
let out_contents_current = out_contents
[index + start_mark.len()..out_contents.len() - end_mark.len() - 1]
.to_string();

//check it's not actually an error
let error_indicators = ["AssertionError","AttributeError","EOFError","FloatingPointError","GeneratorExit","ImportError","IndexError","KeyError","KeyboardInterrupt","MemoryError","NameError","NotImplementedError","OSError","OverflowError","ReferenceError","RuntimeError","StopIteration","SyntaxError","IndentationError","TabError","SystemError", "ModuleNotFoundError"];
let error_indicators = [
"AssertionError",
"AttributeError",
"EOFError",
"FloatingPointError",
"GeneratorExit",
"ImportError",
"IndexError",
"KeyError",
"KeyboardInterrupt",
"MemoryError",
"NameError",
"NotImplementedError",
"OSError",
"OverflowError",
"ReferenceError",
"RuntimeError",
"StopIteration",
"SyntaxError",
"IndentationError",
"TabError",
"SystemError",
"ModuleNotFoundError",
];
for try_error_indicator in error_indicators.iter() {
if out_contents_current.contains(try_error_indicator){
if out_contents_current.contains(try_error_indicator) {
info!("stdout contains error indicator");
err_contents = out_contents.clone();
// info!("file : {:?}", contents);
Expand All @@ -98,8 +122,8 @@ impl Sage_fifo {
}
}

return Ok(out_contents[index + start_mark.len()
..out_contents.len() - end_mark.len() - 1]
return Ok(out_contents
[index + start_mark.len()..out_contents.len() - end_mark.len() - 1]
.to_owned());
}
}
Expand Down Expand Up @@ -237,10 +261,6 @@ impl Interpreter for Sage_fifo {
String::from("Sage_fifo")
}

fn default_for_filetype() -> bool {
false
}

fn behave_repl_like_default() -> bool {
true
}
Expand Down Expand Up @@ -272,6 +292,10 @@ impl Interpreter for Sage_fifo {
SupportLevel::Import
}

fn default_for_filetype() -> bool {
true
}

fn fetch_code(&mut self) -> Result<(), SniprunError> {
self.fetch_config();
self.fetch_python_imports()?;
Expand Down Expand Up @@ -309,7 +333,6 @@ impl Interpreter for Sage_fifo {

impl ReplLikeInterpreter for Sage_fifo {
fn fetch_code_repl(&mut self) -> Result<(), SniprunError> {

if !self.read_previous_code().is_empty() {
// nothing to do, kernel already running
info!("Sage kernel already running");
Expand All @@ -322,12 +345,13 @@ impl ReplLikeInterpreter for Sage_fifo {
} else {
info!("Could not retrieve a previous id even if the kernel is running");
info!("This was in saved code: {}", self.read_previous_code());
return Err(SniprunError::CustomError("Sniprun failed to connect to the running kernel, please SnipReset".to_string()));
return Err(SniprunError::CustomError(
"Sniprun failed to connect to the running kernel, please SnipReset".to_string(),
));
}

self.fetch_code()?;
Ok(())

} else {
self.fetch_config();
// launch everything
Expand All @@ -339,13 +363,12 @@ impl ReplLikeInterpreter for Sage_fifo {
init_repl_cmd, &self.cache_dir
);
match daemon() {

Ok(Fork::Child) => {
let nodotstage_arg = if self.user_sage_config {
""
} else {
"--nodotsage"
};
""
} else {
"--nodotsage"
};

let _res = Command::new("bash")
.args(&[
Expand All @@ -364,9 +387,9 @@ impl ReplLikeInterpreter for Sage_fifo {
));
}
Ok(Fork::Parent(_)) => {}
Err(_) => info!(
"Sage_fifo could not fork itself to the background to launch the kernel"
),
Err(_) => {
info!("Sage_fifo could not fork itself to the background to launch the kernel")
}
};

let pause = std::time::Duration::from_millis(100);
Expand All @@ -377,7 +400,6 @@ impl ReplLikeInterpreter for Sage_fifo {
"Sage kernel launched, re-run your snippet".to_owned(),
))
}

}

fn add_boilerplate_repl(&mut self) -> Result<(), SniprunError> {
Expand Down
3 changes: 3 additions & 0 deletions src/interpreters/TypeScript_original.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ impl Interpreter for TypeScript_original {
self.support_level = level;
}

fn default_for_filetype() -> bool {
true
}
fn get_data(&self) -> DataHolder {
self.data.clone()
}
Expand Down
5 changes: 4 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -429,15 +429,18 @@ pub fn start() {

//main loop
info!("[MAIN] Start of main event loop");
let mut counter = 1;
for (event, values) in receiver {
info!("EVENT {} RECEIVED ##################", counter);
counter += 1;
match Messages::from(event.clone()) {
//Run command
Messages::Run => {
info!("[MAINLOOP] Run command received");

let mut event_handler2 = event_handler.clone();
info!("[RUN] clone event handler");
let _res2 = send.send(HandleAction::New(thread::spawn(move || {
let _ = send.send(HandleAction::New(thread::spawn(move || {
// get up-to-date data
//
info!("[RUN] spawned thread");
Expand Down

0 comments on commit e1202a7

Please sign in to comment.