Skip to content

Commit

Permalink
Merge pull request #112 from michaelb/dev
Browse files Browse the repository at this point in the history
configurable options
  • Loading branch information
michaelb authored Nov 7, 2021
2 parents 2fbf384 + 7425186 commit becb60b
Show file tree
Hide file tree
Showing 21 changed files with 81 additions and 52 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## v1.0.3
- configurable filetypes

## v1.0.2
- fix issue with API functions & callbacks
- fix double checkhealt crash issue
Expand Down
3 changes: 2 additions & 1 deletion CHECKLIST.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

- check compilation success
- update Cargo.lock: `cargo update`
- Bump Cargo.toml to next version
- cargo fmt --all / cargo check / cargo clippy
- create a version bump commit
- merge
- create a new tag vX.Y.Z on master
Expand All @@ -11,4 +13,3 @@

- Check CI status
- Check Releases status
- Bump Cargo.toml to next version
10 changes: 5 additions & 5 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.2"
version = "1.0.3"
authors = ["michaelb <[email protected]>"]
edition = "2018"

Expand Down
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,12 @@ require'sniprun'.setup({
repl_enable = {}, --# enable REPL-like behavior for the given interpreters
repl_disable = {}, --# disable REPL-like behavior for the given interpreters
interpreter_options = {}, --# intepreter-specific options, consult docs / :SnipInfo <name>
interpreter_options = { --# intepreter-specific options, see docs / :SnipInfo <name>
GFM_original = {
use_on_filetypes = {"markdown.pandoc"} --# the 'use_on_filetypes' configuration key is
--# available for every interpreter
}
},
--# you can combo different display modes as desired
display = {
Expand Down Expand Up @@ -317,7 +322,7 @@ EOF
Example, to use the interpreter 'Python3\_jupyter' whenever possible [instead of the 'Python3\_original' default],
`lua require'sniprun'.setup({selected_interpreters = {'Python3_jupyter'}})`

**NOTE**: Some interpreters have specific options and dependencies, such as what version / compiler to use: you cand find more about that with `:SnipInfo <interpreter_name>`
**NOTE**: Some interpreters have specific options and dependencies, such as what version / compiler to use: you cand find more about that with `:SnipInfo <interpreter_name>`. The `use_on_filetypes` key is available for every interpreter (as an `interpreter_option`) and allow sniprun to understand that your particular/custom filetypes are compatible with an interpreter.


Expand Down
6 changes: 1 addition & 5 deletions src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,7 @@ 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 Down
7 changes: 3 additions & 4 deletions src/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ pub trait InterpreterUtils {

fn set_pid(&self, pid: u32);
fn get_pid(&self) -> Option<u32>;
fn get_interpreter_option(&self, option: &str) -> Option<neovim_lib::Value>;
fn get_interpreter_option(data: &DataHolder, option: &str) -> Option<neovim_lib::Value>;
}

impl<T: Interpreter> InterpreterUtils for T {
Expand Down Expand Up @@ -207,7 +207,6 @@ impl<T: Interpreter> InterpreterUtils for T {
let data = self.get_data();
if data.interpreter_data.is_none() {
info!("Unable to save code for next usage");

} else {
{
data.interpreter_data.clone().unwrap().lock().unwrap().owner = T::get_name();
Expand Down Expand Up @@ -260,7 +259,7 @@ impl<T: Interpreter> InterpreterUtils for T {
}

/// get an intepreter option
fn get_interpreter_option(&self, option: &str) -> Option<neovim_lib::Value> {
fn get_interpreter_option(data: &DataHolder, option: &str) -> Option<neovim_lib::Value> {
fn index_from_name(
name: &str,
config: &[(neovim_lib::Value, neovim_lib::Value)],
Expand All @@ -274,7 +273,7 @@ impl<T: Interpreter> InterpreterUtils for T {
None
}
// this is the ugliness required to fetch something from the interpreter options
if let Some(config) = &self.get_data().interpreter_options {
if let Some(config) = &data.interpreter_options {
if let Some(ar) = config.as_map() {
if let Some(i) = index_from_name("interpreter_options", ar) {
if let Some(ar2) = ar[i].1.as_map() {
Expand Down
2 changes: 1 addition & 1 deletion src/interpreters/C_original.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ impl C_original {

fn fetch_config(&mut self) {
let default_compiler = String::from("gcc");
if let Some(used_compiler) = self.get_interpreter_option("compiler") {
if let Some(used_compiler) = C_original::get_interpreter_option(&self.get_data(), "compiler") {
if let Some(compiler_string) = used_compiler.as_str() {
info!("Using custom compiler: {}", compiler_string);
self.compiler = compiler_string.to_string();
Expand Down
2 changes: 1 addition & 1 deletion src/interpreters/Cpp_original.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ impl Cpp_original {

fn fetch_config(&mut self) {
let default_compiler = String::from("g++");
if let Some(used_compiler) = self.get_interpreter_option("compiler") {
if let Some(used_compiler) = Cpp_original::get_interpreter_option(&self.get_data(), "compiler") {
if let Some(compiler_string) = used_compiler.as_str() {
info!("Using custom compiler: {}", compiler_string);
self.compiler = compiler_string.to_string();
Expand Down
2 changes: 1 addition & 1 deletion src/interpreters/GFM_original.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ impl Interpreter for GFM_original {
});


if let Some(value) = gfm_interpreter.get_interpreter_option("default_filetype") {
if let Some(value) = GFM_original::get_interpreter_option(&gfm_interpreter.get_data(), "default_filetype") {
if let Some(valid_string) = value.as_str() {
gfm_interpreter.default_filetype = valid_string.to_string();
}
Expand Down
2 changes: 1 addition & 1 deletion src/interpreters/Go_original.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub struct Go_original {
impl Go_original {
fn fetch_config(&mut self) {
let default_compiler = String::from("go");
if let Some(used_compiler) = self.get_interpreter_option("compiler") {
if let Some(used_compiler) = Go_original::get_interpreter_option(&self.get_data(),"compiler") {
if let Some(compiler_string) = used_compiler.as_str() {
info!("Using custom compiler: {}", compiler_string);
self.compiler = compiler_string.to_string();
Expand Down
10 changes: 5 additions & 5 deletions src/interpreters/Mathematica_original.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ impl Interpreter for Mathematica_original {
let mut preload_graphics = String::from("");
let mut wait_for_graphics = String::from("");
if let Some(use_javagraphics_msgpack) =
self.get_interpreter_option("use_javagraphics_if_contains")
Mathematica_original::get_interpreter_option(&self.get_data(), "use_javagraphics_if_contains")
{
if let Some(use_javagraphics) = use_javagraphics_msgpack.as_array() {
for test_contains_msgpack in use_javagraphics.iter() {
Expand All @@ -179,7 +179,7 @@ impl Interpreter for Mathematica_original {
wait_for_graphics = String::from("Pause[3600];\n");

if let Some(time_mgspack) =
self.get_interpreter_option("keep_plot_open_for")
Mathematica_original::get_interpreter_option(&self.get_data(), "keep_plot_open_for")
{
if let Some(time) = time_mgspack.as_i64() {
if time >= 0 {
Expand All @@ -195,7 +195,7 @@ impl Interpreter for Mathematica_original {
}

if let Some(wrap_all_lines_with_print_msgpack) =
self.get_interpreter_option("wrap_all_lines_with_print")
Mathematica_original::get_interpreter_option(&self.get_data(), "wrap_all_lines_with_print")
{
if let Some(wrap_all_lines_with_print) = wrap_all_lines_with_print_msgpack.as_bool() {
if wrap_all_lines_with_print {
Expand All @@ -208,7 +208,7 @@ impl Interpreter for Mathematica_original {
}
}
if let Some(wrap_last_line_with_print_msgpack) =
self.get_interpreter_option("wrap_last_line_with_print")
Mathematica_original::get_interpreter_option(&self.get_data(), "wrap_last_line_with_print")
{
if let Some(wrap_last_line_with_print) = wrap_last_line_with_print_msgpack.as_bool() {
if wrap_last_line_with_print {
Expand Down Expand Up @@ -318,7 +318,7 @@ impl ReplLikeInterpreter for Mathematica_original {
info!("adding boilerplate");
let mut preload_graphics = "";
if let Some(use_javagraphics_msgpack) =
self.get_interpreter_option("use_javagraphics_if_contains")
Mathematica_original::get_interpreter_option(&self.get_data(), "use_javagraphics_if_contains")
{
if !self.read_previous_code().contains("JavaGraphics loaded") {
if let Some(use_javagraphics) = use_javagraphics_msgpack.as_array() {
Expand Down
2 changes: 1 addition & 1 deletion src/interpreters/OrgMode_original.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ impl Interpreter for OrgMode_original {
});


if let Some(value) = orgmode_interpreter.get_interpreter_option("default_filetype") {
if let Some(value) = OrgMode_original::get_interpreter_option(&orgmode_interpreter.get_data(),"default_filetype") {
if let Some(valid_string) = value.as_str() {
orgmode_interpreter.default_filetype = valid_string.to_string();
}
Expand Down
4 changes: 2 additions & 2 deletions src/interpreters/Python3_fifo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ impl Python3_fifo {

fn fetch_config(&mut self) {
let default_interpreter = String::from("python3");
if let Some(used_interpreter) = self.get_interpreter_option("interpreter") {
if let Some(used_interpreter) = Python3_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();
Expand All @@ -179,7 +179,7 @@ impl Python3_fifo {
self.interpreter = default_interpreter;

if let Ok(path) = env::current_dir() {
if let Some(venv_array_config) = self.get_interpreter_option("venv") {
if let Some(venv_array_config) = Python3_fifo::get_interpreter_option(&self.get_data(), "venv") {
if let Some(actual_vec_of_venv) = venv_array_config.as_array() {
for possible_venv in actual_vec_of_venv.iter() {
if let Some(possible_venv_str) = possible_venv.as_str() {
Expand Down
4 changes: 2 additions & 2 deletions src/interpreters/Python3_original.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ impl Python3_original {
}
fn fetch_config(&mut self) {
let default_compiler = String::from("python3");
if let Some(used_compiler) = self.get_interpreter_option("interpreter") {
if let Some(used_compiler) = Python3_original::get_interpreter_option(&self.get_data(), "interpreter") {
if let Some(compiler_string) = used_compiler.as_str() {
info!("Using custom compiler: {}", compiler_string);
self.interpreter = compiler_string.to_string();
Expand All @@ -95,7 +95,7 @@ impl Python3_original {
self.interpreter = default_compiler;

if let Ok(path) = env::current_dir() {
if let Some(venv_array_config) = self.get_interpreter_option("venv") {
if let Some(venv_array_config) = Python3_original::get_interpreter_option(&self.get_data(), "venv") {
if let Some(actual_vec_of_venv) = venv_array_config.as_array() {
for possible_venv in actual_vec_of_venv.iter() {
if let Some(possible_venv_str) = possible_venv.as_str() {
Expand Down
2 changes: 1 addition & 1 deletion src/interpreters/Rust_original.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub struct Rust_original {
impl Rust_original {
fn fetch_config(&mut self) {
let default_compiler = String::from("rustc");
if let Some(used_compiler) = self.get_interpreter_option("compiler") {
if let Some(used_compiler) = Rust_original::get_interpreter_option(&self.get_data(), "compiler") {
if let Some(compiler_string) = used_compiler.as_str() {
info!("Using custom compiler: {}", compiler_string);
self.compiler = compiler_string.to_string();
Expand Down
4 changes: 2 additions & 2 deletions src/interpreters/Sage_fifo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,14 +213,14 @@ impl Sage_fifo {

fn fetch_config(&mut self) {
let default_interpreter = String::from("sage");
if let Some(used_interpreter) = self.get_interpreter_option("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) = self.get_interpreter_option("sage_user_config") {
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 Down
30 changes: 27 additions & 3 deletions src/launcher.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::interpreter::InterpreterUtils;
use crate::*;
use error::SniprunError;
use interpreter::{Interpreter, SupportLevel};
Expand All @@ -14,6 +15,26 @@ impl Launcher {
Launcher { data }
}

fn match_filetype<T>(filetype: String, data: &DataHolder) -> bool
where
T: Interpreter,
{
if T::get_supported_languages().contains(&filetype) {
return true;
}

if let Some(configured_filetypes) = T::get_interpreter_option(data, "use_on_filetypes") {
if let Some(ft_array) = configured_filetypes.as_array() {
return ft_array
.iter()
.map(|f| f.as_str().unwrap_or("####").to_owned())
.any(|f| f == filetype);
}
}

false
}

pub fn select_and_run(&self) -> Result<String, SniprunError> {
let selection = self.select();
if let Some((name, level)) = selection {
Expand Down Expand Up @@ -46,7 +67,7 @@ impl Launcher {
//select the best interpreter for the language
let mut skip_all = false;
iter_types! {
if Current::get_supported_languages().contains(&self.data.filetype){
if Launcher::match_filetype::<Current>(self.data.filetype.clone(), &self.data){
if !skip_all && Current::get_max_support_level() > max_level_support {
max_level_support = Current::get_max_support_level();
name_best_interpreter = Current::get_name();
Expand Down Expand Up @@ -86,7 +107,7 @@ impl Launcher {
let gitscript = self.data.sniprun_root_dir.clone() + "/ressources/gitscript.sh";
let mut get_version = Command::new(gitscript);
get_version.current_dir(self.data.sniprun_root_dir.clone());
if let Ok(res) = get_version.output(){
if let Ok(res) = get_version.output() {
info!("gitscript result: {:?}", res);
if res.status.success() {
let online_version = String::from_utf8(res.stdout).unwrap();
Expand All @@ -104,7 +125,10 @@ impl Launcher {
"\nCurrently selected interpreter: {}, at support level: {}\n",
name, level
));
v.push(format!("More information may be available via :SnipInfo {}\n\n", name));
v.push(format!(
"More information may be available via :SnipInfo {}\n\n",
name
));
} else {
v.push("No interpreter selected\n\nYou can always get more info about one particular interpreter via:\n:SnipInfo <name>".to_string());
}
Expand Down
5 changes: 4 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,10 @@ impl EventHandler {
}

info!("values length: {}", values.len());
let cli_args = values[3].as_str().unwrap_or({info!("cli arguments are not a string");""});
let cli_args = values[3].as_str().unwrap_or({
info!("cli arguments are not a string");
""
});
{
if !cli_args.is_empty() {
self.data.cli_args = cli_args.split(' ').map(|s| s.to_string()).collect();
Expand Down
2 changes: 0 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@


fn main() {
sniprun::start();
}
Loading

0 comments on commit becb60b

Please sign in to comment.