michaelb / sniprun

A neovim plugin to run lines/blocs of code (independently of the rest of the file), supporting multiples languages
MIT License
1.44k stars 47 forks source link

Julia with Custom Environments #199

Closed storopoli closed 1 year ago

storopoli commented 1 year ago

Sniprun works great if you are using "vanilla" julia (without an environment). However if you opening a file under a julia environment (which is a folder with {Project,Manifest}.toml) sniprun cannot send the code blocks to the right julia command:

julia --project=.

Where . would be the current folder for .jl files that are inside the root folder of the project, e.g. MyProject/myfile.jl; or the root folder for files that are inside a subfolder in the project's root folder, e.g. MyProject/src/myfile.jl.

I think the way to go would add the --project=. to https://github.com/michaelb/sniprun/blob/c37086a0d12b2053bf147c2f4643c0c92318c0c2/src/interpreters/Julia_original.rs#L113-L117

with the care to detect the project folder, e.g. MyProject, which has {Project,Manifest}.toml

michaelb commented 1 year ago

Hey, your requested feature is available as beta on the dev branch.

You can try it out by using sniprun from the dev branch and compiling it locally (you will need the rust toolchain). Example if you use packer:

      use {
         "michaelb/sniprun",
         branch = "dev",
         run = "bash install.sh 1"
     }

As bonus, REPL capability is fixed. Docs & details in :SnipInfo Julia_original

I don't need feedback but if you want to provide any I'll do my best to take it into account

storopoli commented 1 year ago

I've managed to install the dev branch and enable REPL mode for Julia. The snipruns now do not error, but I don't get preview from the code that was executed.

michaelb commented 1 year ago

Thanks, humm could you try executing "--project" -dependent code with REPL disabled and non- "--project" -related (without the interpreter option) code with REPL? To pinpoint where your issue stems from

It's possible that julia in REPL mode doesn't have a way to tell sniprun something errored and just produces a empty stdout. Depending on your config, it might display "(no output)" or just nothing

Relevant logs are in ~/.cache/sniprun/sniprun.log in case

storopoli commented 1 year ago

Here is the full output from ~/.cache/sniprun/sniprun.log:

[00:00:00.000] (555556074900) INFO   [MAIN] SnipRun launched successfully
[00:00:00.000] (555556074900) INFO   [MAIN] Start of main event loop
[00:00:00.000] (555556074900) INFO   EVENT 1 RECEIVED ##################
[00:00:00.000] (555556074900) INFO   [MAINLOOP] Run command received
[00:00:00.000] (555556074900) INFO   [RUN] clone event handler
[00:00:00.000] (70adc3d4b700) INFO   [RUN] spawned thread
[00:00:00.000] (70adc3d4b700) INFO   [FILLDATA] got back eventual interpreter data
[00:00:00.000] (70adc3d4b700) INFO   values length: 4
[00:00:00.000] (70adc3d4b700) INFO   cli arguments are not a string: String(Utf8String { s: Ok("") })
[00:00:00.000] (70adc3d4b700) INFO   got data range: [1, 5]
[00:00:00.000] (70adc3d4b700) INFO   looped on key sniprun_root_dir
[00:00:00.000] (70adc3d4b700) INFO   [FILLDATA] got sniprun root
[00:00:00.002] (70adc3d4b700) INFO   [FILLDATA] got filetype
[00:00:00.004] (70adc3d4b700) INFO   [FILLDATA] got current_line
[00:00:00.006] (70adc3d4b700) INFO   [FILLDATA] got current_bloc
[00:00:00.007] (70adc3d4b700) INFO   [FILLDATA] got filepath
[00:00:00.007] (70adc3d4b700) INFO   [FILLDATA] got nvim_instance
[00:00:00.007] (70adc3d4b700) INFO   looped on key selected_interpreters
[00:00:00.007] (70adc3d4b700) INFO   [FILLDATA] got selected interpreters
[00:00:00.007] (70adc3d4b700) INFO   looped on key repl_enable
[00:00:00.007] (70adc3d4b700) INFO   [FILLDATA] got repl enabled interpreters
[00:00:00.007] (70adc3d4b700) INFO   looped on key repl_disable
[00:00:00.007] (70adc3d4b700) INFO   [FILLDATA] got repl disabled interpreters
[00:00:00.007] (70adc3d4b700) INFO   looped on key display
[00:00:00.007] (70adc3d4b700) INFO   [FILLDATA] display type found : Ok(Classic(Both))
[00:00:00.007] (70adc3d4b700) INFO   [FILLDATA] display type found : Ok(VirtualText(OnlyOk))
[00:00:00.007] (70adc3d4b700) INFO   [FILLDATA] got display types
[00:00:00.008] (70adc3d4b700) INFO   looped on key show_no_output
[00:00:00.008] (70adc3d4b700) INFO   [FILLDATA] display type with 'no output'on found : Ok(Classic(Both))
[00:00:00.008] (70adc3d4b700) INFO   [FILLDATA] display type with 'no output'on found : Ok(TempFloatingWindow(Both))
[00:00:00.008] (70adc3d4b700) INFO   [FILLDATA] got show_no_output
[00:00:00.008] (70adc3d4b700) INFO   looped on key inline_messages
[00:00:00.008] (70adc3d4b700) INFO   [FILLDATA] got inline_messages setting
[00:00:00.008] (70adc3d4b700) INFO   looped on key neovim_pid
[00:00:00.008] (70adc3d4b700) INFO   [FILLDATA] got neovim_pid value setting: 4189
[00:00:00.008] (70adc3d4b700) INFO   [FILLDATA] Done!
[00:00:00.008] (70adc3d4b700) INFO   [OVERRIDE] No data to override
[00:00:00.008] (70adc3d4b700) INFO   [RUN] filled dataholder
[00:00:00.008] (70adc3d4b700) INFO   [RUN] created launcher
[00:00:00.008] (70adc3d4b700) INFO   key 'Bash_original' not found in interpreter option
[00:00:00.008] (70adc3d4b700) INFO   key 'Generic' not found in interpreter option
[00:00:00.008] (70adc3d4b700) INFO   key 'JS_TS_deno' not found in interpreter option
[00:00:00.008] (70adc3d4b700) INFO   key 'Lua_original' not found in interpreter option
[00:00:00.008] (70adc3d4b700) INFO   key 'Sage_fifo' not found in interpreter option
[00:00:00.008] (70adc3d4b700) INFO   key 'Rust_original' not found in interpreter option
[00:00:00.008] (70adc3d4b700) INFO   key 'Prolog_original' not found in interpreter option
[00:00:00.008] (70adc3d4b700) INFO   key 'CS_original' not found in interpreter option
[00:00:00.008] (70adc3d4b700) INFO   key 'Cpp_original' not found in interpreter option
[00:00:00.008] (70adc3d4b700) INFO   key 'TypeScript_original' not found in interpreter option
[00:00:00.008] (70adc3d4b700) INFO   key 'use_on_filetypes' not found in interpreter option
[00:00:00.008] (70adc3d4b700) INFO   key 'Lua_nvim' not found in interpreter option
[00:00:00.008] (70adc3d4b700) INFO   key 'Python3_jupyter' not found in interpreter option
[00:00:00.009] (70adc3d4b700) INFO   key 'R_original' not found in interpreter option
[00:00:00.009] (70adc3d4b700) INFO   key 'Mathematica_original' not found in interpreter option
[00:00:00.009] (70adc3d4b700) INFO   key 'Clojure_fifo' not found in interpreter option
[00:00:00.009] (70adc3d4b700) INFO   key 'Python3_fifo' not found in interpreter option
[00:00:00.009] (70adc3d4b700) INFO   key 'OrgMode_original' not found in interpreter option
[00:00:00.009] (70adc3d4b700) INFO   key 'Ada_original' not found in interpreter option
[00:00:00.009] (70adc3d4b700) INFO   key 'Ruby_original' not found in interpreter option
[00:00:00.009] (70adc3d4b700) INFO   key 'D_original' not found in interpreter option
[00:00:00.009] (70adc3d4b700) INFO   key 'Java_original' not found in interpreter option
[00:00:00.009] (70adc3d4b700) INFO   key 'FSharp_fifo' not found in interpreter option
[00:00:00.009] (70adc3d4b700) INFO   key 'C_original' not found in interpreter option
[00:00:00.009] (70adc3d4b700) INFO   key 'Go_original' not found in interpreter option
[00:00:00.009] (70adc3d4b700) INFO   key 'Haskell_original' not found in interpreter option
[00:00:00.009] (70adc3d4b700) INFO   key 'JS_original' not found in interpreter option
[00:00:00.009] (70adc3d4b700) INFO   key 'Scala_original' not found in interpreter option
[00:00:00.009] (70adc3d4b700) INFO   selected Julia_original
[00:00:00.009] (70adc3d4b700) INFO   [LAUNCHER] Selected interpreter: Julia_original, at level Bloc
[00:00:00.009] (70adc3d4b700) INFO   REPL enabled
[00:00:00.009] (70adc3d4b700) INFO   reading previous code
[00:00:00.009] (70adc3d4b700) INFO   found interpreter_data
[00:00:00.009] (70adc3d4b700) INFO   launching kernel : "/home/user/.local/share/nvim/site/pack/packer/start/sniprun/lua/../ressources/init_repl.sh" on "/home/user/.cache/sniprun/julia_original"
[00:00:00.110] (70adc3d4b700) INFO   reading previous code
[00:00:00.110] (70adc3d4b700) INFO   found interpreter_data
[00:00:00.110] (70adc3d4b700) INFO   reading previous code
[00:00:00.110] (70adc3d4b700) INFO   found interpreter_data
[00:00:00.110] (70adc3d4b700) INFO   code saved: 
kernel_launched
[00:00:00.110] (70adc3d4b700) INFO   [RUN] Interpreter return a result
[00:00:00.110] (70adc3d4b700) INFO   Display type chosen: [Classic(Both), VirtualText(OnlyOk)]
[00:00:00.933] (70adc3d4b700) INFO   range is : [1, 5]
[00:00:02.341] (555556074900) INFO   EVENT 2 RECEIVED ##################
[00:00:02.341] (555556074900) INFO   [MAINLOOP] Run command received
[00:00:02.341] (555556074900) INFO   [RUN] clone event handler
[00:00:02.342] (70adc3b4a700) INFO   [RUN] spawned thread
[00:00:02.342] (70adc3b4a700) INFO   [FILLDATA] got back eventual interpreter data
[00:00:02.342] (70adc3b4a700) INFO   values length: 4
[00:00:02.342] (70adc3b4a700) INFO   cli arguments are not a string: String(Utf8String { s: Ok("") })
[00:00:02.342] (70adc3b4a700) INFO   got data range: [1, 5]
[00:00:02.342] (70adc3b4a700) INFO   looped on key sniprun_root_dir
[00:00:02.342] (70adc3b4a700) INFO   [FILLDATA] got sniprun root
[00:00:02.344] (70adc3b4a700) INFO   [FILLDATA] got filetype
[00:00:02.345] (70adc3b4a700) INFO   [FILLDATA] got current_line
[00:00:02.347] (70adc3b4a700) INFO   [FILLDATA] got current_bloc
[00:00:02.349] (70adc3b4a700) INFO   [FILLDATA] got filepath
[00:00:02.349] (70adc3b4a700) INFO   [FILLDATA] got nvim_instance
[00:00:02.349] (70adc3b4a700) INFO   looped on key selected_interpreters
[00:00:02.349] (70adc3b4a700) INFO   [FILLDATA] got selected interpreters
[00:00:02.349] (70adc3b4a700) INFO   looped on key repl_enable
[00:00:02.349] (70adc3b4a700) INFO   [FILLDATA] got repl enabled interpreters
[00:00:02.349] (70adc3b4a700) INFO   looped on key repl_disable
[00:00:02.349] (70adc3b4a700) INFO   [FILLDATA] got repl disabled interpreters
[00:00:02.349] (70adc3b4a700) INFO   looped on key display
[00:00:02.349] (70adc3b4a700) INFO   [FILLDATA] display type found : Ok(Classic(Both))
[00:00:02.349] (70adc3b4a700) INFO   [FILLDATA] display type found : Ok(VirtualText(OnlyOk))
[00:00:02.349] (70adc3b4a700) INFO   [FILLDATA] got display types
[00:00:02.349] (70adc3b4a700) INFO   looped on key show_no_output
[00:00:02.349] (70adc3b4a700) INFO   [FILLDATA] display type with 'no output'on found : Ok(Classic(Both))
[00:00:02.349] (70adc3b4a700) INFO   [FILLDATA] display type with 'no output'on found : Ok(TempFloatingWindow(Both))
[00:00:02.349] (70adc3b4a700) INFO   [FILLDATA] got show_no_output
[00:00:02.349] (70adc3b4a700) INFO   looped on key inline_messages
[00:00:02.349] (70adc3b4a700) INFO   [FILLDATA] got inline_messages setting
[00:00:02.349] (70adc3b4a700) INFO   looped on key neovim_pid
[00:00:02.349] (70adc3b4a700) INFO   [FILLDATA] got neovim_pid value setting: 4189
[00:00:02.349] (70adc3b4a700) INFO   [FILLDATA] Done!
[00:00:02.349] (70adc3b4a700) INFO   [OVERRIDE] No data to override
[00:00:02.349] (70adc3b4a700) INFO   [RUN] filled dataholder
[00:00:02.349] (70adc3b4a700) INFO   [RUN] created launcher
[00:00:02.349] (70adc3b4a700) INFO   key 'Bash_original' not found in interpreter option
[00:00:02.349] (70adc3b4a700) INFO   key 'Generic' not found in interpreter option
[00:00:02.349] (70adc3b4a700) INFO   key 'JS_TS_deno' not found in interpreter option
[00:00:02.349] (70adc3b4a700) INFO   key 'Lua_original' not found in interpreter option
[00:00:02.349] (70adc3b4a700) INFO   key 'Sage_fifo' not found in interpreter option
[00:00:02.349] (70adc3b4a700) INFO   key 'Rust_original' not found in interpreter option
[00:00:02.349] (70adc3b4a700) INFO   key 'Prolog_original' not found in interpreter option
[00:00:02.349] (70adc3b4a700) INFO   key 'CS_original' not found in interpreter option
[00:00:02.349] (70adc3b4a700) INFO   key 'Cpp_original' not found in interpreter option
[00:00:02.349] (70adc3b4a700) INFO   key 'TypeScript_original' not found in interpreter option
[00:00:02.350] (70adc3b4a700) INFO   key 'use_on_filetypes' not found in interpreter option
[00:00:02.350] (70adc3b4a700) INFO   key 'Lua_nvim' not found in interpreter option
[00:00:02.350] (70adc3b4a700) INFO   key 'Python3_jupyter' not found in interpreter option
[00:00:02.350] (70adc3b4a700) INFO   key 'R_original' not found in interpreter option
[00:00:02.350] (70adc3b4a700) INFO   key 'Mathematica_original' not found in interpreter option
[00:00:02.350] (70adc3b4a700) INFO   key 'Clojure_fifo' not found in interpreter option
[00:00:02.350] (70adc3b4a700) INFO   key 'Python3_fifo' not found in interpreter option
[00:00:02.350] (70adc3b4a700) INFO   key 'OrgMode_original' not found in interpreter option
[00:00:02.350] (70adc3b4a700) INFO   key 'Ada_original' not found in interpreter option
[00:00:02.350] (70adc3b4a700) INFO   key 'Ruby_original' not found in interpreter option
[00:00:02.350] (70adc3b4a700) INFO   key 'D_original' not found in interpreter option
[00:00:02.350] (70adc3b4a700) INFO   key 'Java_original' not found in interpreter option
[00:00:02.350] (70adc3b4a700) INFO   key 'FSharp_fifo' not found in interpreter option
[00:00:02.350] (70adc3b4a700) INFO   key 'C_original' not found in interpreter option
[00:00:02.350] (70adc3b4a700) INFO   key 'Go_original' not found in interpreter option
[00:00:02.350] (70adc3b4a700) INFO   key 'Haskell_original' not found in interpreter option
[00:00:02.350] (70adc3b4a700) INFO   key 'JS_original' not found in interpreter option
[00:00:02.350] (70adc3b4a700) INFO   key 'Scala_original' not found in interpreter option
[00:00:02.350] (70adc3b4a700) INFO   selected Julia_original
[00:00:02.350] (70adc3b4a700) INFO   [LAUNCHER] Selected interpreter: Julia_original, at level Bloc
[00:00:02.350] (70adc3b4a700) INFO   REPL enabled
[00:00:02.350] (70adc3b4a700) INFO   reading previous code
[00:00:02.350] (70adc3b4a700) INFO   found interpreter_data
[00:00:02.350] (70adc3b4a700) INFO   Julia kernel already running
[00:00:02.350] (70adc3b4a700) INFO   running launcher
[00:00:02.356] (70adc3b4a700) ERROR  thread '<unnamed>' panicked at 'could not run launcher: Os { code: 2, kind: NotFound, message: "No such file or directory" }': src/interpreters/Julia_original.rs:258
   0: <backtrace::capture::Backtrace as core::default::Default>::default
   1: log_panics::Config::install_panic_hook::{{closure}}
   2: std::panicking::rust_panic_with_hook
             at /rustc/a55dd71d5fb0ec5a6a3a9e8c27b2127ba491ce52/library/std/src/panicking.rs:702:17
   3: std::panicking::begin_panic_handler::{{closure}}
             at /rustc/a55dd71d5fb0ec5a6a3a9e8c27b2127ba491ce52/library/std/src/panicking.rs:588:13
   4: std::sys_common::backtrace::__rust_end_short_backtrace
             at /rustc/a55dd71d5fb0ec5a6a3a9e8c27b2127ba491ce52/library/std/src/sys_common/backtrace.rs:138:18
   5: rust_begin_unwind
             at /rustc/a55dd71d5fb0ec5a6a3a9e8c27b2127ba491ce52/library/std/src/panicking.rs:584:5
   6: core::panicking::panic_fmt
             at /rustc/a55dd71d5fb0ec5a6a3a9e8c27b2127ba491ce52/library/core/src/panicking.rs:142:14
   7: core::result::unwrap_failed
             at /rustc/a55dd71d5fb0ec5a6a3a9e8c27b2127ba491ce52/library/core/src/result.rs:1814:5
   8: <sniprun::interpreters::Julia_original as sniprun::interpreter::ReplLikeInterpreter>::execute_repl
   9: sniprun::interpreter::Interpreter::run
  10: sniprun::launcher::Launcher::select_and_run
  11: std::sys_common::backtrace::__rust_begin_short_backtrace
  12: core::ops::function::FnOnce::call_once{{vtable.shim}}
  13: <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once
             at /rustc/a55dd71d5fb0ec5a6a3a9e8c27b2127ba491ce52/library/alloc/src/boxed.rs:1935:9
      <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once
             at /rustc/a55dd71d5fb0ec5a6a3a9e8c27b2127ba491ce52/library/alloc/src/boxed.rs:1935:9
      std::sys::unix::thread::Thread::new::thread_start
             at /rustc/a55dd71d5fb0ec5a6a3a9e8c27b2127ba491ce52/library/std/src/sys/unix/thread.rs:108:17
  14: start_thread
             at /build/glibc-SzIz7B/glibc-2.31/nptl/pthread_create.c:477:8
  15: clone

my settings from init.lua:

-- [[ sniprun ]]
require'sniprun'.setup({
  selected_interpreters = {},         -- use those instead of the default for the current filetype
  repl_enable = { 'Julia_original' }, -- enable REPL-like behavior for the given interpreters
  repl_disable = {},                  -- disable REPL-like behavior for the given interpreters

  interpreter_options = {         -- interpreter-specific options, see docs / :SnipInfo <name>

    -- use the interpreter name as key
    GFM_original = {
      use_on_filetypes = {"markdown.pandoc"}    -- the 'use_on_filetypes' configuration key is
                                                -- available for every interpreter
    },
    Python3_original = {
        error_truncate = "auto"         -- Truncate runtime errors 'long', 'short' or 'auto'
                                        -- the hint is available for every interpreter
                                        -- but may not be always respected
    },
    Julia_original = {
        project = ".", -- either a fixed absolute path, or "." for nvim's current directory (from echo getcwd()  )
                       -- This directory has to contain a {Project,Manifest}.toml !
        interpreter = "/home/user/.local/bin/julia"
        }
  },      

  -- you can combo different display modes as desired
  display = {
    "Classic",                    -- display results in the command-line  area
    "VirtualTextOk",              -- display ok results as virtual text (multiline is shortened)

    -- "VirtualTextErr",          -- display error results as virtual text
    -- "TempFloatingWindow",      -- display results in a floating window
    -- "LongTempFloatingWindow",  -- same as above, but only long results. To use with VirtualText__
    -- "Terminal",                -- display results in a vertical split
    -- "TerminalWithCode",        -- display results and code history in a vertical split
    -- "NvimNotify",              -- display with the nvim-notify plugin
    -- "Api"                      -- return output to a programming interface
  },

  display_options = {
    terminal_width = 45,       -- change the terminal display option width
    notification_timeout = 5   -- timeout for nvim_notify output
  },

  -- You can use the same keys to customize whether a sniprun producing
  -- no output should display nothing or '(no output)'
  show_no_output = {
    "Classic",
    "TempFloatingWindow",      -- implies LongTempFloatingWindow, which has no effect on its own
  },

  -- customize highlight groups (setting this overrides colorscheme)
  snipruncolors = {
    SniprunVirtualTextOk   =  { bg="#66eeff", fg="#000000", ctermbg="Cyan", cterfg="Black" },
    SniprunFloatingWinOk   =  { fg="#66eeff",ctermfg="Cyan" },
    SniprunVirtualTextErr  =  { bg="#881515", fg="#000000", ctermbg="DarkRed", cterfg="Black" },
    SniprunFloatingWinErr  =  { fg="#881515", ctermfg="DarkRed" },
  },

  -- miscellaneous compatibility/adjustement settings
  inline_messages = 0,             -- inline_message (0/1) is a one-line way to display messages
                                           -- to workaround sniprun not being able to display anything

  borders = 'single',              -- display borders around floating windows
                                   -- possible values are 'none', 'single', 'double', or 'shadow'
  live_mode_toggle='off'           -- live mode toggle, see Usage - Running for more info   
})
vim.keymap.set('v', 'f', '<Plug>SnipRun', { silent = true, desc = 'Sniprun - Run Selection' })
vim.keymap.set('n', '<leader>f', '<Plug>SnipRunOperator', { silent = true, desc = 'Sniprun - Run Line/Block' })
vim.keymap.set('n', '<leader>ff', '<Plug>SnipRun', { silent = true, desc = 'Sniprun - Run Line/Block' })
vim.keymap.set('n', '<leader>fr', '<Plug>SnipReset', { silent = true, desc = 'Sniprun - Reset/Kill' })
michaelb commented 1 year ago

Thanks, it looks like you didn't recompile sniprun (you probably used bash install.sh instead of bash install.sh 1or didn't uninstall before re-installing. Try to fix the previous points, or alternatively got to ~/.local/share/nvim/packer/...(the location of nvim plugins).../sniprun and run cargo build --release from there.

Your issue was due to using REPL mode with an outdated sniprun binary, but I'd be wary of using all the new options at once, it makes the bug harder to pinpoint.

Should you want to try out the 'interpreter=...' Julia's interpreter option, make sure that /home/user/.julia is a real path to a working julia interpreter. It's good practice to censor your username online but if you copied this from the julia doc you'd better omit this field for it doesn't exist on your computer and sniprun will consequently not work.

storopoli commented 1 year ago

Thank you! Yes, works like a charm now!

michaelb commented 1 year ago

I take it you're happy with the current state pf things, nice :-)

Still, I'll reopen this issue and it will be automatically closed when I merge the PR into master and release a new version, it helps tracking work and if any user has rhe same issue meanwhile they'll find the issue more easily